/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 | 2.43k | RefCountedThreadSafeBase::RefCountedThreadSafeBase() : ref_count_(0) { |
59 | 2.43k | #ifndef NDEBUG |
60 | 2.43k | in_dtor_ = false; |
61 | 2.43k | #endif |
62 | 2.43k | } |
63 | | |
64 | 2.42k | RefCountedThreadSafeBase::~RefCountedThreadSafeBase() { |
65 | 2.42k | #ifndef NDEBUG |
66 | 2.42k | DCHECK(in_dtor_) << "RefCountedThreadSafe object deleted without " |
67 | 0 | "calling Release()"; |
68 | 2.42k | #endif |
69 | 2.42k | } |
70 | | |
71 | 5.91k | void RefCountedThreadSafeBase::AddRef() const { |
72 | 5.91k | #ifndef NDEBUG |
73 | 5.91k | DCHECK(!in_dtor_); |
74 | 5.91k | #endif |
75 | 5.91k | base::RefCountInc(&ref_count_); |
76 | 5.91k | } |
77 | | |
78 | 5.89k | bool RefCountedThreadSafeBase::Release() const { |
79 | 5.89k | #ifndef NDEBUG |
80 | 5.89k | DCHECK(!in_dtor_); |
81 | 5.89k | DCHECK(!base::RefCountIsZero(&ref_count_)); |
82 | 5.89k | #endif |
83 | 5.89k | if (!base::RefCountDec(&ref_count_)) { |
84 | 2.42k | #ifndef NDEBUG |
85 | 2.42k | in_dtor_ = true; |
86 | 2.42k | #endif |
87 | 2.42k | return true; |
88 | 2.42k | } |
89 | 3.47k | return false; |
90 | 5.89k | } |
91 | | |
92 | | } // namespace subtle |
93 | | |
94 | | } // namespace doris |