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

Side by Side Diff: media/base/bind_to_current_loop.h

Issue 1082113004: BindToCurrentLoop should delete callback on original thread Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 4 years, 8 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 | « media/base/BUILD.gn ('k') | media/base/bind_to_current_loop.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) 2015 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2015 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 #ifndef MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 5 #ifndef MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
6 #define MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 6 #define MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
7 7
8 #include "base/bind.h" 8 #include "base/bind.h"
9 #include "base/location.h" 9 #include "base/location.h"
10 #include "base/memory/ref_counted.h"
10 #include "base/memory/scoped_ptr.h" 11 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h" 12 #include "base/memory/scoped_vector.h"
12 #include "base/single_thread_task_runner.h" 13 #include "base/single_thread_task_runner.h"
13 #include "base/thread_task_runner_handle.h" 14 #include "base/thread_task_runner_handle.h"
15 #include "media/base/media_export.h"
14 16
15 // This is a helper utility for base::Bind()ing callbacks to the current 17 // This is a helper utility for base::Bind()ing callbacks to the current
16 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a 18 // MessageLoop. The typical use is when |a| (of class |A|) wants to hand a
17 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that 19 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that
18 // when |b| executes the callback, it does so on |a|'s current MessageLoop. 20 // when |b| executes the callback, it does so on |a|'s current MessageLoop.
19 // 21 //
20 // Typical usage: request to be called back on the current thread: 22 // Typical usage: request to be called back on the current thread:
21 // other->StartAsyncProcessAndCallMeBack( 23 // other->StartAsyncProcessAndCallMeBack(
22 // media::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this))); 24 // media::BindToCurrentLoop(base::Bind(&MyClass::MyMethod, this)));
23 // 25 //
24 // Note that like base::Bind(), BindToCurrentLoop() can't bind non-constant 26 // Note that like base::Bind(), BindToCurrentLoop() can't bind non-constant
25 // references, and that *unlike* base::Bind(), BindToCurrentLoop() makes copies 27 // references, and that *unlike* base::Bind(), BindToCurrentLoop() makes copies
26 // of its arguments, and thus can't be used with arrays. 28 // of its arguments, and thus can't be used with arrays.
29 //
30 // The callback passed in to BindToCurrentLoop is guaranteed to be deleted on
31 // the thread from which BindToCurrentLoop was invoked. This allows objects that
32 // must be deleted on the originating thread to be bound into it. In particular,
33 // it can be useful to use WeakPtr<> in the callback so that the reply operation
34 // can be canceled.
27 35
28 namespace media { 36 namespace media {
29 37
38 namespace internal {
39
30 // Mimic base::internal::CallbackForward, replacing std::move(p) with 40 // Mimic base::internal::CallbackForward, replacing std::move(p) with
31 // base::Passed(&p) to account for the extra layer of indirection. 41 // base::Passed(&p) to account for the extra layer of indirection.
32 namespace internal {
33 template <typename T> 42 template <typename T>
34 T& TrampolineForward(T& t) { return t; } 43 T& TrampolineForward(T& t) { return t; }
35 44
36 template <typename T, typename R> 45 template <typename T, typename R>
37 base::internal::PassedWrapper<scoped_ptr<T, R> > TrampolineForward( 46 base::internal::PassedWrapper<scoped_ptr<T, R> > TrampolineForward(
38 scoped_ptr<T, R>& p) { return base::Passed(&p); } 47 scoped_ptr<T, R>& p) { return base::Passed(&p); }
39 48
40 template <typename T> 49 template <typename T>
41 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( 50 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward(
42 ScopedVector<T>& p) { return base::Passed(&p); } 51 ScopedVector<T>& p) { return base::Passed(&p); }
43 52
44 // First, tell the compiler TrampolineHelper is a struct template with one 53 // Deleter suitable for use with scoped_ptr (or as traits for
45 // type parameter. Then define specializations where the type is a function 54 // RefCountedThreadSafe, if a subclass has a no-arguments constructor).
46 // returning void and taking zero or more arguments. 55 struct MEDIA_EXPORT DeleteOnLoop {
47 template <typename Sig> struct TrampolineHelper; 56 DeleteOnLoop(scoped_refptr<base::SingleThreadTaskRunner> loop);
57 ~DeleteOnLoop();
48 58
49 template <typename... Args> 59 template <typename T>
50 struct TrampolineHelper<void(Args...)> { 60 void Destruct(const T* ptr) const {
51 static void Run( 61 if (loop_->BelongsToCurrentThread()) {
52 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, 62 delete ptr;
53 const base::Callback<void(Args...)>& cb, 63 } else {
54 Args... args) { 64 if (!loop_->DeleteSoon(FROM_HERE, ptr)) {
55 task_runner->PostTask(FROM_HERE, 65 #if defined(UNIT_TEST)
56 base::Bind(cb, TrampolineForward(args)...)); 66 // Only logged under unit testing because leaks at shutdown
67 // are acceptable under normal circumstances.
68 LOG(FATAL) << "DeleteSoon failed";
69 #endif // UNIT_TEST
70 }
71 }
57 } 72 }
73
74 template <typename T>
75 inline void operator()(T* ptr) const {
76 enum { type_must_be_complete = sizeof(T) };
77 Destruct(ptr);
78 }
79
80 private:
81 scoped_refptr<base::SingleThreadTaskRunner> loop_;
58 }; 82 };
59 83
84 template<typename... A>
85 void PostBackToOriginLoop(
86 const scoped_refptr<base::SingleThreadTaskRunner>& origin_task_runner,
87 scoped_ptr<base::Callback<void(A...)>, DeleteOnLoop>* origin_cb_ptr_ptr,
88 A... args) {
89 origin_task_runner->PostTask(
90 FROM_HERE, base::Bind(**origin_cb_ptr_ptr,
91 TrampolineForward(args)...));
92 }
93
60 } // namespace internal 94 } // namespace internal
61 95
62 template<typename T> 96 // Function object which deletes its parameter, which must be a pointer,
63 static base::Callback<T> BindToCurrentLoop( 97 // on the task runner on which this object was constructed.
64 const base::Callback<T>& cb) { 98 //
65 return base::Bind(&internal::TrampolineHelper<T>::Run, 99 // This is more expensive than content::BrowserThread::DeleteOnUIThread etc;
66 base::ThreadTaskRunnerHandle::Get(), cb); 100 // use those instead when possible.
101 //
102 // Sample usage with RefCountedThreadSafe:
103 // class Foo : public base::RefCountedThreadSafe<
104 // Foo, media::DeleteOnCurrentLoop> {
105 // ...
106 // private:
107 // friend struct media::DeleteOnCurrentLoop;
108 // friend class base::DeleteHelper<Foo>;
109 // ~Foo();
110 // }
111 //
112 // Sample usage with scoped_ptr:
113 // scoped_ptr<Foo, media::DeleteOnCurrentLoop> ptr;
114 struct MEDIA_EXPORT DeleteOnCurrentLoop : internal::DeleteOnLoop {
115 DeleteOnCurrentLoop();
116 ~DeleteOnCurrentLoop();
117 };
118
119 template<typename... A>
120 static base::Callback<void(A...)> BindToCurrentLoop(
121 const base::Callback<void(A...)>& origin_cb) {
122 scoped_refptr<base::SingleThreadTaskRunner> loop =
123 base::ThreadTaskRunnerHandle::Get();
124 auto cb_copy_ptr = new base::Callback<void(A...)>(origin_cb);
125 // Normally it's pointless to create a scoped_ptr on the heap; here we're just
126 // (ab)using it to take advantage of its custom deleter support.
127 auto cb_copy_ptr_ptr =
128 new scoped_ptr<base::Callback<void(A...)>, internal::DeleteOnLoop>(
129 cb_copy_ptr, internal::DeleteOnLoop(loop));
130 return base::Bind(&internal::PostBackToOriginLoop<A...>,
131 loop,
132 base::Owned(cb_copy_ptr_ptr));
67 } 133 }
68 134
69 } // namespace media 135 } // namespace media
70 136
71 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_ 137 #endif // MEDIA_BASE_BIND_TO_CURRENT_LOOP_H_
OLDNEW
« no previous file with comments | « media/base/BUILD.gn ('k') | media/base/bind_to_current_loop.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698