blob: 985c42c32381bae150a78e706dc363195980ab72 [file] [log] [blame]
levin@chromium.org5c528682011-03-28 10:54:15 +09001// Copyright (c) 2011 The Chromium Authors. All rights reserved.
mmentovai@google.comaa13be62008-09-03 03:20:34 +09002// 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.org5c528682011-03-28 10:54:15 +09006// counting. Please use base/memory/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_
thakis@chromium.org01d14522010-07-27 08:08:24 +090013#pragma once
mmentovai@google.comaa13be62008-09-03 03:20:34 +090014
15#include "base/atomicops.h"
timurrrr@chromium.orgf39c3ff2010-05-14 17:24:42 +090016#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
mmentovai@google.comaa13be62008-09-03 03:20:34 +090017
18namespace base {
19
agl@chromium.org851300e2009-01-16 07:25:11 +090020typedef subtle::Atomic32 AtomicRefCount;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090021
22// Increment a reference count by "increment", which must exceed 0.
23inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr,
24 AtomicRefCount increment) {
agl@chromium.org851300e2009-01-16 07:25:11 +090025 subtle::NoBarrier_AtomicIncrement(ptr, increment);
mmentovai@google.comaa13be62008-09-03 03:20:34 +090026}
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.
32inline bool AtomicRefCountDecN(volatile AtomicRefCount *ptr,
33 AtomicRefCount decrement) {
deanm@chromium.org7c73faa2009-06-26 20:28:03 +090034 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.comaa13be62008-09-03 03:20:34 +090040}
41
42// Increment a reference count by 1.
43inline 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.
50inline 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.
60inline bool AtomicRefCountIsOne(volatile AtomicRefCount *ptr) {
deanm@chromium.org7c73faa2009-06-26 20:28:03 +090061 bool res = (subtle::Acquire_Load(ptr) == 1);
62 if (res) {
63 ANNOTATE_HAPPENS_AFTER(ptr);
64 }
65 return res;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090066}
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.
71inline bool AtomicRefCountIsZero(volatile AtomicRefCount *ptr) {
deanm@chromium.org7c73faa2009-06-26 20:28:03 +090072 bool res = (subtle::Acquire_Load(ptr) == 0);
73 if (res) {
74 ANNOTATE_HAPPENS_AFTER(ptr);
75 }
76 return res;
mmentovai@google.comaa13be62008-09-03 03:20:34 +090077}
78
79} // namespace base
80
81#endif // BASE_ATOMIC_REF_COUNT_H_