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

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

Issue 7013059: Fix base::internal::WeakReferenceOwner races. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 9 years, 7 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 | « no previous file | base/memory/weak_ptr.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 // Weak pointers help in cases where you have many objects referring back to a 5 // Weak pointers help in cases where you have many objects referring back to a
6 // shared object and you wish for the lifetime of the shared object to not be 6 // shared object and you wish for the lifetime of the shared object to not be
7 // bound to the lifetime of the referrers. In other words, this is useful when 7 // bound to the lifetime of the referrers. In other words, this is useful when
8 // reference counting is not a good fit. 8 // reference counting is not a good fit.
9 // 9 //
10 // A common alternative to weak pointers is to have the shared object hold a 10 // A common alternative to weak pointers is to have the shared object hold a
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 // WARNING: weak pointers are not threadsafe!!! You must only use a WeakPtr 48 // WARNING: weak pointers are not threadsafe!!! You must only use a WeakPtr
49 // instance on thread where it was created. 49 // instance on thread where it was created.
50 50
51 #ifndef BASE_MEMORY_WEAK_PTR_H_ 51 #ifndef BASE_MEMORY_WEAK_PTR_H_
52 #define BASE_MEMORY_WEAK_PTR_H_ 52 #define BASE_MEMORY_WEAK_PTR_H_
53 #pragma once 53 #pragma once
54 54
55 #include "base/base_api.h" 55 #include "base/base_api.h"
56 #include "base/logging.h" 56 #include "base/logging.h"
57 #include "base/memory/ref_counted.h" 57 #include "base/memory/ref_counted.h"
58 #include "base/synchronization/lock.h"
58 #include "base/threading/thread_checker.h" 59 #include "base/threading/thread_checker.h"
59 60
60 namespace base { 61 namespace base {
61 62
62 namespace internal { 63 namespace internal {
63 // These classes are part of the WeakPtr implementation. 64 // These classes are part of the WeakPtr implementation.
64 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF. 65 // DO NOT USE THESE CLASSES DIRECTLY YOURSELF.
65 66
66 class BASE_API WeakReference { 67 class BASE_API WeakReference {
67 public: 68 public:
(...skipping 28 matching lines...) Expand all
96 }; 97 };
97 98
98 class BASE_API WeakReferenceOwner { 99 class BASE_API WeakReferenceOwner {
99 public: 100 public:
100 WeakReferenceOwner(); 101 WeakReferenceOwner();
101 ~WeakReferenceOwner(); 102 ~WeakReferenceOwner();
102 103
103 WeakReference GetRef() const; 104 WeakReference GetRef() const;
104 105
105 bool HasRefs() const { 106 bool HasRefs() const {
107 AutoLock auto_lock(lock_);
106 return flag_ != NULL; 108 return flag_ != NULL;
107 } 109 }
108 110
109 void Invalidate(); 111 void Invalidate();
110 112
111 // Indicates that this object will be used on another thread from now on. 113 // Indicates that this object will be used on another thread from now on.
112 void DetachFromThread() { 114 void DetachFromThread() {
115 AutoLock auto_lock(lock_);
113 if (flag_) flag_->DetachFromThread(); 116 if (flag_) flag_->DetachFromThread();
114 } 117 }
115 118
116 private: 119 private:
120 // Needs locking since WeakReference::Flag may be destroyed on a different
121 // thread.
122 mutable Lock lock_;
117 mutable WeakReference::Flag* flag_; 123 mutable WeakReference::Flag* flag_;
118 }; 124 };
119 125
120 // This class simplifies the implementation of WeakPtr's type conversion 126 // This class simplifies the implementation of WeakPtr's type conversion
121 // constructor by avoiding the need for a public accessor for ref_. A 127 // constructor by avoiding the need for a public accessor for ref_. A
122 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this 128 // WeakPtr<T> cannot access the private members of WeakPtr<U>, so this
123 // base class gives us a way to access ref_ in a protected fashion. 129 // base class gives us a way to access ref_ in a protected fashion.
124 class BASE_API WeakPtrBase { 130 class BASE_API WeakPtrBase {
125 public: 131 public:
126 WeakPtrBase(); 132 WeakPtrBase();
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 252
247 private: 253 private:
248 internal::WeakReferenceOwner weak_reference_owner_; 254 internal::WeakReferenceOwner weak_reference_owner_;
249 T* ptr_; 255 T* ptr_;
250 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory); 256 DISALLOW_IMPLICIT_CONSTRUCTORS(WeakPtrFactory);
251 }; 257 };
252 258
253 } // namespace base 259 } // namespace base
254 260
255 #endif // BASE_MEMORY_WEAK_PTR_H_ 261 #endif // BASE_MEMORY_WEAK_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | base/memory/weak_ptr.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698