Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
lifecycle_controller.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
6
7namespace kcenon::thread
8{
10 : state_(thread_conditions::Created)
11#ifdef USE_STD_JTHREAD
12 , stop_source_(std::nullopt)
13#else
14 , stop_requested_(false)
15#endif
16 {
17 }
18
20 {
21 return state_.load(std::memory_order_acquire);
22 }
23
25 {
26 state_.store(state, std::memory_order_release);
27 }
28
29 auto lifecycle_controller::is_running() const noexcept -> bool
30 {
31 auto condition = state_.load(std::memory_order_acquire);
32 return condition == thread_conditions::Working ||
33 condition == thread_conditions::Waiting;
34 }
35
40
42 {
43#ifdef USE_STD_JTHREAD
44 stop_source_ = std::stop_source();
45#else
46 stop_requested_.store(false, std::memory_order_release);
47#endif
49 }
50
51 auto lifecycle_controller::request_stop() noexcept -> void
52 {
53#ifdef USE_STD_JTHREAD
54 if (stop_source_.has_value())
55 {
56 stop_source_.value().request_stop();
57 }
58#else
59 stop_requested_.store(true, std::memory_order_release);
60#endif
61 }
62
63 auto lifecycle_controller::is_stop_requested() const noexcept -> bool
64 {
65#ifdef USE_STD_JTHREAD
66 if (stop_source_.has_value())
67 {
68 return stop_source_.value().stop_requested();
69 }
70 return true;
71#else
72 return stop_requested_.load(std::memory_order_acquire);
73#endif
74 }
75
76 auto lifecycle_controller::has_active_source() const noexcept -> bool
77 {
78#ifdef USE_STD_JTHREAD
79 return stop_source_.has_value();
80#else
81 // In legacy mode, check if we're in an active state (not stopped/created)
82 auto state = state_.load(std::memory_order_acquire);
83 return state == thread_conditions::Working ||
85#endif
86 }
87
89 {
90#ifdef USE_STD_JTHREAD
91 stop_source_.reset();
92#endif
93 }
94
95 auto lifecycle_controller::acquire_lock() -> std::unique_lock<std::mutex>
96 {
97 return std::unique_lock<std::mutex>(cv_mutex_);
98 }
99
101 {
102 std::scoped_lock<std::mutex> lock(cv_mutex_);
103 condition_.notify_one();
104 }
105
107 {
108 std::scoped_lock<std::mutex> lock(cv_mutex_);
109 condition_.notify_all();
110 }
111
112} // namespace kcenon::thread
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 notify_all() -> void
Notifies all waiting threads.
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).
auto notify_one() -> void
Notifies one waiting thread.
auto initialize_for_start() -> void
Initializes the controller for a new thread start.
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::atomic< thread_conditions > state_
Current thread state.
auto get_state() const noexcept -> thread_conditions
Gets the current thread condition/state.
Centralized thread lifecycle state and synchronization management.
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.
@ Waiting
Thread waiting for work or tasks.
@ Working
Thread currently processing a task.
@ Stopped
Thread fully stopped.
STL namespace.