@@ -301,6 +301,8 @@ ur_adapter_handle_t_::ur_adapter_handle_t_()
301
301
ZeInitResult = ZE_RESULT_ERROR_UNINITIALIZED;
302
302
ZesResult = ZE_RESULT_ERROR_UNINITIALIZED;
303
303
304
+ resetRefCount (0 );
305
+
304
306
#ifdef UR_STATIC_LEVEL_ZERO
305
307
// Given static linking of the L0 Loader, we must delay the loader's
306
308
// destruction of its context until after the UR Adapter is destroyed.
@@ -667,15 +669,15 @@ ur_result_t urAdapterGet(
667
669
uint32_t *NumAdapters) {
668
670
if (NumEntries > 0 && Adapters) {
669
671
if (GlobalAdapter) {
670
- if (GlobalAdapter->incrementRefCount () == 1 ) {
672
+ if (GlobalAdapter->incrementRefCount () == 0 ) {
671
673
adapterStateInit ();
672
674
}
673
675
} else {
674
676
// If the GetAdapter is called after the Library began or was torndown,
675
677
// then temporarily create a new Adapter handle and register a new
676
678
// cleanup.
677
679
GlobalAdapter = new ur_adapter_handle_t_ ();
678
- if (GlobalAdapter->incrementRefCount () == 1 ) {
680
+ if (GlobalAdapter->incrementRefCount () == 0 ) {
679
681
adapterStateInit ();
680
682
}
681
683
std::atexit (globalAdapterOnDemandCleanup);
@@ -693,7 +695,7 @@ ur_result_t urAdapterGet(
693
695
ur_result_t urAdapterRelease (ur_adapter_handle_t ) {
694
696
// Check first if the Adapter pointer is valid
695
697
if (GlobalAdapter) {
696
- if (GlobalAdapter->decrementRefCount () == 0 ) {
698
+ if (GlobalAdapter->decrementAndTest () ) {
697
699
auto result = adapterStateTeardown ();
698
700
#ifdef UR_STATIC_LEVEL_ZERO
699
701
// Given static linking of the L0 Loader, we must delay the loader's
0 commit comments