Flutter Engine
The Flutter Engine
Public Member Functions | Static Public Member Functions | Protected Member Functions | List of all members
fml::ConcurrentMessageLoop Class Reference

#include <concurrent_message_loop.h>

Inheritance diagram for fml::ConcurrentMessageLoop:
fml::ConcurrentMessageLoopDarwin

Public Member Functions

virtual ~ConcurrentMessageLoop ()
 
size_t GetWorkerCount () const
 
std::shared_ptr< ConcurrentTaskRunnerGetTaskRunner ()
 
void Terminate ()
 
void PostTaskToAllWorkers (const fml::closure &task)
 
bool RunsTasksOnCurrentThread ()
 

Static Public Member Functions

static std::shared_ptr< ConcurrentMessageLoopCreate (size_t worker_count=std::thread::hardware_concurrency())
 

Protected Member Functions

 ConcurrentMessageLoop (size_t worker_count)
 
virtual void ExecuteTask (const fml::closure &task)
 

Detailed Description

Definition at line 21 of file concurrent_message_loop.h.

Constructor & Destructor Documentation

◆ ~ConcurrentMessageLoop()

fml::ConcurrentMessageLoop::~ConcurrentMessageLoop ( )
virtual

Definition at line 29 of file concurrent_message_loop.cc.

29 {
30 Terminate();
31 for (auto& worker : workers_) {
32 FML_DCHECK(worker.joinable());
33 worker.join();
34 }
35}
#define FML_DCHECK(condition)
Definition: logging.h:103

◆ ConcurrentMessageLoop()

fml::ConcurrentMessageLoop::ConcurrentMessageLoop ( size_t  worker_count)
explicitprotected

Definition at line 14 of file concurrent_message_loop.cc.

15 : worker_count_(std::max<size_t>(worker_count, 1ul)) {
16 for (size_t i = 0; i < worker_count_; ++i) {
17 workers_.emplace_back([i, this]() {
19 std::string{"io.worker." + std::to_string(i + 1)}));
20 WorkerMain();
21 });
22 }
23
24 for (const auto& worker : workers_) {
25 worker_thread_ids_.emplace_back(worker.get_id());
26 }
27}
static void SetCurrentThreadName(const ThreadConfig &config)
Definition: thread.cc:135
static SkString to_string(int n)
Definition: nanobench.cpp:119
The ThreadConfig is the thread info include thread name, thread priority.
Definition: thread.h:35

Member Function Documentation

◆ Create()

std::shared_ptr< ConcurrentMessageLoop > fml::ConcurrentMessageLoop::Create ( size_t  worker_count = std::thread::hardware_concurrency())
static

Definition at line 9 of file concurrent_message_loop_factory.cc.

10 {
11 return std::shared_ptr<ConcurrentMessageLoop>{
12 new ConcurrentMessageLoop(worker_count)};
13}
ConcurrentMessageLoop(size_t worker_count)

◆ ExecuteTask()

void fml::ConcurrentMessageLoop::ExecuteTask ( const fml::closure task)
protectedvirtual

Reimplemented in fml::ConcurrentMessageLoopDarwin.

Definition at line 115 of file concurrent_message_loop.cc.

115 {
116 task();
117}

◆ GetTaskRunner()

std::shared_ptr< ConcurrentTaskRunner > fml::ConcurrentMessageLoop::GetTaskRunner ( )

Definition at line 41 of file concurrent_message_loop.cc.

41 {
42 return std::make_shared<ConcurrentTaskRunner>(weak_from_this());
43}

◆ GetWorkerCount()

size_t fml::ConcurrentMessageLoop::GetWorkerCount ( ) const

Definition at line 37 of file concurrent_message_loop.cc.

37 {
38 return worker_count_;
39}

◆ PostTaskToAllWorkers()

void fml::ConcurrentMessageLoop::PostTaskToAllWorkers ( const fml::closure task)

Definition at line 125 of file concurrent_message_loop.cc.

125 {
126 if (!task) {
127 return;
128 }
129
130 std::scoped_lock lock(tasks_mutex_);
131 for (const auto& worker_thread_id : worker_thread_ids_) {
132 thread_tasks_[worker_thread_id].emplace_back(task);
133 }
134 tasks_condition_.notify_all();
135}

◆ RunsTasksOnCurrentThread()

bool fml::ConcurrentMessageLoop::RunsTasksOnCurrentThread ( )

Definition at line 172 of file concurrent_message_loop.cc.

172 {
173 std::scoped_lock lock(tasks_mutex_);
174 for (const auto& worker_thread_id : worker_thread_ids_) {
175 if (worker_thread_id == std::this_thread::get_id()) {
176 return true;
177 }
178 }
179 return false;
180}

◆ Terminate()

void fml::ConcurrentMessageLoop::Terminate ( )

Definition at line 119 of file concurrent_message_loop.cc.

119 {
120 std::scoped_lock lock(tasks_mutex_);
121 shutdown_ = true;
122 tasks_condition_.notify_all();
123}

The documentation for this class was generated from the following files: