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 |
11 #include "webrtc/base/asyncinvoker.h" | 11 #include "webrtc/base/asyncinvoker.h" |
12 | 12 |
13 #include "webrtc/base/checks.h" | 13 #include "webrtc/base/checks.h" |
14 #include "webrtc/base/logging.h" | 14 #include "webrtc/base/logging.h" |
15 | 15 |
16 namespace rtc { | 16 namespace rtc { |
17 | 17 |
18 AsyncInvoker::AsyncInvoker() : destroying_(false) {} | 18 AsyncInvoker::AsyncInvoker() : destroying_(false) {} |
19 | 19 |
20 AsyncInvoker::~AsyncInvoker() { | 20 AsyncInvoker::~AsyncInvoker() { |
21 destroying_ = true; | 21 destroying_ = true; |
22 SignalInvokerDestroyed(); | 22 SignalInvokerDestroyed(); |
23 // Messages for this need to be cleared *before* our destructor is complete. | 23 // Messages for this need to be cleared *before* our destructor is complete. |
24 MessageQueueManager::Clear(this); | 24 MessageQueueManager::Clear(this); |
25 } | 25 } |
26 | 26 |
27 void AsyncInvoker::OnMessage(Message* msg) { | 27 void AsyncInvoker::OnMessage(Message* msg) { |
28 // Get the AsyncClosure shared ptr from this message's data. | 28 // Get the AsyncClosure shared ptr from this message's data. |
29 ScopedRefMessageData<AsyncClosure>* data = | 29 ScopedMessageData<AsyncClosure>* data = |
30 static_cast<ScopedRefMessageData<AsyncClosure>*>(msg->pdata); | 30 static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata); |
31 scoped_refptr<AsyncClosure> closure = data->data(); | |
32 delete msg->pdata; | |
33 msg->pdata = NULL; | |
34 | |
35 // Execute the closure and trigger the return message if needed. | 31 // Execute the closure and trigger the return message if needed. |
36 closure->Execute(); | 32 data->inner_data().Execute(); |
| 33 delete data; |
37 } | 34 } |
38 | 35 |
39 void AsyncInvoker::Flush(Thread* thread, uint32_t id /*= MQID_ANY*/) { | 36 void AsyncInvoker::Flush(Thread* thread, uint32_t id /*= MQID_ANY*/) { |
40 if (destroying_) return; | 37 if (destroying_) return; |
41 | 38 |
42 // Run this on |thread| to reduce the number of context switches. | 39 // Run this on |thread| to reduce the number of context switches. |
43 if (Thread::Current() != thread) { | 40 if (Thread::Current() != thread) { |
44 thread->Invoke<void>(RTC_FROM_HERE, | 41 thread->Invoke<void>(RTC_FROM_HERE, |
45 Bind(&AsyncInvoker::Flush, this, thread, id)); | 42 Bind(&AsyncInvoker::Flush, this, thread, id)); |
46 return; | 43 return; |
47 } | 44 } |
48 | 45 |
49 MessageList removed; | 46 MessageList removed; |
50 thread->Clear(this, id, &removed); | 47 thread->Clear(this, id, &removed); |
51 for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) { | 48 for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) { |
52 // This message was pending on this thread, so run it now. | 49 // This message was pending on this thread, so run it now. |
53 thread->Send(it->posted_from, it->phandler, it->message_id, it->pdata); | 50 thread->Send(it->posted_from, it->phandler, it->message_id, it->pdata); |
54 } | 51 } |
55 } | 52 } |
56 | 53 |
57 void AsyncInvoker::DoInvoke(const Location& posted_from, | 54 void AsyncInvoker::DoInvoke(const Location& posted_from, |
58 Thread* thread, | 55 Thread* thread, |
59 const scoped_refptr<AsyncClosure>& closure, | 56 std::unique_ptr<AsyncClosure> closure, |
60 uint32_t id) { | 57 uint32_t id) { |
61 if (destroying_) { | 58 if (destroying_) { |
62 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; | 59 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
63 return; | 60 return; |
64 } | 61 } |
65 thread->Post(posted_from, this, id, | 62 thread->Post(posted_from, this, id, |
66 new ScopedRefMessageData<AsyncClosure>(closure)); | 63 new ScopedMessageData<AsyncClosure>(std::move(closure))); |
67 } | 64 } |
68 | 65 |
69 void AsyncInvoker::DoInvokeDelayed(const Location& posted_from, | 66 void AsyncInvoker::DoInvokeDelayed(const Location& posted_from, |
70 Thread* thread, | 67 Thread* thread, |
71 const scoped_refptr<AsyncClosure>& closure, | 68 std::unique_ptr<AsyncClosure> closure, |
72 uint32_t delay_ms, | 69 uint32_t delay_ms, |
73 uint32_t id) { | 70 uint32_t id) { |
74 if (destroying_) { | 71 if (destroying_) { |
75 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; | 72 LOG(LS_WARNING) << "Tried to invoke while destroying the invoker."; |
76 return; | 73 return; |
77 } | 74 } |
78 thread->PostDelayed(posted_from, delay_ms, this, id, | 75 thread->PostDelayed(posted_from, delay_ms, this, id, |
79 new ScopedRefMessageData<AsyncClosure>(closure)); | 76 new ScopedMessageData<AsyncClosure>(std::move(closure))); |
80 } | 77 } |
81 | 78 |
82 GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) { | 79 GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) { |
83 thread_->SignalQueueDestroyed.connect(this, | 80 thread_->SignalQueueDestroyed.connect(this, |
84 &GuardedAsyncInvoker::ThreadDestroyed); | 81 &GuardedAsyncInvoker::ThreadDestroyed); |
85 } | 82 } |
86 | 83 |
87 GuardedAsyncInvoker::~GuardedAsyncInvoker() { | 84 GuardedAsyncInvoker::~GuardedAsyncInvoker() { |
88 } | 85 } |
89 | 86 |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 void NotifyingAsyncClosureBase::CancelCallback() { | 127 void NotifyingAsyncClosureBase::CancelCallback() { |
131 // If the callback is triggering when this is called, block the | 128 // If the callback is triggering when this is called, block the |
132 // destructor of the dying object here by waiting until the callback | 129 // destructor of the dying object here by waiting until the callback |
133 // is done triggering. | 130 // is done triggering. |
134 CritScope cs(&crit_); | 131 CritScope cs(&crit_); |
135 // calling_thread_ == NULL means do not trigger the callback. | 132 // calling_thread_ == NULL means do not trigger the callback. |
136 calling_thread_ = NULL; | 133 calling_thread_ = NULL; |
137 } | 134 } |
138 | 135 |
139 } // namespace rtc | 136 } // namespace rtc |
OLD | NEW |