2018-11-07 12:24:35 -08:00
|
|
|
// Copyright 2013 The Flutter Authors. All rights reserved.
|
2017-03-20 13:41:41 -07:00
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
#include "flutter/fml/message_loop.h"
|
|
|
|
|
|
|
|
|
|
#include <utility>
|
|
|
|
|
|
2018-07-26 12:49:34 -07:00
|
|
|
#include "flutter/fml/memory/ref_counted.h"
|
|
|
|
|
#include "flutter/fml/memory/ref_ptr.h"
|
2017-03-20 13:41:41 -07:00
|
|
|
#include "flutter/fml/message_loop_impl.h"
|
|
|
|
|
#include "flutter/fml/task_runner.h"
|
|
|
|
|
#include "flutter/fml/thread_local.h"
|
|
|
|
|
|
|
|
|
|
namespace fml {
|
|
|
|
|
|
2019-04-19 17:33:46 -07:00
|
|
|
FML_THREAD_LOCAL ThreadLocalUniquePtr<MessageLoop> tls_message_loop;
|
2017-03-20 13:41:41 -07:00
|
|
|
|
|
|
|
|
MessageLoop& MessageLoop::GetCurrent() {
|
2019-04-19 17:33:46 -07:00
|
|
|
auto* loop = tls_message_loop.get();
|
2018-05-18 15:43:49 -07:00
|
|
|
FML_CHECK(loop != nullptr)
|
2017-03-20 13:41:41 -07:00
|
|
|
<< "MessageLoop::EnsureInitializedForCurrentThread was not called on "
|
|
|
|
|
"this thread prior to message loop use.";
|
|
|
|
|
return *loop;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MessageLoop::EnsureInitializedForCurrentThread() {
|
2019-04-19 17:33:46 -07:00
|
|
|
if (tls_message_loop.get() != nullptr) {
|
2017-03-20 13:41:41 -07:00
|
|
|
// Already initialized.
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-04-19 17:33:46 -07:00
|
|
|
tls_message_loop.reset(new MessageLoop());
|
2017-03-20 13:41:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool MessageLoop::IsInitializedForCurrentThread() {
|
2019-04-19 17:33:46 -07:00
|
|
|
return tls_message_loop.get() != nullptr;
|
2017-03-20 13:41:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MessageLoop::MessageLoop()
|
|
|
|
|
: loop_(MessageLoopImpl::Create()),
|
2018-07-26 12:49:34 -07:00
|
|
|
task_runner_(fml::MakeRefCounted<fml::TaskRunner>(loop_)) {
|
2018-05-18 15:43:49 -07:00
|
|
|
FML_CHECK(loop_);
|
|
|
|
|
FML_CHECK(task_runner_);
|
2017-03-20 13:41:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
MessageLoop::~MessageLoop() = default;
|
|
|
|
|
|
|
|
|
|
void MessageLoop::Run() {
|
|
|
|
|
loop_->DoRun();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MessageLoop::Terminate() {
|
|
|
|
|
loop_->DoTerminate();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 12:49:34 -07:00
|
|
|
fml::RefPtr<fml::TaskRunner> MessageLoop::GetTaskRunner() const {
|
2017-03-20 13:41:41 -07:00
|
|
|
return task_runner_;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-26 12:49:34 -07:00
|
|
|
fml::RefPtr<MessageLoopImpl> MessageLoop::GetLoopImpl() const {
|
2017-03-20 13:41:41 -07:00
|
|
|
return loop_;
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-22 12:20:02 -08:00
|
|
|
void MessageLoop::AddTaskObserver(intptr_t key, const fml::closure& callback) {
|
2018-04-13 13:48:15 -07:00
|
|
|
loop_->AddTaskObserver(key, callback);
|
2017-03-23 15:29:33 -07:00
|
|
|
}
|
|
|
|
|
|
2018-04-13 13:48:15 -07:00
|
|
|
void MessageLoop::RemoveTaskObserver(intptr_t key) {
|
|
|
|
|
loop_->RemoveTaskObserver(key);
|
2017-03-20 13:41:41 -07:00
|
|
|
}
|
|
|
|
|
|
2018-01-17 16:59:36 -08:00
|
|
|
void MessageLoop::RunExpiredTasksNow() {
|
|
|
|
|
loop_->RunExpiredTasksNow();
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-12 12:32:38 -07:00
|
|
|
TaskQueueId MessageLoop::GetCurrentTaskQueueId() {
|
|
|
|
|
auto* loop = tls_message_loop.get();
|
|
|
|
|
FML_CHECK(loop != nullptr)
|
|
|
|
|
<< "MessageLoop::EnsureInitializedForCurrentThread was not called on "
|
|
|
|
|
"this thread prior to message loop use.";
|
|
|
|
|
return loop->GetLoopImpl()->GetTaskQueueId();
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-20 13:41:41 -07:00
|
|
|
} // namespace fml
|