7#include <unordered_map>
49 template <
typename DependentType,
typename... DependencyTypes>
65 template <
typename FunctionType>
66 auto task(FunctionType&& function);
84 template <
typename FunctionType,
typename... ResultTypes>
98 void _execute_task(
ITask*
task, std::unordered_map<
ITask*, std::atomic<int>>& remaining);
111 void _register_dependency(
ITask* dependent,
ITask* dependency);
114 std::deque<std::unique_ptr<ITask>> _tasks;
115 std::atomic<bool> _is_running;
121template <
typename DependentType,
typename... DependencyTypes>
123 if (_is_running.load()) {
124 throw std::runtime_error(
"Cannot add dependencies while Exkit is running.");
127 (_register_dependency(dependent._task, dependencies._task), ...);
130template <
typename FunctionType>
132 if (_is_running.load()) {
133 throw std::runtime_error(
"Cannot add tasks while Exkit is running.");
136 using ResultType = std::invoke_result_t<FunctionType>;
138 auto task = std::make_unique<Task<ResultType>>(std::forward<FunctionType>(function));
141 _tasks.push_back(std::move(
task));
145template <
typename FunctionType,
typename... ResultTypes>
147 if (_is_running.load()) {
148 throw std::runtime_error(
"Cannot add tasks while Exkit is running.");
152 using ResultType =
decltype(std::apply(std::declval<FunctionType>(), std::declval<Args>()));
154 auto wrapper = [function = std::forward<FunctionType>(function), dependencies...]() {
158 auto task = std::make_unique<Task<ResultType>>(std::move(wrapper));
159 (_register_dependency(
task.get(), dependencies._task), ...);
162 _tasks.push_back(std::move(
task));
Exkit(IScheduler &scheduler)
Construct a new Exkit object.
void run()
Executes all the registered tasks while respecting their dependencies.
void add_dependency(TaskHandle< DependentType > &dependent, TaskHandle< DependencyTypes > &... dependencies)
Adds a dependency between a dependent task and one or more dependency tasks.
Definition: exkit.hpp:122
auto task(FunctionType &&function)
Creates a new task with the given function.
Definition: exkit.hpp:131
Definition: scheduler.hpp:5
auto dependency_values(TaskHandle< ResultTypes > const &... deps)
Definition: task.hpp:103