security/sandbox/chromium/base/atomic_ref_count.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial