Skip to content

Commit 47af6b2

Browse files
committed
Add ability to Register a TeardownCallback to notify release of L0 resources
Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
1 parent 37f2501 commit 47af6b2

3 files changed

Lines changed: 50 additions & 0 deletions

File tree

include/loader/ze_loader.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,17 @@ zelEnableTracingLayer();
9494
ZE_DLLEXPORT bool ZE_APICALL
9595
zelCheckIsLoaderInTearDown();
9696

97+
///////////////////////////////////////////////////////////////////////////////
98+
/// @brief Exported function for registering a callback to indicate teardown.
99+
/// @details The callback function will be invoked when the loader is in teardown.
100+
///
101+
typedef void (*zel_teardown_callback_t)(void);
102+
103+
ZE_DLLEXPORT ze_result_t ZE_APICALL
104+
zelRegisterTeardownCallback(
105+
zel_teardown_callback_t callback // [in] Pointer to the callback function
106+
);
107+
97108
///////////////////////////////////////////////////////////////////////////////
98109
/// @brief Exported function for Disabling the Tracing Layer During Runtime.
99110
///

source/lib/ze_lib.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ namespace ze_lib
2727
}
2828
}
2929
bool delayContextDestruction = false;
30+
bool loaderTeardownCallbackReceived = false;
31+
void teardownCallback() {
32+
loaderTeardownCallbackReceived = true;
33+
}
3034
#endif
3135
bool destruction = false;
3236

@@ -39,6 +43,14 @@ namespace ze_lib
3943
///////////////////////////////////////////////////////////////////////////////
4044
__zedlllocal context_t::~context_t()
4145
{
46+
if (debugTraceEnabled) {
47+
debug_trace_message("ze_lib Context Destructor", "");
48+
}
49+
// Call the teardown callbacks
50+
for (auto &callback : teardownCallbacks) {
51+
callback();
52+
}
53+
teardownCallbacks.clear();
4254
#ifdef DYNAMIC_LOAD_LOADER
4355
if (loader) {
4456
FREE_DRIVER_LIBRARY( loader );
@@ -346,6 +358,7 @@ namespace ze_lib
346358
if (!delayContextDestruction) {
347359
std::atexit(context_at_exit_destructor);
348360
}
361+
zelRegisterTeardownCallback(teardownCallback);
349362
#endif
350363
return result;
351364
}
@@ -397,6 +410,13 @@ zelSetDriverTeardown()
397410
{
398411
ze_result_t result = ZE_RESULT_SUCCESS;
399412
if (!ze_lib::destruction) {
413+
if (ze_lib::context) {
414+
// Call the teardown callbacks
415+
for (auto &callback : ze_lib::context->teardownCallbacks) {
416+
callback();
417+
}
418+
}
419+
400420
ze_lib::destruction = true;
401421
}
402422
return result;
@@ -476,6 +496,18 @@ void stabilityCheck(std::promise<int> stabilityPromise) {
476496
}
477497
#endif
478498

499+
ZE_DLLEXPORT ze_result_t ZE_APICALL
500+
zelRegisterTeardownCallback(
501+
zel_teardown_callback_t callback // [in] Pointer to the callback function
502+
) {
503+
ze_result_t result = ZE_RESULT_SUCCESS;
504+
if (nullptr == callback) {
505+
return ZE_RESULT_ERROR_INVALID_ARGUMENT;
506+
}
507+
ze_lib::context->teardownCallbacks.push_back(callback);
508+
return result;
509+
}
510+
479511
/**
480512
* @brief Checks if the loader is in the process of tearing down.
481513
*
@@ -497,6 +529,9 @@ zelCheckIsLoaderInTearDown() {
497529
return true;
498530
}
499531
#if defined(DYNAMIC_LOAD_LOADER) && defined(_WIN32)
532+
if (ze_lib::loaderTeardownCallbackReceived) {
533+
return true;
534+
}
500535
std::promise<int> stabilityPromise;
501536
std::future<int> resultFuture = stabilityPromise.get_future();
502537
int result = -1;

source/lib/ze_lib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <typeinfo>
2525
#include <iostream>
2626

27+
typedef void (*zel_teardown_callback_t)(void);
28+
2729
namespace ze_lib
2830
{
2931
///////////////////////////////////////////////////////////////////////////////
@@ -175,12 +177,14 @@ namespace ze_lib
175177
bool debugTraceEnabled = false;
176178
bool dynamicTracingSupported = true;
177179
ze_pfnDriverGet_t loaderDriverGet = nullptr;
180+
std::vector<zel_teardown_callback_t> teardownCallbacks;
178181
};
179182

180183
extern bool destruction;
181184
extern context_t *context;
182185
#ifdef DYNAMIC_LOAD_LOADER
183186
extern bool delayContextDestruction;
187+
extern bool loaderTeardownCallbackReceived;
184188
#endif
185189

186190
} // namespace ze_lib

0 commit comments

Comments
 (0)