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

Side by Side Diff: base/callback_internal.cc

Issue 2723423002: Start BindStateBase ref count from 1 instead of 0 (Closed)
Patch Set: +comment. +DCHECK. Created 3 years, 9 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
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 bool BindStateBase::HasOneRef() const {
21 return AtomicRefCountIsOne(&ref_count_);
22 }
23
20 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, 24 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
21 void (*destructor)(const BindStateBase*)) 25 void (*destructor)(const BindStateBase*))
22 : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) { 26 : BindStateBase(polymorphic_invoke, destructor, &ReturnFalse) {
23 } 27 }
24 28
25 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke, 29 BindStateBase::BindStateBase(InvokeFuncStorage polymorphic_invoke,
26 void (*destructor)(const BindStateBase*), 30 void (*destructor)(const BindStateBase*),
27 bool (*is_cancelled)(const BindStateBase*)) 31 bool (*is_cancelled)(const BindStateBase*))
28 : polymorphic_invoke_(polymorphic_invoke), 32 : polymorphic_invoke_(polymorphic_invoke),
29 ref_count_(0), 33 ref_count_(1),
30 destructor_(destructor), 34 destructor_(destructor),
31 is_cancelled_(is_cancelled) {} 35 is_cancelled_(is_cancelled) {}
32 36
33 void BindStateBase::AddRef() const { 37 void BindStateBase::AddRef() const {
38 #if DCHECK_IS_ON()
39 AtomicRefCount ref_count = subtle::Barrier_AtomicIncrement(&ref_count_, 1);
40 DCHECK_NE(1, ref_count);
41 #else
34 AtomicRefCountInc(&ref_count_); 42 AtomicRefCountInc(&ref_count_);
43 #endif
35 } 44 }
36 45
37 void BindStateBase::Release() const { 46 void BindStateBase::Release() const {
38 if (!AtomicRefCountDec(&ref_count_)) 47 if (!AtomicRefCountDec(&ref_count_))
39 destructor_(this); 48 destructor_(this);
40 } 49 }
41 50
42 CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default; 51 CallbackBase<CopyMode::MoveOnly>::CallbackBase(CallbackBase&& c) = default;
43 52
44 CallbackBase<CopyMode::MoveOnly>& 53 CallbackBase<CopyMode::MoveOnly>&
(...skipping 18 matching lines...) Expand all
63 bool CallbackBase<CopyMode::MoveOnly>::IsCancelled() const { 72 bool CallbackBase<CopyMode::MoveOnly>::IsCancelled() const {
64 DCHECK(bind_state_); 73 DCHECK(bind_state_);
65 return bind_state_->IsCancelled(); 74 return bind_state_->IsCancelled();
66 } 75 }
67 76
68 bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal( 77 bool CallbackBase<CopyMode::MoveOnly>::EqualsInternal(
69 const CallbackBase& other) const { 78 const CallbackBase& other) const {
70 return bind_state_ == other.bind_state_; 79 return bind_state_ == other.bind_state_;
71 } 80 }
72 81
73 CallbackBase<CopyMode::MoveOnly>::CallbackBase( 82 CallbackBase<CopyMode::MoveOnly>::CallbackBase(BindStateBase* bind_state)
74 BindStateBase* bind_state) 83 : bind_state_(AdoptRef(bind_state)) {
75 : bind_state_(bind_state) {
76 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1); 84 DCHECK(!bind_state_.get() || bind_state_->ref_count_ == 1);
77 } 85 }
78 86
79 CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {} 87 CallbackBase<CopyMode::MoveOnly>::~CallbackBase() {}
80 88
81 CallbackBase<CopyMode::Copyable>::CallbackBase( 89 CallbackBase<CopyMode::Copyable>::CallbackBase(
82 const CallbackBase& c) 90 const CallbackBase& c)
83 : CallbackBase<CopyMode::MoveOnly>(nullptr) { 91 : CallbackBase<CopyMode::MoveOnly>(nullptr) {
84 bind_state_ = c.bind_state_; 92 bind_state_ = c.bind_state_;
85 } 93 }
86 94
87 CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default; 95 CallbackBase<CopyMode::Copyable>::CallbackBase(CallbackBase&& c) = default;
88 96
89 CallbackBase<CopyMode::Copyable>& 97 CallbackBase<CopyMode::Copyable>&
90 CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) { 98 CallbackBase<CopyMode::Copyable>::operator=(const CallbackBase& c) {
91 bind_state_ = c.bind_state_; 99 bind_state_ = c.bind_state_;
92 return *this; 100 return *this;
93 } 101 }
94 102
95 CallbackBase<CopyMode::Copyable>& 103 CallbackBase<CopyMode::Copyable>&
96 CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default; 104 CallbackBase<CopyMode::Copyable>::operator=(CallbackBase&& c) = default;
97 105
98 template class CallbackBase<CopyMode::MoveOnly>; 106 template class CallbackBase<CopyMode::MoveOnly>;
99 template class CallbackBase<CopyMode::Copyable>; 107 template class CallbackBase<CopyMode::Copyable>;
100 108
101 } // namespace internal 109 } // namespace internal
102 } // namespace base 110 } // namespace base
OLDNEW
« no previous file with comments | « base/callback_internal.h ('k') | base/memory/ref_counted.h » ('j') | base/memory/ref_counted.h » ('J')

Powered by Google App Engine
This is Rietveld 408576698