Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
dag_job_builder.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2024, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
13#pragma once
14
15#include "dag_job.h"
16
17#include <functional>
18#include <initializer_list>
19#include <memory>
20#include <optional>
21#include <string>
22#include <vector>
23
24namespace kcenon::thread
25{
57 {
58 public:
63 explicit dag_job_builder(const std::string& name = "dag_job");
64
68 dag_job_builder(dag_job_builder&&) noexcept = default;
69
73 auto operator=(dag_job_builder&&) noexcept -> dag_job_builder& = default;
74
79
83 auto operator=(const dag_job_builder&) -> dag_job_builder& = delete;
84
88 ~dag_job_builder() = default;
89
99 auto work(std::function<common::VoidResult()> callable) -> dag_job_builder&;
100
110 template<typename T>
111 auto work_with_result(std::function<common::Result<T>()> callable) -> dag_job_builder&
112 {
113 work_with_result_func_ = [func = std::move(callable)](dag_job& job) -> common::VoidResult {
114 auto result = func();
115 if (result.is_ok())
116 {
117 job.set_result(result.value());
118 return common::ok();
119 }
120 return common::VoidResult(result.error());
121 };
122 return *this;
123 }
124
147 template<typename T>
149 {
150 has_return_type_ = true;
151 return *this;
152 }
153
159 auto depends_on(job_id dependency) -> dag_job_builder&;
160
166 auto depends_on(std::initializer_list<job_id> dependencies) -> dag_job_builder&;
167
173 auto depends_on(const std::vector<job_id>& dependencies) -> dag_job_builder&;
174
183 auto on_failure(std::function<common::VoidResult()> fallback) -> dag_job_builder&;
184
193 [[nodiscard]] auto is_valid() const -> bool;
194
199 [[nodiscard]] auto get_validation_error() const -> std::optional<std::string>;
200
211 [[nodiscard]] auto build() -> std::unique_ptr<dag_job>;
212
221 auto reset() -> dag_job_builder&;
222
223 private:
224 std::string name_;
225 std::function<common::VoidResult()> work_func_;
226 std::function<common::VoidResult(dag_job&)> work_with_result_func_;
227 std::function<common::VoidResult()> fallback_func_;
229 bool has_return_type_{false};
230 };
231
232} // namespace kcenon::thread
Fluent builder for creating dag_job instances.
auto on_failure(std::function< common::VoidResult()> fallback) -> dag_job_builder &
Sets the fallback function for failure recovery.
auto reset() -> dag_job_builder &
Resets the builder to its initial state.
std::function< common::VoidResult()> fallback_func_
auto build() -> std::unique_ptr< dag_job >
Builds and returns the configured dag_job.
std::vector< job_id > dependencies_
auto is_valid() const -> bool
Validates the builder configuration.
auto get_validation_error() const -> std::optional< std::string >
Gets the validation error message.
auto returns() -> dag_job_builder &
Specifies the result type for the job.
std::function< common::VoidResult(dag_job &)> work_with_result_func_
dag_job_builder(dag_job_builder &&) noexcept=default
Move constructor.
auto work_with_result(std::function< common::Result< T >()> callable) -> dag_job_builder &
Sets the work function with result.
dag_job_builder(const std::string &name="dag_job")
Constructs a new builder with an optional job name.
std::function< common::VoidResult()> work_func_
auto work(std::function< common::VoidResult()> callable) -> dag_job_builder &
Sets the work function to execute.
auto depends_on(job_id dependency) -> dag_job_builder &
Adds a single dependency.
A job with dependency support for DAG-based scheduling.
Definition dag_job.h:157
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
A template class representing either a value or an error.
T & value() &
Gets the value.
bool is_ok() const noexcept
Checks if the result is successful.
DAG-aware job with dependency tracking and unique identifiers.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
std::uint64_t job_id
Unique job identifier for DAG scheduler.
Definition dag_job.h:33
@ fallback
Execute fallback job if available.
STL namespace.