Flutter Engine
The Flutter Engine
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
10namespace flutter {
11
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
22 task_runner_window_->RemoveDelegate(this);
23}
24
25std::chrono::nanoseconds TaskRunner::ProcessTasks() {
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
73TaskRunner::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
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
89 Task task;
90 task.fire_time = GetCurrentTimeForTask();
91 task.variant = std::move(closure);
92 EnqueueTask(std::move(task));
93}
94
95void TaskRunner::EnqueueTask(Task task) {
96 static std::atomic_uint64_t sGlobalTaskOrder(0);
97
98 task.order = ++sGlobalTaskOrder;
99 {
100 std::lock_guard<std::mutex> lock(task_queue_mutex_);
101 task_queue_.push(task);
102
103 // Make sure the queue mutex is unlocked before waking up the loop. In case
104 // the wake causes this thread to be descheduled for the primary thread to
105 // process tasks, the acquisition of the lock on that thread while holding
106 // the lock here momentarily till the end of the scope is a pessimization.
107 }
108
109 WakeUp();
110}
111
113 return GetCurrentThreadId() == main_thread_id_;
114}
115
117 task_runner_window_->WakeUp();
118}
119
120} // namespace flutter
static std::shared_ptr< TaskRunnerWindow > GetSharedInstance()
std::chrono::steady_clock::time_point TaskTimePoint
Definition: task_runner.h:28
void PostFlutterTask(FlutterTask flutter_task, uint64_t flutter_target_time_nanos)
Definition: task_runner.cc:80
TaskRunner(CurrentTimeProc get_current_time, const TaskExpiredCallback &on_task_expired)
Definition: task_runner.cc:12
virtual ~TaskRunner()
Definition: task_runner.cc:21
virtual bool RunsTasksOnCurrentThread() const
Definition: task_runner.cc:112
std::function< void(const FlutterTask *)> TaskExpiredCallback
Definition: task_runner.h:29
virtual void WakeUp()
Definition: task_runner.cc:116
std::function< void()> TaskClosure
Definition: task_runner.h:30
void PostTask(TaskClosure task)
Definition: task_runner.cc:88
virtual TaskTimePoint GetCurrentTimeForTask() const
Definition: task_runner.h:90
std::chrono::nanoseconds ProcessTasks()
Definition: task_runner.cc:25
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
uint64_t(* CurrentTimeProc)()
Definition: task_runner.h:21
std::function< void()> closure
Definition: closure.h:14
Definition: ref_ptr.h:256
Definition: DM.cpp:1161