Skip to content

Commit 6cecaa3

Browse files
committed
Add lock guard around teardown callbacks map
Signed-off-by: Neil R. Spruit <neil.r.spruit@intel.com>
1 parent 2b9b5f1 commit 6cecaa3

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

source/lib/ze_lib.cpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ namespace ze_lib
4949
* @param index The unique identifier of the teardown callback to remove.
5050
*/
5151
void applicationTeardownCallback(uint32_t index) {
52+
std::lock_guard<std::mutex> lock(ze_lib::context->teardownCallbacksMutex);
5253
if (ze_lib::context->teardownCallbacks.find(index) != ze_lib::context->teardownCallbacks.end()) {
5354
if (ze_lib::context->debugTraceEnabled) {
5455
std::string message = "applicationTeardownCallback received for index: " + std::to_string(index);
@@ -506,16 +507,19 @@ zelRegisterTeardownCallback(
506507
if (!ze_lib::context) {
507508
return ZE_RESULT_ERROR_UNINITIALIZED;
508509
}
509-
// Assign the loader's callback function to the application callback such that the application can notify the loader
510-
// that it is tearing down. The loader will then remove the application's callback from the list of callbacks.
511-
*loader_callback = ze_lib::applicationTeardownCallback;
512-
// Increment the teardown callback count and assign the index to the application callback.
513-
ze_lib::context->teardownCallbacksCount.fetch_add(1);
514-
*index = ze_lib::context->teardownCallbacksCount.load();
515-
ze_lib::context->teardownCallbacks.insert(std::pair<uint32_t, zel_loader_teardown_callback_t>(*index, application_callback));
516-
if (ze_lib::context->debugTraceEnabled) {
517-
std::string message = "Registered teardown callback with index: " + std::to_string(*index);
518-
ze_lib::context->debug_trace_message(message, "");
510+
{
511+
std::lock_guard<std::mutex> lock(ze_lib::context->teardownCallbacksMutex);
512+
// Assign the loader's callback function to the application callback such that the application can notify the loader
513+
// that it is tearing down. The loader will then remove the application's callback from the list of callbacks.
514+
*loader_callback = ze_lib::applicationTeardownCallback;
515+
// Increment the teardown callback count and assign the index to the application callback.
516+
ze_lib::context->teardownCallbacksCount.fetch_add(1);
517+
*index = ze_lib::context->teardownCallbacksCount.load();
518+
ze_lib::context->teardownCallbacks.insert(std::pair<uint32_t, zel_loader_teardown_callback_t>(*index, application_callback));
519+
if (ze_lib::context->debugTraceEnabled) {
520+
std::string message = "Registered teardown callback with index: " + std::to_string(*index);
521+
ze_lib::context->debug_trace_message(message, "");
522+
}
519523
}
520524
return result;
521525
}

source/lib/ze_lib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,7 @@ namespace ze_lib
178178
ze_pfnDriverGet_t loaderDriverGet = nullptr;
179179
std::atomic<uint32_t> teardownCallbacksCount{0};
180180
std::map<uint32_t, zel_loader_teardown_callback_t> teardownCallbacks;
181+
std::mutex teardownCallbacksMutex;
181182
#ifdef DYNAMIC_LOAD_LOADER
182183
std::once_flag initTeardownCallbacksOnce;
183184
zel_application_teardown_callback_t loaderTeardownCallback = nullptr;

0 commit comments

Comments
 (0)