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 const std::lock_guard<std::mutex> lock(mutex_);
10 if (!task.time) {
11 immediate_tasks_.push(task);
12 return;
13 }
14
15 if (task.time.value() < std::chrono::steady_clock::now()) {
16 immediate_tasks_.push(task);
17 return;
18 }
19
20 delayed_tasks_.push(task);
21}
22
24 const std::lock_guard<std::mutex> lock(mutex_);
25 // Attempt to execute a task immediately.
26 if (!immediate_tasks_.empty()) {
27 auto task = immediate_tasks_.front();
28 immediate_tasks_.pop();
29 return task.task;
30 }
31
32 // Move all tasks that can be executed to the immediate queue.
33 auto now = std::chrono::steady_clock::now();
34 while (!delayed_tasks_.empty()) {
35 const auto& top = delayed_tasks_.top();
36 if (!top.time.has_value() || top.time.value() > now) {
37 break;
38 }
39 immediate_tasks_.push(top);
40 delayed_tasks_.pop();
41 }
42
43 // Attempt to execute a task immediately.
44 if (!immediate_tasks_.empty()) {
45 auto task = immediate_tasks_.front();
46 immediate_tasks_.pop();
47 return task.task;
48 }
49
50 // If there are no tasks to execute, return the delay until the next task.
51 if (!delayed_tasks_.empty()) {
52 const auto& top = delayed_tasks_.top();
53 if (top.time.has_value()) {
54 return top.time.value() - now;
55 }
56 }
57
58 // If there are no tasks to execute, return the maximum duration.
59 return std::monostate{};
60}
61
62auto TaskQueue::HasImmediateTasks() const -> bool {
63 const std::lock_guard<std::mutex> lock(mutex_);
64 return !immediate_tasks_.empty();
65}
66
67} // namespace ftxui::task
std::uint8_t top
Definition screen.cpp:143
auto HasImmediateTasks() const -> bool
auto Get() -> MaybeTask
std::variant< Task, std::chrono::steady_clock::duration, std::monostate > MaybeTask
auto PostTask(PendingTask task) -> void
Definition task_queue.cpp:8