1 #include "caffe2/utils/signal_handler.h" 2 #include "caffe2/core/logging.h" 9 SignalHandler::SignalHandler(
10 SignalHandler::Action SIGINT_action,
11 SignalHandler::Action SIGHUP_action) {}
12 SignalHandler::~SignalHandler() {}
13 bool SignalHandler::GotSIGINT() {
16 bool SignalHandler::GotSIGHUP() {
19 SignalHandler::Action SignalHandler::CheckForSignals() {
20 return SignalHandler::Action::NONE;
24 #else // defined(_MSC_VER) 30 #include <unordered_set> 34 static struct sigaction previous_sighup;
35 static struct sigaction previous_sigint;
36 static volatile sig_atomic_t sigint_count = 0;
37 static volatile sig_atomic_t sighup_count = 0;
38 static std::atomic<int> hooked_up_count{0};
40 void handle_signal(
int signal) {
44 if (previous_sighup.sa_handler) {
45 previous_sighup.sa_handler(signal);
50 if (previous_sigint.sa_handler) {
51 previous_sigint.sa_handler(signal);
57 void HookupHandler() {
58 if (hooked_up_count++) {
63 sa.sa_handler = &handle_signal;
65 sa.sa_flags = SA_RESTART;
67 sigfillset(&sa.sa_mask);
69 if (sigaction(SIGHUP, &sa,
nullptr) == -1) {
70 LOG(FATAL) <<
"Cannot install SIGHUP handler.";
72 if (sigaction(SIGINT, &sa,
nullptr) == -1) {
73 LOG(FATAL) <<
"Cannot install SIGINT handler.";
78 void UnhookHandler() {
79 if (--hooked_up_count > 0) {
84 sa.sa_handler = SIG_DFL;
86 sa.sa_flags = SA_RESTART;
88 sigfillset(&sa.sa_mask);
90 if (sigaction(SIGHUP, &sa, &previous_sighup) == -1) {
91 LOG(FATAL) <<
"Cannot uninstall SIGHUP handler.";
93 if (sigaction(SIGINT, &sa, &previous_sigint) == -1) {
94 LOG(FATAL) <<
"Cannot uninstall SIGINT handler.";
102 SignalHandler::SignalHandler(SignalHandler::Action SIGINT_action,
103 SignalHandler::Action SIGHUP_action):
104 SIGINT_action_(SIGINT_action),
105 SIGHUP_action_(SIGHUP_action),
106 my_sigint_count_(sigint_count),
107 my_sighup_count_(sighup_count) {
111 SignalHandler::~SignalHandler() {
117 bool SignalHandler::GotSIGINT() {
118 uint64_t count = sigint_count;
119 bool result = (count != my_sigint_count_);
120 my_sigint_count_ = count;
126 bool SignalHandler::GotSIGHUP() {
127 uint64_t count = sighup_count;
128 bool result = (count != my_sighup_count_);
129 my_sighup_count_ = count;
134 SignalHandler::Action SignalHandler::CheckForSignals() {
136 return SIGHUP_action_;
139 return SIGINT_action_;
141 return SignalHandler::Action::NONE;
146 #endif // defined(_MSC_VER) Simple registry implementation in Caffe2 that uses static variables to register object creators durin...