Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1344)

Side by Side Diff: base/callback_internal.cc

Issue 2340153004: Make BindStateBase RefCountedThreadSafe (Closed)
Patch Set: +comment Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« base/callback_internal.h ('K') | « base/callback_internal.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/callback_internal.h" 5 #include "base/callback_internal.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 8
9 namespace base { 9 namespace base {
10 namespace internal { 10 namespace internal {
11 11
12 namespace { 12 namespace {
13 13
14 bool ReturnFalse(const BindStateBase*) { 14 bool ReturnFalse(const BindStateBase*) {
15 return false; 15 return false;
16 } 16 }
17 17
18 } // namespace 18 } // namespace
19 19
20 // static
21 void BindStateBaseRefCountTraits::Destruct(const BindStateBase* obj) {
22 obj->destructor_(obj);
23 }
24
20 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, 25 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
21 void (*destructor)(const BindStateBase*)) 26 void (*destructor)(const BindStateBase*))
22 : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) { 27 : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) {
23 } 28 }
24 29
25 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, 30 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
26 void (*destructor)(const BindStateBase*), 31 void (*destructor)(const BindStateBase*),
27 bool (*is_cancelled)(const BindStateBase*)) 32 bool (*is_cancelled)(const BindStateBase*))
28 : polymorphic_invoke_(polymorphic_invoke), 33 : polymorphic_invoke_(polymorphic_invoke),
29 ref_count_(0),
30 destructor_(destructor), 34 destructor_(destructor),
31 is_cancelled_(is_cancelled) {} 35 is_cancelled_(is_cancelled) {}
32 36
33 void BindStateBase::AddRef() const {
34 AtomicRefCountInc(&ref_count_);
35 }
36
37 void BindStateBase::Release() const {
38 if (!AtomicRefCountDec(&ref_count_))
39 destructor_(this);
40 }
41
42 CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default; 37 CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default;
43 38
44 CallbackBase<CopyMode::MoveOnly>& 39 CallbackBase<CopyMode::MoveOnly>&
45 CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& c) = default; 40 CallbackBase<CopyMode::MoveOnly>::operator=(CallbackBase&& c) = default;
46 41
47 CallbackBase<CopyMode::MoveOnly>::CallbackBase( 42 CallbackBase<CopyMode::MoveOnly>::CallbackBase(
48 const CallbackBase<CopyMode::Copyable>& c) 43 const CallbackBase<CopyMode::Copyable>& c)
49 : bind_state_(c.bind_state_) {} 44 : bind_state_(c.bind_state_) {}
50 45
51 CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=( 46 CallbackBase<CopyMode::MoveOnly>& CallbackBase<CopyMode::MoveOnly>::operator=(
(...skipping 14 matching lines...) Expand all
66 } 61 }
67 62
68 bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal( 63 bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal(
69 const CallbackBase& other) const { 64 const CallbackBase& other) const {
70 return bind_state_ == other.bind_state_; 65 return bind_state_ == other.bind_state_;
71 } 66 }
72 67
73 CallbackBase<CopyMode::MoveOnly>::CallbackBase( 68 CallbackBase<CopyMode::MoveOnly>::CallbackBase(
74 BindStateBase* bind_state) 69 BindStateBase* bind_state)
75 : bind_state_(bind_state) { 70 : bind_state_(bind_state) {
76 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1); 71 DCHECK(!bind_state_.get() || bind_state_->HasOneRef());
77 } 72 }
78 73
79 CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {} 74 CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {}
80 75
81 CallbackBase<CopyMode::Copyable>::CallbackBase( 76 CallbackBase<CopyMode::Copyable>::CallbackBase(
82 const CallbackBase& c) 77 const CallbackBase& c)
83 : CallbackBase<CopyMode::MoveOnly>(nullptr) { 78 : CallbackBase<CopyMode::MoveOnly>(nullptr) {
84 bind_state_ = c.bind_state_; 79 bind_state_ = c.bind_state_;
85 } 80 }
86 81
87 CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default; 82 CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default;
88 83
89 CallbackBase<CopyMode::Copyable>& 84 CallbackBase<CopyMode::Copyable>&
90 CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) { 85 CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) {
91 bind_state_ = c.bind_state_; 86 bind_state_ = c.bind_state_;
92 return *this; 87 return *this;
93 } 88 }
94 89
95 CallbackBase<CopyMode::Copyable>& 90 CallbackBase<CopyMode::Copyable>&
96 CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default; 91 CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default;
97 92
98 template class CallbackBase<CopyMode::MoveOnly>; 93 template class CallbackBase<CopyMode::MoveOnly>;
99 template class CallbackBase<CopyMode::Copyable>; 94 template class CallbackBase<CopyMode::Copyable>;
100 95
101 } // namespace internal 96 } // namespace internal
102 } // namespace base 97 } // namespace base
OLDNEW
« base/callback_internal.h ('K') | « base/callback_internal.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698