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. |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 7 | |
| 8 | #ifndef BASE_ATOMIC_REF_COUNT_H_ |
| 9 | #define BASE_ATOMIC_REF_COUNT_H_ |
| 10 | |
| 11 | #include "base/atomicops.h" |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 12 | |
| 13 | namespace base { |
| 14 | |
agl@chromium.org | 851300e | 2009-01-16 07:25:11 +0900 | [diff] [blame] | 15 | typedef subtle::Atomic32 AtomicRefCount; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 16 | |
| 17 | // Increment a reference count by "increment", which must exceed 0. |
| 18 | inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr, |
| 19 | AtomicRefCount increment) { |
agl@chromium.org | 851300e | 2009-01-16 07:25:11 +0900 | [diff] [blame] | 20 | subtle::NoBarrier_AtomicIncrement(ptr, increment); |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 21 | } |
| 22 | |
| 23 | // Decrement a reference count by "decrement", which must exceed 0, |
| 24 | // and return whether the result is non-zero. |
| 25 | // Insert barriers to ensure that state written before the reference count |
| 26 | // became zero will be visible to a thread that has just made the count zero. |
| 27 | inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr, |
| 28 | AtomicRefCount decrement) { |
deanm@chromium.org | 7c73faa | 2009-06-26 20:28:03 +0900 | [diff] [blame] | 29 | bool res = (subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0); |
deanm@chromium.org | 7c73faa | 2009-06-26 20:28:03 +0900 | [diff] [blame] | 30 | return res; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | // Increment a reference count by 1. |
| 34 | inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) { |
| 35 | base::AtomicRefCountIncN(ptr, 1); |
| 36 | } |
| 37 | |
| 38 | // Decrement a reference count by 1 and return whether the result is non-zero. |
| 39 | // Insert barriers to ensure that state written before the reference count |
| 40 | // became zero will be visible to a thread that has just made the count zero. |
| 41 | inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) { |
| 42 | return base::AtomicRefCountDecN(ptr, 1); |
| 43 | } |
| 44 | |
| 45 | // Return whether the reference count is one. If the reference count is used |
| 46 | // in the conventional way, a refrerence count of 1 implies that the current |
| 47 | // thread owns the reference and no other thread shares it. This call performs |
| 48 | // the test for a reference count of one, and performs the memory barrier |
| 49 | // needed for the owning thread to act on the object, knowing that it has |
| 50 | // exclusive access to the object. |
| 51 | inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) { |
deanm@chromium.org | 7c73faa | 2009-06-26 20:28:03 +0900 | [diff] [blame] | 52 | bool res = (subtle::Acquire_Load(ptr) == 1); |
deanm@chromium.org | 7c73faa | 2009-06-26 20:28:03 +0900 | [diff] [blame] | 53 | return res; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | // Return whether the reference count is zero. With conventional object |
| 57 | // referencing counting, the object will be destroyed, so the reference count |
| 58 | // should never be zero. Hence this is generally used for a debug check. |
| 59 | inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) { |
deanm@chromium.org | 7c73faa | 2009-06-26 20:28:03 +0900 | [diff] [blame] | 60 | bool res = (subtle::Acquire_Load(ptr) == 0); |
deanm@chromium.org | 7c73faa | 2009-06-26 20:28:03 +0900 | [diff] [blame] | 61 | return res; |
mmentovai@google.com | aa13be6 | 2008-09-03 03:20:34 +0900 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // namespace base |
| 65 | |
| 66 | #endif // BASE_ATOMIC_REF_COUNT_H_ |