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 |
4793
|
71 // > 0: interrupt pending |
|
72 // 0: no interrupt pending |
|
73 // < 0: handling interrupt |
|
74 // |
4180
|
75 extern sig_atomic_t octave_interrupt_state; |
|
76 |
|
77 extern sig_atomic_t octave_allocation_error; |
4153
|
78 |
|
79 extern void octave_throw_interrupt_exception (void) GCC_ATTR_NORETURN; |
|
80 |
4180
|
81 extern void octave_throw_bad_alloc (void) GCC_ATTR_NORETURN; |
|
82 |
4153
|
83 #define OCTAVE_QUIT \ |
|
84 do \ |
|
85 { \ |
4793
|
86 if (octave_interrupt_state > 0) \ |
4153
|
87 { \ |
4793
|
88 octave_interrupt_state = -1; \ |
4153
|
89 octave_throw_interrupt_exception (); \ |
|
90 } \ |
|
91 } \ |
|
92 while (0) |
|
93 |
4302
|
94 /* Normally, you just want to use |
|
95 |
|
96 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
97 ... some code that calls a "foreign" function ... |
|
98 END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE; |
|
99 |
|
100 but sometimes it is useful to do something like |
|
101 |
|
102 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; |
|
103 ... custom code here, normally ending in a call to |
|
104 octave_throw_interrupt_exception ... |
|
105 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2; |
|
106 |
|
107 so that you can perform extra clean up operations before throwing |
|
108 the interrupt exception. */ |
|
109 |
4153
|
110 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \ |
4302
|
111 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \ |
|
112 octave_throw_interrupt_exception (); \ |
|
113 BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 |
|
114 |
|
115 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1 \ |
4153
|
116 do \ |
|
117 { \ |
4182
|
118 octave_jmp_buf saved_context; \ |
4153
|
119 \ |
|
120 octave_save_current_context ((char *) saved_context); \ |
|
121 \ |
|
122 if (octave_set_current_context) \ |
|
123 { \ |
4302
|
124 octave_restore_current_context ((char *) saved_context) |
|
125 |
|
126 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 \ |
4153
|
127 } \ |
|
128 else \ |
|
129 { \ |
4182
|
130 octave_interrupt_immediately++ |
4153
|
131 |
|
132 #define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \ |
4182
|
133 octave_interrupt_immediately--; \ |
4153
|
134 octave_restore_current_context ((char *) saved_context); \ |
|
135 } \ |
|
136 } \ |
|
137 while (0) |
|
138 |
4196
|
139 #ifdef __cplusplus |
|
140 |
4180
|
141 #define BEGIN_INTERRUPT_WITH_EXCEPTIONS \ |
4182
|
142 sig_atomic_t saved_octave_interrupt_immediately = octave_interrupt_immediately; \ |
4180
|
143 \ |
4182
|
144 try \ |
4180
|
145 { \ |
4182
|
146 octave_interrupt_immediately = 0; |
4180
|
147 |
|
148 #define END_INTERRUPT_WITH_EXCEPTIONS \ |
|
149 } \ |
4182
|
150 catch (octave_interrupt_exception) \ |
4180
|
151 { \ |
4182
|
152 octave_interrupt_immediately = saved_octave_interrupt_immediately; \ |
4180
|
153 octave_jump_to_enclosing_context (); \ |
|
154 } \ |
4181
|
155 catch (std::bad_alloc) \ |
4180
|
156 { \ |
4182
|
157 octave_interrupt_immediately = saved_octave_interrupt_immediately; \ |
4180
|
158 octave_allocation_error = 1; \ |
|
159 octave_jump_to_enclosing_context (); \ |
|
160 } \ |
|
161 \ |
4182
|
162 octave_interrupt_immediately = saved_octave_interrupt_immediately |
4196
|
163 #endif |
4153
|
164 |
|
165 #ifdef __cplusplus |
|
166 } |
4560
|
167 |
4572
|
168 /* These should only be declared for C++ code, and should also be |
|
169 outside of any extern "C" block. */ |
4560
|
170 |
|
171 extern void (*octave_interrupt_hook) (void); |
|
172 extern void (*octave_bad_alloc_hook) (void); |
|
173 |
4153
|
174 #endif |
|
175 |
|
176 #endif |
|
177 |
|
178 /* |
|
179 ;;; Local Variables: *** |
|
180 ;;; mode: C++ *** |
|
181 ;;; End: *** |
|
182 */ |