1 // Copyright (c) 2014 Marshall A. Greenblatt. Portions copyright (c) 2011
2 // Google Inc. All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // This is a low level implementation of atomic semantics for reference
32 // counting. Please use cef_ref_counted.h directly instead.
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.
38 #ifndef CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_
39 #define CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_
42 #if defined(BASE_ATOMIC_REF_COUNT_H_)
43 // Do nothing if the Chromium header has already been included.
44 // This can happen in cases where Chromium code is used directly by the
45 // client application. When using Chromium code directly always include
46 // the Chromium header first to avoid type conflicts.
47 #elif defined(BUILDING_CEF_SHARED)
48 // When building CEF include the Chromium header directly.
49 #include "base/atomic_ref_count.h"
50 #else // !BUILDING_CEF_SHARED
51 // The following is substantially similar to the Chromium implementation.
52 // If the Chromium implementation diverges the below implementation should be
55 #include "include/base/cef_atomicops.h"
57 // Annotations are not currently supported.
58 #define ANNOTATE_HAPPENS_BEFORE(obj) /* empty */
59 #define ANNOTATE_HAPPENS_AFTER(obj) /* empty */
63 typedef subtle::Atomic32 AtomicRefCount;
65 // Increment a reference count by "increment", which must exceed 0.
66 inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr,
67 AtomicRefCount increment) {
68 subtle::NoBarrier_AtomicIncrement(ptr, increment);
71 // Decrement a reference count by "decrement", which must exceed 0,
72 // and return whether the result is non-zero.
73 // Insert barriers to ensure that state written before the reference count
74 // became zero will be visible to a thread that has just made the count zero.
75 inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr,
76 AtomicRefCount decrement) {
77 ANNOTATE_HAPPENS_BEFORE(ptr);
78 bool res = (subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0);
80 ANNOTATE_HAPPENS_AFTER(ptr);
85 // Increment a reference count by 1.
86 inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) {
87 base::AtomicRefCountIncN(ptr, 1);
90 // Decrement a reference count by 1 and return whether the result is non-zero.
91 // Insert barriers to ensure that state written before the reference count
92 // became zero will be visible to a thread that has just made the count zero.
93 inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) {
94 return base::AtomicRefCountDecN(ptr, 1);
97 // Return whether the reference count is one. If the reference count is used
98 // in the conventional way, a refrerence count of 1 implies that the current
99 // thread owns the reference and no other thread shares it. This call performs
100 // the test for a reference count of one, and performs the memory barrier
101 // needed for the owning thread to act on the object, knowing that it has
102 // exclusive access to the object.
103 inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) {
104 bool res = (subtle::Acquire_Load(ptr) == 1);
106 ANNOTATE_HAPPENS_AFTER(ptr);
111 // Return whether the reference count is zero. With conventional object
112 // referencing counting, the object will be destroyed, so the reference count
113 // should never be zero. Hence this is generally used for a debug check.
114 inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) {
115 bool res = (subtle::Acquire_Load(ptr) == 0);
117 ANNOTATE_HAPPENS_AFTER(ptr);
124 #endif // !BUILDING_CEF_SHARED
126 #endif // CEF_INCLUDE_BASE_CEF_ATOMIC_REF_COUNT_H_