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/AtomicRefCounter.h |
Diffstat (limited to 'Runtime/Threads/AtomicRefCounter.h')
-rw-r--r-- | Runtime/Threads/AtomicRefCounter.h | 30 |
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 |