@@ -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}
0 commit comments