OLD | NEW |
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 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 uint32 id); | 137 uint32 id); |
138 void DoInvokeDelayed(Thread* thread, | 138 void DoInvokeDelayed(Thread* thread, |
139 const scoped_refptr<AsyncClosure>& closure, | 139 const scoped_refptr<AsyncClosure>& closure, |
140 uint32 delay_ms, | 140 uint32 delay_ms, |
141 uint32 id); | 141 uint32 id); |
142 bool destroying_; | 142 bool destroying_; |
143 | 143 |
144 DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); | 144 DISALLOW_COPY_AND_ASSIGN(AsyncInvoker); |
145 }; | 145 }; |
146 | 146 |
| 147 // 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 |
| 149 // thread in the constructor, and will get notified when that thread is |
| 150 // 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 |
| 152 // thread dies, any further calls to AsyncInvoke() will be safely ignored. |
| 153 class GuardedAsyncInvoker : public sigslot::has_slots<> { |
| 154 public: |
| 155 GuardedAsyncInvoker(); |
| 156 ~GuardedAsyncInvoker() override; |
| 157 |
| 158 // Synchronously execute all outstanding calls we own, and wait for calls to |
| 159 // complete before returning. Optionally filter by message id. The destructor |
| 160 // will not wait for outstanding calls, so if that behavior is desired, call |
| 161 // Flush() first. Returns false if the thread has died. |
| 162 bool Flush(uint32 id = MQID_ANY); |
| 163 |
| 164 // Call |functor| asynchronously with no callback upon completion. Returns |
| 165 // immediately. Returns false if the thread has died. |
| 166 template <class ReturnT, class FunctorT> |
| 167 bool AsyncInvoke(const FunctorT& functor, uint32 id = 0) { |
| 168 rtc::CritScope cs(&crit_); |
| 169 if (thread_ == nullptr) |
| 170 return false; |
| 171 invoker_.AsyncInvoke<ReturnT, FunctorT>(thread_, functor, id); |
| 172 return true; |
| 173 } |
| 174 |
| 175 // Call |functor| asynchronously with |delay_ms|, with no callback upon |
| 176 // completion. Returns immediately. Returns false if the thread has died. |
| 177 template <class ReturnT, class FunctorT> |
| 178 bool AsyncInvokeDelayed(const FunctorT& functor, |
| 179 uint32 delay_ms, |
| 180 uint32 id = 0) { |
| 181 rtc::CritScope cs(&crit_); |
| 182 if (thread_ == nullptr) |
| 183 return false; |
| 184 invoker_.AsyncInvokeDelayed<ReturnT, FunctorT>(thread_, functor, delay_ms, |
| 185 id); |
| 186 return true; |
| 187 } |
| 188 |
| 189 // Call |functor| asynchronously, calling |callback| when done. Returns false |
| 190 // if the thread has died. |
| 191 template <class ReturnT, class FunctorT, class HostT> |
| 192 bool AsyncInvoke(const FunctorT& functor, |
| 193 void (HostT::*callback)(ReturnT), |
| 194 HostT* callback_host, |
| 195 uint32 id = 0) { |
| 196 rtc::CritScope cs(&crit_); |
| 197 if (thread_ == nullptr) |
| 198 return false; |
| 199 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback, |
| 200 callback_host, id); |
| 201 return true; |
| 202 } |
| 203 |
| 204 // Call |functor| asynchronously calling |callback| when done. Overloaded for |
| 205 // void return. Returns false if the thread has died. |
| 206 template <class ReturnT, class FunctorT, class HostT> |
| 207 bool AsyncInvoke(const FunctorT& functor, |
| 208 void (HostT::*callback)(), |
| 209 HostT* callback_host, |
| 210 uint32 id = 0) { |
| 211 rtc::CritScope cs(&crit_); |
| 212 if (thread_ == nullptr) |
| 213 return false; |
| 214 invoker_.AsyncInvoke<ReturnT, FunctorT, HostT>(thread_, functor, callback, |
| 215 callback_host, id); |
| 216 return true; |
| 217 } |
| 218 |
| 219 private: |
| 220 // Callback when |thread_| is destroyed. |
| 221 void ThreadDestroyed(); |
| 222 |
| 223 CriticalSection crit_; |
| 224 Thread* thread_ GUARDED_BY(crit_); |
| 225 AsyncInvoker invoker_ GUARDED_BY(crit_); |
| 226 }; |
| 227 |
147 } // namespace rtc | 228 } // namespace rtc |
148 | 229 |
149 | |
150 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ | 230 #endif // WEBRTC_BASE_ASYNCINVOKER_H_ |
OLD | NEW |