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 |
5307
|
19 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
20 02110-1301, USA. |
4153
|
21 |
|
22 */ |
|
23 |
|
24 #if !defined (octave_quit_h) |
|
25 #define octave_quit_h 1 |
|
26 |
|
27 #ifdef __cplusplus |
4268
|
28 #include <new> |
4153
|
29 extern "C" { |
|
30 #endif |
|
31 |
4180
|
32 #include <stdio.h> |
4153
|
33 |
4180
|
34 #include <signal.h> |
4153
|
35 #include <setjmp.h> |
|
36 |
5451
|
37 #if defined (__WIN32__) && ! defined (_POSIX_VERSION) |
|
38 |
|
39 #include <windows.h> |
|
40 |
6107
|
41 CRUFT_API extern void w32_sigint_init (void); /* setup */ |
|
42 CRUFT_API extern void w32_raise_final (void); /* tear down */ |
|
43 CRUFT_API extern void w32_raise (int sig); /* raise signal in main thread */ |
|
44 CRUFT_API extern int w32_in_main_thread (void); /* return true if in main thread */ |
5451
|
45 |
|
46 #endif |
|
47 |
4180
|
48 #if defined (OCTAVE_HAVE_SIG_JUMP) |
|
49 |
|
50 typedef sigjmp_buf octave_jmp_buf; |
|
51 |
|
52 #define octave_set_current_context sigsetjmp (current_context, 1) |
|
53 |
|
54 #else |
|
55 |
|
56 typedef jmp_buf octave_jmp_buf; |
|
57 |
|
58 #define octave_set_current_context setjmp (current_context) |
|
59 |
|
60 #endif |
|
61 |
6107
|
62 CRUFT_API extern octave_jmp_buf current_context; |
4153
|
63 |
6107
|
64 CRUFT_API extern void octave_save_current_context (void *); |
4153
|
65 |
6107
|
66 CRUFT_API extern void octave_restore_current_context (void *); |
4153
|
67 |
6107
|
68 CRUFT_API extern void octave_jump_to_enclosing_context (void) GCC_ATTR_NORETURN; |
4153
|
69 |
6107
|
70 CRUFT_API extern void octave_save_signal_mask (void); |
4153
|
71 |
6107
|
72 CRUFT_API extern void octave_restore_signal_mask (void); |
4153
|
73 |
|
74 #ifdef __cplusplus |
|
75 class |
|
76 octave_interrupt_exception |
|
77 { |
|
78 }; |
|
79 #endif |
|
80 |
6107
|
81 CRUFT_API extern sig_atomic_t octave_interrupt_immediately; |
4153
|
82 |
5133
|
83 /* |
|
84 > 0: interrupt pending |
|
85 0: no interrupt pending |
|
86 < 0: handling interrupt |
|
87 */ |
6107
|
88 CRUFT_API extern sig_atomic_t octave_interrupt_state; |
4180
|
89 |
6107
|
90 CRUFT_API extern sig_atomic_t octave_allocation_error; |
4153
|
91 |
6107
|
92 CRUFT_API extern sig_atomic_t octave_signal_caught; |
5142
|
93 |
6107
|
94 CRUFT_API extern void octave_handle_signal (void); |
5142
|
95 |
6107
|
96 CRUFT_API extern void octave_throw_interrupt_exception (void) GCC_ATTR_NORETURN; |
4153
|
97 |
6107
|
98 CRUFT_API extern void octave_throw_bad_alloc (void) GCC_ATTR_NORETURN; |
4180
|
99 |
4153
|
100 #define OCTAVE_QUIT \ |
|
101 do \ |
|
102 { \ |
5142
|
103 if (octave_signal_caught) \ |
4153
|
104 { \ |
5142
|
105 octave_signal_caught = 0; \ |
|
106 octave_handle_signal (); \ |
4153
|
107 } \ |
|
108 } \ |
|
109 while (0) |
|
110 |
4302
|
111 /* Normally, you just want to use |
|
112 |
|
113 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
114 ... some code that calls a "foreign" function ... |
|
115 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
116 |
|
117 but sometimes it is useful to do something like |
|
118 |
|
119 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; |
|
120 ... custom code here, normally ending in a call to |
|
121 octave_throw_interrupt_exception ... |
|
122 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2; |
|
123 |
|
124 so that you can perform extra clean up operations before throwing |
|
125 the interrupt exception. */ |
|
126 |
4153
|
127 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \ |
4302
|
128 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \ |
|
129 octave_throw_interrupt_exception (); \ |
|
130 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 |
|
131 |
|
132 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1 \ |
4153
|
133 do \ |
|
134 { \ |
4182
|
135 octave_jmp_buf saved_context; \ |
4153
|
136 \ |
5760
|
137 octave_save_current_context (saved_context); \ |
4153
|
138 \ |
|
139 if (octave_set_current_context) \ |
|
140 { \ |
5760
|
141 octave_restore_current_context (saved_context) |
4302
|
142 |
|
143 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 \ |
4153
|
144 } \ |
|
145 else \ |
|
146 { \ |
4182
|
147 octave_interrupt_immediately++ |
4153
|
148 |
|
149 #define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \ |
4182
|
150 octave_interrupt_immediately--; \ |
5760
|
151 octave_restore_current_context (saved_context); \ |
4153
|
152 } \ |
|
153 } \ |
|
154 while (0) |
|
155 |
4196
|
156 #ifdef __cplusplus |
|
157 |
4180
|
158 #define BEGIN_INTERRUPT_WITH_EXCEPTIONS \ |
4182
|
159 sig_atomic_t saved_octave_interrupt_immediately = octave_interrupt_immediately; \ |
4180
|
160 \ |
4182
|
161 try \ |
4180
|
162 { \ |
4182
|
163 octave_interrupt_immediately = 0; |
4180
|
164 |
|
165 #define END_INTERRUPT_WITH_EXCEPTIONS \ |
|
166 } \ |
4182
|
167 catch (octave_interrupt_exception) \ |
4180
|
168 { \ |
4182
|
169 octave_interrupt_immediately = saved_octave_interrupt_immediately; \ |
4180
|
170 octave_jump_to_enclosing_context (); \ |
|
171 } \ |
4181
|
172 catch (std::bad_alloc) \ |
4180
|
173 { \ |
4182
|
174 octave_interrupt_immediately = saved_octave_interrupt_immediately; \ |
4180
|
175 octave_allocation_error = 1; \ |
|
176 octave_jump_to_enclosing_context (); \ |
|
177 } \ |
|
178 \ |
4182
|
179 octave_interrupt_immediately = saved_octave_interrupt_immediately |
4196
|
180 #endif |
4153
|
181 |
|
182 #ifdef __cplusplus |
|
183 } |
4560
|
184 |
4572
|
185 /* These should only be declared for C++ code, and should also be |
|
186 outside of any extern "C" block. */ |
4560
|
187 |
6107
|
188 extern CRUFT_API void (*octave_signal_hook) (void); |
|
189 extern CRUFT_API void (*octave_interrupt_hook) (void); |
|
190 extern CRUFT_API void (*octave_bad_alloc_hook) (void); |
4560
|
191 |
4153
|
192 #endif |
|
193 |
|
194 #endif |
|
195 |
|
196 /* |
|
197 ;;; Local Variables: *** |
|
198 ;;; mode: C++ *** |
|
199 ;;; End: *** |
|
200 */ |