Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
aging_typed_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
7namespace kcenon::thread
8{
9 template <typename job_type>
11 job_type priority,
12 std::function<common::VoidResult()> work,
13 const std::string& name)
14 : base(priority, name)
15 , aged_priority_{priority, 0, std::chrono::steady_clock::now()}
16 , work_(std::move(work))
17 {
18 }
19
20 template <typename job_type>
22
23 template <typename job_type>
24 auto aging_typed_job_t<job_type>::do_work() -> common::VoidResult
25 {
26 if (work_)
27 {
28 return work_();
29 }
30 return common::ok();
31 }
32
33 template <typename job_type>
35 -> const aged_priority<job_type>&
36 {
37 return aged_priority_;
38 }
39
40 template <typename job_type>
43 {
44 return aged_priority_;
45 }
46
47 template <typename job_type>
48 auto aging_typed_job_t<job_type>::apply_boost(int boost_amount) -> void
49 {
50 aged_priority_.apply_boost(boost_amount, max_boost_);
51 }
52
53 template <typename job_type>
55 {
56 aged_priority_.reset_boost();
57 }
58
59 template <typename job_type>
61 {
62 max_boost_ = max;
63 }
64
65 template <typename job_type>
67 {
68 return max_boost_;
69 }
70
71 template <typename job_type>
73 {
74 return aged_priority_.is_max_boosted(max_boost_);
75 }
76
77 template <typename job_type>
79 {
80 return aged_priority_.effective_priority();
81 }
82
83 template <typename job_type>
84 auto aging_typed_job_t<job_type>::wait_time() const -> std::chrono::milliseconds
85 {
86 return aged_priority_.wait_time();
87 }
88
89 template <typename job_type>
91 {
92 return job_info{
93 this->get_name(),
94 aged_priority_.wait_time(),
95 aged_priority_.boost
96 };
97 }
98
99 // Explicit template instantiation for job_types
100 template class aging_typed_job_t<job_types>;
101
102} // namespace kcenon::thread
Typed job with priority aging support to prevent starvation.
auto get_aged_priority() const -> const aged_priority< job_type > &
Gets the aged priority information.
auto do_work() -> common::VoidResult override
Executes the job's work function.
auto effective_priority() const -> job_type
Gets the effective priority after applying boost.
auto set_max_boost(int max) -> void
Sets the maximum allowed boost.
auto to_job_info() const -> job_info
Creates job_info for starvation callbacks.
auto reset_boost() -> void
Resets the priority boost to zero.
auto is_max_boosted() const -> bool
Checks if this job has reached maximum boost.
aging_typed_job_t(job_type priority, std::function< common::VoidResult()> work, const std::string &name="aging_job")
Constructs a new aging typed job.
auto get_max_boost() const -> int
Gets the maximum allowed boost.
~aging_typed_job_t() override
Destroys the aging typed job.
auto apply_boost(int boost_amount) -> void
Applies a priority boost to this job.
auto wait_time() const -> std::chrono::milliseconds
Gets the time this job has been waiting.
Typed job template.
Definition typed_job.h:31
Core threading foundation of the thread system library.
Definition thread_impl.h:17
@ priority
Priority-based scheduling.
STL namespace.
Priority with aging support.
auto is_max_boosted(int max_boost) const -> bool
Checks if this job has reached max boost.
Information about a job for starvation callback.