Minor fixes to opencl_handle

This commit is contained in:
Stephen Seo 2021-11-18 14:05:25 +09:00
parent e3d1012af8
commit 165fa65cc1
2 changed files with 17 additions and 12 deletions

View file

@ -11,7 +11,7 @@ OpenCLContext::OpenCLHandle::OpenCLHandle()
OpenCLContext::OpenCLHandle::~OpenCLHandle() { OpenCLContext::OpenCLHandle::~OpenCLHandle() {
CleanupAllKernels(); CleanupAllKernels();
OpenCLContext::CheckRefCount(); OpenCLContext::CleanupInstance();
} }
bool OpenCLContext::OpenCLHandle::IsValid() const { bool OpenCLContext::OpenCLHandle::IsValid() const {
@ -650,13 +650,9 @@ OpenCLContext::OpenCLHandle::Ptr OpenCLContext::GetHandle() {
return strong_handle; return strong_handle;
} }
void OpenCLContext::CheckRefCount() { void OpenCLContext::CleanupInstance() {
if (instance_) { // OpenCLHandle is destructing, cleanup context by calling destructor
if (instance_->weak_handle_.use_count() <= 1) {
// Last shared_ptr is destructing, cleanup context by calling destructor
instance_.reset(); instance_.reset();
} }
}
}
bool OpenCLContext::IsValid() const { return context_ && queue_; } bool OpenCLContext::IsValid() const { return context_ && queue_; }

View file

@ -22,6 +22,14 @@ class OpenCLContext {
typedef std::shared_ptr<OpenCLContext> Ptr; typedef std::shared_ptr<OpenCLContext> Ptr;
typedef std::weak_ptr<OpenCLContext> WeakPtr; typedef std::weak_ptr<OpenCLContext> 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 { class OpenCLHandle {
public: public:
typedef std::shared_ptr<OpenCLHandle> Ptr; typedef std::shared_ptr<OpenCLHandle> Ptr;
@ -33,9 +41,9 @@ class OpenCLContext {
OpenCLHandle(const OpenCLHandle &other) = delete; OpenCLHandle(const OpenCLHandle &other) = delete;
OpenCLHandle &operator=(const OpenCLHandle &other) = delete; OpenCLHandle &operator=(const OpenCLHandle &other) = delete;
// allow move // no move
OpenCLHandle(OpenCLHandle &&other) = default; OpenCLHandle(OpenCLHandle &&other) = delete;
OpenCLHandle &operator=(OpenCLHandle &&other) = default; OpenCLHandle &operator=(OpenCLHandle &&other) = delete;
bool IsValid() const; bool IsValid() const;
@ -206,6 +214,7 @@ class OpenCLContext {
OpenCLContext(OpenCLContext &&other) = delete; OpenCLContext(OpenCLContext &&other) = delete;
OpenCLContext &operator=(OpenCLContext &&other) = delete; OpenCLContext &operator=(OpenCLContext &&other) = delete;
/// Returns a OpenCLHandle wrapped in a std::shared_ptr
OpenCLHandle::Ptr GetHandle(); OpenCLHandle::Ptr GetHandle();
private: private:
@ -218,7 +227,7 @@ class OpenCLContext {
cl_command_queue queue_; cl_command_queue queue_;
cl_device_id device_id_; cl_device_id device_id_;
static void CheckRefCount(); static void CleanupInstance();
bool IsValid() const; bool IsValid() const;
}; };