@@ -40,10 +40,15 @@ UR_APIEXPORT ur_result_t UR_APICALL urContextCreate(
4040
4141 Context->initialize ();
4242 *RetContext = reinterpret_cast <ur_context_handle_t >(Context);
43+ #ifdef _WIN32
44+ std::scoped_lock<ur_shared_mutex> Lock (Platform->ContextsMutex );
45+ addToCachedList<ur_context_handle_t >(*RetContext, Platform->Contexts );
46+ #else
4347 if (IndirectAccessTrackingEnabled) {
4448 std::scoped_lock<ur_shared_mutex> Lock (Platform->ContextsMutex );
4549 Platform->Contexts .push_back (*RetContext);
4650 }
51+ #endif
4752 } catch (const std::bad_alloc &) {
4853 return UR_RESULT_ERROR_OUT_OF_HOST_MEMORY;
4954 } catch (...) {
@@ -355,13 +360,21 @@ ur_result_t ContextReleaseHelper(ur_context_handle_t Context) {
355360 if (!Context->RefCount .decrementAndTest ())
356361 return UR_RESULT_SUCCESS;
357362
358- if (IndirectAccessTrackingEnabled ) {
363+ auto DeleteFromContextsCache = [&]( ) {
359364 ur_platform_handle_t Plt = Context->getPlatform ();
360365 auto &Contexts = Plt->Contexts ;
361366 auto It = std::find (Contexts.begin (), Contexts.end (), Context);
362367 if (It != Contexts.end ())
363368 Contexts.erase (It);
369+ };
370+
371+ #ifdef _WIN32
372+ DeleteFromContextsCache ();
373+ #else
374+ if (IndirectAccessTrackingEnabled) {
375+ DeleteFromContextsCache ();
364376 }
377+ #endif
365378 ze_context_handle_t DestroyZeContext =
366379 Context->OwnNativeHandle ? Context->ZeContext : nullptr ;
367380
@@ -451,6 +464,12 @@ ur_result_t ur_context_handle_t_::finalize() {
451464 }
452465 }
453466 }
467+
468+ for (auto &kernel : KernelsCache) {
469+ UR_CALL (urKernelRelease (kernel));
470+ }
471+ KernelsCache.clear ();
472+
454473 return UR_RESULT_SUCCESS;
455474}
456475
0 commit comments