Skip to content

Commit 54f319a

Browse files
Close used OpenCL library
When we finalize the OpenCL system we would want to close the library again. Improved logs using the nts variant
1 parent ca35370 commit 54f319a

File tree

3 files changed

+39
-10
lines changed

3 files changed

+39
-10
lines changed

src/common/dlopencl.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of darktable,
3-
Copyright (C) 2011-2023 darktable developers.
3+
Copyright (C) 2011-2026 darktable developers.
44
55
darktable is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -22,6 +22,10 @@
2222
#include "common/dynload.h"
2323
#include "common/dlopencl.h"
2424

25+
#ifdef __APPLE__
26+
#include <dlfcn.h>
27+
#endif
28+
2529
#include <assert.h>
2630
#include <signal.h>
2731
#include <stdio.h>
@@ -36,6 +40,19 @@ static const char *ocllib[] = { "/System/Library/Frameworks/OpenCL.framework/Ver
3640
static const char *ocllib[] = { "libOpenCL", "libOpenCL.so", "libOpenCL.so.1", NULL };
3741
#endif
3842

43+
void dt_dlopencl_close(dt_dlopencl_t *ocl)
44+
{
45+
free(ocl->symbols);
46+
g_free(ocl->library);
47+
#ifndef __APPLE__
48+
if(!g_module_close(ocl->gmodule))
49+
dt_print(DT_DEBUG_OPENCL, "Couldn't close OpenCL library");
50+
#else
51+
if(dlclose(ocl->gmodule))
52+
dt_print(DT_DEBUG_OPENCL, "Couldn't close OpenCL library");
53+
#endif
54+
free(ocl);
55+
}
3956

4057
/* only for debugging: default noop function for all unassigned function pointers */
4158
void dt_dlopencl_noop(void)
@@ -63,9 +80,9 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
6380
library = name;
6481
module = dt_gmodule_open(library);
6582
if(module == NULL)
66-
dt_print(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find specified opencl runtime library '%s'", library);
83+
dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] could not find specified opencl runtime library '%s'\n", library);
6784
else
68-
dt_print(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found specified opencl runtime library '%s'", library);
85+
dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found specified opencl runtime library '%s'\n", library);
6986
}
7087
else
7188
{
@@ -75,15 +92,18 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
7592
library = *iter;
7693
module = dt_gmodule_open(library);
7794
if(module == NULL)
78-
dt_print(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find default opencl runtime library '%s'", library);
95+
dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] could not find default opencl runtime library '%s'\n", library);
7996
else
80-
dt_print(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found default opencl runtime library '%s'", library);
97+
dt_print_nts(DT_DEBUG_OPENCL | DT_DEBUG_VERBOSE, "[dt_dlopencl_init] found default opencl runtime library '%s'\n", library);
8198
iter++;
8299
}
83100
}
84101

85102
if(module == NULL)
103+
{
104+
dt_print_nts(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find any opencl runtime library\n");
86105
return NULL;
106+
}
87107

88108
/* now bind symbols */
89109
ocl = malloc(sizeof(dt_dlopencl_t));
@@ -98,6 +118,7 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
98118

99119
if(ocl->symbols == NULL)
100120
{
121+
dt_print_nts(DT_DEBUG_OPENCL, "[dt_dlopencl_init] could not find symbols in opencl runtime library\n");
101122
free(ocl);
102123
free(module);
103124
return NULL;
@@ -209,7 +230,9 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
209230
ocl->have_opencl = success;
210231

211232
if(!success)
212-
dt_print(DT_DEBUG_OPENCL, "[opencl_init] could not load all required symbols from library");
233+
dt_print_nts(DT_DEBUG_OPENCL, "[opencl_init] could not load all required symbols from library\n");
234+
else
235+
ocl->gmodule = module->gmodule;
213236

214237
free(module);
215238

src/common/dlopencl.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
This file is part of darktable,
3-
Copyright (C) 2011-2023 darktable developers.
3+
Copyright (C) 2011-2026 darktable developers.
44
55
darktable is free software: you can redistribute it and/or modify
66
it under the terms of the GNU General Public License as published by
@@ -215,6 +215,11 @@ typedef struct dt_dlopencl_t
215215
gboolean have_opencl;
216216
dt_dlopencl_symbols_t *symbols;
217217
char *library;
218+
#ifndef __APPLE__
219+
GModule *gmodule;
220+
#else
221+
void *gmodule;
222+
#endif
218223
} dt_dlopencl_t;
219224

220225
/* default noop function for all unassigned function pointers */
@@ -223,6 +228,9 @@ void dt_dlopencl_noop(void);
223228
/* dynamically load OpenCL library and bind needed functions */
224229
dt_dlopencl_t *dt_dlopencl_init(const char *);
225230

231+
/* dynamically close OpenCL library opened by dt_dlopencl_init() */
232+
void dt_dlopencl_close(dt_dlopencl_t *ocl);
233+
226234
#endif // HAVE_OPENCL
227235

228236
// clang-format off

src/common/opencl.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,9 +1659,7 @@ void dt_opencl_cleanup(dt_opencl_t *cl)
16591659

16601660
if(cl->dlocl)
16611661
{
1662-
free(cl->dlocl->symbols);
1663-
g_free(cl->dlocl->library);
1664-
free(cl->dlocl);
1662+
dt_dlopencl_close(cl->dlocl);
16651663
}
16661664

16671665
free(cl->dev);

0 commit comments

Comments
 (0)