| Flutter Windows Embedder |

task_runner.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "flutter/shell/platform/windows/task_runner.h"
6
7 #include <atomic>
8 #include <utility>
9
10 namespace flutter {
11
12 TaskRunner::TaskRunner(CurrentTimeProc get_current_time,
13 const TaskExpiredCallback& on_task_expired)
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 }
20
21 TaskRunner::~TaskRunner() {
22 task_runner_window_->RemoveDelegate(this);
23 }
24
25 std::chrono::nanoseconds TaskRunner::ProcessTasks() {
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 }
72
73 TaskRunner::TaskTimePoint TaskRunner::TimePointFromFlutterTime(
74 uint64_t flutter_target_time_nanos) const {
75 const auto now = GetCurrentTimeForTask();
76 const auto flutter_duration = flutter_target_time_nanos - get_current_time_();
77 return now + std::chrono::nanoseconds(flutter_duration);
78 }
79
80 void TaskRunner::PostFlutterTask(FlutterTask flutter_task,
81 uint64_t flutter_target_time_nanos) {
82 Task task;
83 task.fire_time = TimePointFromFlutterTime(flutter_target_time_nanos);
84 task.variant = flutter_task;
85 EnqueueTask(std::move(task));
86 }
87
88 void TaskRunner::PostTask(TaskClosure closure) {
89 Task task;
90 task.fire_time = GetCurrentTimeForTask();
91 task.variant = std::move(closure);
92 EnqueueTask(std::move(task));
93 }
94
95 void TaskRunner::PollOnce(std::chrono::milliseconds timeout) {
96 task_runner_window_->PollOnce(timeout);
97 }
98
99 void TaskRunner::EnqueueTask(Task task) {
100 static std::atomic_uint64_t sGlobalTaskOrder(0);
101
102 task.order = ++sGlobalTaskOrder;
103 {
104 std::lock_guard<std::mutex> lock(task_queue_mutex_);
105 task_queue_.push(task);
106
107 // Make sure the queue mutex is unlocked before waking up the loop. In case
108 // the wake causes this thread to be descheduled for the primary thread to
109 // process tasks, the acquisition of the lock on that thread while holding
110 // the lock here momentarily till the end of the scope is a pessimization.
111 }
112
113 WakeUp();
114 }
115
116 bool TaskRunner::RunsTasksOnCurrentThread() const {
117 return GetCurrentThreadId() == main_thread_id_;
118 }
119
120 void TaskRunner::WakeUp() {
121 task_runner_window_->WakeUp();
122 }
123
124 } // namespace flutter
flutter::TaskRunner::TaskTimePoint
std::chrono::steady_clock::time_point TaskTimePoint
Definition: task_runner.h:28
flutter::TaskRunner::PostFlutterTask
void PostFlutterTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos)
Definition: task_runner.cc:80
flutter::TaskRunner::TaskRunner
TaskRunner(CurrentTimeProc get_current_time, const TaskExpiredCallback &on_task_expired)
Definition: task_runner.cc:12
flutter::TaskRunner::~TaskRunner
virtual ~TaskRunner()
Definition: task_runner.cc:21
flutter::TaskRunner::RunsTasksOnCurrentThread
virtual bool RunsTasksOnCurrentThread() const
Definition: task_runner.cc:116
flutter::TaskRunner::TaskExpiredCallback
std::function< void(const FlutterTask *)> TaskExpiredCallback
Definition: task_runner.h:29
flutter::TaskRunner::PollOnce
void PollOnce(std::chrono::milliseconds timeout)
Definition: task_runner.cc:95
flutter::TaskRunner::TaskClosure
std::function< void()> TaskClosure
Definition: task_runner.h:30
flutter::TaskRunner::PostTask
void PostTask(TaskClosure task)
Definition: task_runner.cc:88
flutter::TaskRunner::ProcessTasks
std::chrono::nanoseconds ProcessTasks()
Definition: task_runner.cc:25
flutter::TaskRunnerWindow::GetSharedInstance
static std::shared_ptr< TaskRunnerWindow > GetSharedInstance()
Definition: task_runner_window.cc:129
flutter
Definition: accessibility_bridge_windows.cc:11
flutter::CurrentTimeProc
uint64_t(* CurrentTimeProc)()
Definition: task_runner.h:21
task_runner.h