Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions include/loader/ze_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ zelEnableTracingLayer();
ZE_DLLEXPORT bool ZE_APICALL
zelCheckIsLoaderInTearDown();

///////////////////////////////////////////////////////////////////////////////
/// @brief Exported function for registering a callback to indicate teardown.
/// @details The callback function will be invoked when the loader is in teardown.
///
typedef void (*zel_teardown_callback_t)(void);

ZE_DLLEXPORT ze_result_t ZE_APICALL
zelRegisterTeardownCallback(
zel_teardown_callback_t callback // [in] Pointer to the callback function
);

///////////////////////////////////////////////////////////////////////////////
/// @brief Exported function for Disabling the Tracing Layer During Runtime.
///
Expand Down
35 changes: 35 additions & 0 deletions source/lib/ze_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ namespace ze_lib
}
}
bool delayContextDestruction = false;
bool loaderTeardownCallbackReceived = false;
void teardownCallback() {
loaderTeardownCallbackReceived = true;
}
#endif
bool destruction = false;

Expand All @@ -39,6 +43,14 @@ namespace ze_lib
///////////////////////////////////////////////////////////////////////////////
__zedlllocal context_t::~context_t()
{
if (debugTraceEnabled) {
debug_trace_message("ze_lib Context Destructor", "");
}
// Call the teardown callbacks
for (auto &callback : teardownCallbacks) {
callback();
}
teardownCallbacks.clear();
#ifdef DYNAMIC_LOAD_LOADER
if (loader) {
FREE_DRIVER_LIBRARY( loader );
Expand Down Expand Up @@ -346,6 +358,7 @@ namespace ze_lib
if (!delayContextDestruction) {
std::atexit(context_at_exit_destructor);
}
zelRegisterTeardownCallback(teardownCallback);
#endif
return result;
}
Expand Down Expand Up @@ -397,6 +410,13 @@ zelSetDriverTeardown()
{
ze_result_t result = ZE_RESULT_SUCCESS;
if (!ze_lib::destruction) {
if (ze_lib::context) {
// Call the teardown callbacks
for (auto &callback : ze_lib::context->teardownCallbacks) {
callback();
}
}

ze_lib::destruction = true;
}
return result;
Expand Down Expand Up @@ -476,6 +496,18 @@ void stabilityCheck(std::promise<int> stabilityPromise) {
}
#endif

ZE_DLLEXPORT ze_result_t ZE_APICALL
zelRegisterTeardownCallback(
zel_teardown_callback_t callback // [in] Pointer to the callback function
) {
ze_result_t result = ZE_RESULT_SUCCESS;
if (nullptr == callback) {
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
}
ze_lib::context->teardownCallbacks.push_back(callback);
return result;
}

/**
* @brief Checks if the loader is in the process of tearing down.
*
Expand All @@ -497,6 +529,9 @@ zelCheckIsLoaderInTearDown() {
return true;
}
#if defined(DYNAMIC_LOAD_LOADER) && defined(_WIN32)
if (ze_lib::loaderTeardownCallbackReceived) {
return true;
}
std::promise<int> stabilityPromise;
std::future<int> resultFuture = stabilityPromise.get_future();
int result = -1;
Expand Down
4 changes: 4 additions & 0 deletions source/lib/ze_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <typeinfo>
#include <iostream>

typedef void (*zel_teardown_callback_t)(void);

namespace ze_lib
{
///////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -175,12 +177,14 @@ namespace ze_lib
bool debugTraceEnabled = false;
bool dynamicTracingSupported = true;
ze_pfnDriverGet_t loaderDriverGet = nullptr;
std::vector<zel_teardown_callback_t> teardownCallbacks;
};

extern bool destruction;
extern context_t *context;
#ifdef DYNAMIC_LOAD_LOADER
extern bool delayContextDestruction;
extern bool loaderTeardownCallbackReceived;
#endif

} // namespace ze_lib
Loading