[77] | 1 | /* $Id: exception.h 13 2011-04-20 15:41:43Z mmamonski $ */ |
---|
| 2 | /* |
---|
| 3 | * PSNC DRMAA 2.0 utilities library |
---|
| 4 | * Copyright (C) 2012 Poznan Supercomputing and Networking Center |
---|
| 5 | * |
---|
| 6 | * This program is free software: you can redistribute it and/or modify |
---|
| 7 | * it under the terms of the GNU General Public License as published by |
---|
| 8 | * the Free Software Foundation, either version 3 of the License, or |
---|
| 9 | * (at your option) any later version. |
---|
| 10 | * |
---|
| 11 | * This program is distributed in the hope that it will be useful, |
---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 14 | * GNU General Public License for more details. |
---|
| 15 | * |
---|
| 16 | * You should have received a copy of the GNU General Public License |
---|
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 18 | */ |
---|
| 19 | |
---|
| 20 | #ifndef __DRMAA_UTILS__EXCEPTION_H |
---|
| 21 | #define __DRMAA_UTILS__EXCEPTION_H |
---|
| 22 | |
---|
| 23 | #ifdef HAVE_CONFIG_H |
---|
| 24 | # include <config.h> |
---|
| 25 | #endif |
---|
| 26 | |
---|
| 27 | #include <stdarg.h> |
---|
| 28 | #include <setjmp.h> |
---|
| 29 | |
---|
| 30 | #include <drmaa_utils/compat.h> |
---|
| 31 | |
---|
| 32 | #if defined(__GNUC__) && defined(DEBUGGING) |
---|
| 33 | extern int setjmp( jmp_buf __env ) |
---|
| 34 | __attribute__(( returns_twice )); |
---|
| 35 | #endif |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | /** Exception type. */ |
---|
| 39 | typedef struct fsd_exc_s fsd_exc_t; |
---|
| 40 | |
---|
| 41 | fsd_exc_t * |
---|
| 42 | fsd_exc_new( int code, char *message, bool own_message ); |
---|
| 43 | |
---|
| 44 | /** Exception structure. */ |
---|
| 45 | struct fsd_exc_s { |
---|
| 46 | int (*code)( const fsd_exc_t* ); |
---|
| 47 | const char* (*message)( const fsd_exc_t* ); |
---|
| 48 | void (*destroy)( fsd_exc_t* ); |
---|
| 49 | |
---|
| 50 | int _code; |
---|
| 51 | char *_message; |
---|
| 52 | bool _own_message; |
---|
| 53 | bool _own_self; |
---|
| 54 | }; |
---|
| 55 | |
---|
| 56 | void |
---|
| 57 | fsd_exc_raise( fsd_exc_t *exc ) |
---|
| 58 | __attribute__(( noreturn )); |
---|
| 59 | |
---|
| 60 | void |
---|
| 61 | fsd_exc_raise_code( int error_code ) |
---|
| 62 | __attribute__(( noreturn )); |
---|
| 63 | |
---|
| 64 | void |
---|
| 65 | fsd_exc_raise_msg( int error_code, const char *message ) |
---|
| 66 | __attribute__(( noreturn )); |
---|
| 67 | |
---|
| 68 | void |
---|
| 69 | fsd_exc_raise_fmt( int error_code, const char *fmt, ... ) |
---|
| 70 | __attribute__(( noreturn, format( printf, 2, 3 ) )); |
---|
| 71 | |
---|
| 72 | void |
---|
| 73 | fsd_exc_raise_fmtv( int error_code, const char *fmt, va_list args ) |
---|
| 74 | __attribute__(( noreturn )); |
---|
| 75 | |
---|
| 76 | void |
---|
| 77 | fsd_exc_raise_sys( int errno_code ) |
---|
| 78 | __attribute__(( noreturn )); |
---|
| 79 | |
---|
| 80 | void |
---|
| 81 | fsd_exc_reraise(void) |
---|
| 82 | __attribute__(( noreturn )); |
---|
| 83 | |
---|
| 84 | const fsd_exc_t * |
---|
| 85 | fsd_exc_get(void); |
---|
| 86 | |
---|
| 87 | void |
---|
| 88 | fsd_exc_clear(void); |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | #define TRY \ |
---|
| 92 | { \ |
---|
| 93 | fsd_exc_try_block_t* volatile _fsd_exc_try_block = NULL; \ |
---|
| 94 | int _fsd_exc_rc; \ |
---|
| 95 | _fsd_exc_try_block = fsd_exc_try( __FUNCTION__, __LINE__ ); \ |
---|
| 96 | if( _fsd_exc_try_block != NULL ) \ |
---|
| 97 | _fsd_exc_rc = setjmp( _fsd_exc_try_block->env ); \ |
---|
| 98 | else \ |
---|
| 99 | _fsd_exc_rc = FSD_ERRNO_EXC_END; \ |
---|
| 100 | while(1) \ |
---|
| 101 | { \ |
---|
| 102 | bool _fsd_exc_handled = false; \ |
---|
| 103 | fsd_exc_control( _fsd_exc_try_block, &_fsd_exc_rc ); \ |
---|
| 104 | if( _fsd_exc_rc == FSD_ERRNO_EXC_END ) \ |
---|
| 105 | break; \ |
---|
| 106 | switch( _fsd_exc_rc ) \ |
---|
| 107 | { \ |
---|
| 108 | case 0: { |
---|
| 109 | #define EXCEPT(error_code) \ |
---|
| 110 | } break; \ |
---|
| 111 | case error_code: { \ |
---|
| 112 | _fsd_exc_handled = true; |
---|
| 113 | #define EXCEPT_DEFAULT \ |
---|
| 114 | } break; \ |
---|
| 115 | default: \ |
---|
| 116 | if( _fsd_exc_rc > 0 ) \ |
---|
| 117 | { \ |
---|
| 118 | _fsd_exc_handled = true; |
---|
| 119 | #define FINALLY \ |
---|
| 120 | } break; \ |
---|
| 121 | case FSD_ERRNO_EXC_FINALLY: { |
---|
| 122 | #define ELSE \ |
---|
| 123 | } break; \ |
---|
| 124 | case FSD_ERRNO_EXC_ELSE: { |
---|
| 125 | #define END_TRY \ |
---|
| 126 | } break; \ |
---|
| 127 | } \ |
---|
| 128 | if( _fsd_exc_handled ) \ |
---|
| 129 | { \ |
---|
| 130 | fsd_assert( _fsd_exc_try_block->handled_exc != NULL ); \ |
---|
| 131 | _fsd_exc_try_block->handled_exc->destroy( \ |
---|
| 132 | _fsd_exc_try_block->handled_exc ); \ |
---|
| 133 | _fsd_exc_try_block->handled_exc = NULL; \ |
---|
| 134 | } \ |
---|
| 135 | } \ |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | |
---|
| 139 | /** The state of exception handling block. */ |
---|
| 140 | typedef enum { |
---|
| 141 | FSD_EXC_ENTER, /**< Before try. */ |
---|
| 142 | FSD_EXC_TRY_BLOCK, /**< Insidie try-block. */ |
---|
| 143 | FSD_EXC_EXCEPTION_HANDLE, /**< Inside exception handling block. */ |
---|
| 144 | FSD_EXC_ELSE_BLOCK, /**< Inside else-block. */ |
---|
| 145 | FSD_EXC_FINALLY_BLOCK, /**< Inside finally block. */ |
---|
| 146 | FSD_EXC_LEAVE /**< Leaving try/except/finally block (after finally). */ |
---|
| 147 | } fsd_exc_try_block_state_t; |
---|
| 148 | |
---|
| 149 | /** |
---|
| 150 | * Try/except/else/finally block data structure. |
---|
| 151 | * Holds the state for exception handling module. |
---|
| 152 | * |
---|
| 153 | * It also represents a point on the stack and other state of |
---|
| 154 | * thread to wich it can be restored during "stack rollback" |
---|
| 155 | * process after exception is raised. |
---|
| 156 | */ |
---|
| 157 | typedef struct fsd_exc_try_block_s { |
---|
| 158 | jmp_buf env; /**< Stack restore point for \c longjmp. */ |
---|
| 159 | fsd_exc_t *handled_exc; /**< Exception handled within block */ |
---|
| 160 | fsd_exc_try_block_state_t state; |
---|
| 161 | const char *function; /**< Name of function */ |
---|
| 162 | int lineno; /**< Line number where try-block starts */ |
---|
| 163 | } fsd_exc_try_block_t; |
---|
| 164 | |
---|
| 165 | |
---|
| 166 | fsd_exc_try_block_t * |
---|
| 167 | fsd_exc_try( const char *function, int lineno ); |
---|
| 168 | |
---|
| 169 | void |
---|
| 170 | fsd_exc_control( fsd_exc_try_block_t *block, int *rc ); |
---|
| 171 | |
---|
| 172 | |
---|
| 173 | const char * |
---|
| 174 | fsd_strerror( int error_code ); |
---|
| 175 | |
---|
| 176 | /** Error codes. */ |
---|
| 177 | typedef enum { |
---|
| 178 | FSD_ERRNO_SUCCESS = 0, |
---|
| 179 | |
---|
| 180 | FSD_ERRNO_EXC_ELSE = -1, |
---|
| 181 | FSD_ERRNO_EXC_FINALLY = -2, |
---|
| 182 | FSD_ERRNO_EXC_END = -3, |
---|
| 183 | |
---|
| 184 | /* _FSD_ERRNO_START = 1000, */ |
---|
| 185 | |
---|
| 186 | FSD_ERRNO_INTERNAL_ERROR = 1001, |
---|
| 187 | FSD_ERRNO_NO_MEMORY = 1002, |
---|
| 188 | FSD_ERRNO_INVALID_ARGUMENT = 1003, |
---|
| 189 | FSD_ERRNO_INVALID_VALUE = 1004, |
---|
| 190 | FSD_ERRNO_INVALID_VALUE_FORMAT = 1005, |
---|
| 191 | FSD_ERRNO_STOP_ITERATION = 1006, |
---|
| 192 | FSD_ERRNO_NOT_IMPLEMENTED = 1007, |
---|
| 193 | FSD_ERRNO_NOT_INITIALIZED = 1008, |
---|
| 194 | FSD_ERRNO_TIMEOUT = 1009, |
---|
| 195 | FSD_ERRNO_AUTH_FAILURE = 1010, |
---|
| 196 | FSD_ERRNO_AUTHZ_FAILURE = 1011, |
---|
| 197 | FSD_ERRNO_TRY_LATER = 1012, |
---|
| 198 | |
---|
| 199 | FSD_ERRNO_DRM_COMMUNICATION_FAILURE = 1013, |
---|
| 200 | FSD_ERRNO_DRMS_INIT_FAILED = 1014, |
---|
| 201 | FSD_ERRNO_DRMS_EXIT_ERROR = 1015, |
---|
| 202 | FSD_ERRNO_DENIED_BY_DRM = 1016, |
---|
| 203 | |
---|
| 204 | /* DRMAA specific error codes: */ |
---|
| 205 | FSD_DRMAA_ERRNO_NO_ACTIVE_SESSION = 1032, |
---|
| 206 | FSD_DRMAA_ERRNO_INVALID_CONTACT_STRING = 1033, |
---|
| 207 | FSD_DRMAA_ERRNO_DEFAULT_CONTACT_STRING_ERROR = 1034, |
---|
| 208 | FSD_DRMAA_ERRNO_NO_DEFAULT_CONTACT_STRING_SELECTED = 1035, |
---|
| 209 | FSD_DRMAA_ERRNO_ALREADY_ACTIVE_SESSION = 1036, |
---|
| 210 | FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_FORMAT = 1037, |
---|
| 211 | FSD_DRMAA_ERRNO_INVALID_ATTRIBUTE_VALUE = 1038, |
---|
| 212 | FSD_DRMAA_ERRNO_CONFLICTING_ATTRIBUTE_VALUES = 1039, |
---|
| 213 | FSD_DRMAA_ERRNO_INVALID_JOB = 1040, |
---|
| 214 | FSD_DRMAA_ERRNO_RESUME_INCONSISTENT_STATE = 1041, |
---|
| 215 | FSD_DRMAA_ERRNO_SUSPEND_INCONSISTENT_STATE = 1042, |
---|
| 216 | FSD_DRMAA_ERRNO_HOLD_INCONSISTENT_STATE = 1043, |
---|
| 217 | FSD_DRMAA_ERRNO_RELEASE_INCONSISTENT_STATE = 1044, |
---|
| 218 | FSD_DRMAA_ERRNO_EXIT_TIMEOUT = 1045, |
---|
| 219 | FSD_DRMAA_ERRNO_NO_RUSAGE = 1046, |
---|
| 220 | |
---|
| 221 | /* ARes specific error codes: */ |
---|
| 222 | FSD_ARES_ERRNO_INVALID_CONTACT_STRING = 1052, |
---|
| 223 | FSD_ARES_ERRNO_INVALID_ATTRIBUTE_FORMAT = 1053, |
---|
| 224 | FSD_ARES_ERRNO_INVALID_ATTRIBUTE_VALUE = 1054, |
---|
| 225 | FSD_ARES_ERRNO_CONFLICTING_ATTRIBUTE_VALUES = 1055, |
---|
| 226 | FSD_ARES_ERRNO_INVALID_ARES = 1056, |
---|
| 227 | |
---|
| 228 | FSD_MIN_ERRNO = FSD_ERRNO_INTERNAL_ERROR, |
---|
| 229 | FSD_MAX_ERRNO = FSD_ARES_ERRNO_INVALID_ARES |
---|
| 230 | } fsd_errno_t; |
---|
| 231 | |
---|
| 232 | |
---|
| 233 | #ifndef NDEBUG |
---|
| 234 | # define fsd_assert( precondition ) \ |
---|
| 235 | do { \ |
---|
| 236 | if( ! (precondition) ) \ |
---|
| 237 | fsd_assertion_failed( __FILE__, __LINE__, \ |
---|
| 238 | __FUNCTION__, #precondition ); \ |
---|
| 239 | } while(0) |
---|
| 240 | #else |
---|
| 241 | # define fsd_assert( precondition ) do{/* nothing */}while(0) |
---|
| 242 | #endif |
---|
| 243 | |
---|
| 244 | void |
---|
| 245 | fsd_assertion_failed( const char *file, int lineno, |
---|
| 246 | const char *function, const char *precondition ) |
---|
| 247 | __attribute__(( noreturn )); |
---|
| 248 | |
---|
| 249 | void * |
---|
| 250 | fsd_exc_try_except( |
---|
| 251 | void*(*f)(void*), void *data, |
---|
| 252 | int *error_code, |
---|
| 253 | char **error_message |
---|
| 254 | ); |
---|
| 255 | |
---|
| 256 | #endif /* __DRMAA_UTILS__EXCEPTION_H */ |
---|
| 257 | |
---|