From 165fa65cc10ad6d1b4096818bd0c754fa7a98261 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Thu, 18 Nov 2021 14:05:25 +0900 Subject: [PATCH] Minor fixes to opencl_handle --- src/opencl_handle.cc | 12 ++++-------- src/opencl_handle.h | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/opencl_handle.cc b/src/opencl_handle.cc index 9cfff81..c2a4443 100644 --- a/src/opencl_handle.cc +++ b/src/opencl_handle.cc @@ -11,7 +11,7 @@ OpenCLContext::OpenCLHandle::OpenCLHandle() OpenCLContext::OpenCLHandle::~OpenCLHandle() { CleanupAllKernels(); - OpenCLContext::CheckRefCount(); + OpenCLContext::CleanupInstance(); } bool OpenCLContext::OpenCLHandle::IsValid() const { @@ -650,13 +650,9 @@ OpenCLContext::OpenCLHandle::Ptr OpenCLContext::GetHandle() { return strong_handle; } -void OpenCLContext::CheckRefCount() { - if (instance_) { - if (instance_->weak_handle_.use_count() <= 1) { - // Last shared_ptr is destructing, cleanup context by calling destructor - instance_.reset(); - } - } +void OpenCLContext::CleanupInstance() { + // OpenCLHandle is destructing, cleanup context by calling destructor + instance_.reset(); } bool OpenCLContext::IsValid() const { return context_ && queue_; } diff --git a/src/opencl_handle.h b/src/opencl_handle.h index 8b955aa..6a22c1e 100644 --- a/src/opencl_handle.h +++ b/src/opencl_handle.h @@ -22,6 +22,14 @@ class OpenCLContext { typedef std::shared_ptr Ptr; typedef std::weak_ptr WeakPtr; + /*! + * \brief A simplified handle to OpenCL + * + * This class can only be obtained by a call to OpenCLContext::GetHandle() + * + * OpenCL is automatically cleaned up when all shared ptrs of OpenCLHandle are + * destructed + */ class OpenCLHandle { public: typedef std::shared_ptr Ptr; @@ -33,9 +41,9 @@ class OpenCLContext { OpenCLHandle(const OpenCLHandle &other) = delete; OpenCLHandle &operator=(const OpenCLHandle &other) = delete; - // allow move - OpenCLHandle(OpenCLHandle &&other) = default; - OpenCLHandle &operator=(OpenCLHandle &&other) = default; + // no move + OpenCLHandle(OpenCLHandle &&other) = delete; + OpenCLHandle &operator=(OpenCLHandle &&other) = delete; bool IsValid() const; @@ -206,6 +214,7 @@ class OpenCLContext { OpenCLContext(OpenCLContext &&other) = delete; OpenCLContext &operator=(OpenCLContext &&other) = delete; + /// Returns a OpenCLHandle wrapped in a std::shared_ptr OpenCLHandle::Ptr GetHandle(); private: @@ -218,7 +227,7 @@ class OpenCLContext { cl_command_queue queue_; cl_device_id device_id_; - static void CheckRefCount(); + static void CleanupInstance(); bool IsValid() const; };