Skip to content

Commit b7bd280

Browse files
authored
Prevent windows library unloading at process termination. (#275)
1 parent 9f26060 commit b7bd280

5 files changed

Lines changed: 16 additions & 8 deletions

File tree

loader/cllayerinfo.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@ static void run_silently(void (*pfn)(void))
121121
atexit(silence_layers);
122122
}
123123

124+
static void run_silently_int(void (*pfn)(int), int arg)
125+
{
126+
silence_layers();
127+
atexit(restore_outputs);
128+
pfn(arg);
129+
restore_outputs();
130+
atexit(silence_layers);
131+
}
132+
124133
int main (int argc, char *argv[])
125134
{
126135
(void)argc;
@@ -132,6 +141,6 @@ int main (int argc, char *argv[])
132141
printLayerInfo(layer);
133142
layer = layer->next;
134143
}
135-
run_silently(&khrIcdDeinitialize);
144+
run_silently_int(&khrIcdDeinitialize, 1);
136145
return 0;
137146
}

loader/icd.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -584,13 +584,12 @@ void khrIcdContextPropertiesGetPlatform(const cl_context_properties *properties,
584584
static struct KHRLayer deinitLayer = {0};
585585
#endif
586586

587-
void khrIcdDeinitialize(void) {
587+
void khrIcdDeinitialize(int unloadLibraries) {
588588
if (khrForceLegacyTermination)
589589
{
590590
KHR_ICD_TRACE("ICD Loader deinitialization disabled\n");
591591
return;
592592
}
593-
594593
KHR_ICD_TRACE("ICD Loader deinitialization\n");
595594

596595
#if defined(CL_ENABLE_LAYERS)
@@ -614,7 +613,7 @@ void khrIcdDeinitialize(void) {
614613
KHR_ICD_TRACE("error reported in layer deinitialization\n");
615614
}
616615
}
617-
if (!khrDisableLibraryUnloading)
616+
if (unloadLibraries && !khrDisableLibraryUnloading)
618617
khrIcdOsLibraryUnload(cur->library);
619618
head = cur->next;
620619
free(cur);
@@ -626,7 +625,7 @@ void khrIcdDeinitialize(void) {
626625
while (lastVendor) {
627626
KHRicdVendor *cur = lastVendor;
628627
free(cur->suffix);
629-
if (cur->unloadable && !khrDisableLibraryUnloading)
628+
if (cur->unloadable && unloadLibraries && !khrDisableLibraryUnloading)
630629
khrIcdOsLibraryUnload(cur->library);
631630
lastVendor = cur->prev;
632631
free(cur);

loader/icd.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void khrIcdInitialize(void);
158158
void khrIcdInitializeEnvOptions(void);
159159

160160
// entrypoint to release icd resources
161-
void khrIcdDeinitialize(void);
161+
void khrIcdDeinitialize(int unloadLibraries);
162162

163163
// go through the list of vendors (in /etc/OpenCL.conf or through
164164
// the registry) and call khrIcdVendorAdd for each vendor encountered

loader/linux/icd_linux.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,6 @@ void khrIcdOsVendorsEnumerateOnce(void)
239239
#ifndef CL_LAYER_INFO
240240
static
241241
void __attribute__((destructor)) khrIcdDestructor(void) {
242-
khrIcdDeinitialize();
242+
khrIcdDeinitialize(1);
243243
}
244244
#endif

loader/windows/icd_windows.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ BOOL APIENTRY DllMain(HINSTANCE hinst, DWORD reason, LPVOID reserved) {
416416
(void)hinst;
417417
(void)reserved;
418418
if (reason == DLL_PROCESS_DETACH) {
419-
khrIcdDeinitialize();
419+
khrIcdDeinitialize(reserved == NULL ? 1 : 0);
420420
}
421421
return TRUE;
422422
}

0 commit comments

Comments
 (0)