Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
message_loop.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "flutter/fml/message_loop.h"
6
7#include <memory>
8#include <utility>
9
10#include "flutter/fml/memory/ref_counted.h"
11#include "flutter/fml/memory/ref_ptr.h"
12#include "flutter/fml/message_loop_impl.h"
13#include "flutter/fml/task_runner.h"
14
15namespace fml {
16
17static thread_local std::unique_ptr<MessageLoop> tls_message_loop;
18
20 auto* loop = tls_message_loop.get();
21 FML_CHECK(loop != nullptr)
22 << "MessageLoop::EnsureInitializedForCurrentThread was not called on "
23 "this thread prior to message loop use.";
24 return *loop;
25}
26
28 if (tls_message_loop.get() != nullptr) {
29 // Already initialized.
30 return;
31 }
32 tls_message_loop.reset(new MessageLoop());
33}
34
36 return tls_message_loop.get() != nullptr;
37}
38
39MessageLoop::MessageLoop()
40 : loop_(MessageLoopImpl::Create()),
41 task_runner_(fml::MakeRefCounted<fml::TaskRunner>(loop_)) {
42 FML_CHECK(loop_);
43 FML_CHECK(task_runner_);
44}
45
47
49 loop_->DoRun();
50}
51
53 loop_->DoTerminate();
54}
55
57 return task_runner_;
58}
59
60fml::RefPtr<MessageLoopImpl> MessageLoop::GetLoopImpl() const {
61 return loop_;
62}
63
65 loop_->AddTaskObserver(key, callback);
66}
67
69 loop_->RemoveTaskObserver(key);
70}
71
73 loop_->RunExpiredTasksNow();
74}
75
77 auto* loop = tls_message_loop.get();
78 FML_CHECK(loop != nullptr)
79 << "MessageLoop::EnsureInitializedForCurrentThread was not called on "
80 "this thread prior to message loop use.";
81 return loop->GetLoopImpl()->GetTaskQueueId();
82}
83
84} // namespace fml
static sk_sp< Effect > Create()
void RemoveTaskObserver(intptr_t key)
static void EnsureInitializedForCurrentThread()
void AddTaskObserver(intptr_t key, const fml::closure &callback)
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
static bool IsInitializedForCurrentThread()
static TaskQueueId GetCurrentTaskQueueId()
void RunExpiredTasksNow()
FlKeyEvent uint64_t FlKeyResponderAsyncCallback callback
#define FML_CHECK(condition)
Definition logging.h:85
static thread_local std::unique_ptr< MessageLoop > tls_message_loop
RefPtr< T > MakeRefCounted(Args &&... args)
Definition ref_ptr.h:248
std::function< void()> closure
Definition closure.h:14