Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
priority_aging_config.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
12#pragma once
13
28#include <chrono>
29#include <functional>
30#include <string>
31#include <cstdint>
32
33namespace kcenon::thread
34{
39 struct job_info
40 {
41 std::string job_name;
42 std::chrono::milliseconds wait_time;
44 };
45
52 enum class aging_curve : uint8_t
53 {
54 linear,
57 };
58
80 {
86 bool enabled = false;
87
93 std::chrono::milliseconds aging_interval{1000};
94
101
108
117
123 double exponential_factor = 1.5;
124
130 std::chrono::seconds starvation_threshold{30};
131
138 std::function<void(const job_info&)> starvation_callback;
139
145 bool reset_on_dequeue = true;
146 };
147
173 template<typename P>
175 {
180
186 int boost{0};
187
193 std::chrono::steady_clock::time_point enqueue_time;
194
204 [[nodiscard]] auto effective_priority() const -> P
205 {
206 // For enum types, lower values typically mean higher priority
207 // Subtracting boost moves toward higher priority (lower enum value)
208 auto base_value = static_cast<int>(base_priority);
209 auto boosted_value = base_value - boost;
210 // Clamp to valid range (minimum 0)
211 boosted_value = (boosted_value < 0) ? 0 : boosted_value;
212 return static_cast<P>(boosted_value);
213 }
214
220 [[nodiscard]] auto wait_time() const -> std::chrono::milliseconds
221 {
222 return std::chrono::duration_cast<std::chrono::milliseconds>(
223 std::chrono::steady_clock::now() - enqueue_time
224 );
225 }
226
233 auto apply_boost(int boost_amount, int max_boost) -> void
234 {
235 boost += boost_amount;
236 if (boost > max_boost)
237 {
238 boost = max_boost;
239 }
240 }
241
245 auto reset_boost() -> void
246 {
247 boost = 0;
248 }
249
256 [[nodiscard]] auto is_max_boosted(int max_boost) const -> bool
257 {
258 return boost >= max_boost;
259 }
260 };
261
262} // namespace kcenon::thread
Core threading foundation of the thread system library.
Definition thread_impl.h:17
@ linear
Linearly increasing delay.
aging_curve
Defines different aging curve algorithms.
@ logarithmic
Decreasing boost (fast initial, slow later)
@ linear
Constant boost per interval.
@ exponential
Increasing boost over time.
STL namespace.
Priority with aging support.
auto is_max_boosted(int max_boost) const -> bool
Checks if this job has reached max boost.
auto apply_boost(int boost_amount, int max_boost) -> void
Applies a boost to the priority.
auto effective_priority() const -> P
Calculates the effective priority including boost.
auto reset_boost() -> void
Resets the boost to zero.
std::chrono::steady_clock::time_point enqueue_time
The time when the job was enqueued.
int boost
The current priority boost value.
P base_priority
The original priority level of the job.
auto wait_time() const -> std::chrono::milliseconds
Calculates the time this job has been waiting.
Information about a job for starvation callback.
std::chrono::milliseconds wait_time
Configuration for priority aging behavior.
double exponential_factor
Exponential factor for exponential aging curve.
aging_curve curve
The aging curve algorithm to use.
bool enabled
Whether priority aging is enabled.
std::function< void(const job_info &)> starvation_callback
Callback function invoked when a job is starving.
int priority_boost_per_interval
Amount of priority boost applied per aging interval.
int max_priority_boost
Maximum total priority boost that can be applied.
std::chrono::milliseconds aging_interval
Interval at which aging is applied.
std::chrono::seconds starvation_threshold
Threshold for starvation detection.
bool reset_on_dequeue
Whether to reset the boost when a job is dequeued.