|
Lines 27-34
Source/WTF/wtf/DeferrableRefCounted.h_sec1
|
| 27 |
#define DeferrableRefCounted_h |
27 |
#define DeferrableRefCounted_h |
| 28 |
|
28 |
|
| 29 |
#include <wtf/Assertions.h> |
29 |
#include <wtf/Assertions.h> |
|
|
30 |
#include <wtf/Atomics.h> |
| 30 |
#include <wtf/FastAllocBase.h> |
31 |
#include <wtf/FastAllocBase.h> |
| 31 |
#include <wtf/Noncopyable.h> |
32 |
#include <wtf/Noncopyable.h> |
|
|
33 |
#include <wtf/ThreadingPrimitives.h> |
| 32 |
|
34 |
|
| 33 |
namespace WTF { |
35 |
namespace WTF { |
| 34 |
|
36 |
|
|
Lines 38-43
namespace WTF {
Source/WTF/wtf/DeferrableRefCounted.h_sec2
|
| 38 |
// specifically happened - this can be useful either for debugging, or |
40 |
// specifically happened - this can be useful either for debugging, or |
| 39 |
// sometimes even for some additional functionality. |
41 |
// sometimes even for some additional functionality. |
| 40 |
|
42 |
|
|
|
43 |
// Note, this is always thread-safe ref-counted - this works out fine |
| 44 |
// because the users of this class either have low refCount churn, or |
| 45 |
// need thread-safe reference counting, or both. |
| 46 |
|
| 41 |
class DeferrableRefCountedBase { |
47 |
class DeferrableRefCountedBase { |
| 42 |
static const unsigned deferredFlag = 1; |
48 |
static const unsigned deferredFlag = 1; |
| 43 |
static const unsigned normalIncrement = 2; |
49 |
static const unsigned normalIncrement = 2; |
|
Lines 45-51
class DeferrableRefCountedBase {
Source/WTF/wtf/DeferrableRefCounted.h_sec3
|
| 45 |
public: |
51 |
public: |
| 46 |
void ref() |
52 |
void ref() |
| 47 |
{ |
53 |
{ |
|
|
54 |
#if ENABLE(COMPARE_AND_SWAP) |
| 55 |
unsigned oldValue; |
| 56 |
do |
| 57 |
oldValue = m_refCount; |
| 58 |
while (!weakCompareAndSwap(&m_refCount, oldValue, oldValue + normalIncrement)); |
| 59 |
#else // ENABLE(COMPARE_AND_SWAP) |
| 60 |
MutexLocker locker(m_lock); |
| 48 |
m_refCount += normalIncrement; |
61 |
m_refCount += normalIncrement; |
|
|
62 |
#endif // ENABLE(COMPARE_AND_SWAP) |
| 49 |
} |
63 |
} |
| 50 |
|
64 |
|
| 51 |
bool hasOneRef() const |
65 |
bool hasOneRef() const |
|
Lines 75-96
protected:
Source/WTF/wtf/DeferrableRefCounted.h_sec4
|
| 75 |
|
89 |
|
| 76 |
bool derefBase() |
90 |
bool derefBase() |
| 77 |
{ |
91 |
{ |
|
|
92 |
#if ENABLE(COMPARE_AND_SWAP) |
| 93 |
unsigned oldValue; |
| 94 |
unsigned newValue; |
| 95 |
do { |
| 96 |
oldValue = m_refCount; |
| 97 |
newValue = oldValue - normalIncrement; |
| 98 |
} while (!weakCompareAndSwap(&m_refCount, oldValue, newValue)); |
| 99 |
return !newValue; |
| 100 |
#else // ENABLE(COMPARE_AND_SWAP) |
| 101 |
MutexLocker locker(m_lock); |
| 78 |
m_refCount -= normalIncrement; |
102 |
m_refCount -= normalIncrement; |
| 79 |
return !m_refCount; |
103 |
return !m_refCount; |
|
|
104 |
#endif // ENABLE(COMPARE_AND_SWAP) |
| 80 |
} |
105 |
} |
| 81 |
|
106 |
|
| 82 |
bool setIsDeferredBase(bool value) |
107 |
bool setIsDeferredBase(bool value) |
| 83 |
{ |
108 |
{ |
|
|
109 |
#if ENABLE(COMPARE_AND_SWAP) |
| 110 |
unsigned oldValue; |
| 111 |
unsigned newValue; |
| 112 |
bool result; |
| 113 |
do { |
| 114 |
oldValue = m_refCount; |
| 115 |
if (value) { |
| 116 |
newValue = oldValue | deferredFlag; |
| 117 |
result = false; |
| 118 |
} else { |
| 119 |
newValue = oldValue & ~deferredFlag; |
| 120 |
result = !newValue; |
| 121 |
} |
| 122 |
} while (!weakCompareAndSwap(&m_refCount, oldValue, newValue)); |
| 123 |
return result; |
| 124 |
#else // ENABLE(COMPARE_AND_SWAP) |
| 125 |
MutexLocker locker(m_lock); |
| 84 |
if (value) { |
126 |
if (value) { |
| 85 |
m_refCount |= deferredFlag; |
127 |
m_refCount |= deferredFlag; |
| 86 |
return false; |
128 |
return false; |
| 87 |
} |
129 |
} |
| 88 |
m_refCount &= ~deferredFlag; |
130 |
m_refCount &= ~deferredFlag; |
| 89 |
return !m_refCount; |
131 |
return !m_refCount; |
|
|
132 |
#endif // ENABLE(COMPARE_AND_SWAP) |
| 90 |
} |
133 |
} |
| 91 |
|
134 |
|
| 92 |
private: |
135 |
private: |
| 93 |
unsigned m_refCount; |
136 |
unsigned m_refCount; |
|
|
137 |
|
| 138 |
#if !ENABLE(COMPARE_AND_SWAP) |
| 139 |
Mutex m_lock; |
| 140 |
#endif // !ENABLE(COMPARE_AND_SWAP) |
| 94 |
}; |
141 |
}; |
| 95 |
|
142 |
|
| 96 |
template<typename T> |
143 |
template<typename T> |