Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
support signal handler
Browse files Browse the repository at this point in the history
  • Loading branch information
happynear committed Feb 26, 2016
1 parent 682cef8 commit 475ab87
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
26 changes: 24 additions & 2 deletions src/caffe/util/signal_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ namespace {

void handle_signal(int signal) {
switch (signal) {
#ifdef _MSC_VER
case SIGBREAK://there is no SIGHUP in windows, take SIGBREAK instead.
got_sighup = true;
break;
#else
case SIGHUP:
got_sighup = true;
break;
#endif
case SIGINT:
got_sigint = true;
break;
Expand All @@ -27,7 +33,14 @@ namespace {
LOG(FATAL) << "Tried to hookup signal handlers more than once.";
}
already_hooked_up = true;

#ifdef _MSC_VER
if (signal(SIGBREAK, handle_signal) == SIG_ERR) {
LOG(FATAL) << "Cannot install SIGBREAK handler.";
}
if (signal(SIGINT, handle_signal) == SIG_ERR) {
LOG(FATAL) << "Cannot install SIGINT handler.";
}
#else
struct sigaction sa;
// Setup the handler
sa.sa_handler = &handle_signal;
Expand All @@ -42,11 +55,20 @@ namespace {
if (sigaction(SIGINT, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot install SIGINT handler.";
}
#endif
}

// Set the signal handlers to the default.
void UnhookHandler() {
if (already_hooked_up) {
#ifdef _MSC_VER
if (signal(SIGBREAK, SIG_DFL) == SIG_ERR) {
LOG(FATAL) << "Cannot uninstall SIGHUP handler.";
}
if (signal(SIGINT, SIG_DFL) == SIG_ERR) {
LOG(FATAL) << "Cannot uninstall SIGINT handler.";
}
#else
struct sigaction sa;
// Setup the sighub handler
sa.sa_handler = SIG_DFL;
Expand All @@ -61,7 +83,7 @@ namespace {
if (sigaction(SIGINT, &sa, NULL) == -1) {
LOG(FATAL) << "Cannot uninstall SIGINT handler.";
}

#endif
already_hooked_up = false;
}
}
Expand Down
5 changes: 0 additions & 5 deletions tools/caffe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,14 @@ int train() {
Caffe::set_solver_count(gpus.size());
}

#if !defined(_MSC_VER)
// Signals are not properly supported in Windows.
caffe::SignalHandler signal_handler(
GetRequestedAction(FLAGS_sigint_effect),
GetRequestedAction(FLAGS_sighup_effect));
#endif

shared_ptr<caffe::Solver<float> >
solver(caffe::SolverRegistry<float>::CreateSolver(solver_param));

#if !defined(_MSC_VER)
solver->SetActionFunction(signal_handler.GetActionFunction());
#endif

if (FLAGS_snapshot.size()) {
LOG(INFO) << "Resuming from " << FLAGS_snapshot;
Expand Down
1 change: 1 addition & 0 deletions windows/libcaffe/libcaffe.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<ClCompile Include="..\..\src\caffe\util\insert_splits.cpp" />
<ClCompile Include="..\..\src\caffe\util\io.cpp" />
<ClCompile Include="..\..\src\caffe\util\math_functions.cpp" />
<ClCompile Include="..\..\src\caffe\util\signal_handler.cpp" />
<ClCompile Include="..\..\src\caffe\util\upgrade_proto.cpp" />
</ItemGroup>
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions windows/libcaffe/libcaffe.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@
<ClCompile Include="..\..\src\caffe\layers\mil_layer.cpp">
<Filter>src\layers</Filter>
</ClCompile>
<ClCompile Include="..\..\src\caffe\util\signal_handler.cpp">
<Filter>src\util</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\..\include\caffe\proto\caffe.pb.h">
Expand Down

0 comments on commit 475ab87

Please sign in to comment.