Flutter Engine
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.

References ~EventLoop().

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

◆ ~EventLoop()

flutter::EventLoop::~EventLoop ( )
virtualdefault

Referenced by EventLoop().

◆ 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.

References task_queue_mutex_, WaitUntil(), and Wake().

Referenced by flutter::HeadlessEventLoop::HeadlessEventLoop().

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 83 of file event_loop.cc.

References flutter::EventLoop::Task::fire_time, flutter::EventLoop::Task::order, flutter::EventLoop::Task::task, task_queue_, task_queue_mutex_, TimePointFromFlutterTime(), and Wake().

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

◆ RunsTasksOnCurrentThread()

bool flutter::EventLoop::RunsTasksOnCurrentThread ( ) const

Definition at line 19 of file event_loop.cc.

References main_thread_id_.

19  {
20  return std::this_thread::get_id() == main_thread_id_;
21 }
std::thread::id main_thread_id_
Definition: event_loop.h:80

◆ TimePointFromFlutterTime()

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

Definition at line 75 of file event_loop.cc.

References FlutterEngineGetCurrentTime().

Referenced by PostTask().

76  {
77  const auto now = TaskTimePoint::clock::now();
78  const int64_t flutter_duration =
79  flutter_target_time_nanos - FlutterEngineGetCurrentTime();
80  return now + std::chrono::nanoseconds(flutter_duration);
81 }
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:1713

◆ WaitForEvents()

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

Definition at line 23 of file event_loop.cc.

References on_task_expired_, task_queue_, task_queue_mutex_, and WaitUntil().

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

◆ WaitUntil()

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

Referenced by GetTaskQueueMutex(), and WaitForEvents().

◆ Wake()

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

Referenced by GetTaskQueueMutex(), and PostTask().

Member Data Documentation

◆ main_thread_id_

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

Definition at line 80 of file event_loop.h.

Referenced by RunsTasksOnCurrentThread().

◆ on_task_expired_

TaskExpiredCallback flutter::EventLoop::on_task_expired_
protected

Definition at line 81 of file event_loop.h.

Referenced by WaitForEvents().

◆ 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.

Referenced by PostTask(), and WaitForEvents().

◆ task_queue_mutex_

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

Definition at line 82 of file event_loop.h.

Referenced by GetTaskQueueMutex(), PostTask(), and WaitForEvents().


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