Octave logo Octave-Forge - Extra packages for GNU Octave
Home · Packages · Developers · Documentation · Function Reference · FAQ · Bugs · Mailing Lists · Links · SVN
  • Main Page
  • Classes
  • Files

libcruft/misc/quit.h

Go to the documentation of this file.
00001 /*
00002 
00003 Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007 John W. Eaton
00004 
00005 This file is part of Octave.
00006 
00007 Octave is free software; you can redistribute it and/or modify it
00008 under the terms of the GNU General Public License as published by the
00009 Free Software Foundation; either version 3 of the License, or (at your
00010 option) any later version.
00011 
00012 Octave is distributed in the hope that it will be useful, but WITHOUT
00013 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00014 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00015 for more details.
00016 
00017 You should have received a copy of the GNU General Public License
00018 along with Octave; see the file COPYING.  If not, see
00019 <http://www.gnu.org/licenses/>.
00020 
00021 */
00022 
00023 #if !defined (octave_quit_h)
00024 #define octave_quit_h 1
00025 
00026 #ifdef __cplusplus
00027 #include <new>
00028 extern "C" {
00029 #endif
00030 
00031 #include <stdio.h>
00032 
00033 #include <signal.h>
00034 #include <setjmp.h>
00035 
00036 #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
00037 
00038 #include <windows.h>
00039 #undef min
00040 #undef max
00041 
00042 CRUFT_API extern void w32_sigint_init (void);   /* setup */
00043 CRUFT_API extern void w32_raise_final (void);   /* tear down */
00044 CRUFT_API extern void w32_raise (int sig);      /* raise signal in main thread */
00045 CRUFT_API extern int w32_in_main_thread (void); /* return true if in main thread */
00046 
00047 #endif
00048 
00049 #if defined (OCTAVE_HAVE_SIG_JUMP)
00050 
00051 typedef sigjmp_buf octave_jmp_buf;
00052 
00053 #define octave_set_current_context sigsetjmp (current_context, 1)
00054 
00055 #else
00056 
00057 typedef jmp_buf octave_jmp_buf;
00058 
00059 #define octave_set_current_context setjmp (current_context)
00060 
00061 #endif
00062 
00063 CRUFT_API extern octave_jmp_buf current_context;
00064 
00065 CRUFT_API extern void octave_save_current_context (void *);
00066 
00067 CRUFT_API extern void octave_restore_current_context (void *);
00068 
00069 CRUFT_API extern void octave_jump_to_enclosing_context (void) GCC_ATTR_NORETURN;
00070 
00071 CRUFT_API extern void octave_save_signal_mask (void);
00072 
00073 CRUFT_API extern void octave_restore_signal_mask (void);
00074 
00075 #ifdef __cplusplus
00076 class
00077 octave_interrupt_exception
00078 {
00079 };
00080 #endif
00081 
00082 CRUFT_API extern sig_atomic_t octave_interrupt_immediately;
00083 
00084 /*
00085   > 0: interrupt pending
00086     0: no interrupt pending
00087   < 0: handling interrupt
00088 */
00089 CRUFT_API extern sig_atomic_t octave_interrupt_state;
00090 
00091 CRUFT_API extern sig_atomic_t octave_allocation_error;
00092 
00093 CRUFT_API extern sig_atomic_t octave_signal_caught;
00094 
00095 CRUFT_API extern void octave_handle_signal (void);
00096 
00097 CRUFT_API extern void octave_throw_interrupt_exception (void) GCC_ATTR_NORETURN;
00098 
00099 CRUFT_API extern void octave_throw_bad_alloc (void) GCC_ATTR_NORETURN;
00100 
00101 #define OCTAVE_QUIT \
00102   do \
00103     { \
00104       if (octave_signal_caught) \
00105         { \
00106           octave_signal_caught = 0; \
00107           octave_handle_signal (); \
00108         } \
00109     } \
00110   while (0)
00111 
00112 /* Normally, you just want to use
00113 
00114      BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
00115      ... some code that calls a "foreign" function ...
00116      END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE;
00117 
00118    but sometimes it is useful to do something like
00119 
00120      BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1;
00121      ... custom code here, normally ending in a call to
00122          octave_throw_interrupt_exception ...
00123      BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2;
00124 
00125    so that you can perform extra clean up operations before throwing
00126    the interrupt exception.  */
00127 
00128 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
00129   BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1; \
00130   octave_throw_interrupt_exception (); \
00131   BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2
00132 
00133 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_1 \
00134   do \
00135     { \
00136       octave_jmp_buf saved_context; \
00137  \
00138       octave_save_current_context (saved_context); \
00139  \
00140       if (octave_set_current_context) \
00141         { \
00142           octave_restore_current_context (saved_context)
00143 
00144 #define BEGIN_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE_2 \
00145         } \
00146       else \
00147         { \
00148           octave_interrupt_immediately++
00149 
00150 #define END_INTERRUPT_IMMEDIATELY_IN_FOREIGN_CODE \
00151           octave_interrupt_immediately--; \
00152           octave_restore_current_context (saved_context); \
00153         } \
00154     } \
00155   while (0)
00156 
00157 #ifdef __cplusplus
00158 
00159 #define BEGIN_INTERRUPT_WITH_EXCEPTIONS \
00160   sig_atomic_t saved_octave_interrupt_immediately = octave_interrupt_immediately; \
00161  \
00162   try \
00163     { \
00164       octave_interrupt_immediately = 0;
00165 
00166 #define END_INTERRUPT_WITH_EXCEPTIONS \
00167     } \
00168   catch (octave_interrupt_exception) \
00169     { \
00170       octave_interrupt_immediately = saved_octave_interrupt_immediately; \
00171       octave_jump_to_enclosing_context (); \
00172     } \
00173   catch (std::bad_alloc) \
00174     { \
00175       octave_interrupt_immediately = saved_octave_interrupt_immediately; \
00176       octave_allocation_error = 1; \
00177       octave_jump_to_enclosing_context (); \
00178     } \
00179  \
00180   octave_interrupt_immediately = saved_octave_interrupt_immediately
00181 #endif
00182 
00183 #ifdef __cplusplus
00184 }
00185 
00186 /* These should only be declared for C++ code, and should also be
00187    outside of any extern "C" block.  */
00188 
00189 extern CRUFT_API void (*octave_signal_hook) (void);
00190 extern CRUFT_API void (*octave_interrupt_hook) (void);
00191 extern CRUFT_API void (*octave_bad_alloc_hook) (void);
00192 
00193 #endif
00194 
00195 #endif
00196 
00197 /*
00198 ;;; Local Variables: ***
00199 ;;; mode: C++ ***
00200 ;;; End: ***
00201 */
SourceForge.net Logo