PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
kcenon::pacs::samples::signal_handler Class Reference

#include <signal_handler.h>

Collaboration diagram for kcenon::pacs::samples::signal_handler:
Collaboration graph

Public Types

using shutdown_callback = std::function<void()>
 Callback type for shutdown notification.
 

Static Public Member Functions

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 wait_for_shutdown ()
 Block until a shutdown signal is received.
 
static void request_shutdown ()
 Request shutdown programmatically.
 
static void reset ()
 Reset the shutdown state.
 

Static Private Member Functions

static void handler (int signal)
 Signal handler function for C signal API.
 

Static Private Attributes

static std::atomic< bool > shutdown_requested_ {false}
 Shutdown flag.
 
static shutdown_callback callback_
 Callback function.
 
static std::mutex mutex_
 Mutex for condition variable.
 
static std::condition_variable cv_
 Condition variable for wait_for_shutdown.
 
static std::atomic< bool > callback_invoked_ {false}
 Flag to prevent multiple callback invocations.
 

Detailed Description

Definition at line 42 of file signal_handler.h.

Member Typedef Documentation

◆ shutdown_callback

Callback type for shutdown notification.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 45 of file signal_handler.h.

Member Function Documentation

◆ handler()

void kcenon::pacs::samples::signal_handler::handler ( int signal)
staticprivate

Signal handler function for C signal API.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 76 of file signal_handler.cpp.

76 {
78}
static void request_shutdown()
Request shutdown programmatically.

References request_shutdown().

Referenced by install().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ install()

void kcenon::pacs::samples::signal_handler::install ( shutdown_callback callback = nullptr)
static

Install signal handlers with optional callback.

Parameters
callbackFunction to call when shutdown signal is received

Installs handlers for SIGINT and SIGTERM (POSIX) or SIGINT and SIGBREAK (Windows). The callback is invoked once when the first signal is received.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 32 of file signal_handler.cpp.

32 {
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}
static shutdown_callback callback_
Callback function.
static void handler(int signal)
Signal handler function for C signal API.
static std::atomic< bool > callback_invoked_
Flag to prevent multiple callback invocations.
static std::atomic< bool > shutdown_requested_
Shutdown flag.

References callback_, callback_invoked_, handler(), and shutdown_requested_.

Referenced by kcenon::pacs::samples::scoped_signal_handler::scoped_signal_handler().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ request_shutdown()

void kcenon::pacs::samples::signal_handler::request_shutdown ( )
static

Request shutdown programmatically.

Sets the shutdown flag and notifies waiting threads. Useful for triggering shutdown from application code.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 57 of file signal_handler.cpp.

57 {
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}
static std::condition_variable cv_
Condition variable for wait_for_shutdown.

References callback_, callback_invoked_, cv_, and shutdown_requested_.

Referenced by handler().

Here is the caller graph for this function:

◆ reset()

void kcenon::pacs::samples::signal_handler::reset ( )
static

Reset the shutdown state.

Clears the shutdown flag. Useful for tests or when restarting a service within the same process.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 69 of file signal_handler.cpp.

69 {
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}
static std::mutex mutex_
Mutex for condition variable.

References callback_, callback_invoked_, mutex_, and shutdown_requested_.

Referenced by kcenon::pacs::samples::scoped_signal_handler::~scoped_signal_handler().

Here is the caller graph for this function:

◆ should_shutdown()

auto kcenon::pacs::samples::signal_handler::should_shutdown ( ) -> bool
staticnodiscardnoexcept

Check if shutdown has been requested.

Returns
true if a shutdown signal has been received
Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 46 of file signal_handler.cpp.

46 {
47 return shutdown_requested_.load(std::memory_order_acquire);
48}

References shutdown_requested_.

Referenced by kcenon::pacs::samples::scoped_signal_handler::should_shutdown().

Here is the caller graph for this function:

◆ wait_for_shutdown()

void kcenon::pacs::samples::signal_handler::wait_for_shutdown ( )
static

Block until a shutdown signal is received.

This method blocks the calling thread until SIGINT/SIGTERM is received. Useful for main threads that need to wait for graceful shutdown.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 50 of file signal_handler.cpp.

50 {
51 std::unique_lock<std::mutex> lock(mutex_);
52 cv_.wait(lock, []() {
53 return shutdown_requested_.load(std::memory_order_acquire);
54 });
55}

References cv_, mutex_, and shutdown_requested_.

Referenced by kcenon::pacs::samples::scoped_signal_handler::wait().

Here is the caller graph for this function:

Member Data Documentation

◆ callback_

signal_handler::shutdown_callback kcenon::pacs::samples::signal_handler::callback_
staticprivate

◆ callback_invoked_

std::atomic< bool > kcenon::pacs::samples::signal_handler::callback_invoked_ {false}
staticprivate

Flag to prevent multiple callback invocations.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 109 of file signal_handler.h.

Referenced by install(), request_shutdown(), and reset().

◆ cv_

std::condition_variable kcenon::pacs::samples::signal_handler::cv_
staticprivate

Condition variable for wait_for_shutdown.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 106 of file signal_handler.h.

Referenced by request_shutdown(), and wait_for_shutdown().

◆ mutex_

std::mutex kcenon::pacs::samples::signal_handler::mutex_
staticprivate

Mutex for condition variable.

Examples
/home/runner/work/pacs_system/pacs_system/examples/tutorials/common/signal_handler.h.

Definition at line 103 of file signal_handler.h.

Referenced by reset(), and wait_for_shutdown().

◆ shutdown_requested_

std::atomic< bool > kcenon::pacs::samples::signal_handler::shutdown_requested_ {false}
staticprivate

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