mirror of
https://github.com/intel/compute-runtime.git
synced 2026-01-04 07:14:10 +08:00
Update DestructorCallbacks helper
Move common logic to the helper. Signed-off-by: Filip Hazubski <filip.hazubski@intel.com>
This commit is contained in:
committed by
Compute-Runtime-Automation
parent
96d38539e2
commit
4052525091
@@ -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<std::mutex> theLock(mtx);
|
||||
destructorCallbacks.push_front(cb);
|
||||
destructorCallbacks.add(funcNotify, userData);
|
||||
return CL_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -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 <list>
|
||||
#include <map>
|
||||
@@ -184,7 +184,7 @@ class Context : public BaseObject<_cl_context> {
|
||||
std::map<uint32_t, DeviceBitfield> deviceBitfields;
|
||||
std::vector<std::unique_ptr<SharingFunctions>> sharingFunctions;
|
||||
ClDeviceVector devices;
|
||||
std::list<ContextDestructorCallback *> destructorCallbacks;
|
||||
ContextDestructorCallbacks destructorCallbacks;
|
||||
std::unique_ptr<BuiltInKernel> schedulerBuiltIn;
|
||||
|
||||
const cl_context_properties *properties = nullptr;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -1,32 +0,0 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "CL/cl.h"
|
||||
|
||||
namespace NEO {
|
||||
|
||||
template <typename T>
|
||||
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<cl_context>;
|
||||
using MemObjDestructorCallback = DestructorCallback<cl_mem>;
|
||||
|
||||
} // namespace NEO
|
||||
37
opencl/source/helpers/destructor_callbacks.h
Normal file
37
opencl/source/helpers/destructor_callbacks.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
* Copyright (C) 2020 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
#include "CL/cl.h"
|
||||
|
||||
namespace NEO {
|
||||
|
||||
template <typename T>
|
||||
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<std::pair<CallbackType *, void *>> callbacks;
|
||||
};
|
||||
|
||||
using ContextDestructorCallbacks = DestructorCallbacks<cl_context>;
|
||||
using MemObjDestructorCallbacks = DestructorCallbacks<cl_mem>;
|
||||
|
||||
} // namespace NEO
|
||||
@@ -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<std::mutex> theLock(mtx);
|
||||
destructorCallbacks.push_front(cb);
|
||||
destructorCallbacks.add(funcNotify, userData);
|
||||
return CL_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
@@ -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> sharingHandler;
|
||||
std::vector<uint64_t> propertiesVector;
|
||||
|
||||
std::list<MemObjDestructorCallback *> destructorCallbacks;
|
||||
MemObjDestructorCallbacks destructorCallbacks;
|
||||
};
|
||||
} // namespace NEO
|
||||
|
||||
@@ -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"
|
||||
|
||||
Reference in New Issue
Block a user