xref: /aosp_15_r20/external/libchrome/base/memory/ref_counted.cc (revision 635a864187cb8b6c713ff48b7e790a6b21769273)
1*635a8641SAndroid Build Coastguard Worker // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2*635a8641SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be
3*635a8641SAndroid Build Coastguard Worker // found in the LICENSE file.
4*635a8641SAndroid Build Coastguard Worker 
5*635a8641SAndroid Build Coastguard Worker #include "base/memory/ref_counted.h"
6*635a8641SAndroid Build Coastguard Worker 
7*635a8641SAndroid Build Coastguard Worker #include "base/threading/thread_collision_warner.h"
8*635a8641SAndroid Build Coastguard Worker 
9*635a8641SAndroid Build Coastguard Worker namespace base {
10*635a8641SAndroid Build Coastguard Worker namespace {
11*635a8641SAndroid Build Coastguard Worker 
12*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
13*635a8641SAndroid Build Coastguard Worker std::atomic_int g_cross_thread_ref_count_access_allow_count(0);
14*635a8641SAndroid Build Coastguard Worker #endif
15*635a8641SAndroid Build Coastguard Worker 
16*635a8641SAndroid Build Coastguard Worker }  // namespace
17*635a8641SAndroid Build Coastguard Worker 
18*635a8641SAndroid Build Coastguard Worker namespace subtle {
19*635a8641SAndroid Build Coastguard Worker 
HasOneRef() const20*635a8641SAndroid Build Coastguard Worker bool RefCountedThreadSafeBase::HasOneRef() const {
21*635a8641SAndroid Build Coastguard Worker   return ref_count_.IsOne();
22*635a8641SAndroid Build Coastguard Worker }
23*635a8641SAndroid Build Coastguard Worker 
24*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
~RefCountedThreadSafeBase()25*635a8641SAndroid Build Coastguard Worker RefCountedThreadSafeBase::~RefCountedThreadSafeBase() {
26*635a8641SAndroid Build Coastguard Worker   DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without "
27*635a8641SAndroid Build Coastguard Worker                       "calling Release()";
28*635a8641SAndroid Build Coastguard Worker }
29*635a8641SAndroid Build Coastguard Worker #endif
30*635a8641SAndroid Build Coastguard Worker 
31*635a8641SAndroid Build Coastguard Worker #if defined(ARCH_CPU_64_BIT)
AddRefImpl() const32*635a8641SAndroid Build Coastguard Worker void RefCountedBase::AddRefImpl() const {
33*635a8641SAndroid Build Coastguard Worker   // Check if |ref_count_| overflow only on 64 bit archs since the number of
34*635a8641SAndroid Build Coastguard Worker   // objects may exceed 2^32.
35*635a8641SAndroid Build Coastguard Worker   // To avoid the binary size bloat, use non-inline function here.
36*635a8641SAndroid Build Coastguard Worker   CHECK(++ref_count_ > 0);
37*635a8641SAndroid Build Coastguard Worker }
38*635a8641SAndroid Build Coastguard Worker #endif
39*635a8641SAndroid Build Coastguard Worker 
40*635a8641SAndroid Build Coastguard Worker #if !defined(ARCH_CPU_X86_FAMILY)
Release() const41*635a8641SAndroid Build Coastguard Worker bool RefCountedThreadSafeBase::Release() const {
42*635a8641SAndroid Build Coastguard Worker   return ReleaseImpl();
43*635a8641SAndroid Build Coastguard Worker }
AddRef() const44*635a8641SAndroid Build Coastguard Worker void RefCountedThreadSafeBase::AddRef() const {
45*635a8641SAndroid Build Coastguard Worker   AddRefImpl();
46*635a8641SAndroid Build Coastguard Worker }
47*635a8641SAndroid Build Coastguard Worker #endif
48*635a8641SAndroid Build Coastguard Worker 
49*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
CalledOnValidSequence() const50*635a8641SAndroid Build Coastguard Worker bool RefCountedBase::CalledOnValidSequence() const {
51*635a8641SAndroid Build Coastguard Worker   return sequence_checker_.CalledOnValidSequence() ||
52*635a8641SAndroid Build Coastguard Worker          g_cross_thread_ref_count_access_allow_count.load() != 0;
53*635a8641SAndroid Build Coastguard Worker }
54*635a8641SAndroid Build Coastguard Worker #endif
55*635a8641SAndroid Build Coastguard Worker 
56*635a8641SAndroid Build Coastguard Worker }  // namespace subtle
57*635a8641SAndroid Build Coastguard Worker 
58*635a8641SAndroid Build Coastguard Worker #if DCHECK_IS_ON()
ScopedAllowCrossThreadRefCountAccess()59*635a8641SAndroid Build Coastguard Worker ScopedAllowCrossThreadRefCountAccess::ScopedAllowCrossThreadRefCountAccess() {
60*635a8641SAndroid Build Coastguard Worker   ++g_cross_thread_ref_count_access_allow_count;
61*635a8641SAndroid Build Coastguard Worker }
62*635a8641SAndroid Build Coastguard Worker 
~ScopedAllowCrossThreadRefCountAccess()63*635a8641SAndroid Build Coastguard Worker ScopedAllowCrossThreadRefCountAccess::~ScopedAllowCrossThreadRefCountAccess() {
64*635a8641SAndroid Build Coastguard Worker   --g_cross_thread_ref_count_access_allow_count;
65*635a8641SAndroid Build Coastguard Worker }
66*635a8641SAndroid Build Coastguard Worker #endif
67*635a8641SAndroid Build Coastguard Worker 
68*635a8641SAndroid Build Coastguard Worker }  // namespace base
69