SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Signal.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #ifndef WIN32
14 
15 #include <stdlib.h>
16 #include <signal.h>
17 #include <string.h>
18 
19 #include <shogun/io/SGIO.h>
20 #include <shogun/lib/Signal.h>
21 #include <shogun/base/init.h>
22 
23 using namespace shogun;
24 
25 int CSignal::signals[NUMTRAPPEDSIGS]={SIGINT, SIGURG};
26 struct sigaction CSignal::oldsigaction[NUMTRAPPEDSIGS];
27 bool CSignal::active=false;
30 
32 : CSGObject()
33 {
34 }
35 
37 {
38  if (!unset_handler())
39  SG_PRINT("error uninitalizing signal handler\n")
40 }
41 
42 void CSignal::handler(int signal)
43 {
44  if (signal == SIGINT)
45  {
46  SG_SPRINT("\nImmediately return to prompt / Prematurely finish computations / Do nothing (I/P/D)? ")
47  char answer=fgetc(stdin);
48 
49  if (answer == 'I')
50  {
51  unset_handler();
52  set_cancel(true);
53  if (sg_print_error)
54  sg_print_error(stdout, "sg stopped by SIGINT\n");
55  }
56  else if (answer == 'P')
57  set_cancel();
58  else
59  SG_SPRINT("Continuing...\n")
60  }
61  else if (signal == SIGURG)
62  set_cancel();
63  else
64  SG_SPRINT("unknown signal %d received\n", signal)
65 }
66 
68 {
69  if (!active)
70  {
71  struct sigaction act;
72  sigset_t st;
73 
74  sigemptyset(&st);
75  for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
76  sigaddset(&st, signals[i]);
77 
78 #ifndef __INTERIX
79  act.sa_sigaction=NULL; //just in case
80 #endif
81  act.sa_handler=CSignal::handler;
82  act.sa_mask = st;
83  act.sa_flags = 0;
84 
85  for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
86  {
87  if (sigaction(signals[i], &act, &oldsigaction[i]))
88  {
89  SG_SPRINT("Error trapping signals!\n")
90  for (int32_t j=i-1; j>=0; j--)
91  sigaction(signals[i], &oldsigaction[i], NULL);
92 
93  clear();
94  return false;
95  }
96  }
97 
98  active=true;
99  return true;
100  }
101  else
102  return false;
103 }
104 
106 {
107  if (active)
108  {
109  bool result=true;
110 
111  for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
112  {
113  if (sigaction(signals[i], &oldsigaction[i], NULL))
114  {
115  SG_SPRINT("error uninitalizing signal handler for signal %d\n", signals[i])
116  result=false;
117  }
118  }
119 
120  if (result)
121  clear();
122 
123  return result;
124  }
125  else
126  return false;
127 }
128 
130 {
131  cancel_computation=false;
132  cancel_immediately=false;
133 }
134 
135 void CSignal::set_cancel(bool immediately)
136 {
137  cancel_computation=true;
138 
139  if (immediately)
140  cancel_immediately=true;
141 }
142 
144 {
145  clear_cancel();
146  active=false;
147  memset(&CSignal::oldsigaction, 0, sizeof(CSignal::oldsigaction));
148 }
149 #endif //WIN32
static bool set_handler()
Definition: Signal.cpp:67
static bool active
Definition: Signal.h:109
#define SG_PRINT(...)
Definition: SGIO.h:137
#define SG_SPRINT(...)
Definition: SGIO.h:180
void(* sg_print_error)(FILE *target, const char *str)
function called to print error messages
Definition: init.cpp:48
Class SGObject is the base class of all shogun objects.
Definition: SGObject.h:115
static bool cancel_computation
Definition: Signal.h:112
static void clear_cancel()
Definition: Signal.cpp:129
static bool unset_handler()
Definition: Signal.cpp:105
#define NUMTRAPPEDSIGS
Definition: Signal.h:29
static void handler(int signal)
Definition: Signal.cpp:42
static int signals[NUMTRAPPEDSIGS]
Definition: Signal.h:103
static void clear()
Definition: Signal.cpp:143
virtual ~CSignal()
Definition: Signal.cpp:36
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
Class Signal implements signal handling to e.g. allow ctrl+c to cancel a long running process...
Definition: Signal.h:48
static struct sigaction oldsigaction[NUMTRAPPEDSIGS]
Definition: Signal.h:106
static void set_cancel(bool immediately=false)
Definition: Signal.cpp:135
static bool cancel_immediately
Definition: Signal.h:115

SHOGUN Machine Learning Toolbox - Documentation