diff --git a/unified-runtime/source/adapters/opencl/adapter.cpp b/unified-runtime/source/adapters/opencl/adapter.cpp index 797e1c8582ef8..7f79b95b8903a 100644 --- a/unified-runtime/source/adapters/opencl/adapter.cpp +++ b/unified-runtime/source/adapters/opencl/adapter.cpp @@ -78,7 +78,7 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, } auto &adapter = *phAdapters; - adapter->RefCount++; + adapter->RefCount.retain(); } if (pNumAdapters) { @@ -90,13 +90,13 @@ urAdapterGet(uint32_t NumEntries, ur_adapter_handle_t *phAdapters, UR_APIEXPORT ur_result_t UR_APICALL urAdapterRetain(ur_adapter_handle_t hAdapter) { - ++hAdapter->RefCount; + hAdapter->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urAdapterRelease(ur_adapter_handle_t hAdapter) { - if (--hAdapter->RefCount == 0) { + if (hAdapter->RefCount.release()) { delete hAdapter; } return UR_RESULT_SUCCESS; @@ -119,7 +119,7 @@ urAdapterGetInfo(ur_adapter_handle_t hAdapter, ur_adapter_info_t propName, case UR_ADAPTER_INFO_BACKEND: return ReturnValue(UR_BACKEND_OPENCL); case UR_ADAPTER_INFO_REFERENCE_COUNT: - return ReturnValue(hAdapter->RefCount.load()); + return ReturnValue(hAdapter->RefCount.getCount()); case UR_ADAPTER_INFO_VERSION: return ReturnValue(uint32_t{1}); default: diff --git a/unified-runtime/source/adapters/opencl/adapter.hpp b/unified-runtime/source/adapters/opencl/adapter.hpp index 1a83963343c9c..58dbaed6d6668 100644 --- a/unified-runtime/source/adapters/opencl/adapter.hpp +++ b/unified-runtime/source/adapters/opencl/adapter.hpp @@ -15,6 +15,7 @@ #include "CL/cl.h" #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "logger/ur_logger.hpp" struct ur_adapter_handle_t_ : ur::opencl::handle_base { @@ -24,13 +25,14 @@ struct ur_adapter_handle_t_ : ur::opencl::handle_base { ur_adapter_handle_t_(ur_adapter_handle_t_ &) = delete; ur_adapter_handle_t_ &operator=(const ur_adapter_handle_t_ &) = delete; - std::atomic RefCount = 0; logger::Logger &log = logger::get_logger("opencl"); cl_ext::ExtFuncPtrCacheT fnCache{}; std::vector> URPlatforms; uint32_t NumPlatforms = 0; + ur::RefCount RefCount; + // Function pointers to core OpenCL entry points which may not exist in older // versions of the OpenCL-ICD-Loader are tracked here and initialized by // dynamically loading the symbol by name. diff --git a/unified-runtime/source/adapters/opencl/command_buffer.cpp b/unified-runtime/source/adapters/opencl/command_buffer.cpp index e048b2d22175c..f7981973ff548 100644 --- a/unified-runtime/source/adapters/opencl/command_buffer.cpp +++ b/unified-runtime/source/adapters/opencl/command_buffer.cpp @@ -108,13 +108,13 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferCreateExp( UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferRetainExp(ur_exp_command_buffer_handle_t hCommandBuffer) { - hCommandBuffer->incrementReferenceCount(); + hCommandBuffer->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferReleaseExp(ur_exp_command_buffer_handle_t hCommandBuffer) { - if (hCommandBuffer->decrementReferenceCount() == 0) { + if (hCommandBuffer->RefCount.release()) { delete hCommandBuffer; } @@ -783,7 +783,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urCommandBufferGetInfoExp( switch (propName) { case UR_EXP_COMMAND_BUFFER_INFO_REFERENCE_COUNT: - return ReturnValue(hCommandBuffer->getReferenceCount()); + return ReturnValue(hCommandBuffer->RefCount.getCount()); case UR_EXP_COMMAND_BUFFER_INFO_DESCRIPTOR: { ur_exp_command_buffer_desc_t Descriptor{}; Descriptor.stype = UR_STRUCTURE_TYPE_EXP_COMMAND_BUFFER_DESC; diff --git a/unified-runtime/source/adapters/opencl/command_buffer.hpp b/unified-runtime/source/adapters/opencl/command_buffer.hpp index e14989d0ff34f..486150152065b 100644 --- a/unified-runtime/source/adapters/opencl/command_buffer.hpp +++ b/unified-runtime/source/adapters/opencl/command_buffer.hpp @@ -9,6 +9,7 @@ //===----------------------------------------------------------------------===// #include "common.hpp" +#include "common/ur_ref_count.hpp" #include /// Handle to a kernel command. @@ -53,11 +54,11 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base { /// List of commands in the command-buffer. std::vector> CommandHandles; - /// Object reference count - std::atomic_uint32_t RefCount; /// Track last submission of the command-buffer cl_event LastSubmission; + ur::RefCount RefCount; + ur_exp_command_buffer_handle_t_(ur_queue_handle_t hQueue, ur_context_handle_t hContext, ur_device_handle_t hDevice, @@ -66,11 +67,7 @@ struct ur_exp_command_buffer_handle_t_ : ur::opencl::handle_base { : handle_base(), hInternalQueue(hQueue), hContext(hContext), hDevice(hDevice), CLCommandBuffer(CLCommandBuffer), IsUpdatable(IsUpdatable), IsInOrder(IsInOrder), IsFinalized(false), - RefCount(0), LastSubmission(nullptr) {} + LastSubmission(nullptr) {} ~ur_exp_command_buffer_handle_t_(); - - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - uint32_t getReferenceCount() const noexcept { return RefCount; } }; diff --git a/unified-runtime/source/adapters/opencl/context.cpp b/unified-runtime/source/adapters/opencl/context.cpp index 9409dd960838a..30f6f5b878e5e 100644 --- a/unified-runtime/source/adapters/opencl/context.cpp +++ b/unified-runtime/source/adapters/opencl/context.cpp @@ -108,7 +108,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName, return ReturnValue(&hContext->Devices[0], hContext->DeviceCount); } case UR_CONTEXT_INFO_REFERENCE_COUNT: { - return ReturnValue(hContext->getReferenceCount()); + return ReturnValue(hContext->RefCount.getCount()); } default: return UR_RESULT_ERROR_INVALID_ENUMERATION; @@ -117,7 +117,7 @@ urContextGetInfo(ur_context_handle_t hContext, ur_context_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urContextRelease(ur_context_handle_t hContext) { - if (hContext->decrementReferenceCount() == 0) { + if (hContext->RefCount.release()) { delete hContext; } @@ -126,7 +126,7 @@ urContextRelease(ur_context_handle_t hContext) { UR_APIEXPORT ur_result_t UR_APICALL urContextRetain(ur_context_handle_t hContext) { - hContext->incrementReferenceCount(); + hContext->RefCount.retain(); return UR_RESULT_SUCCESS; } diff --git a/unified-runtime/source/adapters/opencl/context.hpp b/unified-runtime/source/adapters/opencl/context.hpp index b3a136ca2de8b..237907e3ad5b6 100644 --- a/unified-runtime/source/adapters/opencl/context.hpp +++ b/unified-runtime/source/adapters/opencl/context.hpp @@ -11,6 +11,7 @@ #include "adapter.hpp" #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "device.hpp" #include @@ -20,8 +21,8 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { native_type CLContext; std::vector Devices; uint32_t DeviceCount; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; + ur::RefCount RefCount; ur_context_handle_t_(native_type Ctx, uint32_t DevCount, const ur_device_handle_t *phDevices) @@ -30,15 +31,8 @@ struct ur_context_handle_t_ : ur::opencl::handle_base { Devices.emplace_back(phDevices[i]); urDeviceRetain(phDevices[i]); } - RefCount = 1; } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - static ur_result_t makeWithNative(native_type Ctx, uint32_t DevCount, const ur_device_handle_t *phDevices, ur_context_handle_t &Context); diff --git a/unified-runtime/source/adapters/opencl/device.cpp b/unified-runtime/source/adapters/opencl/device.cpp index 4e0de3da8e710..d0ab2376f5ca4 100644 --- a/unified-runtime/source/adapters/opencl/device.cpp +++ b/unified-runtime/source/adapters/opencl/device.cpp @@ -1019,7 +1019,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceGetInfo(ur_device_handle_t hDevice, return UR_RESULT_SUCCESS; } case UR_DEVICE_INFO_REFERENCE_COUNT: { - return ReturnValue(hDevice->getReferenceCount()); + return ReturnValue(hDevice->RefCount.getCount()); } case UR_DEVICE_INFO_PARTITION_MAX_SUB_DEVICES: { CL_RETURN_ON_FAILURE(clGetDeviceInfo(hDevice->CLDevice, @@ -1561,7 +1561,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDevicePartition( // Root devices ref count are unchanged through out the program lifetime. UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - hDevice->incrementReferenceCount(); + hDevice->RefCount.retain(); } return UR_RESULT_SUCCESS; @@ -1571,7 +1571,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urDeviceRetain(ur_device_handle_t hDevice) { UR_APIEXPORT ur_result_t UR_APICALL urDeviceRelease(ur_device_handle_t hDevice) { if (hDevice->ParentDevice) { - if (hDevice->decrementReferenceCount() == 0) { + if (hDevice->RefCount.release()) { delete hDevice; } } diff --git a/unified-runtime/source/adapters/opencl/device.hpp b/unified-runtime/source/adapters/opencl/device.hpp index 1c100535c6643..71adb063964f0 100644 --- a/unified-runtime/source/adapters/opencl/device.hpp +++ b/unified-runtime/source/adapters/opencl/device.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "device.hpp" #include "platform.hpp" @@ -19,13 +20,12 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { ur_platform_handle_t Platform; cl_device_type Type = 0; ur_device_handle_t ParentDevice = nullptr; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; + ur::RefCount RefCount; ur_device_handle_t_(native_type Dev, ur_platform_handle_t Plat, ur_device_handle_t Parent) : handle_base(), CLDevice(Dev), Platform(Plat), ParentDevice(Parent) { - RefCount = 1; if (Parent) { Type = Parent->Type; [[maybe_unused]] auto Res = clRetainDevice(CLDevice); @@ -51,12 +51,6 @@ struct ur_device_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - ur_result_t getDeviceVersion(oclv::OpenCLVersion &Version) { size_t DevVerSize = 0; CL_RETURN_ON_FAILURE( diff --git a/unified-runtime/source/adapters/opencl/event.cpp b/unified-runtime/source/adapters/opencl/event.cpp index bb13f297b60bf..3452cae59ac98 100644 --- a/unified-runtime/source/adapters/opencl/event.cpp +++ b/unified-runtime/source/adapters/opencl/event.cpp @@ -136,14 +136,14 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetNativeHandle( } UR_APIEXPORT ur_result_t UR_APICALL urEventRelease(ur_event_handle_t hEvent) { - if (hEvent->decrementReferenceCount() == 0) { + if (hEvent->RefCount.release()) { delete hEvent; } return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urEventRetain(ur_event_handle_t hEvent) { - hEvent->incrementReferenceCount(); + hEvent->RefCount.retain(); return UR_RESULT_SUCCESS; } @@ -188,7 +188,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urEventGetInfo(ur_event_handle_t hEvent, return ReturnValue(hEvent->Queue); } case UR_EVENT_INFO_REFERENCE_COUNT: { - return ReturnValue(hEvent->getReferenceCount()); + return ReturnValue(hEvent->RefCount.getCount()); } default: { size_t CheckPropSize = 0; diff --git a/unified-runtime/source/adapters/opencl/event.hpp b/unified-runtime/source/adapters/opencl/event.hpp index dacddf3d8e1f6..33b74386fa77c 100644 --- a/unified-runtime/source/adapters/opencl/event.hpp +++ b/unified-runtime/source/adapters/opencl/event.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "queue.hpp" #include @@ -19,13 +20,12 @@ struct ur_event_handle_t_ : ur::opencl::handle_base { native_type CLEvent; ur_context_handle_t Context; ur_queue_handle_t Queue; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; + ur::RefCount RefCount; ur_event_handle_t_(native_type Event, ur_context_handle_t Ctx, ur_queue_handle_t Queue) : handle_base(), CLEvent(Event), Context(Ctx), Queue(Queue) { - RefCount = 1; urContextRetain(Context); if (Queue) { urQueueRetain(Queue); @@ -42,12 +42,6 @@ struct ur_event_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - ur_result_t ensureQueue() { if (!Queue) { cl_command_queue native_queue; diff --git a/unified-runtime/source/adapters/opencl/kernel.cpp b/unified-runtime/source/adapters/opencl/kernel.cpp index f0c22a99749a9..b673f9743766c 100644 --- a/unified-runtime/source/adapters/opencl/kernel.cpp +++ b/unified-runtime/source/adapters/opencl/kernel.cpp @@ -152,7 +152,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urKernelGetInfo(ur_kernel_handle_t hKernel, return ReturnValue(hKernel->Context); } case UR_KERNEL_INFO_REFERENCE_COUNT: { - return ReturnValue(hKernel->getReferenceCount()); + return ReturnValue(hKernel->RefCount.getCount()); } default: { size_t CheckPropSize = 0; @@ -343,13 +343,13 @@ urKernelGetSubGroupInfo(ur_kernel_handle_t hKernel, ur_device_handle_t hDevice, } UR_APIEXPORT ur_result_t UR_APICALL urKernelRetain(ur_kernel_handle_t hKernel) { - hKernel->incrementReferenceCount(); + hKernel->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urKernelRelease(ur_kernel_handle_t hKernel) { - if (hKernel->decrementReferenceCount() == 0) { + if (hKernel->RefCount.release()) { delete hKernel; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/kernel.hpp b/unified-runtime/source/adapters/opencl/kernel.hpp index ef73e47c1e319..23bbe62face79 100644 --- a/unified-runtime/source/adapters/opencl/kernel.hpp +++ b/unified-runtime/source/adapters/opencl/kernel.hpp @@ -11,6 +11,7 @@ #include "adapter.hpp" #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "context.hpp" #include "program.hpp" @@ -21,14 +22,13 @@ struct ur_kernel_handle_t_ : ur::opencl::handle_base { native_type CLKernel; ur_program_handle_t Program; ur_context_handle_t Context; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; clSetKernelArgMemPointerINTEL_fn clSetKernelArgMemPointerINTEL = nullptr; + ur::RefCount RefCount; ur_kernel_handle_t_(native_type Kernel, ur_program_handle_t Program, ur_context_handle_t Context) : handle_base(), CLKernel(Kernel), Program(Program), Context(Context) { - RefCount = 1; urProgramRetain(Program); urContextRetain(Context); @@ -46,12 +46,6 @@ struct ur_kernel_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - static ur_result_t makeWithNative(native_type NativeKernel, ur_program_handle_t Program, ur_context_handle_t Context, diff --git a/unified-runtime/source/adapters/opencl/memory.cpp b/unified-runtime/source/adapters/opencl/memory.cpp index 62698e6105520..19e9509987825 100644 --- a/unified-runtime/source/adapters/opencl/memory.cpp +++ b/unified-runtime/source/adapters/opencl/memory.cpp @@ -521,7 +521,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemGetInfo(ur_mem_handle_t hMemory, return ReturnValue(hMemory->Context); } case UR_MEM_INFO_REFERENCE_COUNT: { - return ReturnValue(hMemory->getReferenceCount()); + return ReturnValue(hMemory->RefCount.getCount()); } default: { size_t CheckPropSize = 0; @@ -569,12 +569,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urMemImageGetInfo(ur_mem_handle_t hMemory, } UR_APIEXPORT ur_result_t UR_APICALL urMemRetain(ur_mem_handle_t hMem) { - hMem->incrementReferenceCount(); + hMem->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urMemRelease(ur_mem_handle_t hMem) { - if (hMem->decrementReferenceCount() == 0) { + if (hMem->RefCount.release()) { delete hMem; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/memory.hpp b/unified-runtime/source/adapters/opencl/memory.hpp index a0f8410e3df03..847ffafa76021 100644 --- a/unified-runtime/source/adapters/opencl/memory.hpp +++ b/unified-runtime/source/adapters/opencl/memory.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "context.hpp" #include @@ -18,12 +19,11 @@ struct ur_mem_handle_t_ : ur::opencl::handle_base { using native_type = cl_mem; native_type CLMemory; ur_context_handle_t Context; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; + ur::RefCount RefCount; ur_mem_handle_t_(native_type Mem, ur_context_handle_t Ctx) : handle_base(), CLMemory(Mem), Context(Ctx) { - RefCount = 1; urContextRetain(Context); } @@ -34,12 +34,6 @@ struct ur_mem_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - static ur_result_t makeWithNative(native_type NativeMem, ur_context_handle_t Ctx, ur_mem_handle_t &Mem); diff --git a/unified-runtime/source/adapters/opencl/program.cpp b/unified-runtime/source/adapters/opencl/program.cpp index 1c3a5e45b3bd5..dffd9aed9074b 100644 --- a/unified-runtime/source/adapters/opencl/program.cpp +++ b/unified-runtime/source/adapters/opencl/program.cpp @@ -223,7 +223,7 @@ urProgramGetInfo(ur_program_handle_t hProgram, ur_program_info_t propName, return ReturnValue(hProgram->Devices.data(), hProgram->NumDevices); } case UR_PROGRAM_INFO_REFERENCE_COUNT: { - return ReturnValue(hProgram->getReferenceCount()); + return ReturnValue(hProgram->RefCount.getCount()); } default: { size_t CheckPropSize = 0; @@ -383,13 +383,13 @@ urProgramGetBuildInfo(ur_program_handle_t hProgram, ur_device_handle_t hDevice, UR_APIEXPORT ur_result_t UR_APICALL urProgramRetain(ur_program_handle_t hProgram) { - hProgram->incrementReferenceCount(); + hProgram->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urProgramRelease(ur_program_handle_t hProgram) { - if (hProgram->decrementReferenceCount() == 0) { + if (hProgram->RefCount.release()) { delete hProgram; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/program.hpp b/unified-runtime/source/adapters/opencl/program.hpp index 69b3430d2bc3a..70487dd8e0edc 100644 --- a/unified-runtime/source/adapters/opencl/program.hpp +++ b/unified-runtime/source/adapters/opencl/program.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "context.hpp" #include @@ -18,15 +19,14 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { using native_type = cl_program; native_type CLProgram; ur_context_handle_t Context; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; uint32_t NumDevices = 0; std::vector Devices; + ur::RefCount RefCount; ur_program_handle_t_(native_type Prog, ur_context_handle_t Ctx, uint32_t NumDevices, ur_device_handle_t *Devs) : handle_base(), CLProgram(Prog), Context(Ctx), NumDevices(NumDevices) { - RefCount = 1; urContextRetain(Context); for (uint32_t i = 0; i < NumDevices; i++) { Devices.push_back(Devs[i]); @@ -40,12 +40,6 @@ struct ur_program_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - static ur_result_t makeWithNative(native_type NativeProg, ur_context_handle_t Context, ur_program_handle_t &Program); diff --git a/unified-runtime/source/adapters/opencl/queue.cpp b/unified-runtime/source/adapters/opencl/queue.cpp index 040186e701647..e143d6b912a87 100644 --- a/unified-runtime/source/adapters/opencl/queue.cpp +++ b/unified-runtime/source/adapters/opencl/queue.cpp @@ -234,7 +234,7 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueGetInfo(ur_queue_handle_t hQueue, return ReturnValue(mapCLQueuePropsToUR(QueueProperties)); } case UR_QUEUE_INFO_REFERENCE_COUNT: { - return ReturnValue(hQueue->getReferenceCount()); + return ReturnValue(hQueue->RefCount.getCount()); } default: { size_t CheckPropSize = 0; @@ -289,12 +289,12 @@ UR_APIEXPORT ur_result_t UR_APICALL urQueueFlush(ur_queue_handle_t hQueue) { } UR_APIEXPORT ur_result_t UR_APICALL urQueueRetain(ur_queue_handle_t hQueue) { - hQueue->incrementReferenceCount(); + hQueue->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urQueueRelease(ur_queue_handle_t hQueue) { - if (hQueue->decrementReferenceCount() == 0) { + if (hQueue->RefCount.release()) { delete hQueue; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/queue.hpp b/unified-runtime/source/adapters/opencl/queue.hpp index 5fd429d7b0518..9456c8ae8618d 100644 --- a/unified-runtime/source/adapters/opencl/queue.hpp +++ b/unified-runtime/source/adapters/opencl/queue.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include "context.hpp" #include "device.hpp" @@ -22,17 +23,16 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { ur_device_handle_t Device; // Used to keep a handle to the default queue alive if it is different std::optional DeviceDefault = std::nullopt; - std::atomic RefCount = 0; bool IsNativeHandleOwned = true; // Used to implement UR_QUEUE_INFO_EMPTY query bool IsInOrder; ur_event_handle_t LastEvent = nullptr; + ur::RefCount RefCount; ur_queue_handle_t_(native_type Queue, ur_context_handle_t Ctx, ur_device_handle_t Dev, bool InOrder) : handle_base(), CLQueue(Queue), Context(Ctx), Device(Dev), IsInOrder(InOrder) { - RefCount = 1; urDeviceRetain(Device); urContextRetain(Context); } @@ -53,12 +53,6 @@ struct ur_queue_handle_t_ : ur::opencl::handle_base { } } - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } - // Stores last event for in-order queues. Has no effect if queue is Out Of // Order. The last event is used to implement UR_QUEUE_INFO_EMPTY query. ur_result_t storeLastEvent(ur_event_handle_t Event) { diff --git a/unified-runtime/source/adapters/opencl/sampler.cpp b/unified-runtime/source/adapters/opencl/sampler.cpp index 69f3f5167a986..ee2a1f9958b5d 100644 --- a/unified-runtime/source/adapters/opencl/sampler.cpp +++ b/unified-runtime/source/adapters/opencl/sampler.cpp @@ -175,7 +175,7 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, return ReturnValue(hSampler->Context); } case UR_SAMPLER_INFO_REFERENCE_COUNT: { - return ReturnValue(hSampler->getReferenceCount()); + return ReturnValue(hSampler->RefCount.getCount()); } // ur_bool_t have a size of uint8_t, but cl_bool size have the size of // uint32_t so this adjust UR_SAMPLER_INFO_NORMALIZED_COORDS info to map @@ -221,13 +221,13 @@ urSamplerGetInfo(ur_sampler_handle_t hSampler, ur_sampler_info_t propName, UR_APIEXPORT ur_result_t UR_APICALL urSamplerRetain(ur_sampler_handle_t hSampler) { - hSampler->incrementReferenceCount(); + hSampler->RefCount.retain(); return UR_RESULT_SUCCESS; } UR_APIEXPORT ur_result_t UR_APICALL urSamplerRelease(ur_sampler_handle_t hSampler) { - if (hSampler->decrementReferenceCount() == 0) { + if (hSampler->RefCount.release()) { delete hSampler; } return UR_RESULT_SUCCESS; diff --git a/unified-runtime/source/adapters/opencl/sampler.hpp b/unified-runtime/source/adapters/opencl/sampler.hpp index b661fe195bcb2..26a116c8df0f7 100644 --- a/unified-runtime/source/adapters/opencl/sampler.hpp +++ b/unified-runtime/source/adapters/opencl/sampler.hpp @@ -10,6 +10,7 @@ #pragma once #include "common.hpp" +#include "common/ur_ref_count.hpp" #include @@ -17,12 +18,11 @@ struct ur_sampler_handle_t_ : ur::opencl::handle_base { using native_type = cl_sampler; native_type CLSampler; ur_context_handle_t Context; - std::atomic RefCount = 0; bool IsNativeHandleOwned = false; + ur::RefCount RefCount; ur_sampler_handle_t_(native_type Sampler, ur_context_handle_t Ctx) : handle_base(), CLSampler(Sampler), Context(Ctx) { - RefCount = 1; urContextRetain(Context); } @@ -32,10 +32,4 @@ struct ur_sampler_handle_t_ : ur::opencl::handle_base { clReleaseSampler(CLSampler); } } - - uint32_t incrementReferenceCount() noexcept { return ++RefCount; } - - uint32_t decrementReferenceCount() noexcept { return --RefCount; } - - uint32_t getReferenceCount() const noexcept { return RefCount; } }; diff --git a/unified-runtime/source/common/ur_ref_count.hpp b/unified-runtime/source/common/ur_ref_count.hpp index 7815e1dab65d0..5f0eebd9a6f03 100644 --- a/unified-runtime/source/common/ur_ref_count.hpp +++ b/unified-runtime/source/common/ur_ref_count.hpp @@ -8,8 +8,8 @@ * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception * */ -#ifndef URREFCOUNT_HPP -#define URREFCOUNT_HPP 1 +#ifndef UR_REF_COUNT_HPP +#define UR_REF_COUNT_HPP 1 #include #include @@ -33,4 +33,4 @@ class RefCount { } // namespace ur -#endif // URREFCOUNT_HPP +#endif // UR_REF_COUNT_HPP