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

Side by Side Diff: webrtc/base/asyncinvoker.h

Issue 2019423006: Adding more detail to MessageQueue::Dispatch logging. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Fixing one more place where RTC_FROM_HERE wasn't used. Created 4 years, 6 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 | « webrtc/base/BUILD.gn ('k') | webrtc/base/asyncinvoker.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 /* 1 /*
2 * Copyright 2014 The WebRTC Project Authors. All rights reserved. 2 * Copyright 2014 The WebRTC Project Authors. All rights reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source 5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
(...skipping 27 matching lines...) Expand all
38 // 38 //
39 // The easiest way to ensure lifetimes are handled correctly is to create a 39 // The easiest way to ensure lifetimes are handled correctly is to create a
40 // class that owns the Thread and AsyncInvoker objects, and then call its 40 // class that owns the Thread and AsyncInvoker objects, and then call its
41 // methods asynchronously as needed. 41 // methods asynchronously as needed.
42 // 42 //
43 // Example: 43 // Example:
44 // class MyClass { 44 // class MyClass {
45 // public: 45 // public:
46 // void FireAsyncTaskWithResult(Thread* thread, int x) { 46 // void FireAsyncTaskWithResult(Thread* thread, int x) {
47 // // Specify a callback to get the result upon completion. 47 // // Specify a callback to get the result upon completion.
48 // invoker_.AsyncInvoke<int>( 48 // invoker_.AsyncInvoke<int>(RTC_FROM_HERE,
49 // thread, Bind(&MyClass::AsyncTaskWithResult, this, x), 49 // thread, Bind(&MyClass::AsyncTaskWithResult, this, x),
50 // &MyClass::OnTaskComplete, this); 50 // &MyClass::OnTaskComplete, this);
51 // } 51 // }
52 // void FireAnotherAsyncTask(Thread* thread) { 52 // void FireAnotherAsyncTask(Thread* thread) {
53 // // No callback specified means fire-and-forget. 53 // // No callback specified means fire-and-forget.
54 // invoker_.AsyncInvoke<void>( 54 // invoker_.AsyncInvoke<void>(RTC_FROM_HERE,
55 // thread, Bind(&MyClass::AnotherAsyncTask, this)); 55 // thread, Bind(&MyClass::AnotherAsyncTask, this));
56 // 56 //
57 // private: 57 // private:
58 // int AsyncTaskWithResult(int x) { 58 // int AsyncTaskWithResult(int x) {
59 // // Some long running process... 59 // // Some long running process...
60 // return x * x; 60 // return x * x;
61 // } 61 // }
62 // void AnotherAsyncTask() { 62 // void AnotherAsyncTask() {
63 // // Some other long running process... 63 // // Some other long running process...
64 // } 64 // }
65 // void OnTaskComplete(int result) { result_ = result; } 65 // void OnTaskComplete(int result) { result_ = result; }
66 // 66 //
67 // AsyncInvoker invoker_; 67 // AsyncInvoker invoker_;
68 // int result_; 68 // int result_;
69 // }; 69 // };
70 class AsyncInvoker : public MessageHandler { 70 class AsyncInvoker : public MessageHandler {
71 public: 71 public:
72 AsyncInvoker(); 72 AsyncInvoker();
73 ~AsyncInvoker() override; 73 ~AsyncInvoker() override;
74 74
75 // Call |functor| asynchronously on |thread|, with no callback upon 75 // Call |functor| asynchronously on |thread|, with no callback upon
76 // completion. Returns immediately. 76 // completion. Returns immediately.
77 template <class ReturnT, class FunctorT> 77 template <class ReturnT, class FunctorT>
78 void AsyncInvoke(Thread* thread, const FunctorT& functor, uint32_t id = 0) { 78 void AsyncInvoke(const Location& posted_from,
79 Thread* thread,
80 const FunctorT& functor,
81 uint32_t id = 0) {
79 scoped_refptr<AsyncClosure> closure( 82 scoped_refptr<AsyncClosure> closure(
80 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); 83 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor));
81 DoInvoke(thread, closure, id); 84 DoInvoke(posted_from, thread, closure, id);
82 } 85 }
83 86
84 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback 87 // Call |functor| asynchronously on |thread| with |delay_ms|, with no callback
85 // upon completion. Returns immediately. 88 // upon completion. Returns immediately.
86 template <class ReturnT, class FunctorT> 89 template <class ReturnT, class FunctorT>
87 void AsyncInvokeDelayed(Thread* thread, 90 void AsyncInvokeDelayed(const Location& posted_from,
91 Thread* thread,
88 const FunctorT& functor, 92 const FunctorT& functor,
89 uint32_t delay_ms, 93 uint32_t delay_ms,
90 uint32_t id = 0) { 94 uint32_t id = 0) {
91 scoped_refptr<AsyncClosure> closure( 95 scoped_refptr<AsyncClosure> closure(
92 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor)); 96 new RefCountedObject<FireAndForgetAsyncClosure<FunctorT> >(functor));
93 DoInvokeDelayed(thread, closure, delay_ms, id); 97 DoInvokeDelayed(posted_from, thread, closure, delay_ms, id);
94 } 98 }
95 99
96 // Call |functor| asynchronously on |thread|, calling |callback| when done. 100 // Call |functor| asynchronously on |thread|, calling |callback| when done.
101 // Uses a separate Location for |callback_posted_from| so that the functor
102 // invoke and the callback invoke can be differentiated.
97 template <class ReturnT, class FunctorT, class HostT> 103 template <class ReturnT, class FunctorT, class HostT>
98 void AsyncInvoke(Thread* thread, 104 void AsyncInvoke(const Location& posted_from,
105 const Location& callback_posted_from,
106 Thread* thread,
99 const FunctorT& functor, 107 const FunctorT& functor,
100 void (HostT::*callback)(ReturnT), 108 void (HostT::*callback)(ReturnT),
101 HostT* callback_host, 109 HostT* callback_host,
102 uint32_t id = 0) { 110 uint32_t id = 0) {
103 scoped_refptr<AsyncClosure> closure( 111 scoped_refptr<AsyncClosure> closure(
104 new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >( 112 new RefCountedObject<NotifyingAsyncClosure<ReturnT, FunctorT, HostT> >(
105 this, Thread::Current(), functor, callback, callback_host)); 113 this, callback_posted_from, Thread::Current(), functor, callback,
106 DoInvoke(thread, closure, id); 114 callback_host));
115 DoInvoke(posted_from, thread, closure, id);
107 } 116 }
108 117
109 // Call |functor| asynchronously on |thread|, calling |callback| when done. 118 // Call |functor| asynchronously on |thread|, calling |callback| when done.
119 // Uses a separate Location for |callback_posted_from| so that the functor
120 // invoke and the callback invoke can be differentiated.
110 // Overloaded for void return. 121 // Overloaded for void return.
111 template <class ReturnT, class FunctorT, class HostT> 122 template <class ReturnT, class FunctorT, class HostT>
112 void AsyncInvoke(Thread* thread, 123 void AsyncInvoke(const Location& posted_from,
124 const Location& callback_posted_from,
125 Thread* thread,
113 const FunctorT& functor, 126 const FunctorT& functor,
114 void (HostT::*callback)(), 127 void (HostT::*callback)(),
115 HostT* callback_host, 128 HostT* callback_host,
116 uint32_t id = 0) { 129 uint32_t id = 0) {
117 scoped_refptr<AsyncClosure> closure( 130 scoped_refptr<AsyncClosure> closure(
118 new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >( 131 new RefCountedObject<NotifyingAsyncClosure<void, FunctorT, HostT> >(
119 this, Thread::Current(), functor, callback, callback_host)); 132 this, callback_posted_from, Thread::Current(), functor, callback,
120 DoInvoke(thread, closure, id); 133 callback_host));
134 DoInvoke(posted_from, thread, closure, id);
121 } 135 }
122 136
123 // Synchronously execute on |thread| all outstanding calls we own 137 // Synchronously execute on |thread| all outstanding calls we own
124 // that are pending on |thread|, and wait for calls to complete 138 // that are pending on |thread|, and wait for calls to complete
125 // before returning. Optionally filter by message id. 139 // before returning. Optionally filter by message id.
126 // The destructor will not wait for outstanding calls, so if that 140 // The destructor will not wait for outstanding calls, so if that
127 // behavior is desired, call Flush() before destroying this object. 141 // behavior is desired, call Flush() before destroying this object.
128 void Flush(Thread* thread, uint32_t id = MQID_ANY); 142 void Flush(Thread* thread, uint32_t id = MQID_ANY);
129 143
130 // Signaled when this object is destructed. 144 // Signaled when this object is destructed.
131 sigslot::signal0<> SignalInvokerDestroyed; 145 sigslot::signal0<> SignalInvokerDestroyed;
132 146
133 private: 147 private:
134 void OnMessage(Message* msg) override; 148 void OnMessage(Message* msg) override;
135 void DoInvoke(Thread* thread, 149 void DoInvoke(const Location& posted_from,
150 Thread* thread,
136 const scoped_refptr<AsyncClosure>& closure, 151 const scoped_refptr<AsyncClosure>& closure,
137 uint32_t id); 152 uint32_t id);
138 void DoInvokeDelayed(Thread* thread, 153 void DoInvokeDelayed(const Location& posted_from,
154 Thread* thread,
139 const scoped_refptr<AsyncClosure>& closure, 155 const scoped_refptr<AsyncClosure>& closure,
140 uint32_t delay_ms, 156 uint32_t delay_ms,
141 uint32_t id); 157 uint32_t id);
142 bool destroying_; 158 bool destroying_;
143 159
144 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); 160 RTC_DISALLOW_COPY_AND_ASSIGN(AsyncInvoker);
145 }; 161 };
146 162
147 // Similar to AsyncInvoker, but guards against the Thread being destroyed while 163 // Similar to AsyncInvoker, but guards against the Thread being destroyed while
148 // there are outstanding dangling pointers to it. It will connect to the current 164 // there are outstanding dangling pointers to it. It will connect to the current
149 // thread in the constructor, and will get notified when that thread is 165 // thread in the constructor, and will get notified when that thread is
150 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from 166 // destroyed. After GuardedAsyncInvoker is constructed, it can be used from
151 // other threads to post functors to the thread it was constructed on. If that 167 // other threads to post functors to the thread it was constructed on. If that
152 // thread dies, any further calls to AsyncInvoke() will be safely ignored. 168 // thread dies, any further calls to AsyncInvoke() will be safely ignored.
153 class GuardedAsyncInvoker : public sigslot::has_slots<> { 169 class GuardedAsyncInvoker : public sigslot::has_slots<> {
154 public: 170 public:
155 GuardedAsyncInvoker(); 171 GuardedAsyncInvoker();
156 ~GuardedAsyncInvoker() override; 172 ~GuardedAsyncInvoker() override;
157 173
158 // Synchronously execute all outstanding calls we own, and wait for calls to 174 // Synchronously execute all outstanding calls we own, and wait for calls to
159 // complete before returning. Optionally filter by message id. The destructor 175 // complete before returning. Optionally filter by message id. The destructor
160 // will not wait for outstanding calls, so if that behavior is desired, call 176 // will not wait for outstanding calls, so if that behavior is desired, call
161 // Flush() first. Returns false if the thread has died. 177 // Flush() first. Returns false if the thread has died.
162 bool Flush(uint32_t id = MQID_ANY); 178 bool Flush(uint32_t id = MQID_ANY);
163 179
164 // Call |functor| asynchronously with no callback upon completion. Returns 180 // Call |functor| asynchronously with no callback upon completion. Returns
165 // immediately. Returns false if the thread has died. 181 // immediately. Returns false if the thread has died.
166 template <class ReturnT, class FunctorT> 182 template <class ReturnT, class FunctorT>
167 bool AsyncInvoke(const FunctorT& functor, uint32_t id = 0) { 183 bool AsyncInvoke(const Location& posted_from,
184 const FunctorT& functor,
185 uint32_t id = 0) {
168 rtc::CritScope cs(&crit_); 186 rtc::CritScope cs(&crit_);
169 if (thread_ == nullptr) 187 if (thread_ == nullptr)
170 return false; 188 return false;
171 invoker_.AsyncInvoke<ReturnT, FunctorT>(thread_, functor, id); 189 invoker_.AsyncInvoke<ReturnT, FunctorT>(posted_from, thread_, functor, id);
172 return true; 190 return true;
173 } 191 }
174 192
175 // Call |functor| asynchronously with |delay_ms|, with no callback upon 193 // Call |functor| asynchronously with |delay_ms|, with no callback upon
176 // completion. Returns immediately. Returns false if the thread has died. 194 // completion. Returns immediately. Returns false if the thread has died.
177 template <class ReturnT, class FunctorT> 195 template <class ReturnT, class FunctorT>
178 bool AsyncInvokeDelayed(const FunctorT& functor, 196 bool AsyncInvokeDelayed(const Location& posted_from,
197 const FunctorT& functor,
179 uint32_t delay_ms, 198 uint32_t delay_ms,
180 uint32_t id = 0) { 199 uint32_t id = 0) {
181 rtc::CritScope cs(&crit_); 200 rtc::CritScope cs(&crit_);
182 if (thread_ == nullptr) 201 if (thread_ == nullptr)
183 return false; 202 return false;
184 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(thread_, functor, delay_ms, 203 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(posted_from, thread_,
185 id); 204 functor, delay_ms, id);
186 return true; 205 return true;
187 } 206 }
188 207
189 // Call |functor| asynchronously, calling |callback| when done. Returns false 208 // Call |functor| asynchronously, calling |callback| when done. Returns false
190 // if the thread has died. 209 // if the thread has died.
191 template <class ReturnT, class FunctorT, class HostT> 210 template <class ReturnT, class FunctorT, class HostT>
192 bool AsyncInvoke(const FunctorT& functor, 211 bool AsyncInvoke(const Location& posted_from,
212 const Location& callback_posted_from,
213 const FunctorT& functor,
193 void (HostT::*callback)(ReturnT), 214 void (HostT::*callback)(ReturnT),
194 HostT* callback_host, 215 HostT* callback_host,
195 uint32_t id = 0) { 216 uint32_t id = 0) {
196 rtc::CritScope cs(&crit_); 217 rtc::CritScope cs(&crit_);
197 if (thread_ == nullptr) 218 if (thread_ == nullptr)
198 return false; 219 return false;
199 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback, 220 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
200 callback_host, id); 221 posted_from, callback_posted_from, thread_, functor, callback,
222 callback_host, id);
201 return true; 223 return true;
202 } 224 }
203 225
204 // Call |functor| asynchronously calling |callback| when done. Overloaded for 226 // Call |functor| asynchronously calling |callback| when done. Overloaded for
205 // void return. Returns false if the thread has died. 227 // void return. Returns false if the thread has died.
206 template <class ReturnT, class FunctorT, class HostT> 228 template <class ReturnT, class FunctorT, class HostT>
207 bool AsyncInvoke(const FunctorT& functor, 229 bool AsyncInvoke(const Location& posted_from,
230 const Location& callback_posted_from,
231 const FunctorT& functor,
208 void (HostT::*callback)(), 232 void (HostT::*callback)(),
209 HostT* callback_host, 233 HostT* callback_host,
210 uint32_t id = 0) { 234 uint32_t id = 0) {
211 rtc::CritScope cs(&crit_); 235 rtc::CritScope cs(&crit_);
212 if (thread_ == nullptr) 236 if (thread_ == nullptr)
213 return false; 237 return false;
214 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback, 238 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(
215 callback_host, id); 239 posted_from, callback_posted_from, thread_, functor, callback,
240 callback_host, id);
216 return true; 241 return true;
217 } 242 }
218 243
219 private: 244 private:
220 // Callback when |thread_| is destroyed. 245 // Callback when |thread_| is destroyed.
221 void ThreadDestroyed(); 246 void ThreadDestroyed();
222 247
223 CriticalSection crit_; 248 CriticalSection crit_;
224 Thread* thread_ GUARDED_BY(crit_); 249 Thread* thread_ GUARDED_BY(crit_);
225 AsyncInvoker invoker_ GUARDED_BY(crit_); 250 AsyncInvoker invoker_ GUARDED_BY(crit_);
226 }; 251 };
227 252
228 } // namespace rtc 253 } // namespace rtc
229 254
230 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ 255 #endif // WEBRTC_BASE_ASYNCINVOKER_H_
OLDNEW
« no previous file with comments | « webrtc/base/BUILD.gn ('k') | webrtc/base/asyncinvoker.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698