I am trying to port caffe (developed for Linux) source code to Windows environment. The problem is at sigaction
structure at signal_handler.cpp
and
Based on @nneonneo:
void handle_signal(int signal) {
switch (signal) {
#ifdef _WIN32
case SIGTERM:
case SIGABRT:
case SIGBREAK:
#else
case SIGHUP:
#endif
got_sighup = true;
break;
case SIGINT:
got_sigint = true;
break;
}
}
void HookupHandler() {
if (already_hooked_up) {
LOG(FATAL) << "Tried to hookup signal handlers more than once.";
}
already_hooked_up = true;
#ifdef _WIN32
signal(SIGINT, handle_signal);
signal(SIGTERM, handle_signal);
signal(SIGABRT, handle_signal);
#else
struct sigaction sa;
// Setup the handler
sa.sa_handler = &handle_signal;
// Restart the system call, if at all possible
sa.sa_flags = SA_RESTART;
// Block every signal during the handler
sigfillset(&sa.sa_mask);
// Intercept SIGHUP and SIGINT
if (sigaction(SIGHUP, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot install SIGHUP handler.";
}
if (sigaction(SIGINT, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot install SIGINT handler.";
}
#endif
}
void UnhookHandler() {
if (already_hooked_up) {
#ifdef _WIN32
signal(SIGINT, SIG_DFL);
signal(SIGTERM, SIG_DFL);
signal(SIGABRT, SIG_DFL);
#else
struct sigaction sa;
// Setup the sighub handler
sa.sa_handler = SIG_DFL;
// Restart the system call, if at all possible
sa.sa_flags = SA_RESTART;
// Block every signal during the handler
sigfillset(&sa.sa_mask);
// Intercept SIGHUP and SIGINT
if (sigaction(SIGHUP, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot uninstall SIGHUP handler.";
}
if (sigaction(SIGINT, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot uninstall SIGINT handler.";
}
#endif
already_hooked_up = false;
}
}
sigaction
is part of the UNIX signals API. Windows provides only signal
, which doesn't support SIGHUP
or any flags (such as SA_RESTART
). However, the very basic support is still there, so the code should still work reasonably correctly if you use just signal
(and not sigaction
).