blob: 73d7f4396ed94e1a1c08fd2917659fa0be4e6c5f [file] [log] [blame]
mmentovai@google.comaa13be62008-09-03 03:20:34 +09001// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
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
6// counting. Please use base/ref_counted.h directly instead.
deanm@chromium.org7c73faa2009-06-26 20:28:03 +09007//
8// The implementation includes annotations to avoid some false positives
9// when using data race detection tools.
mmentovai@google.comaa13be62008-09-03 03:20:34 +090010
11#ifndef BASE_ATOMIC_REF_COUNT_H_
12#define BASE_ATOMIC_REF_COUNT_H_
13
14#include "base/atomicops.h"
deanm@chromium.org7c73faa2009-06-26 20:28:03 +090015#include "base/dynamic_annotations.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090016
17namespace base {
18
agl@chromium.org851300e2009-01-16 07:25:11 +090019typedef subtle::Atomic32 AtomicRefCount;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090020
21// Increment a reference count by "increment", which must exceed 0.
22inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr,
23 AtomicRefCount increment) {
agl@chromium.org851300e2009-01-16 07:25:11 +090024 subtle::NoBarrier_AtomicIncrement(ptr, increment);
mmentovai@google.comaa13be62008-09-03 03:20:34 +090025}
26
27// Decrement a reference count by "decrement", which must exceed 0,
28// and return whether the result is non-zero.
29// Insert barriers to ensure that state written before the reference count
30// became zero will be visible to a thread that has just made the count zero.
31inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr,
32 AtomicRefCount decrement) {
deanm@chromium.org7c73faa2009-06-26 20:28:03 +090033 ANNOTATE_HAPPENS_BEFORE(ptr);
34 bool res = (subtle::Barrier_AtomicIncrement(ptr, -decrement) != 0);
35 if (!res) {
36 ANNOTATE_HAPPENS_AFTER(ptr);
37 }
38 return res;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090039}
40
41// Increment a reference count by 1.
42inline void AtomicRefCountInc(volatile AtomicRefCount *ptr) {
43 base::AtomicRefCountIncN(ptr, 1);
44}
45
46// Decrement a reference count by 1 and return whether the result is non-zero.
47// Insert barriers to ensure that state written before the reference count
48// became zero will be visible to a thread that has just made the count zero.
49inline bool AtomicRefCountDec(volatile AtomicRefCount *ptr) {
50 return base::AtomicRefCountDecN(ptr, 1);
51}
52
53// Return whether the reference count is one. If the reference count is used
54// in the conventional way, a refrerence count of 1 implies that the current
55// thread owns the reference and no other thread shares it. This call performs
56// the test for a reference count of one, and performs the memory barrier
57// needed for the owning thread to act on the object, knowing that it has
58// exclusive access to the object.
59inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) {
deanm@chromium.org7c73faa2009-06-26 20:28:03 +090060 bool res = (subtle::Acquire_Load(ptr) == 1);
61 if (res) {
62 ANNOTATE_HAPPENS_AFTER(ptr);
63 }
64 return res;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090065}
66
67// Return whether the reference count is zero. With conventional object
68// referencing counting, the object will be destroyed, so the reference count
69// should never be zero. Hence this is generally used for a debug check.
70inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) {
deanm@chromium.org7c73faa2009-06-26 20:28:03 +090071 bool res = (subtle::Acquire_Load(ptr) == 0);
72 if (res) {
73 ANNOTATE_HAPPENS_AFTER(ptr);
74 }
75 return res;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090076}
77
78} // namespace base
79
80#endif // BASE_ATOMIC_REF_COUNT_H_