diff --git a/opencl/source/context/context.cpp b/opencl/source/context/context.cpp index a7c5093380..5c3b377d19 100644 --- a/opencl/source/context/context.cpp +++ b/opencl/source/context/context.cpp @@ -65,10 +65,7 @@ Context::~Context() { memoryManager->getDeferredDeleter()->removeClient(); } gtpinNotifyContextDestroy((cl_context)this); - for (auto callback : destructorCallbacks) { - callback->invoke(this); - delete callback; - } + destructorCallbacks.invoke(this); for (auto &device : devices) { device->decRefInternal(); } @@ -80,10 +77,8 @@ Context::~Context() { cl_int Context::setDestructorCallback(void(CL_CALLBACK *funcNotify)(cl_context, void *), void *userData) { - auto cb = new ContextDestructorCallback(funcNotify, userData); - std::unique_lock theLock(mtx); - destructorCallbacks.push_front(cb); + destructorCallbacks.add(funcNotify, userData); return CL_SUCCESS; } diff --git a/opencl/source/context/context.h b/opencl/source/context/context.h index ed4b3e4273..f3917b1899 100644 --- a/opencl/source/context/context.h +++ b/opencl/source/context/context.h @@ -14,7 +14,7 @@ #include "opencl/source/context/context_type.h" #include "opencl/source/context/driver_diagnostics.h" #include "opencl/source/helpers/base_object.h" -#include "opencl/source/helpers/destructor_callback.h" +#include "opencl/source/helpers/destructor_callbacks.h" #include #include @@ -184,7 +184,7 @@ class Context : public BaseObject<_cl_context> { std::map deviceBitfields; std::vector> sharingFunctions; ClDeviceVector devices; - std::list destructorCallbacks; + ContextDestructorCallbacks destructorCallbacks; std::unique_ptr schedulerBuiltIn; const cl_context_properties *properties = nullptr; diff --git a/opencl/source/helpers/CMakeLists.txt b/opencl/source/helpers/CMakeLists.txt index 38ce6efb93..450cb3d4ff 100644 --- a/opencl/source/helpers/CMakeLists.txt +++ b/opencl/source/helpers/CMakeLists.txt @@ -17,7 +17,7 @@ set(RUNTIME_SRCS_HELPERS_BASE ${CMAKE_CURRENT_SOURCE_DIR}/cl_hw_helper.h ${CMAKE_CURRENT_SOURCE_DIR}/cl_hw_helper_base.inl ${CMAKE_CURRENT_SOURCE_DIR}/convert_color.h - ${CMAKE_CURRENT_SOURCE_DIR}/destructor_callback.h + ${CMAKE_CURRENT_SOURCE_DIR}/destructor_callbacks.h ${CMAKE_CURRENT_SOURCE_DIR}/dispatch_info.cpp ${CMAKE_CURRENT_SOURCE_DIR}/dispatch_info.h ${CMAKE_CURRENT_SOURCE_DIR}/dispatch_info_builder.h diff --git a/opencl/source/helpers/destructor_callback.h b/opencl/source/helpers/destructor_callback.h deleted file mode 100644 index 3c8b7fc4f4..0000000000 --- a/opencl/source/helpers/destructor_callback.h +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2020 Intel Corporation - * - * SPDX-License-Identifier: MIT - * - */ - -#pragma once -#include "CL/cl.h" - -namespace NEO { - -template -class DestructorCallback { - public: - DestructorCallback(void(CL_CALLBACK *funcNotify)(T, void *), - void *userData) - : funcNotify(funcNotify), userData(userData){}; - - inline void invoke(T object) { - this->funcNotify(object, userData); - } - - private: - void(CL_CALLBACK *funcNotify)(T, void *); - void *userData; -}; - -using ContextDestructorCallback = DestructorCallback; -using MemObjDestructorCallback = DestructorCallback; - -} // namespace NEO diff --git a/opencl/source/helpers/destructor_callbacks.h b/opencl/source/helpers/destructor_callbacks.h new file mode 100644 index 0000000000..4caea2f5b3 --- /dev/null +++ b/opencl/source/helpers/destructor_callbacks.h @@ -0,0 +1,37 @@ +/* + * Copyright (C) 2020 Intel Corporation + * + * SPDX-License-Identifier: MIT + * + */ + +#pragma once +#include "CL/cl.h" + +namespace NEO { + +template +class DestructorCallbacks { + using CallbackType = void CL_CALLBACK(T, void *); + + public: + inline void add(CallbackType *callback, void *userData) { + callbacks.push_back({callback, userData}); + } + inline bool empty() { + return callbacks.empty(); + } + inline void invoke(T object) { + for (auto it = callbacks.rbegin(); it != callbacks.rend(); it++) { + it->first(object, it->second); + } + } + + private: + std::vector> callbacks; +}; + +using ContextDestructorCallbacks = DestructorCallbacks; +using MemObjDestructorCallbacks = DestructorCallbacks; + +} // namespace NEO diff --git a/opencl/source/mem_obj/mem_obj.cpp b/opencl/source/mem_obj/mem_obj.cpp index ee991a52a8..efc2c1bea3 100644 --- a/opencl/source/mem_obj/mem_obj.cpp +++ b/opencl/source/mem_obj/mem_obj.cpp @@ -105,10 +105,7 @@ MemObj::~MemObj() { releaseAllocatedMapPtr(); } } - for (auto callback : destructorCallbacks) { - callback->invoke(this); - delete callback; - } + destructorCallbacks.invoke(this); context->decRefInternal(); } @@ -214,10 +211,8 @@ cl_int MemObj::getMemObjectInfo(cl_mem_info paramName, cl_int MemObj::setDestructorCallback(void(CL_CALLBACK *funcNotify)(cl_mem, void *), void *userData) { - auto cb = new MemObjDestructorCallback(funcNotify, userData); - std::unique_lock theLock(mtx); - destructorCallbacks.push_front(cb); + destructorCallbacks.add(funcNotify, userData); return CL_SUCCESS; } diff --git a/opencl/source/mem_obj/mem_obj.h b/opencl/source/mem_obj/mem_obj.h index 1829fd72f1..8dffa6b536 100644 --- a/opencl/source/mem_obj/mem_obj.h +++ b/opencl/source/mem_obj/mem_obj.h @@ -12,7 +12,7 @@ #include "opencl/extensions/public/cl_ext_private.h" #include "opencl/source/api/cl_types.h" #include "opencl/source/helpers/base_object.h" -#include "opencl/source/helpers/destructor_callback.h" +#include "opencl/source/helpers/destructor_callbacks.h" #include "opencl/source/helpers/mipmap.h" #include "opencl/source/mem_obj/map_operations_handler.h" #include "opencl/source/sharings/sharing.h" @@ -163,6 +163,6 @@ class MemObj : public BaseObject<_cl_mem> { std::shared_ptr sharingHandler; std::vector propertiesVector; - std::list destructorCallbacks; + MemObjDestructorCallbacks destructorCallbacks; }; } // namespace NEO diff --git a/opencl/source/program/program.h b/opencl/source/program/program.h index e3eedebf5d..a11f70e2d7 100644 --- a/opencl/source/program/program.h +++ b/opencl/source/program/program.h @@ -16,7 +16,6 @@ #include "opencl/source/api/cl_types.h" #include "opencl/source/cl_device/cl_device_vector.h" #include "opencl/source/helpers/base_object.h" -#include "opencl/source/helpers/destructor_callback.h" #include "cif/builtins/memory/buffer/buffer.h" #include "patch_list.h"