Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
overflow_policies.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
14#include <chrono>
15#include <memory>
16
19
21
26
49public:
51
57 [[nodiscard]] auto handle_overflow(std::unique_ptr<job>&& /*value*/)
58 -> common::VoidResult {
59 return common::error_info{-120, "queue is full, rejecting new item", "thread_system"};
60 }
61
66 [[nodiscard]] static constexpr auto blocks() noexcept -> bool {
67 return false;
68 }
69
74 [[nodiscard]] static constexpr auto name() noexcept -> const char* {
75 return "overflow_reject";
76 }
77};
78
99public:
101
109 [[nodiscard]] auto handle_overflow(std::unique_ptr<job>&& value)
110 -> std::pair<common::VoidResult, std::unique_ptr<job>> {
111 // Return the job back to the caller for retry after waiting
112 return {common::error_info{-123, "queue full, waiting for space", "thread_system"},
113 std::move(value)};
114 }
115
120 [[nodiscard]] static constexpr auto blocks() noexcept -> bool {
121 return true;
122 }
123
128 [[nodiscard]] static constexpr auto name() noexcept -> const char* {
129 return "overflow_block";
130 }
131};
132
152public:
154
160 [[nodiscard]] auto handle_overflow(std::unique_ptr<job>&& value)
161 -> std::pair<bool, std::unique_ptr<job>> {
162 // Return true to indicate oldest should be dropped
163 return {true, std::move(value)};
164 }
165
170 [[nodiscard]] static constexpr auto blocks() noexcept -> bool {
171 return false;
172 }
173
178 [[nodiscard]] static constexpr auto drops_oldest() noexcept -> bool {
179 return true;
180 }
181
186 [[nodiscard]] static constexpr auto name() noexcept -> const char* {
187 return "overflow_drop_oldest";
188 }
189};
190
208public:
210
216 [[nodiscard]] auto handle_overflow(std::unique_ptr<job>&& /*value*/)
217 -> common::VoidResult {
218 // Silently drop the item
219 return common::ok();
220 }
221
226 [[nodiscard]] static constexpr auto blocks() noexcept -> bool {
227 return false;
228 }
229
234 [[nodiscard]] static constexpr auto drops_newest() noexcept -> bool {
235 return true;
236 }
237
242 [[nodiscard]] static constexpr auto name() noexcept -> const char* {
243 return "overflow_drop_newest";
244 }
245};
246
263public:
265
270 explicit overflow_timeout_policy(std::chrono::milliseconds timeout)
271 : timeout_(timeout) {}
272
276 overflow_timeout_policy() : timeout_(std::chrono::seconds(1)) {}
277
282 [[nodiscard]] auto timeout() const noexcept -> std::chrono::milliseconds {
283 return timeout_;
284 }
285
290 auto set_timeout(std::chrono::milliseconds timeout) noexcept -> void {
292 }
293
299 [[nodiscard]] auto handle_overflow(std::unique_ptr<job>&& value)
300 -> std::pair<common::VoidResult, std::unique_ptr<job>> {
301 return {common::error_info{-123, "queue full, waiting with timeout", "thread_system"},
302 std::move(value)};
303 }
304
309 [[nodiscard]] static constexpr auto blocks() noexcept -> bool {
310 return true;
311 }
312
317 [[nodiscard]] static constexpr auto name() noexcept -> const char* {
318 return "overflow_timeout";
319 }
320
321private:
322 std::chrono::milliseconds timeout_;
323};
324
325} // namespace kcenon::thread::policies
Policy that blocks until space is available.
auto handle_overflow(std::unique_ptr< job > &&value) -> std::pair< common::VoidResult, std::unique_ptr< job > >
Handle overflow by indicating need to wait.
static constexpr auto name() noexcept -> const char *
Get a descriptive name for this policy.
static constexpr auto blocks() noexcept -> bool
Check if this policy blocks on overflow.
Policy that rejects new item when queue is full (same as reject)
static constexpr auto name() noexcept -> const char *
Get a descriptive name for this policy.
static constexpr auto blocks() noexcept -> bool
Check if this policy blocks on overflow.
static constexpr auto drops_newest() noexcept -> bool
Check if this policy drops items on overflow.
auto handle_overflow(std::unique_ptr< job > &&) -> common::VoidResult
Handle overflow by dropping the new item silently.
Policy that drops the oldest item when queue is full.
static constexpr auto blocks() noexcept -> bool
Check if this policy blocks on overflow.
static constexpr auto drops_oldest() noexcept -> bool
Check if this policy drops items on overflow.
static constexpr auto name() noexcept -> const char *
Get a descriptive name for this policy.
auto handle_overflow(std::unique_ptr< job > &&value) -> std::pair< bool, std::unique_ptr< job > >
Handle overflow by requesting oldest item removal.
Policy that rejects new items when queue is full.
static constexpr auto blocks() noexcept -> bool
Check if this policy blocks on overflow.
static constexpr auto name() noexcept -> const char *
Get a descriptive name for this policy.
auto handle_overflow(std::unique_ptr< job > &&) -> common::VoidResult
Handle overflow by rejecting the new item.
Policy that blocks for a limited time when queue is full.
static constexpr auto blocks() noexcept -> bool
Check if this policy blocks on overflow.
static constexpr auto name() noexcept -> const char *
Get a descriptive name for this policy.
overflow_timeout_policy(std::chrono::milliseconds timeout)
Construct with timeout duration.
auto handle_overflow(std::unique_ptr< job > &&value) -> std::pair< common::VoidResult, std::unique_ptr< job > >
Handle overflow by indicating need to wait with timeout.
auto set_timeout(std::chrono::milliseconds timeout) noexcept -> void
Set new timeout duration.
overflow_timeout_policy()
Default constructor with 1 second timeout.
auto timeout() const noexcept -> std::chrono::milliseconds
Get the configured timeout.
Error codes and utilities for the thread system.
Base job class for schedulable work units in the thread system.
STL namespace.
Tag type for overflow policy identification.