forked from flutter/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_loop.cc
78 lines (60 loc) · 1.92 KB
/
message_loop.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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>
#include "flutter/fml/memory/ref_counted.h"
#include "flutter/fml/memory/ref_ptr.h"
#include "flutter/fml/message_loop_impl.h"
#include "flutter/fml/task_runner.h"
#include "flutter/fml/thread_local.h"
namespace fml {
FML_THREAD_LOCAL ThreadLocal tls_message_loop([](intptr_t value) {
delete reinterpret_cast<MessageLoop*>(value);
});
MessageLoop& MessageLoop::GetCurrent() {
auto loop = reinterpret_cast<MessageLoop*>(tls_message_loop.Get());
FML_CHECK(loop != nullptr)
<< "MessageLoop::EnsureInitializedForCurrentThread was not called on "
"this thread prior to message loop use.";
return *loop;
}
void MessageLoop::EnsureInitializedForCurrentThread() {
if (tls_message_loop.Get() != 0) {
// Already initialized.
return;
}
tls_message_loop.Set(reinterpret_cast<intptr_t>(new MessageLoop()));
}
bool MessageLoop::IsInitializedForCurrentThread() {
return tls_message_loop.Get() != 0;
}
MessageLoop::MessageLoop()
: loop_(MessageLoopImpl::Create()),
task_runner_(fml::MakeRefCounted<fml::TaskRunner>(loop_)) {
FML_CHECK(loop_);
FML_CHECK(task_runner_);
}
MessageLoop::~MessageLoop() = default;
void MessageLoop::Run() {
loop_->DoRun();
}
void MessageLoop::Terminate() {
loop_->DoTerminate();
}
fml::RefPtr<fml::TaskRunner> MessageLoop::GetTaskRunner() const {
return task_runner_;
}
fml::RefPtr<MessageLoopImpl> MessageLoop::GetLoopImpl() const {
return loop_;
}
void MessageLoop::AddTaskObserver(intptr_t key, fml::closure callback) {
loop_->AddTaskObserver(key, callback);
}
void MessageLoop::RemoveTaskObserver(intptr_t key) {
loop_->RemoveTaskObserver(key);
}
void MessageLoop::RunExpiredTasksNow() {
loop_->RunExpiredTasksNow();
}
} // namespace fml