-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Signal.cpp
125 lines (106 loc) · 2.67 KB
/
Signal.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 1999-2009 Soeren Sonnenburg
* Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
*/
#include <shogun/lib/config.h>
#include <stdlib.h>
#include <shogun/io/SGIO.h>
#include <shogun/lib/Signal.h>
#include <shogun/base/init.h>
#include <rxcpp/rx-includes.hpp>
#include <rxcpp/rx.hpp>
using namespace shogun;
using namespace rxcpp;
int CSignal::signals[NUMTRAPPEDSIGS]={SIGINT, SIGURG};
struct sigaction CSignal::oldsigaction[NUMTRAPPEDSIGS];
rxcpp::connectable_observable<int> CSignal::m_sigint_observable = rxcpp::observable<>::create<int>(
[](rxcpp::subscriber<int> s){
s.on_completed();
}
).publish();
rxcpp::connectable_observable<int> CSignal::m_sigurg_observable = rxcpp::observable<>::create<int>(
[](rxcpp::subscriber<int> s){
s.on_next(1);
}
).publish();
CSignal::CSignal()
: CSGObject()
{
// Set if the signal handler is active or not
m_active = true;
}
CSignal::CSignal(bool active)
: CSGObject()
{
// Set if the signal handler is active or not
m_active = active;
}
CSignal::~CSignal()
{
}
rxcpp::connectable_observable<int> CSignal::get_SIGINT_observable()
{
return m_sigint_observable;
}
rxcpp::connectable_observable<int> CSignal::get_SIGURG_observable()
{
return m_sigurg_observable;
}
void CSignal::handler(int signal)
{
if (signal == SIGINT)
{
//SG_SPRINT("\nImmediately return to prompt / Prematurely finish computations / Do nothing (I/P/D)? ")
//char answer=fgetc(stdin);
/*switch (answer){
case 'I':
m_sigint_observable.connect();
break;
case 'P':
m_sigurg_observable.connect();
break;
default:
SG_SPRINT("Continuing...\n")
break;
}*/
SG_SPRINT("Killing the application...\n");
m_sigint_observable.connect();
}
else if (signal == SIGURG)
m_sigurg_observable.connect();
else
SG_SPRINT("unknown signal %d received\n", signal)
}
#if defined(__MINGW64__) || defined(_MSC_VER) || defined(__MINGW32__)
#define SIGBAD(signo) ( (signo) <=0 || (signo) >=NSIG)
Sigfunc *handlers[NSIG]={0};
int sigaddset(sigset_t *set, int signo)
{
if (SIGBAD(signo)) {
errno = EINVAL;
return -1;
}
*set |= 1 << (signo-1);
return 0;
}
int sigaction(int signo, const struct sigaction *act, struct sigaction *oact)
{
if (SIGBAD(signo)) {
errno = EINVAL;
return -1;
}
if(oact){
oact->sa_handler = handlers[signo];
oact->sa_mask = 0;
oact->sa_flags =0;
}
if (act)
handlers[signo]=act->sa_handler;
return 0;
}
#endif