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 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 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() && delayed_tasks_.top().time.value() <= now) {
35 immediate_tasks_.push(delayed_tasks_.top());
36 delayed_tasks_.pop();
37 }
38
39 // Attempt to execute a task immediately.
40 if (!immediate_tasks_.empty()) {
41 auto task = immediate_tasks_.front();
42 immediate_tasks_.pop();
43 return task.task;
44 }
45
46 // If there are no tasks to execute, return the delay until the next task.
47 if (!delayed_tasks_.empty()) {
48 return delayed_tasks_.top().time.value() - now;
49 }
50
51 // If there are no tasks to execute, return the maximum duration.
52 return std::monostate{};
53}
54
55auto TaskQueue::HasImmediateTasks() const -> bool {
56 std::lock_guard<std::mutex> lock(mutex_);
57 return !immediate_tasks_.empty();
58}
59
60} // namespace ftxui::task
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