2014-05-13 18:00:26 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright 2014 The WebRTC Project Authors. All rights reserved.
|
|
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by a BSD-style license
|
|
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
|
|
|
|
* in the file PATENTS. All contributing project authors may
|
|
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "webrtc/base/asyncinvoker.h"
|
|
|
|
|
|
2015-08-20 16:42:42 +02:00
|
|
|
#include "webrtc/base/checks.h"
|
2015-04-02 12:30:51 +02:00
|
|
|
#include "webrtc/base/logging.h"
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
namespace rtc {
|
|
|
|
|
|
|
|
|
|
AsyncInvoker::AsyncInvoker() : destroying_(false) {}
|
|
|
|
|
|
|
|
|
|
AsyncInvoker::~AsyncInvoker() {
|
|
|
|
|
destroying_ = true;
|
|
|
|
|
SignalInvokerDestroyed();
|
|
|
|
|
// Messages for this need to be cleared *before* our destructor is complete.
|
|
|
|
|
MessageQueueManager::Clear(this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void AsyncInvoker::OnMessage(Message* msg) {
|
|
|
|
|
// Get the AsyncClosure shared ptr from this message's data.
|
2017-02-17 18:06:26 -08:00
|
|
|
ScopedMessageData<AsyncClosure>* data =
|
|
|
|
|
static_cast<ScopedMessageData<AsyncClosure>*>(msg->pdata);
|
2014-05-13 18:00:26 +00:00
|
|
|
// Execute the closure and trigger the return message if needed.
|
2017-02-17 18:06:26 -08:00
|
|
|
data->inner_data().Execute();
|
|
|
|
|
delete data;
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
void AsyncInvoker::Flush(Thread* thread, uint32_t id /*= MQID_ANY*/) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (destroying_) return;
|
|
|
|
|
|
|
|
|
|
// Run this on |thread| to reduce the number of context switches.
|
|
|
|
|
if (Thread::Current() != thread) {
|
2016-06-10 14:17:27 -07:00
|
|
|
thread->Invoke<void>(RTC_FROM_HERE,
|
|
|
|
|
Bind(&AsyncInvoker::Flush, this, thread, id));
|
2014-05-13 18:00:26 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MessageList removed;
|
|
|
|
|
thread->Clear(this, id, &removed);
|
|
|
|
|
for (MessageList::iterator it = removed.begin(); it != removed.end(); ++it) {
|
|
|
|
|
// This message was pending on this thread, so run it now.
|
2016-06-10 14:17:27 -07:00
|
|
|
thread->Send(it->posted_from, it->phandler, it->message_id, it->pdata);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-10 14:17:27 -07:00
|
|
|
void AsyncInvoker::DoInvoke(const Location& posted_from,
|
|
|
|
|
Thread* thread,
|
2017-02-17 18:06:26 -08:00
|
|
|
std::unique_ptr<AsyncClosure> closure,
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint32_t id) {
|
2014-05-13 18:00:26 +00:00
|
|
|
if (destroying_) {
|
|
|
|
|
LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-10 14:17:27 -07:00
|
|
|
thread->Post(posted_from, this, id,
|
2017-02-17 18:06:26 -08:00
|
|
|
new ScopedMessageData<AsyncClosure>(std::move(closure)));
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
|
2016-06-10 14:17:27 -07:00
|
|
|
void AsyncInvoker::DoInvokeDelayed(const Location& posted_from,
|
|
|
|
|
Thread* thread,
|
2017-02-17 18:06:26 -08:00
|
|
|
std::unique_ptr<AsyncClosure> closure,
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
uint32_t delay_ms,
|
|
|
|
|
uint32_t id) {
|
2015-06-18 14:44:41 -07:00
|
|
|
if (destroying_) {
|
|
|
|
|
LOG(LS_WARNING) << "Tried to invoke while destroying the invoker.";
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-10 14:17:27 -07:00
|
|
|
thread->PostDelayed(posted_from, delay_ms, this, id,
|
2017-02-17 18:06:26 -08:00
|
|
|
new ScopedMessageData<AsyncClosure>(std::move(closure)));
|
2015-06-18 14:44:41 -07:00
|
|
|
}
|
|
|
|
|
|
2015-08-20 16:42:42 +02:00
|
|
|
GuardedAsyncInvoker::GuardedAsyncInvoker() : thread_(Thread::Current()) {
|
|
|
|
|
thread_->SignalQueueDestroyed.connect(this,
|
|
|
|
|
&GuardedAsyncInvoker::ThreadDestroyed);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
GuardedAsyncInvoker::~GuardedAsyncInvoker() {
|
|
|
|
|
}
|
|
|
|
|
|
Use suffixed {uint,int}{8,16,32,64}_t types.
Removes the use of uint8, etc. in favor of uint8_t.
BUG=webrtc:5024
R=henrik.lundin@webrtc.org, henrikg@webrtc.org, perkj@webrtc.org, solenberg@webrtc.org, stefan@webrtc.org, tina.legrand@webrtc.org
Review URL: https://codereview.webrtc.org/1362503003 .
Cr-Commit-Position: refs/heads/master@{#10196}
2015-10-07 12:23:21 +02:00
|
|
|
bool GuardedAsyncInvoker::Flush(uint32_t id) {
|
2015-08-20 16:42:42 +02:00
|
|
|
rtc::CritScope cs(&crit_);
|
|
|
|
|
if (thread_ == nullptr)
|
|
|
|
|
return false;
|
|
|
|
|
invoker_.Flush(thread_, id);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void GuardedAsyncInvoker::ThreadDestroyed() {
|
|
|
|
|
rtc::CritScope cs(&crit_);
|
|
|
|
|
// We should never get more than one notification about the thread dying.
|
2015-09-17 00:24:34 -07:00
|
|
|
RTC_DCHECK(thread_ != nullptr);
|
2015-08-20 16:42:42 +02:00
|
|
|
thread_ = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-10 14:17:27 -07:00
|
|
|
NotifyingAsyncClosureBase::NotifyingAsyncClosureBase(
|
|
|
|
|
AsyncInvoker* invoker,
|
|
|
|
|
const Location& callback_posted_from,
|
|
|
|
|
Thread* calling_thread)
|
|
|
|
|
: invoker_(invoker),
|
|
|
|
|
callback_posted_from_(callback_posted_from),
|
|
|
|
|
calling_thread_(calling_thread) {
|
2014-05-13 18:00:26 +00:00
|
|
|
calling_thread->SignalQueueDestroyed.connect(
|
|
|
|
|
this, &NotifyingAsyncClosureBase::CancelCallback);
|
|
|
|
|
invoker->SignalInvokerDestroyed.connect(
|
|
|
|
|
this, &NotifyingAsyncClosureBase::CancelCallback);
|
|
|
|
|
}
|
|
|
|
|
|
2015-03-09 22:21:53 +00:00
|
|
|
NotifyingAsyncClosureBase::~NotifyingAsyncClosureBase() {
|
|
|
|
|
disconnect_all();
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-13 18:00:26 +00:00
|
|
|
void NotifyingAsyncClosureBase::TriggerCallback() {
|
|
|
|
|
CritScope cs(&crit_);
|
|
|
|
|
if (!CallbackCanceled() && !callback_.empty()) {
|
2016-06-10 14:17:27 -07:00
|
|
|
invoker_->AsyncInvoke<void>(callback_posted_from_, calling_thread_,
|
|
|
|
|
callback_);
|
2014-05-13 18:00:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void NotifyingAsyncClosureBase::CancelCallback() {
|
|
|
|
|
// If the callback is triggering when this is called, block the
|
|
|
|
|
// destructor of the dying object here by waiting until the callback
|
|
|
|
|
// is done triggering.
|
|
|
|
|
CritScope cs(&crit_);
|
|
|
|
|
// calling_thread_ == NULL means do not trigger the callback.
|
|
|
|
|
calling_thread_ = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace rtc
|