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

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

Issue 1384863002: Clarify WeakPtr threading characteristic. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 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
« no previous file with comments | « no previous file | 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 // Weak pointers are pointers to an object that do not affect its lifetime, 5 // Weak pointers are pointers to an object that do not affect its lifetime,
6 // and which may be invalidated (i.e. reset to NULL) by the object, or its 6 // and which may be invalidated (i.e. reset to NULL) by the object, or its
7 // owner, at any time, most commonly when the object is about to be deleted. 7 // owner, at any time, most commonly when the object is about to be deleted.
8 8
9 // Weak pointers are useful when an object needs to be accessed safely by one 9 // Weak pointers are useful when an object needs to be accessed safely by one
10 // or more objects other than its owner, and those callers can cope with the 10 // or more objects other than its owner, and those callers can cope with the
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
52 // dereferenced and invalidated on the same SequencedTaskRunner otherwise 52 // dereferenced and invalidated on the same SequencedTaskRunner otherwise
53 // checking the pointer would be racey. 53 // checking the pointer would be racey.
54 // 54 //
55 // To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory 55 // To ensure correct use, the first time a WeakPtr issued by a WeakPtrFactory
56 // is dereferenced, the factory and its WeakPtrs become bound to the calling 56 // is dereferenced, the factory and its WeakPtrs become bound to the calling
57 // thread or current SequencedWorkerPool token, and cannot be dereferenced or 57 // thread or current SequencedWorkerPool token, and cannot be dereferenced or
58 // invalidated on any other task runner. Bound WeakPtrs can still be handed 58 // invalidated on any other task runner. Bound WeakPtrs can still be handed
59 // off to other task runners, e.g. to use to post tasks back to object on the 59 // off to other task runners, e.g. to use to post tasks back to object on the
60 // bound sequence. 60 // bound sequence.
61 // 61 //
62 // Invalidating the factory's WeakPtrs un-binds it from the sequence, allowing 62 // If all WeakPtr objects are destroyed or invalidated then the factory is
63 // it to be passed for a different sequence to use or delete it. 63 // unbound from the SequencedTaskRunner/Thread. The WeakPtrFactory may then be
64 // destroyed, or new WeakPtr objects may be used, from a different sequence.
65 //
66 // Thus, at least one WeakPtr object must exist and have been dereferenced on
67 // the correct thread to enforce that other WeakPtr objects will enforce they
68 // are used on the desired thread.
64 69
65 #ifndef BASE_MEMORY_WEAK_PTR_H_ 70 #ifndef BASE_MEMORY_WEAK_PTR_H_
66 #define BASE_MEMORY_WEAK_PTR_H_ 71 #define BASE_MEMORY_WEAK_PTR_H_
67 72
68 #include "base/basictypes.h" 73 #include "base/basictypes.h"
69 #include "base/base_export.h" 74 #include "base/base_export.h"
70 #include "base/logging.h" 75 #include "base/logging.h"
71 #include "base/memory/ref_counted.h" 76 #include "base/memory/ref_counted.h"
72 #include "base/sequence_checker.h" 77 #include "base/sequence_checker.h"
73 #include "base/template_util.h" 78 #include "base/template_util.h"
(...skipping 257 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails. 336 // base::WeakPtr<Derived> ptr = derived.AsWeakPtr(); // Fails.
332 337
333 template <typename Derived> 338 template <typename Derived>
334 WeakPtr<Derived> AsWeakPtr(Derived* t) { 339 WeakPtr<Derived> AsWeakPtr(Derived* t) {
335 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t); 340 return internal::SupportsWeakPtrBase::StaticAsWeakPtr<Derived>(t);
336 } 341 }
337 342
338 } // namespace base 343 } // namespace base
339 344
340 #endif // BASE_MEMORY_WEAK_PTR_H_ 345 #endif // BASE_MEMORY_WEAK_PTR_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698