summaryrefslogtreecommitdiff
path: root/Runtime/Threads/ThreadSharedObject.h
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-08-14 22:50:43 +0800
committerchai <chaifix@163.com>2019-08-14 22:50:43 +0800
commit15740faf9fe9fe4be08965098bbf2947e096aeeb (patch)
treea730ec236656cc8cab5b13f088adfaed6bb218fb /Runtime/Threads/ThreadSharedObject.h
+Unity Runtime codeHEADmaster
Diffstat (limited to 'Runtime/Threads/ThreadSharedObject.h')
-rw-r--r--Runtime/Threads/ThreadSharedObject.h30
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