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 17 FML_THREAD_LOCAL ThreadLocalUniquePtr<MessageLoop> tls_message_loop; 18 GetCurrent()19MessageLoop& MessageLoop::GetCurrent() { 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 EnsureInitializedForCurrentThread()27void MessageLoop::EnsureInitializedForCurrentThread() { 28 if (tls_message_loop.get() != nullptr) { 29 // Already initialized. 30 return; 31 } 32 tls_message_loop.reset(new MessageLoop()); 33 } 34 IsInitializedForCurrentThread()35bool MessageLoop::IsInitializedForCurrentThread() { 36 return tls_message_loop.get() != nullptr; 37 } 38 MessageLoop()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 46 MessageLoop::~MessageLoop() = default; 47 Run()48void MessageLoop::Run() { 49 loop_->DoRun(); 50 } 51 Terminate()52void MessageLoop::Terminate() { 53 loop_->DoTerminate(); 54 } 55 GetTaskRunner() const56fml::RefPtr<fml::TaskRunner> MessageLoop::GetTaskRunner() const { 57 return task_runner_; 58 } 59 GetLoopImpl() const60fml::RefPtr<MessageLoopImpl> MessageLoop::GetLoopImpl() const { 61 return loop_; 62 } 63 AddTaskObserver(intptr_t key,fml::closure callback)64void MessageLoop::AddTaskObserver(intptr_t key, fml::closure callback) { 65 loop_->AddTaskObserver(key, callback); 66 } 67 RemoveTaskObserver(intptr_t key)68void MessageLoop::RemoveTaskObserver(intptr_t key) { 69 loop_->RemoveTaskObserver(key); 70 } 71 RunExpiredTasksNow()72void MessageLoop::RunExpiredTasksNow() { 73 loop_->RunExpiredTasksNow(); 74 } 75 GetCurrentTaskQueueId()76TaskQueueId MessageLoop::GetCurrentTaskQueueId() { 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 85