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