Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
dag_job_builder.cpp
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
6
7namespace kcenon::thread
8{
9
10dag_job_builder::dag_job_builder(const std::string& name)
11 : name_(name)
12{
13}
14
15auto dag_job_builder::work(std::function<common::VoidResult()> callable) -> dag_job_builder&
16{
17 work_func_ = std::move(callable);
18 return *this;
19}
20
22{
23 if (dependency != INVALID_JOB_ID)
24 {
25 dependencies_.push_back(dependency);
26 }
27 return *this;
28}
29
30auto dag_job_builder::depends_on(std::initializer_list<job_id> dependencies) -> dag_job_builder&
31{
32 for (const auto& dep : dependencies)
33 {
34 if (dep != INVALID_JOB_ID)
35 {
36 dependencies_.push_back(dep);
37 }
38 }
39 return *this;
40}
41
42auto dag_job_builder::depends_on(const std::vector<job_id>& dependencies) -> dag_job_builder&
43{
44 for (const auto& dep : dependencies)
45 {
46 if (dep != INVALID_JOB_ID)
47 {
48 dependencies_.push_back(dep);
49 }
50 }
51 return *this;
52}
53
54auto dag_job_builder::on_failure(std::function<common::VoidResult()> fallback) -> dag_job_builder&
55{
56 fallback_func_ = std::move(fallback);
57 return *this;
58}
59
60auto dag_job_builder::is_valid() const -> bool
61{
62 return work_func_ != nullptr || work_with_result_func_ != nullptr;
63}
64
65auto dag_job_builder::get_validation_error() const -> std::optional<std::string>
66{
68 {
69 return "No work function specified. Use work() or work_with_result() before build().";
70 }
71 return std::nullopt;
72}
73
74auto dag_job_builder::build() -> std::unique_ptr<dag_job>
75{
76 // Validate configuration
77 if (!is_valid())
78 {
79 return nullptr;
80 }
81
82 auto job = std::make_unique<dag_job>(name_);
83
84 // Set dependencies
85 job->add_dependencies(dependencies_);
86
87 // Set work function
88 if (work_with_result_func_)
89 {
90 // Capture the work_with_result function and create a wrapper
91 auto result_func = std::move(work_with_result_func_);
92 job->set_work([j = job.get(), func = std::move(result_func)]() -> common::VoidResult {
93 return func(*j);
94 });
95 }
96 else if (work_func_)
97 {
98 job->set_work(std::move(work_func_));
99 }
100
101 // Set fallback function
102 if (fallback_func_)
103 {
104 job->set_fallback(std::move(fallback_func_));
105 }
106
107 // Reset builder for reuse (keep name)
108 reset();
109
110 return job;
111}
112
114{
115 work_func_ = nullptr;
116 work_with_result_func_ = nullptr;
117 fallback_func_ = nullptr;
118 dependencies_.clear();
119 has_return_type_ = false;
120 return *this;
121}
122
123} // 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.
auto build() -> std::unique_ptr< dag_job >
Builds and returns the configured dag_job.
auto is_valid() const -> bool
Validates the builder configuration.
auto get_validation_error() const -> std::optional< std::string >
Gets the validation error message.
std::function< common::VoidResult(dag_job &)> work_with_result_func_
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.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
Fluent builder for creating dag_job instances with dependencies.
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
constexpr job_id INVALID_JOB_ID
Invalid job ID constant.
Definition dag_job.h:38
@ fallback
Execute fallback job if available.
STL namespace.