1+
12/*
23 *
34 * Copyright (C) 2019-2021 Intel Corporation
@@ -27,6 +28,10 @@ namespace ze_lib
2728 }
2829 }
2930 bool delayContextDestruction = false ;
31+ std::mutex stabilityMutex;
32+ std::promise<int > stabilityPromiseResult;
33+ std::future<int > resultFutureResult;
34+ std::atomic<int > stabilityCheckThreadStarted{0 };
3035 #endif
3136 bool destruction = false ;
3237
@@ -43,6 +48,7 @@ namespace ze_lib
4348 if (loader) {
4449 FREE_DRIVER_LIBRARY ( loader );
4550 }
51+ ze_lib::stabilityCheckThreadStarted = -1 ;
4652#endif
4753 ze_lib::destruction = true ;
4854 };
@@ -490,18 +496,49 @@ zelCheckIsLoaderInTearDown() {
490496 return true ;
491497 }
492498 #ifdef DYNAMIC_LOAD_LOADER
493- std::promise<int > stabilityPromise;
494- std::future<int > resultFuture = stabilityPromise.get_future ();
495- int result = -1 ;
499+ static bool unstable = false ;
500+ int threadResult = -1 ;
501+ if (unstable) {
502+ return true ;
503+ }
496504 try {
497- // Launch the stability checker thread
498- std::thread stabilityThread (stabilityCheck, std::move (stabilityPromise));
499- result = resultFuture.get (); // Blocks until the result is available
500- if (ze_lib::context->debugTraceEnabled ) {
501- std::string message = " Stability checker thread completed with result: " + std::to_string (result);
502- ze_lib::context->debug_trace_message (message, " " );
503- }
504- stabilityThread.join ();
505+ // Launch the stability checker thread on the first call
506+ static std::once_flag stabilityThreadFlag;
507+ static std::thread stabilityThread;
508+ std::lock_guard<std::mutex> lock (ze_lib::stabilityMutex);
509+ ze_lib::stabilityPromiseResult = std::promise<int >();
510+ ze_lib::resultFutureResult = ze_lib::stabilityPromiseResult.get_future ();
511+ ze_lib::stabilityCheckThreadStarted = 1 ;
512+ std::call_once (stabilityThreadFlag, []() {
513+ stabilityThread = std::thread ([]() {
514+ while (true ) {
515+ std::promise<int > stabilityPromise;
516+ std::future<int > resultFuture = stabilityPromise.get_future ();
517+ while (ze_lib::stabilityCheckThreadStarted == 0 ) {
518+ std::this_thread::sleep_for (std::chrono::milliseconds (1 ));
519+ }
520+ if (ze_lib::stabilityCheckThreadStarted == -1 ) {
521+ break ;
522+ }
523+ ze_lib::stabilityCheckThreadStarted = 0 ;
524+ stabilityCheck (std::move (stabilityPromise));
525+ int result = resultFuture.get ();
526+ if (result != ZEL_STABILITY_CHECK_RESULT_SUCCESS) {
527+ if (ze_lib::context->debugTraceEnabled ) {
528+ std::string message = " Loader stability check thread failed with result: " + std::to_string (result);
529+ ze_lib::context->debug_trace_message (message, " " );
530+ }
531+ ze_lib::stabilityPromiseResult.set_value (result);
532+ break ; // Exit the thread if stability check fails
533+ }
534+ ze_lib::stabilityPromiseResult.set_value (result);
535+ }
536+ });
537+ if (stabilityThread.joinable ()) {
538+ stabilityThread.detach ();
539+ }
540+ });
541+ threadResult = ze_lib::resultFutureResult.get ();
505542 } catch (const std::exception& e) {
506543 if (ze_lib::context->debugTraceEnabled ) {
507544 std::string message = " Exception caught in parent thread: " + std::string (e.what ());
@@ -513,11 +550,12 @@ zelCheckIsLoaderInTearDown() {
513550 ze_lib::context->debug_trace_message (message, " " );
514551 }
515552 }
516- if (result != ZEL_STABILITY_CHECK_RESULT_SUCCESS) {
553+ if (threadResult != ZEL_STABILITY_CHECK_RESULT_SUCCESS) {
517554 if (ze_lib::context->debugTraceEnabled ) {
518- std::string message = " Loader stability check failed with result: " + std::to_string (result );
555+ std::string message = " Loader stability check failed with result: " + std::to_string (threadResult );
519556 ze_lib::context->debug_trace_message (message, " " );
520557 }
558+ unstable = true ;
521559 return true ;
522560 }
523561 #endif
0 commit comments