SHOGUN  6.1.3
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  * Written (W) 2017 Giovanni De Toni
9  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
10  */
11 
12 #include <csignal>
13 #include <stdlib.h>
14 
15 #include <rxcpp/rx-lite.hpp>
16 #include <shogun/io/SGIO.h>
17 #include <shogun/lib/Signal.h>
18 
19 using namespace shogun;
20 using namespace rxcpp;
21 
22 bool CSignal::m_active = false;
24 
26  new CSignal::SGObservableS(CSignal::m_subject->get_observable());
28  new CSignal::SGSubscriberS(CSignal::m_subject->get_subscriber());
29 
31 {
32 }
33 
35 {
36 }
37 
38 void CSignal::handler(int signal)
39 {
40  /* If the handler is not enabled, then return */
41  if (!m_active)
42  return;
43 
44  if (signal == SIGINT)
45  {
46  SG_SPRINT(
47  "\n[ShogunSignalHandler] "
48  "Immediately return to prompt / "
49  "Prematurely finish computations / "
50  "Pause current computation / "
51  "Do nothing (I/C/P/D)? ")
52  char answer = getchar();
53  getchar();
54  switch (answer)
55  {
56  case 'I':
57  SG_SPRINT("[ShogunSignalHandler] Killing the application...\n");
58  m_subscriber->on_completed();
59  exit(0);
60  break;
61  case 'C':
62  SG_SPRINT(
63  "[ShogunSignalHandler] Terminating"
64  " prematurely current algorithm...\n");
65  m_subscriber->on_next(SG_BLOCK_COMP);
66  break;
67  case 'P':
68  SG_SPRINT("[ShogunSignalHandler] Pausing current computation...")
69  m_subscriber->on_next(SG_PAUSE_COMP);
70  break;
71  default:
72  SG_SPRINT("[ShogunSignalHandler] Continuing...\n")
73  break;
74  }
75  }
76  else
77  {
78  SG_SPRINT("[ShogunSignalHandler] Unknown signal %d received\n", signal)
79  }
80 }
81 
83 {
84  delete m_subject;
85  delete m_observable;
86  delete m_subscriber;
87 
88  m_subject = new rxcpp::subjects::subject<int>();
89  m_observable = new CSignal::SGObservableS(m_subject->get_observable());
90  m_subscriber = new CSignal::SGSubscriberS(m_subject->get_subscriber());
91 }
rxcpp::subscriber< int, rxcpp::observer< int, void, void, void, void > > SGSubscriberS
Definition: Signal.h:45
rxcpp::observable< int, rxcpp::dynamic_observable< int > > SGObservableS
Definition: Signal.h:42
static SGSubjectS * m_subject
Definition: Signal.h:98
#define SG_SPRINT(...)
Definition: SGIO.h:165
static SGObservableS * m_observable
Definition: Signal.h:99
static void handler(int signal)
Definition: Signal.cpp:38
static void reset_handler()
Definition: Signal.cpp:82
virtual ~CSignal()
Definition: Signal.cpp:34
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
static SGSubscriberS * m_subscriber
Definition: Signal.h:100

SHOGUN Machine Learning Toolbox - Documentation