Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
kcenon::thread::job_builder::built_job Class Reference

Internal job implementation created by the builder. More...

Inheritance diagram for kcenon::thread::job_builder::built_job:
Inheritance graph
Collaboration diagram for kcenon::thread::job_builder::built_job:
Collaboration graph

Public Member Functions

 built_job (const std::string &name, const std::vector< uint8_t > &data, std::function< common::VoidResult()> work_fn, std::function< common::VoidResult(const std::vector< uint8_t > &)> data_work_fn)
 
auto do_work () -> common::VoidResult override
 The core task execution method to be overridden by derived classes.
 
- 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.
 
virtual auto to_string (void) const -> std::string
 Provides a string representation of the job for logging or debugging.
 
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

std::function< common::VoidResult()> work_fn_
 
std::function< common::VoidResult(const std::vector< uint8_t > &)> data_work_fn_
 

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_queuejob_queue_
 A weak reference to the job_queue that currently manages this job.
 
cancellation_token cancellation_token_
 The cancellation token associated with this job.
 

Detailed Description

Internal job implementation created by the builder.

This class is used when no custom job type is specified via from<>(). It wraps the work function provided to the builder.

Definition at line 336 of file job_builder.h.

Constructor & Destructor Documentation

◆ built_job()

kcenon::thread::job_builder::built_job::built_job ( const std::string & name,
const std::vector< uint8_t > & data,
std::function< common::VoidResult()> work_fn,
std::function< common::VoidResult(const std::vector< uint8_t > &)> data_work_fn )
inline
Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 339 of file job_builder.h.

344 : job(data.empty() ? name : name)
345 , work_fn_(std::move(work_fn))
346 , data_work_fn_(std::move(data_work_fn))
347 {
348 if (!data.empty())
349 {
350 data_ = data;
351 }
352 }
std::function< common::VoidResult()> work_fn_
std::function< common::VoidResult(const std::vector< uint8_t > &)> data_work_fn_
auto name(const std::string &name) -> job_builder &
Sets the job name.
std::vector< uint8_t > data_
An optional container of raw byte data that may be used by the job.
Definition job.h:488
job(const std::string &name="job")
Constructs a new job with an optional human-readable name.
Definition job.cpp:53

Member Function Documentation

◆ do_work()

auto kcenon::thread::job_builder::built_job::do_work ( void ) -> common::VoidResult
inlinenodiscardoverridevirtual

The core task execution method to be overridden by derived classes.

Default implementation of work execution (must be overridden).

Returns
A common::VoidResult indicating success or error:
  • A success result (constructed with common::ok()) if no error occurred.
  • An error result (constructed with common::error_info{code, message}) on failure.

Default Behavior

The base class implementation simply returns a success result. Override this method in a derived class to perform meaningful work.

Concurrency

  • Typically invoked by worker threads in a job_queue.
  • Ensure that any shared data or resources accessed here are protected with appropriate synchronization mechanisms (mutexes, locks, etc.) if needed.
  • This method should check the cancellation token if one is set and return an error with code operation_canceled if the token is cancelled.

Implementation details:

  • Base implementation always returns "not implemented" error
  • Forces derived classes to provide actual work implementation
  • Maintains compatibility with result_void error handling
  • Should never be called in production (indicates missing override)

Design Pattern:

  • Template method pattern: defines interface, requires implementation
  • Pure virtual in spirit (returns error instead of being pure virtual)
  • Enables compilation while encouraging proper inheritance

Derived Class Requirements:

  • Must override this method to provide actual work logic
  • Should return common::ok() on success
  • Should return common::error_info{...} on failure with descriptive message
Returns
Error indicating method needs to be implemented in derived class

Reimplemented from kcenon::thread::job.

Examples
/home/runner/work/thread_system/thread_system/include/kcenon/thread/core/job_builder.h.

Definition at line 354 of file job_builder.h.

355 {
356 // Check cancellation before starting
358 {
360 invoke_callbacks(result);
361 return result;
362 }
363
364 common::VoidResult result = [this]() -> common::VoidResult {
365 if (data_work_fn_ && !data_.empty())
366 {
367 return data_work_fn_(data_);
368 }
369 else if (work_fn_)
370 {
371 return work_fn_();
372 }
373 else
374 {
375 return common::error_info{
376 static_cast<int>(error_code::not_implemented),
377 "No work function provided to job_builder",
378 "thread_system"
379 };
380 }
381 }();
382
383 // Invoke callbacks
384 invoke_callbacks(result);
385
386 return result;
387 }
bool is_cancelled() const
Checks if the token has been canceled.
auto invoke_callbacks(const common::VoidResult &result) -> void
Invokes the completion callbacks if they are set.
Definition job.cpp:484
cancellation_token cancellation_token_
The cancellation token associated with this job.
Definition job.h:504
common::VoidResult make_error_result(error_code code, const std::string &message="")
Create a common::VoidResult error from a thread::error_code.

References kcenon::thread::job::cancellation_token_, kcenon::thread::job::data_, data_work_fn_, kcenon::thread::job::invoke_callbacks(), kcenon::thread::cancellation_token::is_cancelled(), kcenon::thread::make_error_result(), kcenon::thread::not_implemented, kcenon::thread::operation_canceled, and work_fn_.

Here is the call graph for this function:

Member Data Documentation

◆ data_work_fn_

std::function<common::VoidResult(const std::vector<uint8_t>&)> kcenon::thread::job_builder::built_job::data_work_fn_
private

◆ work_fn_

std::function<common::VoidResult()> kcenon::thread::job_builder::built_job::work_fn_
private

The documentation for this class was generated from the following file: