14 : get_current_time_(get_current_time),
15 on_task_expired_(std::move(on_task_expired)) {
16 main_thread_id_ = GetCurrentThreadId();
18 task_runner_window_->AddDelegate(
this);
22 task_runner_window_->RemoveDelegate(
this);
28 std::vector<Task> expired_tasks;
32 std::lock_guard<std::mutex> lock(task_queue_mutex_);
33 while (!task_queue_.empty()) {
34 const auto& top = task_queue_.top();
37 if (top.fire_time > now) {
45 expired_tasks.push_back(task_queue_.top());
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))
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;
69 return std::min(next_wake - now, std::chrono::nanoseconds::max());
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);
81 uint64_t flutter_target_time_nanos) {
83 task.fire_time = TimePointFromFlutterTime(flutter_target_time_nanos);
84 task.variant = flutter_task;
85 EnqueueTask(std::move(task));
90 task.fire_time = GetCurrentTimeForTask();
91 task.variant = std::move(closure);
92 EnqueueTask(std::move(task));
95 void TaskRunner::EnqueueTask(Task task) {
96 static std::atomic_uint64_t sGlobalTaskOrder(0);
98 task.order = ++sGlobalTaskOrder;
100 std::lock_guard<std::mutex> lock(task_queue_mutex_);
101 task_queue_.push(task);
113 return GetCurrentThreadId() == main_thread_id_;
116 void TaskRunner::WakeUp() {
117 task_runner_window_->WakeUp();