blob: 985c42c32381bae150a78e706dc363195980ab72 [file] [log] [blame]
[email protected]3b63f8f42011-03-28 01:54:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]b2e97292008-09-02 18:20:342// 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
[email protected]3b63f8f42011-03-28 01:54:156// counting. Please use base/memory/ref_counted.h directly instead.
[email protected]001b6942009-06-26 11:28:037//
8// The implementation includes annotations to avoid some false positives
9// when using data race detection tools.
[email protected]b2e97292008-09-02 18:20:3410
11#ifndef BASE_ATOMIC_REF_COUNT_H_
12#define BASE_ATOMIC_REF_COUNT_H_
[email protected]32b76ef2010-07-26 23:08:2413#pragma once
[email protected]b2e97292008-09-02 18:20:3414
15#include "base/atomicops.h"
[email protected]ee857512010-05-14 08:24:4216#include "base/third_party/dynamic_annotations/dynamic_annotations.h"
[email protected]b2e97292008-09-02 18:20:3417
18namespace base {
19
[email protected]1c4947f2009-01-15 22:25:1120typedef subtle::Atomic32 AtomicRefCount;
[email protected]b2e97292008-09-02 18:20:3421
22// Increment a reference count by "increment", which must exceed 0.
23inline void AtomicRefCountIncN(volatile AtomicRefCount *ptr,
24 AtomicRefCount increment) {
[email protected]1c4947f2009-01-15 22:25:1125 subtle::NoBarrier_AtomicIncrement(ptr, increment);
[email protected]b2e97292008-09-02 18:20:3426}
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) {
[email protected]001b6942009-06-26 11:28:0334 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;
[email protected]b2e97292008-09-02 18:20:3440}
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) {
[email protected]001b6942009-06-26 11:28:0361 bool res = (subtle::Acquire_Load(ptr) == 1);
62 if (res) {
63 ANNOTATE_HAPPENS_AFTER(ptr);
64 }
65 return res;
[email protected]b2e97292008-09-02 18:20:3466}
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) {
[email protected]001b6942009-06-26 11:28:0372 bool res = (subtle::Acquire_Load(ptr) == 0);
73 if (res) {
74 ANNOTATE_HAPPENS_AFTER(ptr);
75 }
76 return res;
[email protected]b2e97292008-09-02 18:20:3477}
78
79} // namespace base
80
81#endif // BASE_ATOMIC_REF_COUNT_H_