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

Side by Side Diff: base/memory/weak_ptr.cc

Issue 7677028: Make WeakPtr thread-safe, i.e. allow cross-thread copying of WeakPtr (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: add suppression Created 9 years, 4 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 | Annotate | Revision Log
« no previous file with comments | « base/memory/weak_ptr.h ('k') | base/memory/weak_ptr_unittest.cc » ('j') | 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) 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(Flag** handle) : handle_(handle) { 10 WeakReference::Flag::Flag() : is_valid_(true) {
11 } 11 }
12 12
13 void WeakReference::Flag::Invalidate() { 13 void WeakReference::Flag::Invalidate() {
14 DCHECK(thread_checker_.CalledOnValidThread()); 14 // The flag being invalidated with a single ref implies that there are no
15 handle_ = NULL; 15 // weak pointers in existence. Allow deletion on other thread in this case.
16 DCHECK(thread_checker_.CalledOnValidThread() || HasOneRef());
17 is_valid_ = false;
16 } 18 }
17 19
18 bool WeakReference::Flag::IsValid() const { 20 bool WeakReference::Flag::IsValid() const {
19 DCHECK(thread_checker_.CalledOnValidThread()); 21 DCHECK(thread_checker_.CalledOnValidThread());
20 return handle_ != NULL; 22 return is_valid_;
21 } 23 }
22 24
23 WeakReference::Flag::~Flag() { 25 WeakReference::Flag::~Flag() {
24 if (handle_)
25 *handle_ = NULL;
26 } 26 }
27 27
28 WeakReference::WeakReference() { 28 WeakReference::WeakReference() {
29 } 29 }
30 30
31 WeakReference::WeakReference(Flag* flag) : flag_(flag) { 31 WeakReference::WeakReference(const Flag* flag) : flag_(flag) {
32 } 32 }
33 33
34 WeakReference::~WeakReference() { 34 WeakReference::~WeakReference() {
35 } 35 }
36 36
37 bool WeakReference::is_valid() const { 37 bool WeakReference::is_valid() const {
38 return flag_ && flag_->IsValid(); 38 return flag_ && flag_->IsValid();
39 } 39 }
40 40
41 WeakReferenceOwner::WeakReferenceOwner() : flag_(NULL) { 41 WeakReferenceOwner::WeakReferenceOwner() {
42 } 42 }
43 43
44 WeakReferenceOwner::~WeakReferenceOwner() { 44 WeakReferenceOwner::~WeakReferenceOwner() {
45 Invalidate(); 45 Invalidate();
46 } 46 }
47 47
48 WeakReference WeakReferenceOwner::GetRef() const { 48 WeakReference WeakReferenceOwner::GetRef() const {
49 if (!flag_) 49 // We also want to reattach to the current thread if all previous references
50 flag_ = new WeakReference::Flag(&flag_); 50 // have gone away.
51 if (!HasRefs())
52 flag_ = new WeakReference::Flag();
51 return WeakReference(flag_); 53 return WeakReference(flag_);
52 } 54 }
53 55
54 void WeakReferenceOwner::Invalidate() { 56 void WeakReferenceOwner::Invalidate() {
55 if (flag_) { 57 if (flag_) {
56 flag_->Invalidate(); 58 flag_->Invalidate();
57 flag_ = NULL; 59 flag_ = NULL;
58 } 60 }
59 } 61 }
60 62
61 WeakPtrBase::WeakPtrBase() { 63 WeakPtrBase::WeakPtrBase() {
62 } 64 }
63 65
64 WeakPtrBase::~WeakPtrBase() { 66 WeakPtrBase::~WeakPtrBase() {
65 } 67 }
66 68
67 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) { 69 WeakPtrBase::WeakPtrBase(const WeakReference& ref) : ref_(ref) {
68 } 70 }
69 71
70 } // namespace internal 72 } // namespace internal
71 } // namespace base 73 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/weak_ptr.h ('k') | base/memory/weak_ptr_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698