Skip to content

Commit 7575853

Browse files
[SYCL] Release global shared implementation pointers early (#4786)
Currently the global handler keeps shared pointers to SYCL object implementations alive until an intentionally late destruction. Backends that rely on global state may destroy its global state prior to the global handler releasing its references to SYCL object implementations that are in turn keeping backend objects alive. These changes enforce a release of the global handler's references to globally tracked SYCL objects at regular global destructor time to avoid the objects unintentionally staying alive for too long. Signed-off-by: Steffen Larsen <[email protected]>
1 parent c2ccc43 commit 7575853

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

sycl/source/detail/global_handler.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ std::mutex &GlobalHandler::getHandlerExtendedMembersMutex() {
8989
return getOrCreate(MHandlerExtendedMembersMutex);
9090
}
9191

92-
void shutdown() {
93-
// First, release resources, that may access plugins.
94-
GlobalHandler::instance().MScheduler.Inst.reset(nullptr);
95-
GlobalHandler::instance().MProgramManager.Inst.reset(nullptr);
92+
void releaseSharedGlobalHandles() {
93+
// Release shared-pointers to SYCL objects.
9694
#ifndef _WIN32
9795
GlobalHandler::instance().MPlatformToDefaultContextCache.Inst.reset(nullptr);
9896
#else
@@ -104,6 +102,12 @@ void shutdown() {
104102
GlobalHandler::instance().MPlatformToDefaultContextCache.Inst.release();
105103
#endif
106104
GlobalHandler::instance().MPlatformCache.Inst.reset(nullptr);
105+
}
106+
107+
void shutdown() {
108+
// First, release resources, that may access plugins.
109+
GlobalHandler::instance().MScheduler.Inst.reset(nullptr);
110+
GlobalHandler::instance().MProgramManager.Inst.reset(nullptr);
107111

108112
// Call to GlobalHandler::instance().getPlugins() initializes plugins. If
109113
// user application has loaded SYCL runtime, and never called any APIs,
@@ -131,6 +135,7 @@ extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL,
131135
// Perform actions based on the reason for calling.
132136
switch (fdwReason) {
133137
case DLL_PROCESS_DETACH:
138+
releaseSharedGlobalHandles();
134139
shutdown();
135140
break;
136141
case DLL_PROCESS_ATTACH:
@@ -141,6 +146,14 @@ extern "C" __SYCL_EXPORT BOOL WINAPI DllMain(HINSTANCE hinstDLL,
141146
return TRUE; // Successful DLL_PROCESS_ATTACH.
142147
}
143148
#else
149+
// Release shared SYCL object implementation handles at normal destructor
150+
// priority to avoid the global handler from keeping the objects alive after
151+
// the backends have destroyed any state they may rely on to correctly handle
152+
// further operations.
153+
__attribute__((destructor)) static void syclPreunload() {
154+
releaseSharedGlobalHandles();
155+
}
156+
144157
// Setting low priority on destructor ensures it runs after all other global
145158
// destructors. Priorities 0-100 are reserved by the compiler. The priority
146159
// value 110 allows SYCL users to run their destructors after runtime library

sycl/source/detail/global_handler.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class GlobalHandler {
6969
std::mutex &getHandlerExtendedMembersMutex();
7070

7171
private:
72+
friend void releaseSharedGlobalHandles();
7273
friend void shutdown();
7374

7475
// Constructor and destructor are declared out-of-line to allow incomplete

0 commit comments

Comments
 (0)