1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/security/sandbox/chromium/base/atomic_ref_count.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,80 @@ 1.4 +// Copyright (c) 2011 The Chromium Authors. All rights reserved. 1.5 +// Use of this source code is governed by a BSD-style license that can be 1.6 +// found in the LICENSE file. 1.7 + 1.8 +// This is a low level implementation of atomic semantics for reference 1.9 +// counting. Please use base/memory/ref_counted.h directly instead. 1.10 +// 1.11 +// The implementation includes annotations to avoid some false positives 1.12 +// when using data race detection tools. 1.13 + 1.14 +#ifndef BASE_ATOMIC_REF_COUNT_H_ 1.15 +#define BASE_ATOMIC_REF_COUNT_H_ 1.16 + 1.17 +#include "base/atomicops.h" 1.18 +#include "base/third_party/dynamic_annotations/dynamic_annotations.h" 1.19 + 1.20 +namespace base { 1.21 + 1.22 +typedef subtle::Atomic32 AtomicRefCount; 1.23 + 1.24 +// Increment a reference count by "increment", which must exceed 0. 1.25 +inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr, 1.26 + AtomicRefCount increment) { 1.27 + subtle::NoBarrier_AtomicIncrement(ptr, increment); 1.28 +} 1.29 + 1.30 +// Decrement a reference count by "decrement", which must exceed 0, 1.31 +// and return whether the result is non-zero. 1.32 +// Insert barriers to ensure that state written before the reference count 1.33 +// became zero will be visible to a thread that has just made the count zero. 1.34 +inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr, 1.35 + AtomicRefCount decrement) { 1.36 + ANNOTATE_HAPPENS_BEFORE(ptr); 1.37 + bool res = (subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0); 1.38 + if (!res) { 1.39 + ANNOTATE_HAPPENS_AFTER(ptr); 1.40 + } 1.41 + return res; 1.42 +} 1.43 + 1.44 +// Increment a reference count by 1. 1.45 +inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) { 1.46 + base::AtomicRefCountIncN(ptr, 1); 1.47 +} 1.48 + 1.49 +// Decrement a reference count by 1 and return whether the result is non-zero. 1.50 +// Insert barriers to ensure that state written before the reference count 1.51 +// became zero will be visible to a thread that has just made the count zero. 1.52 +inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) { 1.53 + return base::AtomicRefCountDecN(ptr, 1); 1.54 +} 1.55 + 1.56 +// Return whether the reference count is one. If the reference count is used 1.57 +// in the conventional way, a refrerence count of 1 implies that the current 1.58 +// thread owns the reference and no other thread shares it. This call performs 1.59 +// the test for a reference count of one, and performs the memory barrier 1.60 +// needed for the owning thread to act on the object, knowing that it has 1.61 +// exclusive access to the object. 1.62 +inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) { 1.63 + bool res = (subtle::Acquire_Load(ptr) == 1); 1.64 + if (res) { 1.65 + ANNOTATE_HAPPENS_AFTER(ptr); 1.66 + } 1.67 + return res; 1.68 +} 1.69 + 1.70 +// Return whether the reference count is zero. With conventional object 1.71 +// referencing counting, the object will be destroyed, so the reference count 1.72 +// should never be zero. Hence this is generally used for a debug check. 1.73 +inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) { 1.74 + bool res = (subtle::Acquire_Load(ptr) == 0); 1.75 + if (res) { 1.76 + ANNOTATE_HAPPENS_AFTER(ptr); 1.77 + } 1.78 + return res; 1.79 +} 1.80 + 1.81 +} // namespace base 1.82 + 1.83 +#endif // BASE_ATOMIC_REF_COUNT_H_