Neko  0.9.99
A portable framework for high-order spectral element flow simulations
sighdl.c
Go to the documentation of this file.
1 /*
2  Copyright (c) 2021, The Neko Authors
3  All rights reserved.
4 
5  Redistribution and use in source and binary forms, with or without
6  modification, are permitted provided that the following conditions
7  are met:
8 
9  * Redistributions of source code must retain the above copyright
10  notice, this list of conditions and the following disclaimer.
11 
12  * Redistributions in binary form must reproduce the above
13  copyright notice, this list of conditions and the following
14  disclaimer in the documentation and/or other materials provided
15  with the distribution.
16 
17  * Neither the name of the authors nor the names of its
18  contributors may be used to endorse or promote products derived
19  from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  POSSIBILITY OF SUCH DAMAGE.
33 */
34 
35 #include <signal.h>
36 #include <stdint.h>
37 #include <time.h>
38 #include <sys/time.h>
39 
40 #define SIGHDL_XCPU 0x01
41 #define SIGHDL_ALRM 0x02
42 
43 #define SIGHDL_USR1 0x01
44 #define SIGHDL_USR2 0x02
45 
46 volatile uint8_t TIMEOUT = 0;
47 volatile uint8_t USR = 0;
48 
52 void sighdl(int sig_code) {
53  switch (sig_code) {
54  case SIGXCPU:
56  break;
57  case SIGALRM:
59  break;
60  case SIGUSR1:
61  USR |= SIGHDL_USR1;
62  break;
63  case SIGUSR2:
64  USR |= SIGHDL_USR2;
65  break;
66  default:
67  break;
68  }
69 }
70 
74 int8_t sighdl_timeout() {
75  return TIMEOUT;
76 }
77 
81 int8_t sighdl_usr() {
82  return USR;
83 }
84 
85 
89 int sighdl_set_timeout(int *sec) {
90  struct itimerval itv;
91  struct sigaction sig_param;
92 
93  TIMEOUT &= ~SIGHDL_ALRM;
94  sig_param.sa_handler = sighdl;
95  sigemptyset(&sig_param.sa_mask);
96  sig_param.sa_flags = SA_RESTART;
97  if (sigaction(SIGALRM, &sig_param, 0) < 0)
98  return -1;
99 
100  itv.it_value.tv_sec = (*sec);
101  itv.it_value.tv_usec = 0;
102  itv.it_interval.tv_sec = 0;
103  itv.it_interval.tv_usec = 0;
104  return setitimer(ITIMER_REAL, &itv, 0);
105 }
106 
111  struct sigaction sig_param;
112 
113  TIMEOUT &= ~SIGHDL_XCPU;
114  sig_param.sa_handler = sighdl;
115  sigemptyset(&sig_param.sa_mask);
116  sig_param.sa_flags = SA_RESTART;
117  return sigaction(SIGXCPU, &sig_param, 0);
118 }
119 
124  struct sigaction sig_param;
125 
126  USR = 0;
127  sig_param.sa_handler = sighdl;
128  sigemptyset(&sig_param.sa_mask);
129  sig_param.sa_flags = SA_RESTART;
130  if (sigaction(SIGUSR1, &sig_param, 0) < 0)
131  return -1;
132  return sigaction(SIGUSR2, &sig_param, 0);
133 }
void sighdl(int sig_code)
Definition: sighdl.c:52
#define SIGHDL_XCPU
Definition: sighdl.c:40
int sighdl_trap_cpulimit()
Definition: sighdl.c:110
volatile uint8_t TIMEOUT
Definition: sighdl.c:46
int8_t sighdl_usr()
Definition: sighdl.c:81
#define SIGHDL_USR1
Definition: sighdl.c:43
#define SIGHDL_USR2
Definition: sighdl.c:44
volatile uint8_t USR
Definition: sighdl.c:47
int sighdl_trap_usr()
Definition: sighdl.c:123
int sighdl_set_timeout(int *sec)
Definition: sighdl.c:89
int8_t sighdl_timeout()
Definition: sighdl.c:74
#define SIGHDL_ALRM
Definition: sighdl.c:41