Skip to content

Commit c629333

Browse files
committed
Simplify the fallback teardown check and check only once before failing.
Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
1 parent 792af56 commit c629333

1 file changed

Lines changed: 31 additions & 83 deletions

File tree

source/lib/ze_lib.cpp

Lines changed: 31 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ namespace ze_lib
6969
__zedlllocal context_t::~context_t()
7070
{
7171
#ifdef DYNAMIC_LOAD_LOADER
72-
if (!loaderTeardownCallbackReceived) {
72+
if (loaderTeardownRegistrationEnabled && !loaderTeardownCallbackReceived) {
7373
loaderTeardownCallback(loaderTeardownCallbackIndex);
7474
}
7575
if (loader) {
@@ -476,70 +476,6 @@ zelSetDelayLoaderContextTeardown()
476476
#endif
477477
}
478478

479-
#ifdef DYNAMIC_LOAD_LOADER
480-
#define ZEL_STABILITY_CHECK_RESULT_SUCCESS 0
481-
#define ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_NULL 1
482-
#define ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_FAILED 2
483-
#define ZEL_STABILITY_CHECK_RESULT_EXCEPTION 3
484-
485-
/**
486-
* @brief Performs a stability check for the Level Zero loader.
487-
*
488-
* This function checks the stability of the Level Zero loader by verifying
489-
* the presence of the loader module, the validity of the `zeDriverGet` function
490-
* pointer, and the ability to retrieve driver information. The result of the
491-
* stability check is communicated through the provided promise.
492-
*
493-
* @param stabilityPromise A promise object used to communicate the result of
494-
* the stability check. The promise is set with one of
495-
* the following values:
496-
* - ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_NULL: The
497-
* `zeDriverGet` function pointer is invalid.
498-
* - ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_FAILED: The
499-
* loader failed to retrieve driver information.
500-
* - ZEL_STABILITY_CHECK_RESULT_EXCEPTION: An
501-
* exception occurred during the stability check.
502-
* - ZEL_STABILITY_CHECK_RESULT_SUCCESS: The stability
503-
* check was successful.
504-
*
505-
* @note If debug tracing is enabled, debug messages are logged for each failure
506-
* scenario.
507-
* @note If the Loader is completely torn down, this thread is expected to be killed
508-
* due to invalid memory access and the stability check will determine a failure.
509-
*
510-
* @exception This function catches all exceptions internally and does not throw.
511-
*/
512-
void stabilityCheck(std::promise<int> stabilityPromise) {
513-
try {
514-
if (!ze_lib::context->loaderDriverGet) {
515-
if (ze_lib::context->debugTraceEnabled) {
516-
std::string message = "LoaderDriverGet is a bad pointer. Exiting stability checker thread.";
517-
ze_lib::context->debug_trace_message(message, "");
518-
}
519-
stabilityPromise.set_value(ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_NULL);
520-
return;
521-
}
522-
523-
uint32_t driverCount = 0;
524-
ze_result_t result = ZE_RESULT_ERROR_UNINITIALIZED;
525-
result = ze_lib::context->loaderDriverGet(&driverCount, nullptr);
526-
if (result != ZE_RESULT_SUCCESS || driverCount == 0) {
527-
if (ze_lib::context->debugTraceEnabled) {
528-
std::string message = "Loader stability check failed. Exiting stability checker thread.";
529-
ze_lib::context->debug_trace_message(message, "");
530-
}
531-
stabilityPromise.set_value(ZEL_STABILITY_CHECK_RESULT_DRIVER_GET_FAILED);
532-
return;
533-
}
534-
stabilityPromise.set_value(ZEL_STABILITY_CHECK_RESULT_SUCCESS);
535-
return;
536-
} catch (...) {
537-
stabilityPromise.set_value(ZEL_STABILITY_CHECK_RESULT_EXCEPTION);
538-
return;
539-
}
540-
}
541-
#endif
542-
543479
/// @brief Registers a teardown callback function to be invoked during loader teardown.
544480
///
545481
/// This function allows an application to register a callback that will be called when the loader is being torn down.
@@ -605,38 +541,50 @@ zelCheckIsLoaderInTearDown() {
605541
return true;
606542
}
607543
#if defined(DYNAMIC_LOAD_LOADER) && defined(_WIN32)
544+
static bool loaderIsStable = true;
545+
if (!loaderIsStable) {
546+
if (ze_lib::context->debugTraceEnabled) {
547+
std::string message = "Loader Teardown check failed before, exiting.";
548+
ze_lib::context->debug_trace_message(message, "");
549+
}
550+
return true;
551+
}
608552
if (ze_lib::loaderTeardownCallbackReceived) {
609553
if (ze_lib::context->debugTraceEnabled) {
610554
std::string message = "Loader Teardown Notification Received, loader in teardown state.";
611555
ze_lib::context->debug_trace_message(message, "");
612556
}
557+
loaderIsStable = false;
613558
return true;
614559
}
615560
if (!ze_lib::loaderTeardownRegistrationEnabled) {
616-
std::promise<int> stabilityPromise;
617-
std::future<int> resultFuture = stabilityPromise.get_future();
618-
int result = -1;
619561
try {
620-
// Launch the stability checker thread
621-
std::thread stabilityThread(stabilityCheck, std::move(stabilityPromise));
622-
result = resultFuture.get(); // Blocks until the result is available
623-
stabilityThread.join();
624-
} catch (const std::exception& e) {
625-
if (ze_lib::context->debugTraceEnabled) {
626-
std::string message = "Exception caught in parent thread: " + std::string(e.what());
627-
ze_lib::context->debug_trace_message(message, "");
562+
if (!ze_lib::context->loaderDriverGet) {
563+
if (ze_lib::context->debugTraceEnabled) {
564+
std::string message = "LoaderDriverGet is a bad pointer. Exiting stability checker.";
565+
ze_lib::context->debug_trace_message(message, "");
566+
}
567+
loaderIsStable = false;
568+
return true;
628569
}
629-
} catch (...) {
630-
if (ze_lib::context->debugTraceEnabled) {
631-
std::string message = "Unknown exception caught in parent thread.";
632-
ze_lib::context->debug_trace_message(message, "");
570+
571+
uint32_t driverCount = 0;
572+
ze_result_t result = ZE_RESULT_ERROR_UNINITIALIZED;
573+
result = ze_lib::context->loaderDriverGet(&driverCount, nullptr);
574+
if (result != ZE_RESULT_SUCCESS || driverCount == 0) {
575+
if (ze_lib::context->debugTraceEnabled) {
576+
std::string message = "Loader stability check failed. Exiting stability checker.";
577+
ze_lib::context->debug_trace_message(message, "");
578+
}
579+
loaderIsStable = false;
580+
return true;
633581
}
634-
}
635-
if (result != ZEL_STABILITY_CHECK_RESULT_SUCCESS) {
582+
} catch (...) {
636583
if (ze_lib::context->debugTraceEnabled) {
637-
std::string message = "Loader stability check failed with result: " + std::to_string(result);
584+
std::string message = "Loader stability check failed. Exception occurred.";
638585
ze_lib::context->debug_trace_message(message, "");
639586
}
587+
loaderIsStable = false;
640588
return true;
641589
}
642590
}

0 commit comments

Comments
 (0)