/root/doris/be/src/gutil/ref_counted.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011 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 | | #include "gutil/ref_counted.h" |
6 | | |
7 | | #include "gutil/atomic_refcount.h" |
8 | | |
9 | | namespace doris { |
10 | | |
11 | | namespace subtle { |
12 | | |
13 | | RefCountedBase::RefCountedBase() |
14 | | : ref_count_(0) |
15 | | #ifndef NDEBUG |
16 | | , |
17 | | in_dtor_(false) |
18 | | #endif |
19 | 5 | { |
20 | 5 | } |
21 | | |
22 | 5 | RefCountedBase::~RefCountedBase() { |
23 | 5 | #ifndef NDEBUG |
24 | 5 | DCHECK(in_dtor_) << "RefCounted object deleted without calling Release()"; |
25 | 5 | #endif |
26 | 5 | } |
27 | | |
28 | 27 | void RefCountedBase::AddRef() const { |
29 | | // TODO(maruel): Add back once it doesn't assert 500 times/sec. |
30 | | // Current thread books the critical section "AddRelease" without release it. |
31 | | // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); |
32 | 27 | #ifndef NDEBUG |
33 | 27 | DCHECK(!in_dtor_); |
34 | 27 | #endif |
35 | 27 | ++ref_count_; |
36 | 27 | } |
37 | | |
38 | 27 | bool RefCountedBase::Release() const { |
39 | | // TODO(maruel): Add back once it doesn't assert 500 times/sec. |
40 | | // Current thread books the critical section "AddRelease" without release it. |
41 | | // DFAKE_SCOPED_LOCK_THREAD_LOCKED(add_release_); |
42 | 27 | #ifndef NDEBUG |
43 | 27 | DCHECK(!in_dtor_); |
44 | 27 | #endif |
45 | 27 | if (--ref_count_ == 0) { |
46 | 5 | #ifndef NDEBUG |
47 | 5 | in_dtor_ = true; |
48 | 5 | #endif |
49 | 5 | return true; |
50 | 5 | } |
51 | 22 | return false; |
52 | 27 | } |
53 | | |
54 | 0 | bool RefCountedThreadSafeBase::HasOneRef() const { |
55 | 0 | return base::RefCountIsOne(&const_cast<RefCountedThreadSafeBase*>(this)->ref_count_); |
56 | 0 | } |
57 | | |
58 | 3.45k | RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { |
59 | 3.45k | #ifndef NDEBUG |
60 | 3.45k | in_dtor_ = false; |
61 | 3.45k | #endif |
62 | 3.45k | } |
63 | | |
64 | 3.44k | RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
65 | 3.44k | #ifndef NDEBUG |
66 | 3.44k | DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
67 | 2 | "calling Release()"; |
68 | 3.44k | #endif |
69 | 3.44k | } |
70 | | |
71 | 8.12k | void RefCountedThreadSafeBase::AddRef() const { |
72 | 8.12k | #ifndef NDEBUG |
73 | 8.12k | DCHECK(!in_dtor_); |
74 | 8.12k | #endif |
75 | 8.12k | base::RefCountInc(&ref_count_); |
76 | 8.12k | } |
77 | | |
78 | 8.12k | bool RefCountedThreadSafeBase::Release() const { |
79 | 8.12k | #ifndef NDEBUG |
80 | 8.12k | DCHECK(!in_dtor_); |
81 | 8.12k | DCHECK(!base::RefCountIsZero(&ref_count_)); |
82 | 8.12k | #endif |
83 | 8.12k | if (!base::RefCountDec(&ref_count_)) { |
84 | 3.44k | #ifndef NDEBUG |
85 | 3.44k | in_dtor_ = true; |
86 | 3.44k | #endif |
87 | 3.44k | return true; |
88 | 3.44k | } |
89 | 4.67k | return false; |
90 | 8.12k | } |
91 | | |
92 | | } // namespace subtle |
93 | | |
94 | | } // namespace doris |