Flutter Engine
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 <utility>
8 
9 #include "flutter/fml/memory/ref_counted.h"
10 #include "flutter/fml/memory/ref_ptr.h"
11 #include "flutter/fml/message_loop_impl.h"
12 #include "flutter/fml/task_runner.h"
13 #include "flutter/fml/thread_local.h"
14 
15 namespace fml {
16 
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 
39 MessageLoop::MessageLoop()
40  : loop_(MessageLoopImpl::Create()),
41  task_runner_(fml::MakeRefCounted<fml::TaskRunner>(loop_)) {
42  FML_CHECK(loop_);
43  FML_CHECK(task_runner_);
44 }
45 
46 MessageLoop::~MessageLoop() = default;
47 
49  loop_->DoRun();
50 }
51 
53  loop_->DoTerminate();
54 }
55 
57  return task_runner_;
58 }
59 
60 fml::RefPtr<MessageLoopImpl> MessageLoop::GetLoopImpl() const {
61  return loop_;
62 }
63 
64 void MessageLoop::AddTaskObserver(intptr_t key, const fml::closure& callback) {
65  loop_->AddTaskObserver(key, callback);
66 }
67 
68 void MessageLoop::RemoveTaskObserver(intptr_t key) {
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
FML_THREAD_LOCAL ThreadLocalUniquePtr< MessageLoop > tls_message_loop
Definition: message_loop.cc:17
void AddTaskObserver(intptr_t key, const fml::closure &callback)
Definition: message_loop.cc:64
static FML_EMBEDDER_ONLY MessageLoop & GetCurrent()
Definition: message_loop.cc:19
static void EnsureInitializedForCurrentThread()
Definition: message_loop.cc:27
Definition: ascii_trie.cc:9
fml::RefPtr< fml::TaskRunner > GetTaskRunner() const
Definition: message_loop.cc:56
static TaskQueueId GetCurrentTaskQueueId()
Definition: message_loop.cc:76
std::function< void()> closure
Definition: closure.h:14
static fml::RefPtr< MessageLoopImpl > Create()
#define FML_THREAD_LOCAL
Definition: thread_local.h:61
void RunExpiredTasksNow()
Definition: message_loop.cc:72
#define FML_CHECK(condition)
Definition: logging.h:68
void RemoveTaskObserver(intptr_t key)
Definition: message_loop.cc:68
static bool IsInitializedForCurrentThread()
Definition: message_loop.cc:35