OpenCLContext::OpenCLHandle::~OpenCLHandle() {
CleanupAllKernels();
- OpenCLContext::CheckRefCount();
+ OpenCLContext::CleanupInstance();
}
bool OpenCLContext::OpenCLHandle::IsValid() const {
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_; }
typedef std::shared_ptr<OpenCLContext> Ptr;
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 {
public:
typedef std::shared_ptr<OpenCLHandle> Ptr;
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;
OpenCLContext(OpenCLContext &&other) = delete;
OpenCLContext &operator=(OpenCLContext &&other) = delete;
+ /// Returns a OpenCLHandle wrapped in a std::shared_ptr
OpenCLHandle::Ptr GetHandle();
private:
cl_command_queue queue_;
cl_device_id device_id_;
- static void CheckRefCount();
+ static void CleanupInstance();
bool IsValid() const;
};