Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
work_stealing_pool_policy.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
7
8namespace kcenon::thread
9{
10
12 : policy_(config)
13 , enabled_(config.enable_work_stealing)
14{
15}
16
17auto work_stealing_pool_policy::on_enqueue(job& j) -> common::VoidResult
18{
19 (void)j; // Job not used, work-stealing doesn't affect enqueue
20 return common::ok();
21}
22
24{
25 (void)j; // Currently no action needed on job start
26}
27
28void work_stealing_pool_policy::on_job_complete(job& j, bool success, const std::exception* error)
29{
30 (void)j; // Job not used for work-stealing statistics
31 (void)success;
32 (void)error;
33 // Statistics are tracked through record_successful_steal/record_failed_steal
34}
35
37{
38 return "work_stealing_pool_policy";
39}
40
42{
43 return enabled_.load(std::memory_order_acquire);
44}
45
47{
48 enabled_.store(enabled, std::memory_order_release);
50}
51
53{
54 return policy_;
55}
56
58{
59 policy_ = config;
60 enabled_.store(config.enable_work_stealing, std::memory_order_release);
61}
62
67
72
77
79{
80 policy_.max_steal_attempts = attempts;
81}
82
83auto work_stealing_pool_policy::get_steal_backoff() const -> std::chrono::microseconds
84{
86}
87
88void work_stealing_pool_policy::set_steal_backoff(std::chrono::microseconds backoff)
89{
90 policy_.steal_backoff = backoff;
91}
92
94{
95 return successful_steals_.load(std::memory_order_acquire);
96}
97
99{
100 return failed_steals_.load(std::memory_order_acquire);
101}
102
104{
105 successful_steals_.store(0, std::memory_order_release);
106 failed_steals_.store(0, std::memory_order_release);
107}
108
110{
111 successful_steals_.fetch_add(1, std::memory_order_relaxed);
112}
113
115{
116 failed_steals_.fetch_add(1, std::memory_order_relaxed);
117}
118
119} // namespace kcenon::thread
Represents an error in the thread system.
Represents a unit of work (task) to be executed, typically by a job queue.
Definition job.h:136
void set_enabled(bool enabled) override
Enables or disables the policy.
void set_steal_backoff(std::chrono::microseconds backoff)
Sets the steal backoff duration.
void set_steal_policy(steal_policy policy)
Sets the steal policy (victim selection strategy).
void set_max_steal_attempts(std::size_t attempts)
Sets the maximum steal attempts per steal cycle.
void set_policy(const worker_policy &config)
Updates the worker policy configuration.
void record_failed_steal()
Records a failed steal attempt.
auto get_steal_backoff() const -> std::chrono::microseconds
Gets the steal backoff duration.
auto get_successful_steals() const -> std::uint64_t
Gets the total number of successful steals.
auto is_enabled() const -> bool override
Checks if the policy is enabled.
auto get_steal_policy() const -> steal_policy
Gets the steal policy (victim selection strategy).
auto get_policy() const -> const worker_policy &
Gets the current worker policy configuration.
void on_job_complete(job &j, bool success, const std::exception *error=nullptr) override
Called when a job completes.
auto on_enqueue(job &j) -> common::VoidResult override
Called before a job is enqueued.
auto get_name() const -> std::string override
Gets the policy name.
work_stealing_pool_policy(const worker_policy &config={})
Constructs a work-stealing policy with the given configuration.
void record_successful_steal()
Records a successful steal operation.
void on_job_start(job &j) override
Called when job starts executing.
auto get_failed_steals() const -> std::uint64_t
Gets the total number of failed steal attempts.
auto get_max_steal_attempts() const -> std::size_t
Gets the maximum steal attempts per steal cycle.
Base job class for schedulable work units in the thread system.
Core threading foundation of the thread system library.
Definition thread_impl.h:17
steal_policy
Policy for selecting steal victims.
STL namespace.
Worker behavior policy configuration.
std::chrono::microseconds steal_backoff
Pool policy implementing work-stealing for load balancing.