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

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

Issue 2694723004: Making AsyncInvoker destructor thread-safe. (Closed)
Patch Set: Switch to MessageQueueManager::Clear, for when multiple threads are blocked. Created 3 years, 10 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
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
11 #ifndef WEBRTC_BASE_ASYNCINVOKER_INL_H_ 11 #ifndef WEBRTC_BASE_ASYNCINVOKER_INL_H_
12 #define WEBRTC_BASE_ASYNCINVOKER_INL_H_ 12 #define WEBRTC_BASE_ASYNCINVOKER_INL_H_
13 13
14 #include "webrtc/base/atomicops.h"
14 #include "webrtc/base/bind.h" 15 #include "webrtc/base/bind.h"
15 #include "webrtc/base/callback.h" 16 #include "webrtc/base/callback.h"
16 #include "webrtc/base/criticalsection.h" 17 #include "webrtc/base/criticalsection.h"
17 #include "webrtc/base/messagehandler.h" 18 #include "webrtc/base/messagehandler.h"
18 #include "webrtc/base/refcount.h" 19 #include "webrtc/base/refcount.h"
19 #include "webrtc/base/scoped_ref_ptr.h" 20 #include "webrtc/base/scoped_ref_ptr.h"
20 #include "webrtc/base/sigslot.h" 21 #include "webrtc/base/sigslot.h"
21 #include "webrtc/base/thread.h" 22 #include "webrtc/base/thread.h"
22 23
23 namespace rtc { 24 namespace rtc {
24 25
25 class AsyncInvoker; 26 class AsyncInvoker;
26 27
27 // Helper class for AsyncInvoker. Runs a task and triggers a callback 28 // Helper class for AsyncInvoker. Runs a task and triggers a callback
28 // on the calling thread if necessary. Instances are ref-counted so their 29 // on the calling thread if necessary. Instances are ref-counted so their
29 // lifetime can be independent of AsyncInvoker. 30 // lifetime can be independent of AsyncInvoker.
30 class AsyncClosure : public RefCountInterface { 31 class AsyncClosure : public RefCountInterface {
31 public: 32 public:
33 explicit AsyncClosure(AsyncInvoker* invoker) : invoker_(invoker) {}
32 // Runs the asynchronous task, and triggers a callback to the calling 34 // Runs the asynchronous task, and triggers a callback to the calling
33 // thread if needed. Should be called from the target thread. 35 // thread if needed. Should be called from the target thread.
34 virtual void Execute() = 0; 36 virtual void Execute() = 0;
35 protected: 37 protected:
36 ~AsyncClosure() override {} 38 ~AsyncClosure() override;
39
40 AsyncInvoker* invoker_;
37 }; 41 };
38 42
39 // Simple closure that doesn't trigger a callback for the calling thread. 43 // Simple closure that doesn't trigger a callback for the calling thread.
40 template <class FunctorT> 44 template <class FunctorT>
41 class FireAndForgetAsyncClosure : public AsyncClosure { 45 class FireAndForgetAsyncClosure : public AsyncClosure {
42 public: 46 public:
43 explicit FireAndForgetAsyncClosure(const FunctorT& functor) 47 explicit FireAndForgetAsyncClosure(AsyncInvoker* invoker,
44 : functor_(functor) {} 48 const FunctorT& functor)
49 : AsyncClosure(invoker), functor_(functor) {}
45 virtual void Execute() { 50 virtual void Execute() {
46 functor_(); 51 functor_();
47 } 52 }
48 private: 53 private:
49 FunctorT functor_; 54 FunctorT functor_;
50 }; 55 };
51 56
52 // Base class for closures that may trigger a callback for the calling thread. 57 // Base class for closures that may trigger a callback for the calling thread.
53 // Listens for the "destroyed" signals from the calling thread and the invoker, 58 // Listens for the "destroyed" signals from the calling thread and the invoker,
54 // and cancels the callback to the calling thread if either is destroyed. 59 // and cancels the callback to the calling thread if either is destroyed.
55 class NotifyingAsyncClosureBase : public AsyncClosure, 60 class NotifyingAsyncClosureBase : public AsyncClosure,
56 public sigslot::has_slots<> { 61 public sigslot::has_slots<> {
57 public: 62 public:
58 ~NotifyingAsyncClosureBase() override; 63 ~NotifyingAsyncClosureBase() override;
59 64
60 protected: 65 protected:
61 NotifyingAsyncClosureBase(AsyncInvoker* invoker, 66 NotifyingAsyncClosureBase(AsyncInvoker* invoker,
62 const Location& callback_posted_from, 67 const Location& callback_posted_from,
63 Thread* calling_thread); 68 Thread* calling_thread);
64 void TriggerCallback(); 69 void TriggerCallback();
65 void SetCallback(const Callback0<void>& callback) { 70 void SetCallback(const Callback0<void>& callback) {
66 CritScope cs(&crit_); 71 CritScope cs(&crit_);
67 callback_ = callback; 72 callback_ = callback;
68 } 73 }
69 bool CallbackCanceled() const { return calling_thread_ == NULL; } 74 bool CallbackCanceled() const { return calling_thread_ == NULL; }
70 75
71 private: 76 private:
72 AsyncInvoker* invoker_;
73 Location callback_posted_from_; 77 Location callback_posted_from_;
74 Callback0<void> callback_; 78 Callback0<void> callback_;
75 CriticalSection crit_; 79 CriticalSection crit_;
76 Thread* calling_thread_; 80 Thread* calling_thread_;
77 81
78 void CancelCallback(); 82 void CancelCallback();
79 }; 83 };
80 84
81 // Closures that have a non-void return value and require a callback. 85 // Closures that have a non-void return value and require a callback.
82 template <class ReturnT, class FunctorT, class HostT> 86 template <class ReturnT, class FunctorT, class HostT>
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
130 TriggerCallback(); 134 TriggerCallback();
131 } 135 }
132 136
133 private: 137 private:
134 FunctorT functor_; 138 FunctorT functor_;
135 }; 139 };
136 140
137 } // namespace rtc 141 } // namespace rtc
138 142
139 #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_ 143 #endif // WEBRTC_BASE_ASYNCINVOKER_INL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698