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