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