OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 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/memory/weak_ptr.h" | 5 #include "base/memory/weak_ptr.h" |
6 | 6 |
7 namespace base { | 7 namespace base { |
8 namespace internal { | 8 namespace internal { |
9 | 9 |
10 WeakReference::Flag::Flag() : is_valid_(true) { | 10 WeakReference::Flag::Flag() : is_valid_(true) { |
| 11 // Flags only become bound when checked for validity, or invalidated, |
| 12 // so that we can check that later validity/invalidation operations on |
| 13 // the same Flag take place on the same thread. |
| 14 thread_checker_.DetachFromThread(); |
11 } | 15 } |
12 | 16 |
13 void WeakReference::Flag::Invalidate() { | 17 void WeakReference::Flag::Invalidate() { |
14 // The flag being invalidated with a single ref implies that there are no | 18 // The flag being invalidated with a single ref implies that there are no |
15 // weak pointers in existence. Allow deletion on other thread in this case. | 19 // weak pointers in existence. Allow deletion on other thread in this case. |
16 DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef()); | 20 DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef()) |
| 21 << "WeakPtrs must be checked and invalidated on the same thread."; |
17 is_valid_ = false; | 22 is_valid_ = false; |
18 } | 23 } |
19 | 24 |
20 bool WeakReference::Flag::IsValid() const { | 25 bool WeakReference::Flag::IsValid() const { |
21 DCHECK(thread_checker_.CalledOnValidThread()); | 26 DCHECK(thread_checker_.CalledOnValidThread()) |
| 27 << "WeakPtrs must be checked and invalidated on the same thread."; |
22 return is_valid_; | 28 return is_valid_; |
23 } | 29 } |
24 | 30 |
25 WeakReference::Flag::~Flag() { | 31 WeakReference::Flag::~Flag() { |
26 } | 32 } |
27 | 33 |
28 WeakReference::WeakReference() { | 34 WeakReference::WeakReference() { |
29 } | 35 } |
30 | 36 |
31 WeakReference::WeakReference(const Flag* flag) : flag_(flag) { | 37 WeakReference::WeakReference(const Flag* flag) : flag_(flag) { |
32 } | 38 } |
33 | 39 |
34 WeakReference::~WeakReference() { | 40 WeakReference::~WeakReference() { |
35 } | 41 } |
36 | 42 |
37 bool WeakReference::is_valid() const { | 43 bool WeakReference::is_valid() const { |
38 return flag_ && flag_->IsValid(); | 44 return flag_ && flag_->IsValid(); |
39 } | 45 } |
40 | 46 |
41 WeakReferenceOwner::WeakReferenceOwner() { | 47 WeakReferenceOwner::WeakReferenceOwner() { |
42 } | 48 } |
43 | 49 |
44 WeakReferenceOwner::~WeakReferenceOwner() { | 50 WeakReferenceOwner::~WeakReferenceOwner() { |
45 Invalidate(); | 51 Invalidate(); |
46 } | 52 } |
47 | 53 |
48 WeakReference WeakReferenceOwner::GetRef() const { | 54 WeakReference WeakReferenceOwner::GetRef() const { |
49 // We also want to reattach to the current thread if all previous references | 55 // If we hold the last reference to the Flag then create a new one. |
50 // have gone away. | |
51 if (!HasRefs()) | 56 if (!HasRefs()) |
52 flag_ = new WeakReference::Flag(); | 57 flag_ = new WeakReference::Flag(); |
| 58 |
53 return WeakReference(flag_); | 59 return WeakReference(flag_); |
54 } | 60 } |
55 | 61 |
56 void WeakReferenceOwner::Invalidate() { | 62 void WeakReferenceOwner::Invalidate() { |
57 if (flag_) { | 63 if (flag_) { |
58 flag_->Invalidate(); | 64 flag_->Invalidate(); |
59 flag_ = NULL; | 65 flag_ = NULL; |
60 } | 66 } |
61 } | 67 } |
62 | 68 |
63 WeakPtrBase::WeakPtrBase() { | 69 WeakPtrBase::WeakPtrBase() { |
64 } | 70 } |
65 | 71 |
66 WeakPtrBase::~WeakPtrBase() { | 72 WeakPtrBase::~WeakPtrBase() { |
67 } | 73 } |
68 | 74 |
69 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { | 75 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { |
70 } | 76 } |
71 | 77 |
72 } // namespace internal | 78 } // namespace internal |
73 } // namespace base | 79 } // namespace base |
OLD | NEW |