blob: a1fe2eacde573155f0628b6468a60b2c61d09117 [file] [log] [blame]
Austin Schuh9049e202022-02-20 17:34:16 -08001/*
2 * Implements signal handling (ctrl-c) for OSQP.
3 *
4 * Under Windows, we use SetConsoleCtrlHandler.
5 * Under Unix systems, we use sigaction.
6 * For Mex files, we use utSetInterruptEnabled/utIsInterruptPending.
7 *
8 */
9
10#include "ctrlc.h"
11
12#if defined MATLAB
13
14static int istate;
15
16void osqp_start_interrupt_listener(void) {
17 istate = utSetInterruptEnabled(1);
18}
19
20void osqp_end_interrupt_listener(void) {
21 utSetInterruptEnabled(istate);
22}
23
24int osqp_is_interrupted(void) {
25 return utIsInterruptPending();
26}
27
28#elif defined IS_WINDOWS
29
30static int int_detected;
31static BOOL WINAPI handle_ctrlc(DWORD dwCtrlType) {
32 if (dwCtrlType != CTRL_C_EVENT) return FALSE;
33
34 int_detected = 1;
35 return TRUE;
36}
37
38void osqp_start_interrupt_listener(void) {
39 int_detected = 0;
40 SetConsoleCtrlHandler(handle_ctrlc, TRUE);
41}
42
43void osqp_end_interrupt_listener(void) {
44 SetConsoleCtrlHandler(handle_ctrlc, FALSE);
45}
46
47int osqp_is_interrupted(void) {
48 return int_detected;
49}
50
51#else /* Unix */
52
53# include <signal.h>
54static int int_detected;
55struct sigaction oact;
56static void handle_ctrlc(int dummy) {
57 int_detected = dummy ? dummy : -1;
58}
59
60void osqp_start_interrupt_listener(void) {
61 struct sigaction act;
62
63 int_detected = 0;
64 act.sa_flags = 0;
65 sigemptyset(&act.sa_mask);
66 act.sa_handler = handle_ctrlc;
67 sigaction(SIGINT, &act, &oact);
68}
69
70void osqp_end_interrupt_listener(void) {
71 struct sigaction act;
72
73 sigaction(SIGINT, &oact, &act);
74}
75
76int osqp_is_interrupted(void) {
77 return int_detected;
78}
79
80#endif /* END IF IS_MATLAB / WINDOWS */