Skip to content

Commit 5518b48

Browse files
committed
Remove error return for adapter and change get to create temp adapter
- To handle the customer usecases, don't return uinit when the adapter is used after the adapter library has already closed. - Given the global adapter is gone, but get adapter is called, then a temp adapter struct is allocated and an ondemand atexit cleanup of this memory is registered. Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
1 parent 91a37cf commit 5518b48

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

source/adapters/level_zero/adapter.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,11 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
105105
};
106106
}
107107

108-
#if defined(_WIN32)
109-
void globalAdapterWindowsCleanup() {
108+
void globalAdapterOnDemandCleanup() {
110109
if (GlobalAdapter) {
111110
delete GlobalAdapter;
112111
}
113112
}
114-
#endif
115113

116114
ur_result_t adapterStateTeardown() {
117115
bool LeakFound = false;
@@ -201,7 +199,7 @@ ur_result_t adapterStateTeardown() {
201199
// Due to multiple DLLMain definitions with SYCL, register to cleanup the
202200
// Global Adapter after refcnt is 0
203201
#if defined(_WIN32)
204-
std::atexit(globalAdapterWindowsCleanup);
202+
std::atexit(globalAdapterOnDemandCleanup);
205203
#endif
206204

207205
return UR_RESULT_SUCCESS;
@@ -227,10 +225,18 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
227225
if (GlobalAdapter->RefCount++ == 0) {
228226
adapterStateInit();
229227
}
230-
*Adapters = GlobalAdapter;
231228
} else {
232-
return UR_RESULT_ERROR_UNINITIALIZED;
229+
// If the GetAdapter is called after the Library began or was torndown,
230+
// then temporarily create a new Adapter handle and register a new
231+
// cleanup.
232+
GlobalAdapter = new ur_adapter_handle_t_();
233+
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
234+
if (GlobalAdapter->RefCount++ == 0) {
235+
adapterStateInit();
236+
}
237+
std::atexit(globalAdapterOnDemandCleanup);
233238
}
239+
*Adapters = GlobalAdapter;
234240
}
235241

236242
if (NumAdapters) {
@@ -256,8 +262,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
256262
if (GlobalAdapter) {
257263
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
258264
GlobalAdapter->RefCount++;
259-
} else {
260-
return UR_RESULT_ERROR_UNINITIALIZED;
261265
}
262266

263267
return UR_RESULT_SUCCESS;

0 commit comments

Comments
 (0)