4153
|
1 /* |
|
2 |
|
3 Copyright (C) 2002 John W. Eaton |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
20 |
|
21 */ |
|
22 |
|
23 #if !defined (octave_quit_h) |
|
24 #define octave_quit_h 1 |
|
25 |
|
26 #ifdef __cplusplus |
4268
|
27 #include <new> |
4153
|
28 extern "C" { |
|
29 #endif |
|
30 |
4180
|
31 #include <stdio.h> |
4153
|
32 |
4180
|
33 #include <signal.h> |
4153
|
34 #include <setjmp.h> |
|
35 |
4180
|
36 #if defined (OCTAVE_HAVE_SIG_JUMP) |
|
37 |
|
38 typedef sigjmp_buf octave_jmp_buf; |
|
39 |
|
40 #define octave_set_current_context sigsetjmp (current_context, 1) |
|
41 |
|
42 #else |
|
43 |
|
44 typedef jmp_buf octave_jmp_buf; |
|
45 |
|
46 #define octave_set_current_context setjmp (current_context) |
|
47 |
|
48 #endif |
|
49 |
|
50 extern octave_jmp_buf current_context; |
4153
|
51 |
|
52 extern void octave_save_current_context (void *); |
|
53 |
|
54 extern void octave_restore_current_context (void *); |
|
55 |
|
56 extern void octave_jump_to_enclosing_context (void) GCC_ATTR_NORETURN; |
|
57 |
|
58 extern void octave_save_signal_mask (void); |
|
59 |
|
60 extern void octave_restore_signal_mask (void); |
|
61 |
|
62 #ifdef __cplusplus |
|
63 class |
|
64 octave_interrupt_exception |
|
65 { |
|
66 }; |
|
67 #endif |
|
68 |
4180
|
69 extern sig_atomic_t octave_interrupt_immediately; |
4153
|
70 |
5133
|
71 /* |
|
72 > 0: interrupt pending |
|
73 0: no interrupt pending |
|
74 < 0: handling interrupt |
|
75 */ |
4180
|
76 extern sig_atomic_t octave_interrupt_state; |
|
77 |
|
78 extern sig_atomic_t octave_allocation_error; |
4153
|
79 |
|
80 extern void octave_throw_interrupt_exception (void) GCC_ATTR_NORETURN; |
|
81 |
4180
|
82 extern void octave_throw_bad_alloc (void) GCC_ATTR_NORETURN; |
|
83 |
4153
|
84 #define OCTAVE_QUIT \ |
|
85 do \ |
|
86 { \ |
4793
|
87 if (octave_interrupt_state > 0) \ |
4153
|
88 { \ |
4793
|
89 octave_interrupt_state = -1; \ |
4153
|
90 octave_throw_interrupt_exception (); \ |
|
91 } \ |
|
92 } \ |
|
93 while (0) |
|
94 |
4302
|
95 /* Normally, you just want to use |
|
96 |
|
97 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
98 ... some code that calls a "foreign" function ... |
|
99 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
100 |
|
101 but sometimes it is useful to do something like |
|
102 |
|
103 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; |
|
104 ... custom code here, normally ending in a call to |
|
105 octave_throw_interrupt_exception ... |
|
106 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2; |
|
107 |
|
108 so that you can perform extra clean up operations before throwing |
|
109 the interrupt exception. */ |
|
110 |
4153
|
111 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \ |
4302
|
112 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \ |
|
113 octave_throw_interrupt_exception (); \ |
|
114 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 |
|
115 |
|
116 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1 \ |
4153
|
117 do \ |
|
118 { \ |
4182
|
119 octave_jmp_buf saved_context; \ |
4153
|
120 \ |
|
121 octave_save_current_context ((char *) saved_context); \ |
|
122 \ |
|
123 if (octave_set_current_context) \ |
|
124 { \ |
4302
|
125 octave_restore_current_context ((char *) saved_context) |
|
126 |
|
127 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 \ |
4153
|
128 } \ |
|
129 else \ |
|
130 { \ |
4182
|
131 octave_interrupt_immediately++ |
4153
|
132 |
|
133 #define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \ |
4182
|
134 octave_interrupt_immediately--; \ |
4153
|
135 octave_restore_current_context ((char *) saved_context); \ |
|
136 } \ |
|
137 } \ |
|
138 while (0) |
|
139 |
4196
|
140 #ifdef __cplusplus |
|
141 |
4180
|
142 #define BEGIN_INTERRUPT_WITH_EXCEPTIONS \ |
4182
|
143 sig_atomic_t saved_octave_interrupt_immediately = octave_interrupt_immediately; \ |
4180
|
144 \ |
4182
|
145 try \ |
4180
|
146 { \ |
4182
|
147 octave_interrupt_immediately = 0; |
4180
|
148 |
|
149 #define END_INTERRUPT_WITH_EXCEPTIONS \ |
|
150 } \ |
4182
|
151 catch (octave_interrupt_exception) \ |
4180
|
152 { \ |
4182
|
153 octave_interrupt_immediately = saved_octave_interrupt_immediately; \ |
4180
|
154 octave_jump_to_enclosing_context (); \ |
|
155 } \ |
4181
|
156 catch (std::bad_alloc) \ |
4180
|
157 { \ |
4182
|
158 octave_interrupt_immediately = saved_octave_interrupt_immediately; \ |
4180
|
159 octave_allocation_error = 1; \ |
|
160 octave_jump_to_enclosing_context (); \ |
|
161 } \ |
|
162 \ |
4182
|
163 octave_interrupt_immediately = saved_octave_interrupt_immediately |
4196
|
164 #endif |
4153
|
165 |
|
166 #ifdef __cplusplus |
|
167 } |
4560
|
168 |
4572
|
169 /* These should only be declared for C++ code, and should also be |
|
170 outside of any extern "C" block. */ |
4560
|
171 |
|
172 extern void (*octave_interrupt_hook) (void); |
|
173 extern void (*octave_bad_alloc_hook) (void); |
|
174 |
4153
|
175 #endif |
|
176 |
|
177 #endif |
|
178 |
|
179 /* |
|
180 ;;; Local Variables: *** |
|
181 ;;; mode: C++ *** |
|
182 ;;; End: *** |
|
183 */ |