diff options
author | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2019-08-14 22:50:43 +0800 |
commit | 15740faf9fe9fe4be08965098bbf2947e096aeeb (patch) | |
tree | a730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Threads/ThreadSharedObject.h |
Diffstat (limited to 'Runtime/Threads/ThreadSharedObject.h')
-rw-r--r-- | Runtime/Threads/ThreadSharedObject.h | 30 |
1 files changed, 30 insertions, 0 deletions
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<ThreadSharedObject*>(this)); + } + } + int GetRefCount() const { return m_RefCount; } + +protected: + ThreadSharedObject(int refs = 1) : m_RefCount(refs) {} + virtual ~ThreadSharedObject() {} + +private: + volatile mutable int m_RefCount; +}; + +#endif |