PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
signal_handler.cpp
Go to the documentation of this file.
1
6#include "signal_handler.h"
7
8#include <iostream>
9
10#ifdef _WIN32
11#include <windows.h>
12#else
13#include <unistd.h>
14#endif
15
16namespace kcenon::pacs::samples {
17
18// ============================================================================
19// Static Member Definitions
20// ============================================================================
21
22std::atomic<bool> signal_handler::shutdown_requested_{false};
24std::mutex signal_handler::mutex_;
25std::condition_variable signal_handler::cv_;
26std::atomic<bool> signal_handler::callback_invoked_{false};
27
28// ============================================================================
29// Signal Handler Implementation
30// ============================================================================
31
33 callback_ = std::move(callback);
34 shutdown_requested_.store(false, std::memory_order_release);
35 callback_invoked_.store(false, std::memory_order_release);
36
37#ifdef _WIN32
38 std::signal(SIGINT, handler);
39 std::signal(SIGBREAK, handler);
40#else
41 std::signal(SIGINT, handler);
42 std::signal(SIGTERM, handler);
43#endif
44}
45
46auto signal_handler::should_shutdown() noexcept -> bool {
47 return shutdown_requested_.load(std::memory_order_acquire);
48}
49
51 std::unique_lock<std::mutex> lock(mutex_);
52 cv_.wait(lock, []() {
53 return shutdown_requested_.load(std::memory_order_acquire);
54 });
55}
56
58 bool expected = false;
59 if (shutdown_requested_.compare_exchange_strong(expected, true,
60 std::memory_order_release, std::memory_order_relaxed)) {
61 // Invoke callback only once
62 if (callback_ && !callback_invoked_.exchange(true, std::memory_order_acq_rel)) {
63 callback_();
64 }
65 cv_.notify_all();
66 }
67}
68
70 std::lock_guard<std::mutex> lock(mutex_);
71 shutdown_requested_.store(false, std::memory_order_release);
72 callback_invoked_.store(false, std::memory_order_release);
73 callback_ = nullptr;
74}
75
76void signal_handler::handler(int /*signal*/) {
78}
79
80// ============================================================================
81// Scoped Signal Handler Implementation
82// ============================================================================
83
87
91
95
96auto scoped_signal_handler::should_shutdown() const noexcept -> bool {
98}
99
100} // namespace kcenon::pacs::samples
scoped_signal_handler(signal_handler::shutdown_callback callback=nullptr)
Construct and install signal handlers.
auto should_shutdown() const noexcept -> bool
Check if shutdown has been requested.
void wait()
Block until shutdown signal is received.
~scoped_signal_handler()
Destructor - resets signal handler state.
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.
Cross-platform signal handling for graceful shutdown.