-
-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Signal.h
125 lines (104 loc) · 3.15 KB
/
Signal.h
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
*/
#ifndef __SIGNAL__H_
#define __SIGNAL__H_
#include <rxcpp/rx-includes.hpp>
#include <shogun/lib/config.h>
#if defined(__MINGW64__) || defined(_MSC_VER)
typedef unsigned long sigset_t;
#endif
#if defined(__MINGW32__) && !defined(__MINGW64__)
typedef int sigset_t;
#endif
#ifndef SIGURG
#define SIGURG -16
#endif
#if defined(__MINGW64__) || defined(_MSC_VER) || defined(__MINGW32__)
typedef void Sigfunc (int);
struct sigaction {
Sigfunc *sa_handler;
sigset_t sa_mask;
int sa_flags;
};
#define sigemptyset(ptr) (*(ptr) = 0)
#define sigfillset(ptr) ( *(ptr) = ~(sigset_t)0,0)
int sigaddset(sigset_t*, int);
int sigaction(int signo, const struct sigaction *act, struct sigaction *oact);
#endif
#ifndef DISABLE_CANCEL_CALLBACK
namespace shogun
{
extern void (*sg_cancel_computations)(bool &delayed, bool &immediately);
}
#endif
#include <shogun/lib/ShogunException.h>
#include <shogun/base/SGObject.h>
#include <csignal>
#define NUMTRAPPEDSIGS 2
namespace shogun
{
/** @brief Class Signal implements signal handling to e.g. allow ctrl+c to cancel a
* long running process.
*
* This is done in two ways:
*
* -# A signal handler is attached to trap the SIGINT and SIGURG signal.
* Pressing ctrl+c or sending the SIGINT (kill ...) signal to the shogun
* process will make shogun print a message asking to immediately exit the
* running method and to fall back to the command line.
* -# When an URG signal is received or ctrl+c P is pressed shogun will
* prematurely stop a method and continue execution. For example when an SVM
* solver takes a long time without progressing much, one might still be
* interested in the result and should thus send SIGURG or interactively
* prematurely stop the method
*/
class CSignal : public CSGObject
{
public:
CSignal();
CSignal(bool active);
virtual ~CSignal();
/** Signal handler. Need to be registered with std::signal.
*
* @param signal signal number
*/
static void handler(int signal);
/**
* Get SIGINT observable
* @return observable
*/
rxcpp::connectable_observable<int> get_SIGINT_observable();
/**
* Get SIGURG observable
* @ return observable
*/
rxcpp::connectable_observable<int> get_SIGURG_observable();
/** cancel computations
*
* @return if computations should be cancelled
*/
static inline bool cancel_computations()
{
return false;
}
/** @return object name */
virtual const char* get_name() const { return "Signal"; }
private:
/** signals; handling external lib */
static int signals[NUMTRAPPEDSIGS];
/** signal actions */
static struct sigaction oldsigaction[NUMTRAPPEDSIGS];
/** active signal */
bool m_active;
static rxcpp::connectable_observable<int> m_sigint_observable;
static rxcpp::connectable_observable<int> m_sigurg_observable;
};
}
#endif // __SIGNAL__H_