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/AtomicRefCounter.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 Runtime/Threads/AtomicRefCounter.h (limited to 'Runtime/Threads/AtomicRefCounter.h') 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 -- cgit v1.1-26-g67d0