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