SHOGUN  v2.0.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 
22 using namespace shogun;
23 
24 int CSignal::signals[NUMTRAPPEDSIGS]={SIGINT, SIGURG};
25 struct sigaction CSignal::oldsigaction[NUMTRAPPEDSIGS];
26 bool CSignal::active=false;
29 
31 : CSGObject()
32 {
33 }
34 
36 {
37  if (!unset_handler())
38  SG_PRINT("error uninitalizing signal handler\n");
39 }
40 
41 void CSignal::handler(int signal)
42 {
43  if (signal == SIGINT)
44  {
45  SG_SPRINT("\nImmediately return to prompt / Prematurely finish computations / Do nothing (I/P/D)? ");
46  char answer=fgetc(stdin);
47 
48  if (answer == 'I')
49  {
50  unset_handler();
51  set_cancel(true);
52  if (sg_print_error)
53  sg_print_error(stdout, "sg stopped by SIGINT\n");
54  }
55  else if (answer == 'P')
56  set_cancel();
57  else
58  SG_SPRINT("Continuing...\n");
59  }
60  else if (signal == SIGURG)
61  set_cancel();
62  else
63  SG_SPRINT("unknown signal %d received\n", signal);
64 }
65 
67 {
68  if (!active)
69  {
70  struct sigaction act;
71  sigset_t st;
72 
73  sigemptyset(&st);
74  for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
75  sigaddset(&st, signals[i]);
76 
77 #ifndef __INTERIX
78  act.sa_sigaction=NULL; //just in case
79 #endif
80  act.sa_handler=CSignal::handler;
81  act.sa_mask = st;
82  act.sa_flags = 0;
83 
84  for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
85  {
86  if (sigaction(signals[i], &act, &oldsigaction[i]))
87  {
88  SG_SPRINT("Error trapping signals!\n");
89  for (int32_t j=i-1; j>=0; j--)
90  sigaction(signals[i], &oldsigaction[i], NULL);
91 
92  clear();
93  return false;
94  }
95  }
96 
97  active=true;
98  return true;
99  }
100  else
101  return false;
102 }
103 
105 {
106  if (active)
107  {
108  bool result=true;
109 
110  for (int32_t i=0; i<NUMTRAPPEDSIGS; i++)
111  {
112  if (sigaction(signals[i], &oldsigaction[i], NULL))
113  {
114  SG_SPRINT("error uninitalizing signal handler for signal %d\n", signals[i]);
115  result=false;
116  }
117  }
118 
119  if (result)
120  clear();
121 
122  return result;
123  }
124  else
125  return false;
126 }
127 
129 {
130  cancel_computation=false;
131  cancel_immediately=false;
132 }
133 
134 void CSignal::set_cancel(bool immediately)
135 {
136  cancel_computation=true;
137 
138  if (immediately)
139  cancel_immediately=true;
140 }
141 
143 {
144  clear_cancel();
145  active=false;
146  memset(&CSignal::oldsigaction, 0, sizeof(CSignal::oldsigaction));
147 }
148 #endif //WIN32

SHOGUN Machine Learning Toolbox - Documentation