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))); |
(...skipping 17 matching lines...) Expand all Loading... | |
48 template <typename T, typename R> | 49 template <typename T, typename R> |
49 base::internal::PassedWrapper<scoped_ptr_malloc<T, R> > TrampolineForward( | 50 base::internal::PassedWrapper<scoped_ptr_malloc<T, R> > TrampolineForward( |
50 scoped_ptr_malloc<T, R>& p) { return base::Passed(&p); } | 51 scoped_ptr_malloc<T, R>& p) { return base::Passed(&p); } |
51 | 52 |
52 template <typename T> | 53 template <typename T> |
53 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( | 54 base::internal::PassedWrapper<ScopedVector<T> > TrampolineForward( |
54 ScopedVector<T>& p) { return base::Passed(&p); } | 55 ScopedVector<T>& p) { return base::Passed(&p); } |
55 | 56 |
56 template <typename T> struct TrampolineHelper; | 57 template <typename T> struct TrampolineHelper; |
57 | 58 |
59 // Caller helper to call a base::Closure synchronously | |
60 void TrampolineSyncCaller(const base::Closure& closure, | |
61 base::WaitableEvent* waiter); | |
62 | |
58 $range ARITY 0..MAX_ARITY | 63 $range ARITY 0..MAX_ARITY |
59 $for ARITY [[ | 64 $for ARITY [[ |
60 $range ARG 1..ARITY | 65 $range ARG 1..ARITY |
61 | 66 |
62 template <$for ARG , [[typename A$(ARG)]]> | 67 template <$for ARG , [[typename A$(ARG)]]> |
63 struct TrampolineHelper<void($for ARG , [[A$(ARG)]])> { | 68 struct TrampolineHelper<void($for ARG , [[A$(ARG)]])> { |
64 static void Run( | 69 static void Run( |
65 const scoped_refptr<base::MessageLoopProxy>& loop, | 70 const scoped_refptr<base::MessageLoopProxy>& loop, |
66 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb | 71 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb |
67 $if ARITY != 0 [[, ]] | 72 $if ARITY != 0 [[, ]] |
68 $for ARG , [[A$(ARG) a$(ARG)]] | 73 $for ARG , [[A$(ARG) a$(ARG)]] |
69 ) { | 74 ) { |
70 loop->PostTask(FROM_HERE, base::Bind(cb | 75 loop->PostTask(FROM_HERE, base::Bind(cb |
71 $if ARITY != 0 [[, ]] | 76 $if ARITY != 0 [[, ]] |
72 $for ARG , [[internal::TrampolineForward(a$(ARG))]])); | 77 $for ARG , [[internal::TrampolineForward(a$(ARG))]])); |
73 } | 78 } |
79 static void RunSync( | |
80 const scoped_refptr<base::MessageLoopProxy>& loop, | |
81 const base::Callback<void($for ARG , [[A$(ARG)]])>& cb | |
82 $if ARITY != 0 [[, ]] | |
83 $for ARG , [[A$(ARG) a$(ARG)]] | |
84 ) { | |
85 base::WaitableEvent waiter(false, false); | |
86 loop->PostTask( | |
87 FROM_HERE, | |
88 base::Bind( | |
89 &TrampolineSyncCaller, | |
90 base::Bind(cb | |
91 $if ARITY != 0 [[, ]] | |
92 $for ARG , [[internal::TrampolineForward(a$(ARG))]]), | |
93 &waiter)); | |
94 waiter.Wait(); | |
95 } | |
74 }; | 96 }; |
75 | 97 |
76 | 98 |
77 ]] $$ for ARITY | 99 ]] $$ for ARITY |
78 | 100 |
79 } // namespace internal | 101 } // namespace internal |
80 | 102 |
81 template<typename T> | 103 template<typename T> |
82 static base::Callback<T> BindToLoop( | 104 static base::Callback<T> BindToLoop( |
83 const scoped_refptr<base::MessageLoopProxy>& loop, | 105 const scoped_refptr<base::MessageLoopProxy>& loop, |
84 const base::Callback<T>& cb) { | 106 const base::Callback<T>& cb) { |
85 return base::Bind(&internal::TrampolineHelper<T>::Run, loop, cb); | 107 return base::Bind(&internal::TrampolineHelper<T>::Run, loop, cb); |
86 } | 108 } |
87 | 109 |
88 template<typename T> | 110 template<typename T> |
111 static base::Callback<T> BindToLoopSync( | |
112 const scoped_refptr<base::MessageLoopProxy>& loop, | |
113 const base::Callback<T>& cb) { | |
114 DCHECK(!loop->BelongsToCurrentThread()); | |
Ami GONE FROM CHROMIUM
2013/10/22 17:21:19
This wants to live inside RunSync, not here, right
sheu
2013/10/24 01:16:39
That's true. Done.
| |
115 return base::Bind(&internal::TrampolineHelper<T>::RunSync, loop, cb); | |
116 } | |
117 | |
118 template<typename T> | |
89 static base::Callback<T> BindToCurrentLoop( | 119 static base::Callback<T> BindToCurrentLoop( |
Ami GONE FROM CHROMIUM
2013/10/22 17:21:19
...which gives rise to wondering whether there's g
| |
90 const base::Callback<T>& cb) { | 120 const base::Callback<T>& cb) { |
91 return BindToLoop(base::MessageLoopProxy::current(), cb); | 121 return BindToLoop(base::MessageLoopProxy::current(), cb); |
92 } | 122 } |
93 | 123 |
94 } // namespace media | 124 } // namespace media |
95 | 125 |
96 #endif // MEDIA_BASE_BIND_TO_LOOP_H_ | 126 #endif // MEDIA_BASE_BIND_TO_LOOP_H_ |
OLD | NEW |