From 15740faf9fe9fe4be08965098bbf2947e096aeeb Mon Sep 17 00:00:00 2001 From: chai Date: Wed, 14 Aug 2019 22:50:43 +0800 Subject: +Unity Runtime code --- Runtime/Threads/ThreadSharedObject.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Runtime/Threads/ThreadSharedObject.h (limited to 'Runtime/Threads/ThreadSharedObject.h') diff --git a/Runtime/Threads/ThreadSharedObject.h b/Runtime/Threads/ThreadSharedObject.h new file mode 100644 index 0000000..d53239a --- /dev/null +++ b/Runtime/Threads/ThreadSharedObject.h @@ -0,0 +1,30 @@ +#ifndef THREAD_SHARED_OBJECT_H +#define THREAD_SHARED_OBJECT_H + +#include "Runtime/Utilities/NonCopyable.h" +#include "Runtime/Threads/AtomicOps.h" + +class ThreadSharedObject : public NonCopyable +{ +public: + void AddRef() const { AtomicIncrement(&m_RefCount); } + void Release() const { if (AtomicDecrement(&m_RefCount) == 0) delete this; } + void Release(MemLabelId label) const + { + if (AtomicDecrement(&m_RefCount) == 0) + { + this->~ThreadSharedObject(); + UNITY_FREE(label,const_cast(this)); + } + } + int GetRefCount() const { return m_RefCount; } + +protected: + ThreadSharedObject(int refs = 1) : m_RefCount(refs) {} + virtual ~ThreadSharedObject() {} + +private: + volatile mutable int m_RefCount; +}; + +#endif -- cgit v1.1-26-g67d0