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 | 7 |
8 #ifndef BASE_ATOMIC_REF_COUNT_H_ | 8 #ifndef BASE_ATOMIC_REF_COUNT_H_ |
9 #define BASE_ATOMIC_REF_COUNT_H_ | 9 #define BASE_ATOMIC_REF_COUNT_H_ |
10 | 10 |
11 #include "base/atomicops.h" | 11 #include "base/atomicops.h" |
12 #include "base/basictypes.h" | 12 #include "base/basictypes.h" |
13 | 13 |
14 namespace base { | 14 namespace base { |
15 | 15 |
16 typedef base::subtle::Atomic32 AtomicRefCount; | 16 typedef subtle::Atomic32 AtomicRefCount; |
17 | 17 |
18 // Increment a reference count by "increment", which must exceed 0. | 18 // Increment a reference count by "increment", which must exceed 0. |
19 inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr, | 19 inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr, |
20 AtomicRefCount increment) { | 20 AtomicRefCount increment) { |
21 base::subtle::NoBarrier_AtomicIncrement(ptr, increment); | 21 subtle::NoBarrier_AtomicIncrement(ptr, increment); |
22 } | 22 } |
23 | 23 |
24 // Decrement a reference count by "decrement", which must exceed 0, | 24 // Decrement a reference count by "decrement", which must exceed 0, |
25 // and return whether the result is non-zero. | 25 // and return whether the result is non-zero. |
26 // Insert barriers to ensure that state written before the reference count | 26 // Insert barriers to ensure that state written before the reference count |
27 // became zero will be visible to a thread that has just made the count zero. | 27 // became zero will be visible to a thread that has just made the count zero. |
28 inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr, | 28 inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr, |
29 AtomicRefCount decrement) { | 29 AtomicRefCount decrement) { |
30 return base::subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0; | 30 return subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0; |
31 } | 31 } |
32 | 32 |
33 // Increment a reference count by 1. | 33 // Increment a reference count by 1. |
34 inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) { | 34 inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) { |
35 base::AtomicRefCountIncN(ptr, 1); | 35 base::AtomicRefCountIncN(ptr, 1); |
36 } | 36 } |
37 | 37 |
38 // Decrement a reference count by 1 and return whether the result is non-zero. | 38 // Decrement a reference count by 1 and return whether the result is non-zero. |
39 // Insert barriers to ensure that state written before the reference count | 39 // Insert barriers to ensure that state written before the reference count |
40 // became zero will be visible to a thread that has just made the count zero. | 40 // became zero will be visible to a thread that has just made the count zero. |
41 inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) { | 41 inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) { |
42 return base::AtomicRefCountDecN(ptr, 1); | 42 return base::AtomicRefCountDecN(ptr, 1); |
43 } | 43 } |
44 | 44 |
45 // Return whether the reference count is one. If the reference count is used | 45 // Return whether the reference count is one. If the reference count is used |
46 // in the conventional way, a refrerence count of 1 implies that the current | 46 // in the conventional way, a refrerence count of 1 implies that the current |
47 // thread owns the reference and no other thread shares it. This call performs | 47 // thread owns the reference and no other thread shares it. This call performs |
48 // the test for a reference count of one, and performs the memory barrier | 48 // the test for a reference count of one, and performs the memory barrier |
49 // needed for the owning thread to act on the object, knowing that it has | 49 // needed for the owning thread to act on the object, knowing that it has |
50 // exclusive access to the object. | 50 // exclusive access to the object. |
51 inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) { | 51 inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) { |
52 return base::subtle::Acquire_Load(ptr) == 1; | 52 return subtle::Acquire_Load(ptr) == 1; |
53 } | 53 } |
54 | 54 |
55 // Return whether the reference count is zero. With conventional object | 55 // Return whether the reference count is zero. With conventional object |
56 // referencing counting, the object will be destroyed, so the reference count | 56 // referencing counting, the object will be destroyed, so the reference count |
57 // should never be zero. Hence this is generally used for a debug check. | 57 // should never be zero. Hence this is generally used for a debug check. |
58 inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) { | 58 inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) { |
59 return base::subtle::Acquire_Load(ptr) == 0; | 59 return subtle::Acquire_Load(ptr) == 0; |
60 } | 60 } |
61 | 61 |
62 } // namespace base | 62 } // namespace base |
63 | 63 |
64 #endif // BASE_ATOMIC_REF_COUNT_H_ | 64 #endif // BASE_ATOMIC_REF_COUNT_H_ |
65 | 65 |
OLD | NEW |