OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This is a low level implementation of atomic semantics for reference | 5 // This is a low level implementation of atomic semantics for reference |
6 // counting. Please use base/memory/ref_counted.h directly instead. | 6 // counting. Please use base/memory/ref_counted.h directly instead. |
7 // | 7 // |
8 // The implementation includes annotations to avoid some false positives | 8 // The implementation includes annotations to avoid some false positives |
9 // when using data race detection tools. | 9 // when using data race detection tools. |
10 | 10 |
11 #ifndef BASE_ATOMIC_REF_COUNT_H_ | 11 #ifndef BASE_ATOMIC_REF_COUNT_H_ |
12 #define BASE_ATOMIC_REF_COUNT_H_ | 12 #define BASE_ATOMIC_REF_COUNT_H_ |
13 | 13 |
14 #include "base/atomicops.h" | 14 #include "base/atomicops.h" |
15 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
16 | 15 |
17 namespace base { | 16 namespace base { |
18 | 17 |
19 typedef subtle::Atomic32 AtomicRefCount; | 18 typedef subtle::Atomic32 AtomicRefCount; |
20 | 19 |
21 // Increment a reference count by "increment", which must exceed 0. | 20 // Increment a reference count by "increment", which must exceed 0. |
22 inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr, | 21 inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr, |
23 AtomicRefCount increment) { | 22 AtomicRefCount increment) { |
24 subtle::NoBarrier_AtomicIncrement(ptr, increment); | 23 subtle::NoBarrier_AtomicIncrement(ptr, increment); |
25 } | 24 } |
26 | 25 |
27 // Decrement a reference count by "decrement", which must exceed 0, | 26 // Decrement a reference count by "decrement", which must exceed 0, |
28 // and return whether the result is non-zero. | 27 // and return whether the result is non-zero. |
29 // Insert barriers to ensure that state written before the reference count | 28 // Insert barriers to ensure that state written before the reference count |
30 // became zero will be visible to a thread that has just made the count zero. | 29 // became zero will be visible to a thread that has just made the count zero. |
31 inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr, | 30 inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr, |
32 AtomicRefCount decrement) { | 31 AtomicRefCount decrement) { |
33 ANNOTATE_HAPPENS_BEFORE(ptr); | |
34 bool res = (subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0); | 32 bool res = (subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0); |
35 if (!res) { | |
36 ANNOTATE_HAPPENS_AFTER(ptr); | |
37 } | |
38 return res; | 33 return res; |
39 } | 34 } |
40 | 35 |
41 // Increment a reference count by 1. | 36 // Increment a reference count by 1. |
42 inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) { | 37 inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) { |
43 base::AtomicRefCountIncN(ptr, 1); | 38 base::AtomicRefCountIncN(ptr, 1); |
44 } | 39 } |
45 | 40 |
46 // Decrement a reference count by 1 and return whether the result is non-zero. | 41 // Decrement a reference count by 1 and return whether the result is non-zero. |
47 // Insert barriers to ensure that state written before the reference count | 42 // Insert barriers to ensure that state written before the reference count |
48 // became zero will be visible to a thread that has just made the count zero. | 43 // became zero will be visible to a thread that has just made the count zero. |
49 inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) { | 44 inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) { |
50 return base::AtomicRefCountDecN(ptr, 1); | 45 return base::AtomicRefCountDecN(ptr, 1); |
51 } | 46 } |
52 | 47 |
53 // Return whether the reference count is one. If the reference count is used | 48 // Return whether the reference count is one. If the reference count is used |
54 // in the conventional way, a refrerence count of 1 implies that the current | 49 // in the conventional way, a refrerence count of 1 implies that the current |
55 // thread owns the reference and no other thread shares it. This call performs | 50 // thread owns the reference and no other thread shares it. This call performs |
56 // the test for a reference count of one, and performs the memory barrier | 51 // the test for a reference count of one, and performs the memory barrier |
57 // needed for the owning thread to act on the object, knowing that it has | 52 // needed for the owning thread to act on the object, knowing that it has |
58 // exclusive access to the object. | 53 // exclusive access to the object. |
59 inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) { | 54 inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) { |
60 bool res = (subtle::Acquire_Load(ptr) == 1); | 55 bool res = (subtle::Acquire_Load(ptr) == 1); |
61 if (res) { | |
62 ANNOTATE_HAPPENS_AFTER(ptr); | |
63 } | |
64 return res; | 56 return res; |
65 } | 57 } |
66 | 58 |
67 // Return whether the reference count is zero. With conventional object | 59 // Return whether the reference count is zero. With conventional object |
68 // referencing counting, the object will be destroyed, so the reference count | 60 // referencing counting, the object will be destroyed, so the reference count |
69 // should never be zero. Hence this is generally used for a debug check. | 61 // should never be zero. Hence this is generally used for a debug check. |
70 inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) { | 62 inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) { |
71 bool res = (subtle::Acquire_Load(ptr) == 0); | 63 bool res = (subtle::Acquire_Load(ptr) == 0); |
72 if (res) { | |
73 ANNOTATE_HAPPENS_AFTER(ptr); | |
74 } | |
75 return res; | 64 return res; |
76 } | 65 } |
77 | 66 |
78 } // namespace base | 67 } // namespace base |
79 | 68 |
80 #endif // BASE_ATOMIC_REF_COUNT_H_ | 69 #endif // BASE_ATOMIC_REF_COUNT_H_ |
OLD | NEW |