FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
task_queue.cpp
Go to the documentation of this file.
1// Copyright 2024 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
5
6namespace ftxui::task {
7
8auto TaskQueue::PostTask(PendingTask task) -> void {
9 if (!task.time) {
10 immediate_tasks_.push(task);
11 return;
12 }
13
14 if (task.time.value() < std::chrono::steady_clock::now()) {
15 immediate_tasks_.push(task);
16 return;
17 }
18
19 delayed_tasks_.push(task);
20}
21
23 // Attempt to execute a task immediately.
24 if (!immediate_tasks_.empty()) {
25 auto task = immediate_tasks_.front();
26 immediate_tasks_.pop();
27 return task.task;
28 }
29
30 // Move all tasks that can be executed to the immediate queue.
31 auto now = std::chrono::steady_clock::now();
32 while (!delayed_tasks_.empty() && delayed_tasks_.top().time.value() <= now) {
33 immediate_tasks_.push(delayed_tasks_.top());
34 delayed_tasks_.pop();
35 }
36
37 // Attempt to execute a task immediately.
38 if (!immediate_tasks_.empty()) {
39 auto task = immediate_tasks_.front();
40 immediate_tasks_.pop();
41 return task.task;
42 }
43
44 // If there are no tasks to execute, return the delay until the next task.
45 if (!delayed_tasks_.empty()) {
46 return delayed_tasks_.top().time.value() - now;
47 }
48
49 // If there are no tasks to execute, return the maximum duration.
50 return std::monostate{};
51}
52
53} // namespace ftxui::task
auto Get() -> MaybeTask
std::variant< Task, std::chrono::steady_clock::duration, std::monostate > MaybeTask
auto PostTask(PendingTask task) -> void
Definition task_queue.cpp:8