Skip to content

Commit e26a401

Browse files
Close used OpenCL library
When we finalize the OpenCL system we would want to close the library again.
1 parent 1bbdd50 commit e26a401

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

src/common/dlopencl.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ static const char *ocllib[] = { "/System/Library/Frameworks/OpenCL.framework/Ver
3636
static const char *ocllib[] = { "libOpenCL", "libOpenCL.so", "libOpenCL.so.1", NULL };
3737
#endif
3838

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

4053
/* only for debugging: default noop function for all unassigned function pointers */
4154
void dt_dlopencl_noop(void)
@@ -210,6 +223,8 @@ dt_dlopencl_t *dt_dlopencl_init(const char *name)
210223

211224
if(!success)
212225
dt_print(DT_DEBUG_OPENCL, "[opencl_init] could not load all required symbols from library");
226+
else
227+
ocl->gmodule = module->gmodule;
213228

214229
free(module);
215230

src/common/dlopencl.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,16 @@ typedef struct dt_dlopencl_symbols_t
208208
dt_clEnqueueWaitForEvents_t dt_clEnqueueWaitForEvents;
209209
} dt_dlopencl_symbols_t;
210210

211-
212-
213211
typedef struct dt_dlopencl_t
214212
{
215213
gboolean have_opencl;
216214
dt_dlopencl_symbols_t *symbols;
217215
char *library;
216+
#ifndef __APPLE__
217+
GModule *gmodule;
218+
#else
219+
void *gmodule;
220+
#endif
218221
} dt_dlopencl_t;
219222

220223
/* default noop function for all unassigned function pointers */
@@ -223,6 +226,9 @@ void dt_dlopencl_noop(void);
223226
/* dynamically load OpenCL library and bind needed functions */
224227
dt_dlopencl_t *dt_dlopencl_init(const char *);
225228

229+
/* dynamically close OpenCL library opened by dt_dlopencl_init() */
230+
void dt_dlopencl_close(dt_dlopencl_t *ocl);
231+
226232
#endif // HAVE_OPENCL
227233

228234
// clang-format off

src/common/opencl.c

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

16501650
if(cl->dlocl)
16511651
{
1652-
free(cl->dlocl->symbols);
1653-
g_free(cl->dlocl->library);
1654-
free(cl->dlocl);
1652+
dt_dlopencl_close(cl->dlocl);
16551653
}
16561654

16571655
free(cl->dev);

0 commit comments

Comments
 (0)