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