/[ascend]/trunk/base/generic/utilities/ascSignal.h
ViewVC logotype

Contents of /trunk/base/generic/utilities/ascSignal.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 997 - (show annotations) (download) (as text)
Sun Dec 24 01:33:59 2006 UTC (15 years, 7 months ago) by johnpye
File MIME type: text/x-chdr
File size: 12126 byte(s)
Refactored tests into a shared library of tests and a executable of
just the CUnit driver. This will allow the CUnit test suite to be executed via
python unittest as part of the growing Python-based test suite.
Renamed setjmp and longjmp to SETJMP and LONGJMP throughout ASCEND, to allow
some debugging output to be added at each call.
1 /* ASCEND modelling environment
2 Copyright (C) 1997 Benjamin Andrew Allan
3 Copyright (C) 2006 Carnegie Mellon University
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19 *//**
20 @file
21 Signal handling protocol definitions for ASCEND.
22
23 This file standardizes the handling of signals because some OS
24 reset signals to SIG_DFL when a trap goes off while others
25 process the signal but leave the trapping function in place.
26 We want the second behavior and this gives it to us.
27
28 This module implements limited support for managing signal handlers.
29 This includes:
30 - a standard signal handler - Asc_SignalTrap()
31 - global jmp_buf's for use with Asc_SignalTrap()
32 - functions for managing nested signal handlers
33
34 The following signal types are currently supported:
35 - SIGFPE - floating point exception
36 - SIGINT - CTRL-C interactive attention request
37 - SIGSEGV - segmentation fault
38
39 A simple use of these facilities to trap floating point exceptions
40 might be as follows:
41 <pre>
42 Asc_SignalInit();
43 Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap);
44 if (setjmp(g_fpe_env)==0) {
45 y = sqrt(x);
46 } else {
47 y = sqrt(-x);
48 }
49 Asc_SignHandlerPop(SIGFPE,Asc_SignalTrap);
50 Asc_SignalDestroy();
51 </pre>
52
53 This example uses the built-in signal handler Asc_SignalTrap()
54 and the global <code>jmp_buf</code> g_fpe_env. After initializing
55 the signal manager and registering the handler, <code>setjmp</code>
56 is used to select normal and exception paths. The <code>setjmp</code>
57 returns 0 when initially called and the sqrt(x) is calculated. If
58 x is negative, a SIGFPE exception occurs and the handler is called. It
59 uses <code>lngjmp</code> and returns to the if statement, and now
60 'setjmp' returns non-zero and the <code>else</code> clause is executed.
61 Finally, the handler is removed and the signal manager cleaned up.<br><br>
62
63 The stack mechanism also allows nested handlers to be registered. It is
64 important to note that nested handlers for the same signal type cannot
65 both use Asc_SignalTrap() as the handler. This is because different
66 <code>jmp_buf</code> variables must be used and Asc_SignalTrap() uses
67 the same global <code>jmp_buf</code> each time. However, you can use
68 custome <code>jmp_buf</code>'s and handlers:
69
70 <pre>
71 Asc_SignalInit();
72 Asc_SignalHandlerPush(SIGFPE, Asc_SignalTrap);
73 if (setjmp(g_fpe_env) == 0) {
74 y = sqrt(x);
75 Asc_SignalHandlerPush(SIGFPE, my_handler);
76 if (setjmp(my_jmp_buf) == 0) {
77 y = z/x;
78 } else {
79 Asc_Panic(1, NULL, "Div by zero error.");
80 }
81 Asc_SignHandlerPop(SIGFPE, my_handler);
82 } else {
83 y = sqrt(-x);
84 }
85 Asc_SignHandlerPop(SIGFPE,Asc_SignalTrap);
86 Asc_SignalDestroy();
87 </pre>
88
89 Here, exceptions in the sqrt(x) calculation are handled by the standard
90 Asc_SignalTrap(), while the division is handled by my_handler.<br><br>
91
92 Avoid mixing use of the signal manager with direct calls to signal().
93 Once Asc_SignalInit() has been called, use of signal() directly is likely
94 to be lost or to corrupt the managed handlers.<br><br>
95
96 Another warning: setjmp is expensive if called inside a fast loop.
97
98 Requires:
99 #include "utilities/ascConfig.h"
100 *//*
101 by Benjamin Andrew Allan, May 27, 1997
102 Last in CVS: $Revision: 1.6 $ $Date: 1998/01/10 18:00:05 $ $Author: ballan $
103 */
104
105 #ifndef ASC_ASCSIGNAL_H
106 #define ASC_ASCSIGNAL_H
107
108 #include <signal.h>
109 #include <setjmp.h>
110 #include "utilities/ascConfig.h"
111
112 #ifdef __WIN32__
113 # include <process.h>
114 #else
115 # include <unistd.h>
116 #endif
117
118 #ifdef __WIN32__
119 # define FPRESET _fpreset()
120 #else
121 # define FPRESET (void)0
122 #endif
123
124 typedef void SigHandlerFn(int);
125 /**< Signature of a signal handling function. */
126
127 #define MAX_TRAP_DEPTH 40L
128 /**< The maximum number of traps that can be nested. */
129
130 #define ASC_JMP_INFO
131 /**< Whether to store additional information before making a setjmp call */
132
133 #ifndef ASC_JMP_INFO
134 # define SETJMP set_jmp
135 # define LONGJMP longjmp
136 typedef JMP_BUF jmp_buf
137 #else
138 # define SETJMP(ENV) (ENV.filename = __FILE__, ENV.line = __LINE__, ENV.func = __FUNCTION__, setjmp(ENV.jmp))
139 # define LONGJMP(ENV,VAL) (CONSOLE_DEBUG("LONGJMP back to %s:%d (%s)",ENV.filename,ENV.line,ENV.func), longjmp(ENV.jmp, VAL))
140 typedef struct{
141 jmp_buf jmp;
142 const char *filename;
143 int line;
144 const char *func;
145 } asc_jmp_buf;
146 #define JMP_BUF asc_jmp_buf
147 #endif
148
149
150
151 ASC_DLLSPEC(JMP_BUF) g_fpe_env; /**< Standard signal jmp_buf - floating point error. */
152 ASC_DLLSPEC(JMP_BUF) g_seg_env; /**< Standard signal jmp_buf - segmentation fault. */
153 ASC_DLLSPEC(JMP_BUF) g_int_env; /**< Standard signal jmp_buf - interactive attention (<CTRL>C). */
154
155 #if 0
156 extern jmp_buf g_foreign_code_call_env;
157 /**<
158 Not currently in use. Should be when we get to a unified
159 standard for signal handling.
160 @todo Implement use of g_foreign_code_call_env?
161 */
162 #endif
163
164 ASC_DLLSPEC(void ) Asc_SignalTrap(int sigval);
165 /**<
166 * Standard signal handler.
167 * This is the trap that should be used for most applications in
168 * ASCEND. It prints a message then calls longjmp(GLOBAL, sigval)
169 * where GLOBAL is one of g_fpe_env, g_seg_env, or g_int_env.
170 * Because the jmp_buf is global, so you can't nest calls to
171 * setjmp where both use this trap function.<br><br>
172 *
173 * Trivial Example:
174 * <pre>
175 * Asc_SignalHandlerPush(SIGFPE,Asc_SignalTrap);
176 * if (setjmp(g_fpe_env)==0) {
177 * y = sqrt(x);
178 * } else {
179 * y = sqrt(-x);
180 * }
181 * Asc_SignHandlerPop(SIGFPE,Asc_SignalTrap);
182 *
183 * For x < 0 the else is called because setjmp returns nonzero
184 * when the body of the 'if' signals range error.
185 * </pre>
186 * Remember always to use Asc_SignalHandlerPush() and
187 * Asc_SignalHandlerPop(). You can write an alternate function
188 * to use instead of AscSignalTrap() if need be. The signals
189 * SIGFPE, SIGINT, SIGSEGV are understood.<br><br>
190 *
191 * Note - this handler does not reinstall itself. After an exception,
192 * you need to reinstall the handler (if desired) using
193 * Asc_SignalRecover().
194 *
195 * @todo Should utilities/ascSignal.c:Asc_SignalTrap() reinstall itself
196 * after it catches an expection using Asc_SignalRecover()?
197 * @param sigval Holds the signal type code when called during
198 * an exception.
199 */
200
201 ASC_DLLSPEC(int ) Asc_SignalInit(void);
202 /**<
203 * Initializes the signal manager.
204 * This should be called before using any of the signal handling
205 * functions in this module. It initializes the internal stacks
206 * for mangaging signal handlers. This function does not install
207 * any signal handlers (although any existing handlers are left
208 * in place). Calling this function more than once will have no
209 * effect and an error code will be returned.<br><br>
210 *
211 * @return Returns 0 if successful, 1 if memory could not be
212 * allocated, and 2 if an error occurred.
213 */
214
215 ASC_DLLSPEC(void ) Asc_SignalDestroy(void);
216 /**<
217 * Cleans up and destroys the stacks of signal handlers.
218 * It does not change the status of any registered signal handlers
219 * That is, any handlers registered when this function is called
220 * will still be registered. It is important to call
221 * Asc_SignalHandlerPop() for each occurrence of Asc_SignalHandlerPush()
222 * before calling this function. Otherwise, any signal handlers
223 * that were installed before Asc_SignalInit() was called will be lost.
224 */
225
226 ASC_DLLSPEC(void ) Asc_SignalRecover(int force);
227 /**<
228 * Reinstalls the most recently pushed handler that has been
229 * installed for each supported signal type. This should be called
230 * after every trapped exception and at any other time when the
231 * status of exception handlers may have become not well-defined.
232 * If no handler has been pushed for a given signal type, SIG_DFL is
233 * installed. Note that the standard handler function Asc_SignalTrap()
234 * does not call this function. If you use the standard handler and
235 * you want it reinstalled after an exception, be sure to call this
236 * function after the longjmp return. This call is not particularly
237 * cheap if it does the reinstallation.<br><br>
238 *
239 * This module tests on startup for whether the OS reverts to
240 * SIG_DFL when a trap function is called. If it does NOT then
241 * this function will simply return unless force != 0. You don't
242 * want to call this function with force == 1 normally after a
243 * caught exception. However, if you're not sure of the handler
244 * installation status and want to make sure the handlers are
245 * installed, call with force == 1. Also, gdb or other
246 * debuggers which intercept and screw up signals may require
247 * applying force (manually) to ensure that the signals get
248 * reinstalled.
249 *
250 * @param force If non-zero, the most recent handlers are
251 * reinstalled even if not required by the
252 * compiler/platform.
253 */
254
255 ASC_DLLSPEC(int ) Asc_SignalHandlerPushDefault(int signum);
256 ASC_DLLSPEC(int ) Asc_SignalHandlerPush(int signum, SigHandlerFn *func);
257 /**<
258 * Adds a handler to the stack of signal handlers for the given signal.
259 * There is a maximum stack limit, so returns 1 if limit exceeded.
260 * Returns -1 if stack of signal requested does not exist.
261 * Pushing a NULL handler func does NOT change anything at all.
262 * On a successful return, the handler has been installed and will
263 * remain installed until a Asc_SignalHandlerPop() or another push.
264 * The handler will remain installed as long as Asc_SignalRecover()
265 * is used properly after every exception.
266 *
267 * @param signum The signal type that func should handle.
268 * @param func The signal handler to register for signum signal types.
269 * @return Returns 1 if the stack limit is exceeded, -1 if managing
270 * of signals for the specified signum is not supported or
271 * initialized, or 0 if the function completes successfully.
272 * @todo Shouldn't utilities/ascSignal.c:Asc_SignalHandlerPush() return
273 * an error code on a NULL func? It seems too easy for someone to
274 * accidentally push a NULL without realizing it, and then later
275 * popping an unintended handler.
276 */
277
278 ASC_DLLSPEC(int ) Asc_SignalHandlerPopDefault(int signum);
279 ASC_DLLSPEC(int ) Asc_SignalHandlerPop(int signum, SigHandlerFn *func);
280 /**<
281 * Removes the last-pushed handler from the stack for signum signal types.
282 * If the removed handler is the same as func, it is uninstalled and
283 * replaced with the handler now at the top of the stack. If not, non-zero
284 * is returned and you need to call Asc_SignalRecover() to uninstall the
285 * current handler if desired. Note that the top handler is popped off
286 * the stack whether it matches func or not. Non-zero is also returned if
287 * the stack is empty. A side effect is that all managed signal types will
288 * have the registered handlers reinstalled.
289 *
290 * @param signum The signal type whose top-most handler should be replaced.
291 * @param func The handler function that should be at the top of the
292 * stack (and currently installed) for signals of type signum.
293 * @return Returns non-zero if func is not the replaced handler or if
294 * the stack is empty, 0 if the function completed successfully.
295 * @todo Does it make more sense for utilities/ascSignal.c:Asc_SignalHanderPop()
296 * to fail completely if func is not the top-most handler? It is not
297 * clear why the function should pop the top handler no matter what, but
298 * only call Asc_SignalRecover() if it matches func.
299 */
300
301 #endif /* ASC_ASCSIGNAL_H */
302

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22