Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
kcenon::network::utils::lifecycle_manager Class Reference

Thread-safe lifecycle state management for network components. More...

#include <lifecycle_manager.h>

Collaboration diagram for kcenon::network::utils::lifecycle_manager:
Collaboration graph

Public Member Functions

 lifecycle_manager ()=default
 Default constructor.
 
 ~lifecycle_manager ()=default
 Destructor.
 
 lifecycle_manager (const lifecycle_manager &)=delete
 
lifecycle_manageroperator= (const lifecycle_manager &)=delete
 
 lifecycle_manager (lifecycle_manager &&other) noexcept
 
lifecycle_manageroperator= (lifecycle_manager &&other) noexcept
 
auto is_running () const -> bool
 Checks if the component is currently running.
 
auto try_start () -> bool
 Attempts to transition from stopped to running state.
 
auto set_running () -> void
 Marks the component as running.
 
auto mark_stopped () -> void
 Marks the component as stopped and signals waiters.
 
auto wait_for_stop () -> void
 Blocks until the component has stopped.
 
auto prepare_stop () -> bool
 Prepares for stop operation.
 
auto reset () -> void
 Resets the lifecycle manager to initial state.
 

Private Attributes

std::atomic< bool > is_running_ {false}
 
std::atomic< bool > stop_initiated_ {false}
 
std::optional< std::promise< void > > stop_promise_
 
std::future< void > stop_future_
 

Detailed Description

Thread-safe lifecycle state management for network components.

This utility class encapsulates the common lifecycle management logic that was previously duplicated across all CRTP base classes. It handles:

  • Running state tracking with atomic operations
  • Stop synchronization via promise/future
  • Thread-safe state transitions

Thread Safety

All methods are thread-safe and use atomic operations for state changes.

Usage Example

class my_client {
lifecycle_manager lifecycle_;
auto start() -> VoidResult {
if (!lifecycle_.try_start()) {
return make_error("Already running");
}
// ... start operations ...
return success();
}
auto stop() -> VoidResult {
if (!lifecycle_.prepare_stop()) {
return make_error("Not running");
}
// ... stop operations ...
lifecycle_.mark_stopped();
return success();
}
};
Thread-safe lifecycle state management for network components.
auto try_start() -> bool
Attempts to transition from stopped to running state.
auto prepare_stop() -> bool
Prepares for stop operation.
auto mark_stopped() -> void
Marks the component as stopped and signals waiters.

Definition at line 65 of file lifecycle_manager.h.

Constructor & Destructor Documentation

◆ lifecycle_manager() [1/3]

kcenon::network::utils::lifecycle_manager::lifecycle_manager ( )
default

Default constructor.

◆ ~lifecycle_manager()

kcenon::network::utils::lifecycle_manager::~lifecycle_manager ( )
default

Destructor.

◆ lifecycle_manager() [2/3]

kcenon::network::utils::lifecycle_manager::lifecycle_manager ( const lifecycle_manager & )
delete

◆ lifecycle_manager() [3/3]

kcenon::network::utils::lifecycle_manager::lifecycle_manager ( lifecycle_manager && other)
inlinenoexcept

Definition at line 83 of file lifecycle_manager.h.

84 : is_running_(other.is_running_.load(std::memory_order_acquire))
85 , stop_initiated_(other.stop_initiated_.load(std::memory_order_acquire))
86 , stop_promise_(std::move(other.stop_promise_))
87 , stop_future_(std::move(other.stop_future_))
88 {
89 other.is_running_.store(false, std::memory_order_release);
90 other.stop_initiated_.store(false, std::memory_order_release);
91 }
std::optional< std::promise< void > > stop_promise_

Member Function Documentation

◆ is_running()

auto kcenon::network::utils::lifecycle_manager::is_running ( ) const -> bool
inlinenodiscard

Checks if the component is currently running.

Returns
true if running, false otherwise.

Definition at line 114 of file lifecycle_manager.h.

115 {
116 return is_running_.load(std::memory_order_acquire);
117 }

References is_running_.

Referenced by kcenon::network::utils::startable_base< Derived >::do_stop(), kcenon::network::core::messaging_quic_client::is_running(), kcenon::network::core::messaging_quic_server::is_running(), kcenon::network::core::messaging_udp_client::is_running(), kcenon::network::core::messaging_udp_server::is_running(), kcenon::network::core::messaging_ws_client::is_running(), kcenon::network::core::messaging_ws_server::is_running(), kcenon::network::core::secure_messaging_client::is_running(), kcenon::network::core::secure_messaging_server::is_running(), kcenon::network::core::secure_messaging_udp_client::is_running(), kcenon::network::utils::startable_base< Derived >::is_running(), kcenon::network::core::messaging_quic_client::~messaging_quic_client(), kcenon::network::core::messaging_quic_server::~messaging_quic_server(), kcenon::network::core::messaging_udp_client::~messaging_udp_client(), kcenon::network::core::messaging_udp_server::~messaging_udp_server(), kcenon::network::core::messaging_ws_client::~messaging_ws_client(), kcenon::network::core::messaging_ws_server::~messaging_ws_server(), kcenon::network::core::secure_messaging_client::~secure_messaging_client(), kcenon::network::core::secure_messaging_server::~secure_messaging_server(), and kcenon::network::core::secure_messaging_udp_client::~secure_messaging_udp_client().

Here is the caller graph for this function:

◆ mark_stopped()

auto kcenon::network::utils::lifecycle_manager::mark_stopped ( ) -> void
inline

Marks the component as stopped and signals waiters.

This method:

  1. Sets is_running_ to false
  2. Sets the promise value (if exists) to unblock wait_for_stop()
  3. Resets the stop_initiated flag

Definition at line 152 of file lifecycle_manager.h.

153 {
154 is_running_.store(false, std::memory_order_release);
155 if (stop_promise_)
156 {
157 stop_promise_->set_value();
158 stop_promise_.reset();
159 }
160 stop_initiated_.store(false, std::memory_order_release);
161 }

References is_running_, stop_initiated_, and stop_promise_.

Referenced by kcenon::network::utils::startable_base< Derived >::do_start(), and kcenon::network::utils::startable_base< Derived >::do_stop().

Here is the caller graph for this function:

◆ operator=() [1/2]

lifecycle_manager & kcenon::network::utils::lifecycle_manager::operator= ( const lifecycle_manager & )
delete

◆ operator=() [2/2]

lifecycle_manager & kcenon::network::utils::lifecycle_manager::operator= ( lifecycle_manager && other)
inlinenoexcept

Definition at line 93 of file lifecycle_manager.h.

94 {
95 if (this != &other)
96 {
97 is_running_.store(other.is_running_.load(std::memory_order_acquire),
98 std::memory_order_release);
99 stop_initiated_.store(other.stop_initiated_.load(std::memory_order_acquire),
100 std::memory_order_release);
101 stop_promise_ = std::move(other.stop_promise_);
102 stop_future_ = std::move(other.stop_future_);
103
104 other.is_running_.store(false, std::memory_order_release);
105 other.stop_initiated_.store(false, std::memory_order_release);
106 }
107 return *this;
108 }

References is_running_, stop_future_, stop_initiated_, and stop_promise_.

◆ prepare_stop()

auto kcenon::network::utils::lifecycle_manager::prepare_stop ( ) -> bool
inlinenodiscard

Prepares for stop operation.

Returns
true if stop can proceed (was running), false if not running or already stopping.

This method:

  1. Checks if a stop is already initiated
  2. Creates the promise/future pair for synchronization
  3. Returns whether the caller should proceed with stop logic

Definition at line 186 of file lifecycle_manager.h.

187 {
188 // Check if already stopping
189 bool expected = false;
190 if (!stop_initiated_.compare_exchange_strong(expected, true,
191 std::memory_order_acq_rel))
192 {
193 return false; // Already stopping
194 }
195
196 // Check if running
197 if (!is_running_.load(std::memory_order_acquire))
198 {
199 stop_initiated_.store(false, std::memory_order_release);
200 return false; // Not running
201 }
202
203 // Create synchronization primitives
204 stop_promise_.emplace();
205 stop_future_ = stop_promise_->get_future();
206 return true;
207 }

References is_running_, stop_future_, stop_initiated_, and stop_promise_.

◆ reset()

auto kcenon::network::utils::lifecycle_manager::reset ( ) -> void
inline

Resets the lifecycle manager to initial state.

Use this to prepare for reuse after a stop operation.

Definition at line 214 of file lifecycle_manager.h.

215 {
216 is_running_.store(false, std::memory_order_release);
217 stop_initiated_.store(false, std::memory_order_release);
218 stop_promise_.reset();
219 stop_future_ = std::future<void>{};
220 }

References is_running_, stop_future_, stop_initiated_, and stop_promise_.

◆ set_running()

auto kcenon::network::utils::lifecycle_manager::set_running ( ) -> void
inline

Marks the component as running.

Use this when you need to set running state without the atomic check (e.g., after successful initialization).

Definition at line 139 of file lifecycle_manager.h.

140 {
141 is_running_.store(true, std::memory_order_release);
142 }

References is_running_.

◆ try_start()

auto kcenon::network::utils::lifecycle_manager::try_start ( ) -> bool
inlinenodiscard

Attempts to transition from stopped to running state.

Returns
true if transition succeeded (was not running), false if already running.

This method uses compare-and-exchange to ensure only one caller can successfully start the component.

Definition at line 126 of file lifecycle_manager.h.

127 {
128 bool expected = false;
129 return is_running_.compare_exchange_strong(
130 expected, true, std::memory_order_acq_rel);
131 }

References is_running_.

Referenced by kcenon::network::utils::startable_base< Derived >::do_start().

Here is the caller graph for this function:

◆ wait_for_stop()

auto kcenon::network::utils::lifecycle_manager::wait_for_stop ( ) -> void
inline

Blocks until the component has stopped.

If prepare_stop() was called, this will block until mark_stopped() is called. Returns immediately if no stop is in progress.

Definition at line 169 of file lifecycle_manager.h.

170 {
171 if (stop_future_.valid())
172 {
173 stop_future_.wait();
174 }
175 }

References stop_future_.

Referenced by kcenon::network::utils::startable_base< Derived >::wait_for_stop().

Here is the caller graph for this function:

Member Data Documentation

◆ is_running_

std::atomic<bool> kcenon::network::utils::lifecycle_manager::is_running_ {false}
private

Definition at line 223 of file lifecycle_manager.h.

223{false};

Referenced by is_running(), mark_stopped(), operator=(), prepare_stop(), reset(), set_running(), and try_start().

◆ stop_future_

std::future<void> kcenon::network::utils::lifecycle_manager::stop_future_
private

Definition at line 226 of file lifecycle_manager.h.

Referenced by operator=(), prepare_stop(), reset(), and wait_for_stop().

◆ stop_initiated_

std::atomic<bool> kcenon::network::utils::lifecycle_manager::stop_initiated_ {false}
private

Definition at line 224 of file lifecycle_manager.h.

224{false};

Referenced by mark_stopped(), operator=(), prepare_stop(), and reset().

◆ stop_promise_

std::optional<std::promise<void> > kcenon::network::utils::lifecycle_manager::stop_promise_
private

Definition at line 225 of file lifecycle_manager.h.

Referenced by mark_stopped(), operator=(), prepare_stop(), and reset().


The documentation for this class was generated from the following file: