OLD | NEW |
1 $$ This is a pump file for generating file templates. Pump is a python | 1 $$ This is a pump file for generating file templates. Pump is a python |
2 $$ script that is part of the Google Test suite of utilities. Description | 2 $$ script that is part of the Google Test suite of utilities. Description |
3 $$ can be found here: | 3 $$ can be found here: |
4 $$ | 4 $$ |
5 $$ http://code.google.com/p/googletest/wiki/PumpManual | 5 $$ http://code.google.com/p/googletest/wiki/PumpManual |
6 $$ | 6 $$ |
7 | 7 |
8 $$ See comment for MAX_ARITY in base/bind.h.pump. | 8 $$ See comment for MAX_ARITY in base/bind.h.pump. |
9 $var MAX_ARITY = 7 | 9 $var MAX_ARITY = 7 |
10 | 10 |
11 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 11 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
12 // Use of this source code is governed by a BSD-style license that can be | 12 // Use of this source code is governed by a BSD-style license that can be |
13 // found in the LICENSE file. | 13 // found in the LICENSE file. |
14 | 14 |
15 #ifndef MEDIA_BASE_BIND_TO_LOOP_H_ | 15 #ifndef MEDIA_BASE_BIND_TO_LOOP_H_ |
16 #define MEDIA_BASE_BIND_TO_LOOP_H_ | 16 #define MEDIA_BASE_BIND_TO_LOOP_H_ |
17 | 17 |
18 #include "base/bind.h" | 18 #include "base/bind.h" |
19 #include "base/location.h" | 19 #include "base/location.h" |
20 #include "base/message_loop/message_loop_proxy.h" | 20 #include "base/message_loop/message_loop_proxy.h" |
| 21 #include "base/synchronization/waitable_event.h" |
21 | 22 |
22 // This is a helper utility for base::Bind()ing callbacks on to particular | 23 // This is a helper utility for base::Bind()ing callbacks on to particular |
23 // MessageLoops. A typical use is when |a| (of class |A|) wants to hand a | 24 // MessageLoops. A typical use is when |a| (of class |A|) wants to hand a |
24 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that | 25 // callback such as base::Bind(&A::AMethod, a) to |b|, but needs to ensure that |
25 // when |b| executes the callback, it does so on a particular MessageLoop. | 26 // when |b| executes the callback, it does so on a particular MessageLoop. |
26 // | 27 // |
27 // Typical usage: request to be called back on the current thread: | 28 // Typical usage: request to be called back on the current thread: |
28 // other->StartAsyncProcessAndCallMeBack( | 29 // other->StartAsyncProcessAndCallMeBack( |
29 // media::BindToLoop(MessageLoopProxy::current(), | 30 // media::BindToLoop(MessageLoopProxy::current(), |
30 // base::Bind(&MyClass::MyMethod, this))); | 31 // base::Bind(&MyClass::MyMethod, this))); |
31 // | 32 // |
32 // Note that like base::Bind(), BindToLoop() can't bind non-constant references, | 33 // For synchronous usage, where the callback needs to be executed on another |
33 // and that *unlike* base::Bind(), BindToLoop() makes copies of its arguments, | 34 // MessageLoop, but the calling thread should block until the callback has been |
34 // and thus can't be used with arrays. | 35 // completed, there is BindToLoopSync(), as below: |
| 36 // |
| 37 // Typical usage: make a request to another thread, and wait for it: |
| 38 // do_something_callback = |
| 39 // media::BindToLoopSync(other_message_loop, &OtherClass::DoRequest, other); |
| 40 // // ... |
| 41 // do_something_callback.Run(); // will block until completion |
| 42 // |
| 43 // Note that like base::Bind(), BindToLoop() and BindToLoopSync() can't bind |
| 44 // non-constant references, and that *unlike* base::Bind(), BindToLoop*() makes |
| 45 // copies of its arguments, and thus can't be used with arrays. |
35 | 46 |
36 namespace media { | 47 namespace media { |
37 | 48 |
38 // Mimic base::internal::CallbackForward, replacing p.Pass() with | 49 // Mimic base::internal::CallbackForward, replacing p.Pass() with |
39 // base::Passed(&p) to account for the extra layer of indirection. | 50 // base::Passed(&p) to account for the extra layer of indirection. |
40 namespace internal { | 51 namespace internal { |
41 template <typename T> | 52 template <typename T> |
42 T& TrampolineForward(T& t) { return t; } | 53 T& TrampolineForward(T& t) { return t; } |
43 | 54 |
44 template <typename T> | 55 template <typename T> |
45 base::internal::PassedWrapper<scoped_ptr<T> > TrampolineForward( | 56 base::internal::PassedWrapper<scoped_ptr<T> > TrampolineForward( |
46 scoped_ptr<T>& p) { return base::Passed(&p); } | 57 scoped_ptr<T>& p) { return base::Passed(&p); } |
47 | 58 |
48 template <typename T, typename R> | 59 template <typename T, typename R> |
49 base::internal::PassedWrapper<scoped_ptr_malloc<T, R> > TrampolineForward( | 60 base::internal::PassedWrapper<scoped_ptr_malloc<T, R> > TrampolineForward( |
50 scoped_ptr_malloc<T, R>& p) { return base::Passed(&p); } | 61 scoped_ptr_malloc<T, R>& p) { return base::Passed(&p); } |
51 | 62 |
52 template <typename T> | 63 template <typename T> |
53 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( | 64 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( |
54 ScopedVector<T>& p) { return base::Passed(&p); } | 65 ScopedVector<T>& p) { return base::Passed(&p); } |
55 | 66 |
56 template <typename T> struct TrampolineHelper; | 67 template <typename T> struct TrampolineHelper; |
57 | 68 |
| 69 // Caller helper to call a base::Closure synchronously |
| 70 void TrampolineSyncCaller(const base::Closure& closure, |
| 71 base::WaitableEvent* waiter); |
| 72 |
58 $range ARITY 0..MAX_ARITY | 73 $range ARITY 0..MAX_ARITY |
59 $for ARITY [[ | 74 $for ARITY [[ |
60 $range ARG 1..ARITY | 75 $range ARG 1..ARITY |
61 | 76 |
62 template <$for ARG , [[typename A$(ARG)]]> | 77 template <$for ARG , [[typename A$(ARG)]]> |
63 struct TrampolineHelper<void($for ARG , [[A$(ARG)]])> { | 78 struct TrampolineHelper<void($for ARG , [[A$(ARG)]])> { |
64 static void Run( | 79 static void Run( |
65 const scoped_refptr<base::MessageLoopProxy>& loop, | 80 const scoped_refptr<base::MessageLoopProxy>& loop, |
66 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb | 81 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb |
67 $if ARITY != 0 [[, ]] | 82 $if ARITY != 0 [[, ]] |
68 $for ARG , [[A$(ARG) a$(ARG)]] | 83 $for ARG , [[A$(ARG) a$(ARG)]] |
69 ) { | 84 ) { |
70 loop->PostTask(FROM_HERE, base::Bind(cb | 85 loop->PostTask(FROM_HERE, base::Bind(cb |
71 $if ARITY != 0 [[, ]] | 86 $if ARITY != 0 [[, ]] |
72 $for ARG , [[internal::TrampolineForward(a$(ARG))]])); | 87 $for ARG , [[internal::TrampolineForward(a$(ARG))]])); |
73 } | 88 } |
| 89 static void RunSync( |
| 90 const scoped_refptr<base::MessageLoopProxy>& loop, |
| 91 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb |
| 92 $if ARITY != 0 [[, ]] |
| 93 $for ARG , [[A$(ARG) a$(ARG)]] |
| 94 ) { |
| 95 DCHECK(!loop->BelongsToCurrentThread()); |
| 96 base::WaitableEvent waiter(false, false); |
| 97 loop->PostTask( |
| 98 FROM_HERE, |
| 99 base::Bind( |
| 100 &TrampolineSyncCaller, |
| 101 base::Bind(cb |
| 102 $if ARITY != 0 [[, ]] |
| 103 $for ARG , [[internal::TrampolineForward(a$(ARG))]]), |
| 104 &waiter)); |
| 105 waiter.Wait(); |
| 106 } |
74 }; | 107 }; |
75 | 108 |
76 | 109 |
77 ]] $$ for ARITY | 110 ]] $$ for ARITY |
78 | 111 |
79 } // namespace internal | 112 } // namespace internal |
80 | 113 |
81 template<typename T> | 114 template<typename T> |
82 static base::Callback<T> BindToLoop( | 115 static base::Callback<T> BindToLoop( |
83 const scoped_refptr<base::MessageLoopProxy>& loop, | 116 const scoped_refptr<base::MessageLoopProxy>& loop, |
84 const base::Callback<T>& cb) { | 117 const base::Callback<T>& cb) { |
85 return base::Bind(&internal::TrampolineHelper<T>::Run, loop, cb); | 118 return base::Bind(&internal::TrampolineHelper<T>::Run, loop, cb); |
86 } | 119 } |
87 | 120 |
88 template<typename T> | 121 template<typename T> |
| 122 static base::Callback<T> BindToLoopSync( |
| 123 const scoped_refptr<base::MessageLoopProxy>& loop, |
| 124 const base::Callback<T>& cb) { |
| 125 return base::Bind(&internal::TrampolineHelper<T>::RunSync, loop, cb); |
| 126 } |
| 127 |
| 128 template<typename T> |
89 static base::Callback<T> BindToCurrentLoop( | 129 static base::Callback<T> BindToCurrentLoop( |
90 const base::Callback<T>& cb) { | 130 const base::Callback<T>& cb) { |
91 return BindToLoop(base::MessageLoopProxy::current(), cb); | 131 return BindToLoop(base::MessageLoopProxy::current(), cb); |
92 } | 132 } |
93 | 133 |
94 } // namespace media | 134 } // namespace media |
95 | 135 |
96 #endif // MEDIA_BASE_BIND_TO_LOOP_H_ | 136 #endif // MEDIA_BASE_BIND_TO_LOOP_H_ |
OLD | NEW |