Add a task system.

This commit is contained in:
ArthurSonzogni
2025-07-02 16:53:48 +02:00
parent 3b359e8cd7
commit 1b479ee12d
9 changed files with 375 additions and 8 deletions

View File

@@ -0,0 +1,42 @@
// Copyright 2024 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#ifndef TASK_RUNNER_HPP
#define TASK_RUNNER_HPP
#include "task.hpp"
#include "task_queue.hpp"
namespace ftxui::task {
class TaskRunner {
public:
TaskRunner();
~TaskRunner();
// Returns the task runner for the current thread.
static auto Current() -> TaskRunner*;
/// Schedules a task to be executed immediately.
auto PostTask(Task task) -> void;
/// Schedules a task to be executed after a certain duration.
auto PostDelayedTask(Task task,
std::chrono::steady_clock::duration duration) -> void;
/// Runs the tasks in the queue, return the delay until the next delayed task
/// can be executed.
auto RunUntilIdle() -> std::optional<std::chrono::steady_clock::duration>;
// Runs the tasks in the queue, blocking until all tasks are executed.
auto Run() -> void;
private:
TaskRunner* previous_task_runner_ = nullptr;
TaskQueue queue_;
};
} // namespace ftxui::task
#endif // TASK_RUNNER_HPP