Skip to content

Commit

Permalink
Remove error return for adapter and change get to create temp adapter
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
nrspruit committed Mar 8, 2024
1 parent 91a37cf commit 5518b48
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions source/adapters/level_zero/adapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,11 @@ ur_adapter_handle_t_::ur_adapter_handle_t_() {
};
}

#if defined(_WIN32)
void globalAdapterWindowsCleanup() {
void globalAdapterOnDemandCleanup() {
if (GlobalAdapter) {
delete GlobalAdapter;
}
}
#endif

ur_result_t adapterStateTeardown() {
bool LeakFound = false;
Expand Down Expand Up @@ -201,7 +199,7 @@ ur_result_t adapterStateTeardown() {
// Due to multiple DLLMain definitions with SYCL, register to cleanup the
// Global Adapter after refcnt is 0
#if defined(_WIN32)
std::atexit(globalAdapterWindowsCleanup);
std::atexit(globalAdapterOnDemandCleanup);
#endif

return UR_RESULT_SUCCESS;
Expand All @@ -227,10 +225,18 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterGet(
if (GlobalAdapter->RefCount++ == 0) {
adapterStateInit();
}
*Adapters = GlobalAdapter;
} else {
return UR_RESULT_ERROR_UNINITIALIZED;
// If the GetAdapter is called after the Library began or was torndown,
// then temporarily create a new Adapter handle and register a new
// cleanup.
GlobalAdapter = new ur_adapter_handle_t_();
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
if (GlobalAdapter->RefCount++ == 0) {
adapterStateInit();
}
std::atexit(globalAdapterOnDemandCleanup);
}
*Adapters = GlobalAdapter;
}

if (NumAdapters) {
Expand All @@ -256,8 +262,6 @@ UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t) {
if (GlobalAdapter) {
std::lock_guard<std::mutex> Lock{GlobalAdapter->Mutex};
GlobalAdapter->RefCount++;
} else {
return UR_RESULT_ERROR_UNINITIALIZED;
}

return UR_RESULT_SUCCESS;
Expand Down

0 comments on commit 5518b48

Please sign in to comment.