PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
signal_handler.h
Go to the documentation of this file.
1
9#pragma once
10
11#include <atomic>
12#include <condition_variable>
13#include <csignal>
14#include <functional>
15#include <mutex>
16
17namespace kcenon::pacs::samples {
18
43public:
45 using shutdown_callback = std::function<void()>;
46
47 // ========================================================================
48 // Static Interface
49 // ========================================================================
50
59 static void install(shutdown_callback callback = nullptr);
60
65 [[nodiscard]] static auto should_shutdown() noexcept -> bool;
66
74 static void wait_for_shutdown();
75
82 static void request_shutdown();
83
90 static void reset();
91
92private:
94 static void handler(int signal);
95
97 static std::atomic<bool> shutdown_requested_;
98
101
103 static std::mutex mutex_;
104
106 static std::condition_variable cv_;
107
109 static std::atomic<bool> callback_invoked_;
110};
111
112// ============================================================================
113// RAII Wrapper
114// ============================================================================
115
139public:
144 explicit scoped_signal_handler(signal_handler::shutdown_callback callback = nullptr);
145
150
151 // Non-copyable, non-movable
156
160 void wait();
161
166 [[nodiscard]] auto should_shutdown() const noexcept -> bool;
167};
168
169} // namespace kcenon::pacs::samples
auto operator=(const scoped_signal_handler &) -> scoped_signal_handler &=delete
auto operator=(scoped_signal_handler &&) -> scoped_signal_handler &=delete
scoped_signal_handler(scoped_signal_handler &&)=delete
scoped_signal_handler(const scoped_signal_handler &)=delete
static void wait_for_shutdown()
Block until a shutdown signal is received.
static shutdown_callback callback_
Callback function.
static void install(shutdown_callback callback=nullptr)
Install signal handlers with optional callback.
static auto should_shutdown() noexcept -> bool
Check if shutdown has been requested.
static void handler(int signal)
Signal handler function for C signal API.
static void request_shutdown()
Request shutdown programmatically.
static std::condition_variable cv_
Condition variable for wait_for_shutdown.
static std::atomic< bool > callback_invoked_
Flag to prevent multiple callback invocations.
std::function< void()> shutdown_callback
Callback type for shutdown notification.
static void reset()
Reset the shutdown state.
static std::mutex mutex_
Mutex for condition variable.
static std::atomic< bool > shutdown_requested_
Shutdown flag.
std::shared_mutex mutex
Mutex for thread-safe access.