Network System 0.1.1
High-performance modular networking library for scalable client-server applications
Loading...
Searching...
No Matches
lifecycle_manager.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
11#pragma once
12
21#include <atomic>
22#include <future>
23#include <optional>
24
26{
27
66 {
67 public:
71 lifecycle_manager() = default;
72
76 ~lifecycle_manager() = default;
77
78 // Non-copyable
81
82 // Movable
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 }
92
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 }
109
114 [[nodiscard]] auto is_running() const -> bool
115 {
116 return is_running_.load(std::memory_order_acquire);
117 }
118
126 [[nodiscard]] auto try_start() -> bool
127 {
128 bool expected = false;
129 return is_running_.compare_exchange_strong(
130 expected, true, std::memory_order_acq_rel);
131 }
132
139 auto set_running() -> void
140 {
141 is_running_.store(true, std::memory_order_release);
142 }
143
152 auto mark_stopped() -> void
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 }
162
169 auto wait_for_stop() -> void
170 {
171 if (stop_future_.valid())
172 {
173 stop_future_.wait();
174 }
175 }
176
186 [[nodiscard]] auto prepare_stop() -> bool
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 }
208
214 auto reset() -> void
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 }
221
222 private:
223 std::atomic<bool> is_running_{false};
224 std::atomic<bool> stop_initiated_{false};
225 std::optional<std::promise<void>> stop_promise_;
226 std::future<void> stop_future_;
227 };
228
229} // namespace kcenon::network::utils
Thread-safe lifecycle state management for network components.
lifecycle_manager(const lifecycle_manager &)=delete
std::optional< std::promise< void > > stop_promise_
auto try_start() -> bool
Attempts to transition from stopped to running state.
auto reset() -> void
Resets the lifecycle manager to initial state.
lifecycle_manager & operator=(lifecycle_manager &&other) noexcept
auto prepare_stop() -> bool
Prepares for stop operation.
lifecycle_manager()=default
Default constructor.
auto mark_stopped() -> void
Marks the component as stopped and signals waiters.
auto is_running() const -> bool
Checks if the component is currently running.
auto wait_for_stop() -> void
Blocks until the component has stopped.
lifecycle_manager & operator=(const lifecycle_manager &)=delete
auto set_running() -> void
Marks the component as running.
lifecycle_manager(lifecycle_manager &&other) noexcept
Utility components for network_system.