1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011
2 // Google Inc. All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the name Chromium Embedded
15 // Framework nor the names of its contributors may be used to endorse
16 // or promote products derived from this software without specific prior
17 // written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // This is a low level implementation of atomic semantics for reference
32 // counting. Please use cef_ref_counted.h directly instead.
33 //
34 // The Chromium implementation includes annotations to avoid some false
35 // positives when using data race detection tools. Annotations are not
36 // currently supported by the CEF implementation.
37
38 #ifndef CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_
39 #define CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_
40 #pragma once
41
42 #if defined(USING_CHROMIUM_INCLUDES)
43 // When building CEF include the Chromium header directly.
44 #include "base/atomic_ref_count.h"
45
46 // Used when declaring a base::AtomicRefCount value. This is an object type with
47 // Chromium headers.
48 #define ATOMIC_DECLARATION (0)
49
50 // Maintaining compatibility with AtompicRefCount* functions that were removed
51 // from Chromium in http://crrev.com/ee96d561.
52 namespace base {
53
54 // Increment a reference count by 1.
AtomicRefCountInc(volatile AtomicRefCount * ptr)55 inline void AtomicRefCountInc(volatile AtomicRefCount* ptr) {
56 const_cast<AtomicRefCount*>(ptr)->Increment();
57 }
58
59 // Decrement a reference count by 1 and return whether the result is non-zero.
60 // Insert barriers to ensure that state written before the reference count
61 // became zero will be visible to a thread that has just made the count zero.
AtomicRefCountDec(volatile AtomicRefCount * ptr)62 inline bool AtomicRefCountDec(volatile AtomicRefCount* ptr) {
63 return const_cast<AtomicRefCount*>(ptr)->Decrement();
64 }
65
66 // Return whether the reference count is one. If the reference count is used
67 // in the conventional way, a refrerence count of 1 implies that the current
68 // thread owns the reference and no other thread shares it. This call performs
69 // the test for a reference count of one, and performs the memory barrier
70 // needed for the owning thread to act on the object, knowing that it has
71 // exclusive access to the object.
AtomicRefCountIsOne(volatile AtomicRefCount * ptr)72 inline bool AtomicRefCountIsOne(volatile AtomicRefCount* ptr) {
73 return const_cast<AtomicRefCount*>(ptr)->IsOne();
74 }
75
76 // Return whether the reference count is zero. With conventional object
77 // referencing counting, the object will be destroyed, so the reference count
78 // should never be zero. Hence this is generally used for a debug check.
AtomicRefCountIsZero(volatile AtomicRefCount * ptr)79 inline bool AtomicRefCountIsZero(volatile AtomicRefCount* ptr) {
80 return const_cast<AtomicRefCount*>(ptr)->IsZero();
81 }
82
83 } // namespace base
84
85 #else // !USING_CHROMIUM_INCLUDES
86 // The following is substantially similar to the Chromium implementation.
87 // If the Chromium implementation diverges the below implementation should be
88 // updated to match.
89
90 #include "include/base/cef_atomicops.h"
91
92 // Annotations are not currently supported.
93 #define ANNOTATE_HAPPENS_BEFORE(obj) /* empty */
94 #define ANNOTATE_HAPPENS_AFTER(obj) /* empty */
95
96 // Used when declaring a base::AtomicRefCount value. This is an integer/ptr type
97 // with CEF headers.
98 #define ATOMIC_DECLARATION = 0
99
100 namespace base {
101
102 typedef subtle::Atomic32 AtomicRefCount;
103
104 // Increment a reference count by "increment", which must exceed 0.
AtomicRefCountIncN(volatile AtomicRefCount * ptr,AtomicRefCount increment)105 inline void AtomicRefCountIncN(volatile AtomicRefCount* ptr,
106 AtomicRefCount increment) {
107 subtle::NoBarrier_AtomicIncrement(ptr, increment);
108 }
109
110 // Decrement a reference count by "decrement", which must exceed 0,
111 // and return whether the result is non-zero.
112 // Insert barriers to ensure that state written before the reference count
113 // became zero will be visible to a thread that has just made the count zero.
AtomicRefCountDecN(volatile AtomicRefCount * ptr,AtomicRefCount decrement)114 inline bool AtomicRefCountDecN(volatile AtomicRefCount* ptr,
115 AtomicRefCount decrement) {
116 ANNOTATE_HAPPENS_BEFORE(ptr);
117 bool res = (subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0);
118 if (!res) {
119 ANNOTATE_HAPPENS_AFTER(ptr);
120 }
121 return res;
122 }
123
124 // Increment a reference count by 1.
AtomicRefCountInc(volatile AtomicRefCount * ptr)125 inline void AtomicRefCountInc(volatile AtomicRefCount* ptr) {
126 base::AtomicRefCountIncN(ptr, 1);
127 }
128
129 // Decrement a reference count by 1 and return whether the result is non-zero.
130 // Insert barriers to ensure that state written before the reference count
131 // became zero will be visible to a thread that has just made the count zero.
AtomicRefCountDec(volatile AtomicRefCount * ptr)132 inline bool AtomicRefCountDec(volatile AtomicRefCount* ptr) {
133 return base::AtomicRefCountDecN(ptr, 1);
134 }
135
136 // Return whether the reference count is one. If the reference count is used
137 // in the conventional way, a refrerence count of 1 implies that the current
138 // thread owns the reference and no other thread shares it. This call performs
139 // the test for a reference count of one, and performs the memory barrier
140 // needed for the owning thread to act on the object, knowing that it has
141 // exclusive access to the object.
AtomicRefCountIsOne(volatile AtomicRefCount * ptr)142 inline bool AtomicRefCountIsOne(volatile AtomicRefCount* ptr) {
143 bool res = (subtle::Acquire_Load(ptr) == 1);
144 if (res) {
145 ANNOTATE_HAPPENS_AFTER(ptr);
146 }
147 return res;
148 }
149
150 // Return whether the reference count is zero. With conventional object
151 // referencing counting, the object will be destroyed, so the reference count
152 // should never be zero. Hence this is generally used for a debug check.
AtomicRefCountIsZero(volatile AtomicRefCount * ptr)153 inline bool AtomicRefCountIsZero(volatile AtomicRefCount* ptr) {
154 bool res = (subtle::Acquire_Load(ptr) == 0);
155 if (res) {
156 ANNOTATE_HAPPENS_AFTER(ptr);
157 }
158 return res;
159 }
160
161 } // namespace base
162
163 #endif // !USING_CHROMIUM_INCLUDES
164
165 #endif // CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_
166