GNU Octave  4.2.1
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
quit.h
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 2002-2017 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 3 of the License, or (at your
10 option) any 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, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #if ! defined (octave_quit_h)
24 #define octave_quit_h 1
25 
26 #include "octave-config.h"
27 
28 #include <setjmp.h>
29 
30 /* The signal header is just needed for the sig_atomic_t type. */
31 #if defined (__cplusplus)
32 # include <csignal>
33 # include <string>
34 extern "C" {
35 #else
36 # include <signal.h>
37 #endif
38 
39 #if defined (OCTAVE_HAVE_SIG_JUMP)
40 
41 typedef sigjmp_buf octave_jmp_buf;
42 
43 #define octave_set_current_context sigsetjmp (current_context, 1)
44 
45 #else
46 
47 typedef jmp_buf octave_jmp_buf;
48 
49 #define octave_set_current_context setjmp (current_context)
50 
51 #endif
52 
53 OCTAVE_API extern octave_jmp_buf current_context;
54 
55 OCTAVE_API extern void octave_save_current_context (void *);
56 
57 OCTAVE_API extern void octave_restore_current_context (void *);
58 
59 OCTAVE_NORETURN OCTAVE_API extern void octave_jump_to_enclosing_context (void);
60 
61 #if defined (__cplusplus)
62 
63 namespace octave
64 {
65  class
67  {
68  public:
69 
70  execution_exception (void) : m_stack_trace () { }
71 
73  : m_stack_trace (x.m_stack_trace) { }
74 
75  execution_exception& operator = (const execution_exception& x)
76  {
77  if (&x != this)
78  m_stack_trace = x.m_stack_trace;
79 
80  return *this;
81  }
82 
83  ~execution_exception (void) { }
84 
85  virtual void set_stack_trace (const std::string& st)
86  {
87  m_stack_trace = st;
88  }
89 
90  virtual void set_stack_trace (void)
91  {
92  m_stack_trace = "";
93  }
94 
95  virtual std::string info (void) const
96  {
97  return m_stack_trace;
98  }
99 
100  private:
101 
102  std::string m_stack_trace;
103  };
104 
105  class
106  exit_exception
107  {
108  public:
109 
110  exit_exception (int exit_status = 0, bool safe_to_return = false)
111  : m_exit_status (exit_status), m_safe_to_return (safe_to_return)
112  { }
113 
114  exit_exception (const exit_exception& ex)
115  : m_exit_status (ex.m_exit_status), m_safe_to_return (ex.m_safe_to_return)
116  { }
117 
118  exit_exception& operator = (exit_exception& ex)
119  {
120  if (this != &ex)
121  {
122  m_exit_status = ex.m_exit_status;
123  m_safe_to_return = ex.m_safe_to_return;
124  }
125 
126  return *this;
127  }
128 
129  ~exit_exception (void) { }
130 
131  int exit_status (void) const { return m_exit_status; }
132 
133  bool safe_to_return (void) const { return m_safe_to_return; }
134 
135  private:
136 
137  int m_exit_status;
138 
139  bool m_safe_to_return;
140  };
141 
142  class
143  interrupt_exception
144  {
145  };
146 }
147 
148 OCTAVE_DEPRECATED ("use 'octave::execution_exception' instead")
149 typedef octave::exit_exception octave_execution_exception;
150 
151 OCTAVE_DEPRECATED ("use 'octave::exit_exception' instead")
152 typedef octave::exit_exception octave_exit_exception;
153 
154 OCTAVE_DEPRECATED ("use 'octave::interrupt_exception' instead")
155 typedef octave::interrupt_exception octave_interrupt_exception;
156 
157 #endif
158 
160 {
165 };
166 
167 OCTAVE_API extern sig_atomic_t octave_interrupt_immediately;
168 
169 /*
170  > 0: interrupt pending
171  0: no interrupt pending
172  < 0: handling interrupt
173 */
174 OCTAVE_API extern sig_atomic_t octave_interrupt_state;
175 
176 OCTAVE_API extern sig_atomic_t octave_exception_state;
177 
178 OCTAVE_API extern sig_atomic_t octave_exit_exception_status;
179 
180 OCTAVE_API extern sig_atomic_t octave_exit_exception_safe_to_return;
181 
182 OCTAVE_API extern volatile sig_atomic_t octave_signal_caught;
183 
184 OCTAVE_API extern void octave_handle_signal (void);
185 
186 OCTAVE_NORETURN OCTAVE_API extern void octave_throw_interrupt_exception (void);
187 
188 OCTAVE_NORETURN OCTAVE_API extern void octave_throw_execution_exception (void);
189 
190 OCTAVE_NORETURN OCTAVE_API extern void octave_throw_bad_alloc (void);
191 
192 OCTAVE_API extern void octave_rethrow_exception (void);
193 
194 #if defined (__cplusplus)
195 
196 extern OCTAVE_API void
197 clean_up_and_exit (int exit_status, bool safe_to_return = false);
198 
199 inline void octave_quit (void)
200 {
201  if (octave_signal_caught)
202  {
203  octave_signal_caught = 0;
205  }
206 };
207 
208 #define OCTAVE_QUIT octave_quit ()
209 
210 #else
211 
212 #define OCTAVE_QUIT \
213  do \
214  { \
215  if (octave_signal_caught) \
216  { \
217  octave_signal_caught = 0; \
218  octave_handle_signal (); \
219  } \
220  } \
221  while (0)
222 #endif
223 
224 /* Normally, you just want to use
225 
226  BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
227  ... some code that calls a "foreign" function ...
228  END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
229 
230  but sometimes it is useful to do something like
231 
232  BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1;
233  ... custom code here, normally ending in a call to
234  octave_rethrow_exception ...
235  BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2;
236 
237  so that you can perform extra clean up operations before throwing
238  the interrupt exception. */
239 
240 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
241  BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \
242  octave_rethrow_exception (); \
243  BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2
244 
245 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1 \
246  do \
247  { \
248  octave_jmp_buf saved_context; \
249  \
250  octave_save_current_context (saved_context); \
251  \
252  if (octave_set_current_context) \
253  { \
254  octave_restore_current_context (saved_context)
255 
256 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 \
257  } \
258  else \
259  { \
260  octave_interrupt_immediately++
261 
262 #define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
263  octave_interrupt_immediately--; \
264  octave_restore_current_context (saved_context); \
265 } \
266 } \
267  while (0)
268 
269 #if defined (__cplusplus)
270 
271 #define BEGIN_INTERRUPT_WITH_EXCEPTIONS \
272  sig_atomic_t saved_octave_interrupt_immediately = octave_interrupt_immediately; \
273  \
274  try \
275  { \
276  octave_interrupt_immediately = 0;
277 
278 #define END_INTERRUPT_WITH_EXCEPTIONS \
279  } \
280  catch (const octave::interrupt_exception&) \
281  { \
282  octave_interrupt_immediately = saved_octave_interrupt_immediately; \
283  octave_jump_to_enclosing_context (); \
284  } \
285  catch (const octave::execution_exception&) \
286  { \
287  octave_interrupt_immediately = saved_octave_interrupt_immediately; \
288  octave_exception_state = octave_exec_exception; \
289  octave_jump_to_enclosing_context (); \
290  } \
291  catch (const std::bad_alloc&) \
292  { \
293  octave_interrupt_immediately = saved_octave_interrupt_immediately; \
294  octave_exception_state = octave_alloc_exception; \
295  octave_jump_to_enclosing_context (); \
296  } \
297  catch (const octave::exit_exception& ex) \
298  { \
299  octave_interrupt_immediately = saved_octave_interrupt_immediately; \
300  octave_exception_state = octave_quit_exception; \
301  octave_exit_exception_status = ex.exit_status (); \
302  octave_exit_exception_safe_to_return = ex.safe_to_return (); \
303  octave_jump_to_enclosing_context (); \
304  } \
305  \
306  octave_interrupt_immediately = saved_octave_interrupt_immediately
307 #endif
308 
309 #if defined (__cplusplus)
310 }
311 
312 /* These should only be declared for C++ code, and should also be
313  outside of any extern "C" block. */
314 
315 extern OCTAVE_API void (*octave_signal_hook) (void);
316 extern OCTAVE_API void (*octave_interrupt_hook) (void);
317 extern OCTAVE_API void (*octave_bad_alloc_hook) (void);
318 
319 #endif
320 
321 #endif
octave_exception
Definition: quit.h:159
Octave interface to the compression and uncompression libraries.
Definition: aepbalance.cc:47
void(* octave_interrupt_hook)(void)=0
Definition: quit.cc:35
OCTAVE_API void octave_save_current_context(void *)
Definition: cquit.c:35
jmp_buf octave_jmp_buf
Definition: quit.h:47
OCTAVE_API sig_atomic_t octave_interrupt_state
Definition: cquit.c:58
void(* octave_bad_alloc_hook)(void)=0
Definition: quit.cc:36
OCTAVE_API sig_atomic_t octave_interrupt_immediately
Definition: cquit.c:56
OCTAVE_NORETURN OCTAVE_API void octave_throw_execution_exception(void)
Definition: quit.cc:69
OCTAVE_API void octave_handle_signal(void)
Definition: quit.cc:39
OCTAVE_API void clean_up_and_exit(int exit_status, bool safe_to_return)
Definition: quit.cc:52
OCTAVE_NORETURN OCTAVE_API void octave_throw_bad_alloc(void)
Definition: quit.cc:79
OCTAVE_NORETURN OCTAVE_API void octave_jump_to_enclosing_context(void)
Definition: cquit.c:47
OCTAVE_API void octave_rethrow_exception(void)
Definition: quit.cc:90
OCTAVE_API sig_atomic_t octave_exception_state
Definition: cquit.c:60
void(* octave_signal_hook)(void)=0
Definition: quit.cc:34
OCTAVE_NORETURN OCTAVE_API void octave_throw_interrupt_exception(void)
Definition: quit.cc:60
OCTAVE_API void octave_restore_current_context(void *)
Definition: cquit.c:41
OCTAVE_API sig_atomic_t octave_exit_exception_status
Definition: cquit.c:62
OCTAVE_API sig_atomic_t octave_exit_exception_safe_to_return
Definition: cquit.c:64
OCTAVE_API octave_jmp_buf current_context
Definition: cquit.c:32
If this string is the system will ring the terminal sometimes it is useful to be able to print the original representation of the string
Definition: utils.cc:854
OCTAVE_API volatile sig_atomic_t octave_signal_caught
Definition: cquit.c:66
F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_REAL const F77_REAL F77_REAL &F77_RET_T const F77_DBLE const F77_DBLE F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T F77_DBLE &F77_RET_T F77_REAL &F77_RET_T F77_REAL &F77_RET_T F77_DBLE &F77_RET_T const F77_DBLE F77_DBLE &F77_RET_T const F77_REAL F77_REAL &F77_RET_T F77_REAL F77_REAL &F77_RET_T F77_DBLE F77_DBLE &F77_RET_T const F77_DBLE * x