|
Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
|
A job with dependency support for DAG-based scheduling. More...
#include <dag_job.h>


Public Member Functions | |
| dag_job (const std::string &name="dag_job") | |
| Constructs a new dag_job with a name. | |
| ~dag_job () override | |
| Virtual destructor. | |
| auto | get_dag_id () const -> job_id |
| Gets the unique DAG job identifier. | |
| auto | get_state () const -> dag_job_state |
| Gets the current state of the job. | |
| auto | set_state (dag_job_state new_state) -> void |
| Sets the job state. | |
| auto | try_transition_state (dag_job_state expected, dag_job_state desired) -> bool |
| Attempts to transition state atomically. | |
| auto | get_dependencies () const -> const std::vector< job_id > & |
| Gets the list of dependency job IDs. | |
| auto | add_dependency (job_id dependency_id) -> void |
| Adds a dependency on another job. | |
| auto | add_dependencies (const std::vector< job_id > &dependency_ids) -> void |
| Adds multiple dependencies. | |
| auto | set_work (std::function< common::VoidResult()> work_func) -> void |
| Sets the work function to execute. | |
| template<typename T > | |
| auto | set_work_with_result (std::function< common::Result< T >()> work_func) -> void |
| Sets the work function with result. | |
| auto | set_fallback (std::function< common::VoidResult()> fallback_func) -> void |
| Sets the fallback function to execute on failure. | |
| auto | get_fallback () const -> const std::function< common::VoidResult()> & |
| Gets the fallback function. | |
| auto | has_fallback () const -> bool |
| Checks if a fallback function is set. | |
| template<typename T > | |
| auto | set_result (T &&value) -> void |
| Sets the result value. | |
| template<typename T > | |
| auto | get_result () const -> const T & |
| Gets the result value. | |
| auto | has_result () const -> bool |
| Checks if the job has a result. | |
| auto | get_result_any () const -> const std::any & |
| Gets the result as std::any. | |
| auto | set_error_message (const std::string &message) -> void |
| Sets the error message for failed jobs. | |
| auto | get_error_message () const -> const std::optional< std::string > & |
| Gets the error message. | |
| auto | record_start_time () -> void |
| Records the start time. | |
| auto | record_end_time () -> void |
| Records the end time. | |
| auto | get_submit_time () const -> std::chrono::steady_clock::time_point |
| Gets the submit time. | |
| auto | get_start_time () const -> std::chrono::steady_clock::time_point |
| Gets the start time. | |
| auto | get_end_time () const -> std::chrono::steady_clock::time_point |
| Gets the end time. | |
| auto | get_info () const -> dag_job_info |
| Creates a dag_job_info snapshot. | |
| auto | do_work () -> common::VoidResult override |
| Executes the job's work function. | |
| auto | to_string () const -> std::string override |
| Returns a string representation of the job. | |
Public Member Functions inherited from kcenon::thread::job | |
| auto | get_job_id () const -> std::uint64_t |
| Gets the unique ID of this job. | |
| auto | get_enqueue_time () const -> std::chrono::steady_clock::time_point |
| Gets the time when this job was created (enqueued). | |
| job (const std::string &name="job") | |
Constructs a new job with an optional human-readable name. | |
| job (const std::vector< uint8_t > &data, const std::string &name="data_job") | |
Constructs a new job with associated raw byte data and a name. | |
| virtual | ~job (void) |
Virtual destructor for the job class to allow proper cleanup in derived classes. | |
| auto | get_name (void) const -> std::string |
| Retrieves the name of this job. | |
| virtual auto | set_cancellation_token (const cancellation_token &token) -> void |
| Sets a cancellation token that can be used to cancel the job. | |
| virtual auto | get_cancellation_token () const -> cancellation_token |
| Gets the cancellation token associated with this job. | |
| virtual auto | set_job_queue (const std::shared_ptr< job_queue > &job_queue) -> void |
Associates this job with a specific job_queue. | |
| virtual auto | get_job_queue (void) const -> std::shared_ptr< job_queue > |
Retrieves the job_queue associated with this job, if any. | |
| auto | with_on_complete (std::function< void(common::VoidResult)> callback) -> job & |
| Attaches a completion callback to this job. | |
| auto | with_on_error (std::function< void(const common::error_info &)> callback) -> job & |
| Attaches an error callback to this job. | |
| auto | with_priority (job_priority priority) -> job & |
| Sets the priority level for this job. | |
| auto | with_cancellation (const cancellation_token &token) -> job & |
| Attaches a cancellation token to this job via composition. | |
| auto | with_retry (const retry_policy &policy) -> job & |
| Attaches a retry policy to this job. | |
| auto | with_timeout (std::chrono::milliseconds timeout) -> job & |
| Sets a timeout for job execution. | |
| auto | get_priority () const -> job_priority |
| Gets the priority level of this job. | |
| auto | get_retry_policy () const -> std::optional< retry_policy > |
| Gets the retry policy of this job. | |
| auto | get_timeout () const -> std::optional< std::chrono::milliseconds > |
| Gets the timeout duration for this job. | |
| auto | has_explicit_cancellation () const -> bool |
| Checks if this job has an explicit cancellation set via composition. | |
| auto | has_components () const -> bool |
| Checks if this job has any composed components. | |
Private Attributes | |
| job_id | dag_id_ |
| Unique identifier for this job in the DAG. | |
| std::atomic< dag_job_state > | state_ {dag_job_state::pending} |
| Current state of the job. | |
| std::vector< job_id > | dependencies_ |
| List of job IDs this job depends on. | |
| std::function< common::VoidResult()> | work_func_ |
| The work function to execute. | |
| std::function< common::VoidResult()> | fallback_func_ |
| The fallback function to execute on failure. | |
| std::any | result_ |
| Result value for passing between jobs. | |
| std::optional< std::string > | error_message_ |
| Error message if job failed. | |
| std::chrono::steady_clock::time_point | submit_time_ |
| Time when the job was created. | |
| std::chrono::steady_clock::time_point | start_time_ |
| Time when execution started. | |
| std::chrono::steady_clock::time_point | end_time_ |
| Time when execution ended. | |
Static Private Attributes | |
| static std::atomic< job_id > | next_dag_id_ {1} |
| Static counter for generating unique DAG job IDs. | |
Additional Inherited Members | |
Protected Member Functions inherited from kcenon::thread::job | |
| auto | invoke_callbacks (const common::VoidResult &result) -> void |
| Invokes the completion callbacks if they are set. | |
Protected Attributes inherited from kcenon::thread::job | |
| std::string | name_ |
| The descriptive name of the job, used primarily for identification and logging. | |
| std::vector< uint8_t > | data_ |
| An optional container of raw byte data that may be used by the job. | |
| std::weak_ptr< job_queue > | job_queue_ |
A weak reference to the job_queue that currently manages this job. | |
| cancellation_token | cancellation_token_ |
| The cancellation token associated with this job. | |
A job with dependency support for DAG-based scheduling.
The dag_job class extends the base job class to support:
|
explicit |
Constructs a new dag_job with a name.
| name | Human-readable name for the job |
Definition at line 14 of file dag_job.cpp.
|
overridedefault |
Virtual destructor.
|
inline |
Adds multiple dependencies.
| dependency_ids | Vector of job IDs to depend on |
Thread Safety: Not thread-safe, should be called before scheduling
Definition at line 243 of file dag_job.h.
References add_dependency().

|
inline |
Adds a dependency on another job.
| dependency_id | The ID of the job to depend on |
Thread Safety: Not thread-safe, should be called before scheduling
Definition at line 229 of file dag_job.h.
References dependencies_, and kcenon::thread::INVALID_JOB_ID.
Referenced by add_dependencies().

|
nodiscardoverridevirtual |
Executes the job's work function.
Thread Safety: Should only be called by the scheduler
Reimplemented from kcenon::thread::job.
Definition at line 43 of file dag_job.cpp.
References kcenon::thread::cancelled, kcenon::thread::job_execution_failed, kcenon::thread::make_error_result(), and kcenon::thread::operation_canceled.

|
inlinenodiscard |
|
inlinenodiscard |
Gets the list of dependency job IDs.
Thread Safety: Not thread-safe, should be set before scheduling
Definition at line 221 of file dag_job.h.
References dependencies_.
|
inlinenodiscard |
Gets the end time.
Definition at line 410 of file dag_job.h.
References end_time_.
|
inlinenodiscard |
Gets the error message.
Definition at line 367 of file dag_job.h.
References error_message_.
|
inlinenodiscard |
Gets the fallback function.
Definition at line 298 of file dag_job.h.
References fallback_func_.
|
nodiscard |
Creates a dag_job_info snapshot.
Definition at line 23 of file dag_job.cpp.
References dag_id_, dependencies_, end_time_, error_message_, kcenon::thread::job::get_name(), get_state(), has_result(), kcenon::thread::dag_job_info::id, kcenon::thread::info, result_, start_time_, and submit_time_.

|
inlinenodiscard |
|
inlinenodiscard |
|
inlinenodiscard |
Gets the start time.
Definition at line 401 of file dag_job.h.
References start_time_.
|
inlinenodiscard |
Gets the current state of the job.
Thread Safety: Atomic read
Definition at line 184 of file dag_job.h.
References state_.
Referenced by get_info(), and to_string().

|
inlinenodiscard |
Gets the submit time.
Definition at line 392 of file dag_job.h.
References submit_time_.
|
inlinenodiscard |
Checks if a fallback function is set.
Definition at line 307 of file dag_job.h.
References fallback_func_.
|
inlinenodiscard |
Checks if the job has a result.
Definition at line 343 of file dag_job.h.
References result_.
Referenced by get_info().

|
inline |
|
inline |
|
inline |
Sets the error message for failed jobs.
| message | The error message |
Definition at line 358 of file dag_job.h.
References error_message_.
|
inline |
Sets the fallback function to execute on failure.
| fallback_func | The fallback function |
Thread Safety: Not thread-safe, should be called before scheduling
Definition at line 289 of file dag_job.h.
References fallback_func_.
|
inline |
Sets the result value.
| T | The result type |
| value | The result value |
Thread Safety: Not thread-safe, should be called from worker thread only
Definition at line 320 of file dag_job.h.
References result_.
Referenced by set_work_with_result().

|
inline |
|
inline |
Sets the work function to execute.
| work_func | The function to execute |
Thread Safety: Not thread-safe, should be called before scheduling
Definition at line 257 of file dag_job.h.
References work_func_.
|
inline |
Sets the work function with result.
| T | The result type |
| work_func | The function to execute that returns a result |
Thread Safety: Not thread-safe, should be called before scheduling
Definition at line 270 of file dag_job.h.
References kcenon::thread::result< T >::is_ok(), set_result(), kcenon::thread::result< T >::value(), and work_func_.

|
nodiscardoverridevirtual |
Returns a string representation of the job.
Reimplemented from kcenon::thread::job.
Definition at line 75 of file dag_job.cpp.
References dag_id_, kcenon::thread::dag_job_state_to_string(), kcenon::thread::job::get_name(), and get_state().

|
inlinenodiscard |
Attempts to transition state atomically.
| expected | The expected current state |
| desired | The desired new state |
Thread Safety: Atomic compare-exchange
Definition at line 208 of file dag_job.h.
References state_.
|
private |
Unique identifier for this job in the DAG.
Definition at line 444 of file dag_job.h.
Referenced by get_dag_id(), get_info(), and to_string().
|
private |
List of job IDs this job depends on.
Definition at line 454 of file dag_job.h.
Referenced by add_dependency(), get_dependencies(), and get_info().
|
private |
Time when execution ended.
Definition at line 489 of file dag_job.h.
Referenced by get_end_time(), get_info(), and record_end_time().
|
private |
Error message if job failed.
Definition at line 474 of file dag_job.h.
Referenced by get_error_message(), get_info(), and set_error_message().
|
private |
The fallback function to execute on failure.
Definition at line 464 of file dag_job.h.
Referenced by get_fallback(), has_fallback(), and set_fallback().
|
staticprivate |
|
private |
Result value for passing between jobs.
Definition at line 469 of file dag_job.h.
Referenced by get_info(), get_result(), get_result_any(), has_result(), and set_result().
|
private |
Time when execution started.
Definition at line 484 of file dag_job.h.
Referenced by get_info(), get_start_time(), and record_start_time().
|
private |
Current state of the job.
Definition at line 449 of file dag_job.h.
Referenced by get_state(), set_state(), and try_transition_state().
|
private |
Time when the job was created.
Definition at line 479 of file dag_job.h.
Referenced by get_info(), and get_submit_time().
|
private |
The work function to execute.
Definition at line 459 of file dag_job.h.
Referenced by set_work(), and set_work_with_result().