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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 997 - (hide 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 johnpye 526 /* ASCEND modelling environment
2     Copyright (C) 1997 Benjamin Andrew Allan
3     Copyright (C) 2006 Carnegie Mellon University
4 jds 54
5 johnpye 526 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 jds 54
53 johnpye 968 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 johnpye 526 Requires:
99     #include "utilities/ascConfig.h"
100     *//*
101 johnpye 953 by Benjamin Andrew Allan, May 27, 1997
102     Last in CVS: $Revision: 1.6 $ $Date: 1998/01/10 18:00:05 $ $Author: ballan $
103 johnpye 526 */
104    
105 johnpye 67 #ifndef ASC_ASCSIGNAL_H
106     #define ASC_ASCSIGNAL_H
107 jds 54
108 aw0a 1 #include <signal.h>
109     #include <setjmp.h>
110 johnpye 669 #include "utilities/ascConfig.h"
111    
112 aw0a 1 #ifdef __WIN32__
113 jds 102 # include <process.h>
114 jds 101 #else
115 jds 102 # include <unistd.h>
116 aw0a 1 #endif
117    
118 johnpye 968 #ifdef __WIN32__
119     # define FPRESET _fpreset()
120     #else
121     # define FPRESET (void)0
122     #endif
123    
124 johnpye 953 typedef void SigHandlerFn(int);
125 jds 59 /**< Signature of a signal handling function. */
126 jds 54
127 aw0a 1 #define MAX_TRAP_DEPTH 40L
128 jds 59 /**< The maximum number of traps that can be nested. */
129 aw0a 1
130 johnpye 997 #define ASC_JMP_INFO
131     /**< Whether to store additional information before making a setjmp call */
132 aw0a 1
133 johnpye 997 #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 aw0a 1 extern jmp_buf g_foreign_code_call_env;
157 jds 54 /**<
158 johnpye 953 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 johnpye 997 #endif
163 aw0a 1
164 johnpye 522 ASC_DLLSPEC(void ) Asc_SignalTrap(int sigval);
165 jds 54 /**<
166 jds 59 * Standard signal handler.
167 jds 54 * This is the trap that should be used for most applications in
168 jds 59 * 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 aw0a 1 *
173 jds 54 * 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 jds 59 *
183 aw0a 1 * For x < 0 the else is called because setjmp returns nonzero
184     * when the body of the 'if' signals range error.
185 jds 54 * </pre>
186 johnpye 485 * Remember always to use Asc_SignalHandlerPush() and
187     * Asc_SignalHandlerPop(). You can write an alternate function
188 jds 59 * to use instead of AscSignalTrap() if need be. The signals
189 jds 54 * SIGFPE, SIGINT, SIGSEGV are understood.<br><br>
190 aw0a 1 *
191 jds 59 * 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 aw0a 1 */
200    
201 johnpye 593 ASC_DLLSPEC(int ) Asc_SignalInit(void);
202 johnpye 485 /**<
203 jds 59 * Initializes the signal manager.
204 johnpye 485 * This should be called before using any of the signal handling
205 jds 59 * 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 aw0a 1 */
214    
215 johnpye 593 ASC_DLLSPEC(void ) Asc_SignalDestroy(void);
216 jds 59 /**<
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 aw0a 1 */
225    
226 johnpye 522 ASC_DLLSPEC(void ) Asc_SignalRecover(int force);
227 jds 59 /**<
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 johnpye 485 * 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 jds 59 * cheap if it does the reinstallation.<br><br>
238 aw0a 1 *
239 jds 59 * 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 johnpye 485 * installation status and want to make sure the handlers are
245 jds 59 * 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 johnpye 485 * @param force If non-zero, the most recent handlers are
251 jds 59 * reinstalled even if not required by the
252     * compiler/platform.
253 aw0a 1 */
254    
255 johnpye 968 ASC_DLLSPEC(int ) Asc_SignalHandlerPushDefault(int signum);
256 johnpye 953 ASC_DLLSPEC(int ) Asc_SignalHandlerPush(int signum, SigHandlerFn *func);
257 jds 54 /**<
258 aw0a 1 * 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 jds 54 * Pushing a NULL handler func does NOT change anything at all.
262 aw0a 1 * On a successful return, the handler has been installed and will
263 jds 59 * 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 johnpye 485 * accidentally push a NULL without realizing it, and then later
275 jds 59 * popping an unintended handler.
276 aw0a 1 */
277    
278 johnpye 968 ASC_DLLSPEC(int ) Asc_SignalHandlerPopDefault(int signum);
279 johnpye 953 ASC_DLLSPEC(int ) Asc_SignalHandlerPop(int signum, SigHandlerFn *func);
280 jds 54 /**<
281 jds 59 * Removes the last-pushed handler from the stack for signum signal types.
282 johnpye 485 * If the removed handler is the same as func, it is uninstalled and
283 jds 59 * 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 johnpye 485 * 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 jds 59 * 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 johnpye 485 * 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 jds 59 * only call Asc_SignalRecover() if it matches func.
299 aw0a 1 */
300    
301 johnpye 67 #endif /* ASC_ASCSIGNAL_H */
302 jds 54

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