Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
bound_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 <cstddef>
15#include <limits>
16#include <optional>
17
19
24
39public:
41
45 constexpr unbounded_policy() noexcept = default;
46
52 [[nodiscard]] constexpr auto is_full(std::size_t /*current_size*/) const noexcept -> bool {
53 return false;
54 }
55
60 [[nodiscard]] constexpr auto max_size() const noexcept -> std::optional<std::size_t> {
61 return std::nullopt;
62 }
63
68 [[nodiscard]] static constexpr auto is_bounded() noexcept -> bool {
69 return false;
70 }
71
77 [[nodiscard]] constexpr auto remaining_capacity(std::size_t /*current_size*/) const noexcept -> std::size_t {
78 return std::numeric_limits<std::size_t>::max();
79 }
80};
81
100public:
102
107 explicit constexpr bounded_policy(std::size_t max) noexcept
108 : max_size_(max) {}
109
115 [[nodiscard]] constexpr auto is_full(std::size_t current_size) const noexcept -> bool {
116 return current_size >= max_size_;
117 }
118
123 [[nodiscard]] constexpr auto max_size() const noexcept -> std::optional<std::size_t> {
124 return max_size_;
125 }
126
131 [[nodiscard]] static constexpr auto is_bounded() noexcept -> bool {
132 return true;
133 }
134
140 [[nodiscard]] constexpr auto remaining_capacity(std::size_t current_size) const noexcept -> std::size_t {
141 if (current_size >= max_size_) {
142 return 0;
143 }
144 return max_size_ - current_size;
145 }
146
154 auto set_max_size(std::size_t new_max) noexcept -> void {
155 max_size_ = new_max;
156 }
157
158private:
159 std::size_t max_size_;
160};
161
178public:
180
185 explicit constexpr dynamic_bounded_policy(
186 std::optional<std::size_t> max = std::nullopt) noexcept
187 : max_size_(max) {}
188
194 [[nodiscard]] constexpr auto is_full(std::size_t current_size) const noexcept -> bool {
195 if (!max_size_.has_value()) {
196 return false;
197 }
198 return current_size >= max_size_.value();
199 }
200
205 [[nodiscard]] constexpr auto max_size() const noexcept -> std::optional<std::size_t> {
206 return max_size_;
207 }
208
213 [[nodiscard]] constexpr auto is_bounded() const noexcept -> bool {
214 return max_size_.has_value();
215 }
216
222 [[nodiscard]] constexpr auto remaining_capacity(std::size_t current_size) const noexcept -> std::size_t {
223 if (!max_size_.has_value()) {
224 return std::numeric_limits<std::size_t>::max();
225 }
226 if (current_size >= max_size_.value()) {
227 return 0;
228 }
229 return max_size_.value() - current_size;
230 }
231
236 auto set_max_size(std::size_t new_max) noexcept -> void {
237 max_size_ = new_max;
238 }
239
243 auto set_unbounded() noexcept -> void {
244 max_size_ = std::nullopt;
245 }
246
247private:
248 std::optional<std::size_t> max_size_;
249};
250
251} // namespace kcenon::thread::policies
Policy that limits queue size to a maximum.
constexpr bounded_policy(std::size_t max) noexcept
Construct bounded policy with max size.
constexpr auto remaining_capacity(std::size_t current_size) const noexcept -> std::size_t
Get remaining capacity.
auto set_max_size(std::size_t new_max) noexcept -> void
Set new maximum size.
constexpr auto is_full(std::size_t current_size) const noexcept -> bool
Check if queue is at capacity.
constexpr auto max_size() const noexcept -> std::optional< std::size_t >
Get maximum size.
static constexpr auto is_bounded() noexcept -> bool
Check if this is a bounded policy.
Policy with dynamically adjustable size limit.
auto set_unbounded() noexcept -> void
Remove size limit (become unbounded)
constexpr dynamic_bounded_policy(std::optional< std::size_t > max=std::nullopt) noexcept
Construct with optional max size.
auto set_max_size(std::size_t new_max) noexcept -> void
Set new maximum size.
constexpr auto is_full(std::size_t current_size) const noexcept -> bool
Check if queue is at capacity.
constexpr auto max_size() const noexcept -> std::optional< std::size_t >
Get maximum size.
constexpr auto is_bounded() const noexcept -> bool
Check if this is currently bounded.
constexpr auto remaining_capacity(std::size_t current_size) const noexcept -> std::size_t
Get remaining capacity.
Policy that allows unlimited queue size.
constexpr auto is_full(std::size_t) const noexcept -> bool
Check if queue is at capacity.
constexpr unbounded_policy() noexcept=default
Construct unbounded policy.
constexpr auto max_size() const noexcept -> std::optional< std::size_t >
Get maximum size.
constexpr auto remaining_capacity(std::size_t) const noexcept -> std::size_t
Get remaining capacity.
static constexpr auto is_bounded() noexcept -> bool
Check if this is a bounded policy.
STL namespace.
Tag type for bound policy identification.