summaryrefslogtreecommitdiff
path: root/Runtime/Threads/AtomicRefCounter.h
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Threads/AtomicRefCounter.h')
-rw-r--r--Runtime/Threads/AtomicRefCounter.h30
1 files changed, 30 insertions, 0 deletions
diff --git a/Runtime/Threads/AtomicRefCounter.h b/Runtime/Threads/AtomicRefCounter.h
new file mode 100644
index 0000000..9e6b5e7
--- /dev/null
+++ b/Runtime/Threads/AtomicRefCounter.h
@@ -0,0 +1,30 @@
+#ifndef __UNITY_ATOMIC_REFCOUNTER_H
+#define __UNITY_ATOMIC_REFCOUNTER_H
+
+#include "AtomicOps.h"
+
+// Used for threadsafe refcounting
+class AtomicRefCounter {
+private:
+ volatile int m_Counter;
+public:
+ // Upon the construction the self-counter is always set to 1,
+ // which means that this instance is already accounted for.
+ // This scheme shaves off the cycles that would be consumed
+ // during the unavoidable first Retain call otherwise.
+ AtomicRefCounter() : m_Counter(1) {}
+
+ FORCE_INLINE void Retain ()
+ {
+ AtomicIncrement(&m_Counter);
+ }
+
+ FORCE_INLINE bool Release ()
+ {
+ int afterDecrement = AtomicDecrement(&m_Counter);
+ AssertIf( afterDecrement < 0 ); // If we hit this assert, someone is Releasing without matching it with Retain
+ return afterDecrement == 0;
+ }
+};
+
+#endif // __UNITY_ATOMIC_REFCOUNTER_H