Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
lifecycle_controller.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 "thread_conditions.h"
15
16#include <mutex>
17#include <atomic>
18#include <chrono>
19#include <optional>
20#include <condition_variable>
21
22#ifdef USE_STD_JTHREAD
23#include <stop_token>
24#endif
25
26namespace kcenon::thread
27{
80 {
81 public:
86
91
96
97 // =========================================================================
98 // State Management
99 // =========================================================================
100
109 [[nodiscard]] auto get_state() const noexcept -> thread_conditions;
110
119 auto set_state(thread_conditions state) noexcept -> void;
120
125 [[nodiscard]] auto is_running() const noexcept -> bool;
126
132 auto set_stopped() noexcept -> void;
133
134 // =========================================================================
135 // Stop Request Management
136 // =========================================================================
137
146 auto initialize_for_start() -> void;
147
158 auto request_stop() noexcept -> void;
159
167 [[nodiscard]] auto is_stop_requested() const noexcept -> bool;
168
175 [[nodiscard]] auto has_active_source() const noexcept -> bool;
176
183 auto reset_stop_source() noexcept -> void;
184
185 // =========================================================================
186 // Condition Variable Operations
187 // =========================================================================
188
195 [[nodiscard]] auto acquire_lock() -> std::unique_lock<std::mutex>;
196
205 template<typename Predicate>
206 auto wait(std::unique_lock<std::mutex>& lock, Predicate pred) -> void
207 {
208#ifdef USE_STD_JTHREAD
209 if (stop_source_.has_value())
210 {
211 auto stop_token = stop_source_.value().get_token();
212 condition_.wait(lock, [this, &stop_token, &pred]() {
213 return stop_token.stop_requested() || pred();
214 });
215 }
216 else
217 {
218 condition_.wait(lock, pred);
219 }
220#else
221 condition_.wait(lock, [this, &pred]() {
222 return stop_requested_.load(std::memory_order_acquire) || pred();
223 });
224#endif
225 }
226
237 template<typename Rep, typename Period, typename Predicate>
238 auto wait_for(std::unique_lock<std::mutex>& lock,
239 const std::chrono::duration<Rep, Period>& timeout,
240 Predicate pred) -> bool
241 {
242#ifdef USE_STD_JTHREAD
243 if (stop_source_.has_value())
244 {
245 auto stop_token = stop_source_.value().get_token();
246 return condition_.wait_for(lock, timeout, [this, &stop_token, &pred]() {
247 return stop_token.stop_requested() || pred();
248 });
249 }
250 else
251 {
252 return condition_.wait_for(lock, timeout, pred);
253 }
254#else
255 return condition_.wait_for(lock, timeout, [this, &pred]() {
256 return stop_requested_.load(std::memory_order_acquire) || pred();
257 });
258#endif
259 }
260
267 auto notify_one() -> void;
268
275 auto notify_all() -> void;
276
277 private:
279 std::mutex cv_mutex_;
280
282 std::condition_variable condition_;
283
285 std::atomic<thread_conditions> state_{thread_conditions::Created};
286
287#ifdef USE_STD_JTHREAD
289 std::optional<std::stop_source> stop_source_;
290#else
292 std::atomic<bool> stop_requested_{false};
293#endif
294 };
295
296} // namespace kcenon::thread
Centralized thread lifecycle state and synchronization management.
auto wait(std::unique_lock< std::mutex > &lock, Predicate pred) -> void
Waits on the condition variable with a predicate.
auto is_stop_requested() const noexcept -> bool
Checks if a stop has been requested.
auto set_state(thread_conditions state) noexcept -> void
Sets the thread condition/state.
auto wait_for(std::unique_lock< std::mutex > &lock, const std::chrono::duration< Rep, Period > &timeout, Predicate pred) -> bool
Waits on the condition variable with a timeout and predicate.
auto request_stop() noexcept -> void
Requests the thread to stop.
auto has_active_source() const noexcept -> bool
Checks if the controller has an active stop source (C++20 only).
lifecycle_controller(lifecycle_controller &&)=delete
auto initialize_for_start() -> void
Initializes the controller for a new thread start.
~lifecycle_controller()=default
Destructor.
auto set_stopped() noexcept -> void
Marks the thread as stopped.
std::atomic< bool > stop_requested_
Atomic flag for stop request (legacy mode).
auto acquire_lock() -> std::unique_lock< std::mutex >
Acquires a unique lock on the condition variable mutex.
auto is_running() const noexcept -> bool
Checks if the thread is currently running.
lifecycle_controller()
Constructs a new lifecycle_controller in Created state.
auto reset_stop_source() noexcept -> void
Resets the stop control mechanism after thread completion.
std::mutex cv_mutex_
Mutex for condition variable operations.
lifecycle_controller & operator=(const lifecycle_controller &)=delete
auto get_state() const noexcept -> thread_conditions
Gets the current thread condition/state.
lifecycle_controller & operator=(lifecycle_controller &&)=delete
std::condition_variable condition_
Condition variable for thread signaling.
lifecycle_controller(const lifecycle_controller &)=delete
Core threading foundation of the thread system library.
Definition thread_impl.h:17
thread_conditions
Enumeration of various states in a thread's lifecycle.
@ Created
Thread created but not started.
STL namespace.
Enumeration of thread lifecycle states (starting, running, stopping, stopped).