Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
dag_job.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
7#include <format>
8
9namespace kcenon::thread
10{
11
12std::atomic<job_id> dag_job::next_dag_id_{1};
13
14dag_job::dag_job(const std::string& name)
15 : job(name)
16 , dag_id_(next_dag_id_.fetch_add(1, std::memory_order_relaxed))
17 , submit_time_(std::chrono::steady_clock::now())
18{
19}
20
21dag_job::~dag_job() = default;
22
24{
26 info.id = dag_id_;
27 info.name = get_name();
28 info.state = get_state();
29 info.dependencies = dependencies_;
30 info.submit_time = submit_time_;
31 info.start_time = start_time_;
32 info.end_time = end_time_;
33 info.error_message = error_message_;
34
35 if (has_result())
36 {
37 info.result = result_;
38 }
39
40 return info;
41}
42
43auto dag_job::do_work() -> common::VoidResult
44{
45 // Check cancellation token
46 if (cancellation_token_.is_cancelled())
47 {
48 set_state(dag_job_state::cancelled);
49 return make_error_result(error_code::operation_canceled, "Job was cancelled");
50 }
51
52 // If no work function is set, return success
53 if (!work_func_)
54 {
55 return common::ok();
56 }
57
58 // Execute the work function
59 try
60 {
61 return work_func_();
62 }
63 catch (const std::exception& e)
64 {
65 set_error_message(e.what());
67 }
68 catch (...)
69 {
70 set_error_message("Unknown exception");
71 return make_error_result(error_code::job_execution_failed, "Unknown exception during job execution");
72 }
73}
74
75auto dag_job::to_string() const -> std::string
76{
77 return std::format("[dag_job: {} (id={}, state={})]",
78 get_name(),
79 dag_id_,
81}
82
83} // namespace kcenon::thread
auto get_info() const -> dag_job_info
Creates a dag_job_info snapshot.
Definition dag_job.cpp:23
auto get_state() const -> dag_job_state
Gets the current state of the job.
Definition dag_job.h:184
std::chrono::steady_clock::time_point submit_time_
Time when the job was created.
Definition dag_job.h:479
dag_job(const std::string &name="dag_job")
Constructs a new dag_job with a name.
Definition dag_job.cpp:14
std::chrono::steady_clock::time_point start_time_
Time when execution started.
Definition dag_job.h:484
job_id dag_id_
Unique identifier for this job in the DAG.
Definition dag_job.h:444
std::vector< job_id > dependencies_
List of job IDs this job depends on.
Definition dag_job.h:454
auto has_result() const -> bool
Checks if the job has a result.
Definition dag_job.h:343
std::optional< std::string > error_message_
Error message if job failed.
Definition dag_job.h:474
std::any result_
Result value for passing between jobs.
Definition dag_job.h:469
auto do_work() -> common::VoidResult override
Executes the job's work function.
Definition dag_job.cpp:43
std::chrono::steady_clock::time_point end_time_
Time when execution ended.
Definition dag_job.h:489
~dag_job() override
Virtual destructor.
static std::atomic< job_id > next_dag_id_
Static counter for generating unique DAG job IDs.
Definition dag_job.h:439
auto to_string() const -> std::string override
Returns a string representation of the job.
Definition dag_job.cpp:75
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
auto get_name(void) const -> std::string
Retrieves the name of this job.
Definition job.cpp:112
DAG-aware job with dependency tracking and unique identifiers.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
common::VoidResult make_error_result(error_code code, const std::string &message="")
Create a common::VoidResult error from a thread::error_code.
auto dag_job_state_to_string(dag_job_state state) -> std::string
Convert dag_job_state to string representation.
Definition dag_job.h:60
@ cancelled
Cancelled by user or dependency failure.
@ info
Informational messages highlighting progress.
STL namespace.
Information about a job in the DAG.
Definition dag_job.h:83
job_id id
Unique job identifier.
Definition dag_job.h:84