Flutter Engine
The Flutter Engine
Classes | Public Types | Public Member Functions | Private Member Functions | List of all members
flutter::TaskRunner Class Reference

#include <task_runner.h>

Inheritance diagram for flutter::TaskRunner:
flutter::TaskRunnerWindow::Delegate

Public Types

using TaskTimePoint = std::chrono::steady_clock::time_point
 
using TaskExpiredCallback = std::function< void(const FlutterTask *)>
 
using TaskClosure = std::function< void()>
 

Public Member Functions

 TaskRunner (CurrentTimeProc get_current_time, const TaskExpiredCallback &on_task_expired)
 
virtual ~TaskRunner ()
 
virtual bool RunsTasksOnCurrentThread () const
 
void PostFlutterTask (FlutterTask flutter_task, uint64_t flutter_target_time_nanos)
 
void PostTask (TaskClosure task)
 
void RunNowOrPostTask (TaskClosure task)
 
std::chrono::nanoseconds ProcessTasks ()
 
virtual std::chrono::nanoseconds ProcessTasks ()=0
 

Private Member Functions

virtual void WakeUp ()
 
virtual TaskTimePoint GetCurrentTimeForTask () const
 

Detailed Description

Definition at line 26 of file task_runner.h.

Member Typedef Documentation

◆ TaskClosure

Definition at line 30 of file task_runner.h.

◆ TaskExpiredCallback

Definition at line 29 of file task_runner.h.

◆ TaskTimePoint

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

Definition at line 28 of file task_runner.h.

Constructor & Destructor Documentation

◆ TaskRunner()

flutter::TaskRunner::TaskRunner ( CurrentTimeProc  get_current_time,
const TaskExpiredCallback on_task_expired 
)

Definition at line 12 of file task_runner.cc.

14 : get_current_time_(get_current_time),
15 on_task_expired_(std::move(on_task_expired)) {
16 main_thread_id_ = GetCurrentThreadId();
17 task_runner_window_ = TaskRunnerWindow::GetSharedInstance();
18 task_runner_window_->AddDelegate(this);
19}
static std::shared_ptr< TaskRunnerWindow > GetSharedInstance()

◆ ~TaskRunner()

flutter::TaskRunner::~TaskRunner ( )
virtual

Definition at line 21 of file task_runner.cc.

21 {
22 task_runner_window_->RemoveDelegate(this);
23}

Member Function Documentation

◆ GetCurrentTimeForTask()

virtual TaskTimePoint flutter::TaskRunner::GetCurrentTimeForTask ( ) const
inlineprivatevirtual

Definition at line 90 of file task_runner.h.

90 {
91 return TaskTimePoint::clock::now();
92 }

◆ PostFlutterTask()

void flutter::TaskRunner::PostFlutterTask ( FlutterTask  flutter_task,
uint64_t  flutter_target_time_nanos 
)

Definition at line 80 of file task_runner.cc.

81 {
82 Task task;
83 task.fire_time = TimePointFromFlutterTime(flutter_target_time_nanos);
84 task.variant = flutter_task;
85 EnqueueTask(std::move(task));
86}
Definition: DM.cpp:1161

◆ PostTask()

void flutter::TaskRunner::PostTask ( TaskClosure  task)

Definition at line 88 of file task_runner.cc.

88 {
89 Task task;
90 task.fire_time = GetCurrentTimeForTask();
91 task.variant = std::move(closure);
92 EnqueueTask(std::move(task));
93}
virtual TaskTimePoint GetCurrentTimeForTask() const
Definition: task_runner.h:90
std::function< void()> closure
Definition: closure.h:14

◆ ProcessTasks()

std::chrono::nanoseconds flutter::TaskRunner::ProcessTasks ( )
virtual

Implements flutter::TaskRunnerWindow::Delegate.

Definition at line 25 of file task_runner.cc.

25 {
27
28 std::vector<Task> expired_tasks;
29
30 // Process expired tasks.
31 {
32 std::lock_guard<std::mutex> lock(task_queue_mutex_);
33 while (!task_queue_.empty()) {
34 const auto& top = task_queue_.top();
35 // If this task (and all tasks after this) has not yet expired, there is
36 // nothing more to do. Quit iterating.
37 if (top.fire_time > now) {
38 break;
39 }
40
41 // Make a record of the expired task. Do NOT service the task here
42 // because we are still holding onto the task queue mutex. We don't want
43 // other threads to block on posting tasks onto this thread till we are
44 // done processing expired tasks.
45 expired_tasks.push_back(task_queue_.top());
46
47 // Remove the tasks from the delayed tasks queue.
48 task_queue_.pop();
49 }
50 }
51
52 // Fire expired tasks.
53 {
54 // Flushing tasks here without holing onto the task queue mutex.
55 for (const auto& task : expired_tasks) {
56 if (auto flutter_task = std::get_if<FlutterTask>(&task.variant)) {
57 on_task_expired_(flutter_task);
58 } else if (auto closure = std::get_if<TaskClosure>(&task.variant))
59 (*closure)();
60 }
61 }
62
63 // Calculate duration to sleep for on next iteration.
64 {
65 std::lock_guard<std::mutex> lock(task_queue_mutex_);
66 const auto next_wake = task_queue_.empty() ? TaskTimePoint::max()
67 : task_queue_.top().fire_time;
68
69 return std::min(next_wake - now, std::chrono::nanoseconds::max());
70 }
71}
std::chrono::steady_clock::time_point TaskTimePoint
Definition: task_runner.h:28
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

◆ RunNowOrPostTask()

void flutter::TaskRunner::RunNowOrPostTask ( TaskClosure  task)
inline

Definition at line 51 of file task_runner.h.

51 {
53 task();
54 } else {
55 PostTask(std::move(task));
56 }
57 }
virtual bool RunsTasksOnCurrentThread() const
Definition: task_runner.cc:112
void PostTask(TaskClosure task)
Definition: task_runner.cc:88

◆ RunsTasksOnCurrentThread()

bool flutter::TaskRunner::RunsTasksOnCurrentThread ( ) const
virtual

Definition at line 112 of file task_runner.cc.

112 {
113 return GetCurrentThreadId() == main_thread_id_;
114}

◆ WakeUp()

void flutter::TaskRunner::WakeUp ( )
privatevirtual

Definition at line 116 of file task_runner.cc.

116 {
117 task_runner_window_->WakeUp();
118}

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