Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
typed_job_interface.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
5#pragma once
6
15#include "forward_declarations.h"
16#include "type_traits.h"
18#include <string>
19#include <memory>
20
21namespace kcenon::thread {
22
30 template<detail::JobType job_type>
32 public:
33 virtual ~typed_job_interface() = default;
34
39 [[nodiscard]] virtual job_type type() const noexcept = 0;
40
45 [[nodiscard]] virtual common::VoidResult execute() = 0;
46
51 [[nodiscard]] virtual std::string description() const = 0;
52
57 [[nodiscard]] virtual bool is_ready() const noexcept {
58 return true; // Default implementation - job is always ready
59 }
60
65 [[nodiscard]] virtual bool is_cancelled() const noexcept {
66 return false; // Default implementation - job is not cancellable
67 }
68
73 virtual bool cancel() noexcept {
74 return false; // Default implementation - job cannot be cancelled
75 }
76
81 [[nodiscard]] virtual uint64_t estimated_execution_time_us() const noexcept {
82 return 0; // Default implementation - unknown execution time
83 }
84
90 [[nodiscard]] virtual bool has_higher_priority(const typed_job_interface& other) const noexcept {
91 return detail::higher_priority(type(), other.type());
92 }
93 };
94
98 template<detail::JobType job_type>
99 using typed_job_ptr = std::shared_ptr<typed_job_interface<job_type>>;
100
104 template<detail::JobType job_type>
105 using typed_job_weak_ptr = std::weak_ptr<typed_job_interface<job_type>>;
106
113 template<detail::JobType job_type>
115 public:
116 virtual ~typed_job_factory_interface() = default;
117
125 template<detail::JobCallable F>
126 [[nodiscard]] virtual typed_job_ptr<job_type> create_job(
127 job_type type,
128 F&& callback,
129 const std::string& description = "callback_job"
130 ) = 0;
131
138 virtual void cleanup() noexcept {}
139 };
140
141} // namespace kcenon::thread
Factory interface for creating typed jobs.
virtual typed_job_ptr< job_type > create_job(job_type type, F &&callback, const std::string &description="callback_job")=0
Create a new job with the specified type and callback.
virtual void cleanup() noexcept
Release resources associated with completed jobs.
Typed job interface template.
virtual bool is_ready() const noexcept
Check if this job can be executed.
virtual bool is_cancelled() const noexcept
Check if this job has been cancelled.
virtual common::VoidResult execute()=0
Execute the job's work.
virtual job_type type() const noexcept=0
Get the type/priority of this job.
virtual bool has_higher_priority(const typed_job_interface &other) const noexcept
Check if this job has higher priority than another job.
virtual ~typed_job_interface()=default
virtual std::string description() const =0
Get a human-readable description of this job.
virtual uint64_t estimated_execution_time_us() const noexcept
Get the estimated execution time for this job.
virtual bool cancel() noexcept
Cancel this job if possible.
@ callback
Call user callback for custom decision.
Error codes and utilities for the thread system.
constexpr bool higher_priority(T lhs, T rhs) noexcept
Compile-time priority comparison.
Definition type_traits.h:90
Core threading foundation of the thread system library.
Definition thread_impl.h:17
std::shared_ptr< typed_job_interface< job_type > > typed_job_ptr
Shared pointer type for typed job interfaces.
std::weak_ptr< typed_job_interface< job_type > > typed_job_weak_ptr
Weak pointer type for typed job interfaces.
STL namespace.
Type traits for typed_thread_pool module.