Flutter Windows Embedder
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 ()
 

Detailed Description

Definition at line 26 of file task_runner.h.

Member Typedef Documentation

◆ TaskClosure

using flutter::TaskRunner::TaskClosure = std::function<void()>

Definition at line 30 of file task_runner.h.

◆ TaskExpiredCallback

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

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 }

References flutter::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

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

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

Referenced by RunNowOrPostTask(), flutter::SettingsPlugin::WatchPreferredBrightnessChanged(), and flutter::SettingsPlugin::WatchTextScaleFactorChanged().

◆ ProcessTasks()

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

Implements flutter::TaskRunnerWindow::Delegate.

Definition at line 25 of file task_runner.cc.

25  {
26  const TaskTimePoint now = GetCurrentTimeForTask();
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 }

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

References PostTask(), and RunsTasksOnCurrentThread().

Referenced by flutter::FlutterWindowsTextureRegistrar::MarkTextureFrameAvailable(), and flutter::FlutterWindowsTextureRegistrar::UnregisterTexture().

◆ RunsTasksOnCurrentThread()

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

Definition at line 112 of file task_runner.cc.

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

Referenced by RunNowOrPostTask().


The documentation for this class was generated from the following files:
flutter::TaskRunnerWindow::GetSharedInstance
static std::shared_ptr< TaskRunnerWindow > GetSharedInstance()
Definition: task_runner_window.cc:43
flutter::TaskRunner::TaskTimePoint
std::chrono::steady_clock::time_point TaskTimePoint
Definition: task_runner.h:28
flutter::TaskRunner::PostTask
void PostTask(TaskClosure task)
Definition: task_runner.cc:88
flutter::TaskRunner::RunsTasksOnCurrentThread
virtual bool RunsTasksOnCurrentThread() const
Definition: task_runner.cc:112