Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | Protected Types | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
flutter::EventLoop Class Referenceabstract

#include <event_loop.h>

Inheritance diagram for flutter::EventLoop:
flutter::GLFWEventLoop flutter::HeadlessEventLoop

Classes

struct  Task
 

Public Types

using TaskExpiredCallback = std::function< void(const FlutterTask *)>
 

Public Member Functions

 EventLoop (std::thread::id main_thread_id, const TaskExpiredCallback &on_task_expired)
 
virtual ~EventLoop ()
 
 EventLoop (const EventLoop &)=delete
 
EventLoopoperator= (const EventLoop &)=delete
 
bool RunsTasksOnCurrentThread () const
 
void WaitForEvents (std::chrono::nanoseconds max_wait=std::chrono::nanoseconds::max())
 
void PostTask (FlutterTask flutter_task, uint64_t flutter_target_time_nanos)
 

Protected Types

using TaskTimePoint = std::chrono::steady_clock::time_point
 

Protected Member Functions

std::mutex & GetTaskQueueMutex ()
 
virtual void WaitUntil (const TaskTimePoint &time)=0
 
virtual void Wake ()=0
 

Static Protected Member Functions

static TaskTimePoint TimePointFromFlutterTime (uint64_t flutter_target_time_nanos)
 

Protected Attributes

std::thread::id main_thread_id_
 
TaskExpiredCallback on_task_expired_
 
std::mutex task_queue_mutex_
 
std::priority_queue< Task, std::deque< Task >, Task::Comparertask_queue_
 

Detailed Description

Definition at line 20 of file event_loop.h.

Member Typedef Documentation

◆ TaskExpiredCallback

Definition at line 22 of file event_loop.h.

◆ TaskTimePoint

using flutter::EventLoop::TaskTimePoint = std::chrono::steady_clock::time_point
protected

Definition at line 50 of file event_loop.h.

Constructor & Destructor Documentation

◆ EventLoop() [1/2]

flutter::EventLoop::EventLoop ( std::thread::id  main_thread_id,
const TaskExpiredCallback on_task_expired 
)

Definition at line 12 of file event_loop.cc.

14 : main_thread_id_(main_thread_id), on_task_expired_(on_task_expired) {}
TaskExpiredCallback on_task_expired_
Definition: event_loop.h:81
std::thread::id main_thread_id_
Definition: event_loop.h:80

◆ ~EventLoop()

flutter::EventLoop::~EventLoop ( )
virtualdefault

◆ EventLoop() [2/2]

flutter::EventLoop::EventLoop ( const EventLoop )
delete

Member Function Documentation

◆ GetTaskQueueMutex()

std::mutex & flutter::EventLoop::GetTaskQueueMutex ( )
inlineprotected

Definition at line 58 of file event_loop.h.

58{ return task_queue_mutex_; }
std::mutex task_queue_mutex_
Definition: event_loop.h:82

◆ operator=()

EventLoop & flutter::EventLoop::operator= ( const EventLoop )
delete

◆ PostTask()

void flutter::EventLoop::PostTask ( FlutterTask  flutter_task,
uint64_t  flutter_target_time_nanos 
)

Definition at line 82 of file event_loop.cc.

83 {
84 static std::atomic_uint64_t sGlobalTaskOrder(0);
85
86 Task task;
87 task.order = ++sGlobalTaskOrder;
88 task.fire_time = TimePointFromFlutterTime(flutter_target_time_nanos);
89 task.task = flutter_task;
90
91 {
92 std::lock_guard<std::mutex> lock(task_queue_mutex_);
93 task_queue_.push(task);
94
95 // Make sure the queue mutex is unlocked before waking up the loop. In case
96 // the wake causes this thread to be descheduled for the primary thread to
97 // process tasks, the acquisition of the lock on that thread while holding
98 // the lock here momentarily till the end of the scope is a pessimization.
99 }
100 Wake();
101}
virtual void Wake()=0
static TaskTimePoint TimePointFromFlutterTime(uint64_t flutter_target_time_nanos)
Definition: event_loop.cc:74
std::priority_queue< Task, std::deque< Task >, Task::Comparer > task_queue_
Definition: event_loop.h:83
Definition: DM.cpp:1161

◆ RunsTasksOnCurrentThread()

bool flutter::EventLoop::RunsTasksOnCurrentThread ( ) const

Definition at line 18 of file event_loop.cc.

18 {
19 return std::this_thread::get_id() == main_thread_id_;
20}

◆ TimePointFromFlutterTime()

EventLoop::TaskTimePoint flutter::EventLoop::TimePointFromFlutterTime ( uint64_t  flutter_target_time_nanos)
staticprotected

Definition at line 74 of file event_loop.cc.

75 {
76 const auto now = TaskTimePoint::clock::now();
77 const int64_t flutter_duration =
78 flutter_target_time_nanos - FlutterEngineGetCurrentTime();
79 return now + std::chrono::nanoseconds(flutter_duration);
80}
uint64_t FlutterEngineGetCurrentTime()
Get the current time in nanoseconds from the clock used by the flutter engine. This is the system mon...
Definition: embedder.cc:2955

◆ WaitForEvents()

void flutter::EventLoop::WaitForEvents ( std::chrono::nanoseconds  max_wait = std::chrono::nanoseconds::max())

Definition at line 22 of file event_loop.cc.

22 {
23 const auto now = TaskTimePoint::clock::now();
24 std::vector<FlutterTask> expired_tasks;
25
26 // Process expired tasks.
27 {
28 std::lock_guard<std::mutex> lock(task_queue_mutex_);
29 while (!task_queue_.empty()) {
30 const auto& top = task_queue_.top();
31 // If this task (and all tasks after this) has not yet expired, there is
32 // nothing more to do. Quit iterating.
33 if (top.fire_time > now) {
34 break;
35 }
36
37 // Make a record of the expired task. Do NOT service the task here
38 // because we are still holding onto the task queue mutex. We don't want
39 // other threads to block on posting tasks onto this thread till we are
40 // done processing expired tasks.
41 expired_tasks.push_back(task_queue_.top().task);
42
43 // Remove the tasks from the delayed tasks queue.
44 task_queue_.pop();
45 }
46 }
47
48 // Fire expired tasks.
49 {
50 // Flushing tasks here without holing onto the task queue mutex.
51 for (const auto& task : expired_tasks) {
52 on_task_expired_(&task);
53 }
54 }
55
56 // Sleep till the next task needs to be processed. If a new task comes
57 // along, the wait will be resolved early because PostTask calls Wake().
58 {
59 TaskTimePoint next_wake;
60 {
61 std::lock_guard<std::mutex> lock(task_queue_mutex_);
62 TaskTimePoint max_wake_timepoint =
64 : now + max_wait;
65 TaskTimePoint next_event_timepoint = task_queue_.empty()
67 : task_queue_.top().fire_time;
68 next_wake = std::min(max_wake_timepoint, next_event_timepoint);
69 }
70 WaitUntil(next_wake);
71 }
72}
virtual void WaitUntil(const TaskTimePoint &time)=0
std::chrono::steady_clock::time_point TaskTimePoint
Definition: event_loop.h:50
static float max(float r, float g, float b)
Definition: hsl.cpp:49
static float min(float r, float g, float b)
Definition: hsl.cpp:48

◆ WaitUntil()

virtual void flutter::EventLoop::WaitUntil ( const TaskTimePoint time)
protectedpure virtual

◆ Wake()

virtual void flutter::EventLoop::Wake ( )
protectedpure virtual

Member Data Documentation

◆ main_thread_id_

std::thread::id flutter::EventLoop::main_thread_id_
protected

Definition at line 80 of file event_loop.h.

◆ on_task_expired_

TaskExpiredCallback flutter::EventLoop::on_task_expired_
protected

Definition at line 81 of file event_loop.h.

◆ task_queue_

std::priority_queue<Task, std::deque<Task>, Task::Comparer> flutter::EventLoop::task_queue_
protected

Definition at line 83 of file event_loop.h.

◆ task_queue_mutex_

std::mutex flutter::EventLoop::task_queue_mutex_
protected

Definition at line 82 of file event_loop.h.


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