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 b88647c commit f9bc445
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 @@ -116,13 +116,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 @@ -212,7 +210,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 Down Expand Up @@ -245,10 +243,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 @@ -274,8 +280,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 f9bc445

Please sign in to comment.