Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include <pj/except.h> |
| 20 | #include <pj/os.h> |
| 21 | #include <pj/assert.h> |
| 22 | #include <pj/log.h> |
| 23 | #include <pj/errno.h> |
Benny Prijono | 9df1d09 | 2006-07-03 01:09:20 +0000 | [diff] [blame] | 24 | #include <pj/string.h> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 25 | |
| 26 | static long thread_local_id = -1; |
| 27 | |
| 28 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 29 | static const char *exception_id_names[PJ_MAX_EXCEPTION_ID]; |
| 30 | #else |
| 31 | /* |
| 32 | * Start from 1 (not 0)!!! |
| 33 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 34 | */ |
| 35 | static int last_exception_id = 1; |
| 36 | #endif /* PJ_HAS_EXCEPTION_NAMES */ |
| 37 | |
| 38 | |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 39 | #if !defined(PJ_EXCEPTION_USE_WIN32_SEH) || PJ_EXCEPTION_USE_WIN32_SEH==0 |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 40 | PJ_DEF(void) pj_throw_exception_(int exception_id) |
| 41 | { |
| 42 | struct pj_exception_state_t *handler; |
| 43 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 44 | handler = (struct pj_exception_state_t*) |
| 45 | pj_thread_local_get(thread_local_id); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 46 | if (handler == NULL) { |
Benny Prijono | 9df1d09 | 2006-07-03 01:09:20 +0000 | [diff] [blame] | 47 | PJ_LOG(1,("except.c", "!!!FATAL: unhandled exception %s!\n", |
| 48 | pj_exception_id_name(exception_id))); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 49 | pj_assert(handler != NULL); |
| 50 | /* This will crash the system! */ |
| 51 | } |
| 52 | pj_longjmp(handler->state, exception_id); |
| 53 | } |
| 54 | |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 55 | static void exception_cleanup(void) |
| 56 | { |
| 57 | if (thread_local_id != -1) { |
| 58 | pj_thread_local_free(thread_local_id); |
| 59 | thread_local_id = -1; |
| 60 | } |
Benny Prijono | f762ee7 | 2006-12-01 11:14:37 +0000 | [diff] [blame] | 61 | |
| 62 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 63 | { |
| 64 | unsigned i; |
| 65 | for (i=0; i<PJ_MAX_EXCEPTION_ID; ++i) |
| 66 | exception_id_names[i] = NULL; |
| 67 | } |
| 68 | #else |
| 69 | last_exception_id = 1; |
| 70 | #endif |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 73 | PJ_DEF(void) pj_push_exception_handler_(struct pj_exception_state_t *rec) |
| 74 | { |
| 75 | struct pj_exception_state_t *parent_handler = NULL; |
| 76 | |
| 77 | if (thread_local_id == -1) { |
| 78 | pj_thread_local_alloc(&thread_local_id); |
| 79 | pj_assert(thread_local_id != -1); |
Benny Prijono | af1bb1e | 2006-11-21 12:39:31 +0000 | [diff] [blame] | 80 | pj_atexit(&exception_cleanup); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 81 | } |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 82 | parent_handler = (struct pj_exception_state_t *) |
| 83 | pj_thread_local_get(thread_local_id); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 84 | rec->prev = parent_handler; |
| 85 | pj_thread_local_set(thread_local_id, rec); |
| 86 | } |
| 87 | |
| 88 | PJ_DEF(void) pj_pop_exception_handler_(void) |
| 89 | { |
| 90 | struct pj_exception_state_t *handler; |
| 91 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 92 | handler = (struct pj_exception_state_t *) |
| 93 | pj_thread_local_get(thread_local_id); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 94 | pj_assert(handler != NULL); |
| 95 | pj_thread_local_set(thread_local_id, handler->prev); |
| 96 | } |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 97 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 98 | |
| 99 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 100 | PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, |
| 101 | pj_exception_id_t *id) |
| 102 | { |
| 103 | unsigned i; |
| 104 | |
| 105 | pj_enter_critical_section(); |
| 106 | |
| 107 | /* |
| 108 | * Start from 1 (not 0)!!! |
| 109 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 110 | */ |
| 111 | for (i=1; i<PJ_MAX_EXCEPTION_ID; ++i) { |
| 112 | if (exception_id_names[i] == NULL) { |
| 113 | exception_id_names[i] = name; |
| 114 | *id = i; |
| 115 | pj_leave_critical_section(); |
| 116 | return PJ_SUCCESS; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | pj_leave_critical_section(); |
| 121 | return PJ_ETOOMANY; |
| 122 | } |
| 123 | |
| 124 | PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) |
| 125 | { |
| 126 | /* |
| 127 | * Start from 1 (not 0)!!! |
| 128 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 129 | */ |
| 130 | PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, PJ_EINVAL); |
| 131 | |
| 132 | pj_enter_critical_section(); |
| 133 | exception_id_names[id] = NULL; |
| 134 | pj_leave_critical_section(); |
| 135 | |
| 136 | return PJ_SUCCESS; |
| 137 | |
| 138 | } |
| 139 | |
| 140 | PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id) |
| 141 | { |
Benny Prijono | 9df1d09 | 2006-07-03 01:09:20 +0000 | [diff] [blame] | 142 | static char unknown_name[32]; |
| 143 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 144 | /* |
| 145 | * Start from 1 (not 0)!!! |
| 146 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 147 | */ |
| 148 | PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, "<Invalid ID>"); |
| 149 | |
Benny Prijono | 9df1d09 | 2006-07-03 01:09:20 +0000 | [diff] [blame] | 150 | if (exception_id_names[id] == NULL) { |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 151 | pj_ansi_snprintf(unknown_name, sizeof(unknown_name), |
| 152 | "exception %d", id); |
Benny Prijono | 9df1d09 | 2006-07-03 01:09:20 +0000 | [diff] [blame] | 153 | return unknown_name; |
| 154 | } |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 155 | |
| 156 | return exception_id_names[id]; |
| 157 | } |
| 158 | |
| 159 | #else /* PJ_HAS_EXCEPTION_NAMES */ |
| 160 | PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, |
| 161 | pj_exception_id_t *id) |
| 162 | { |
| 163 | PJ_ASSERT_RETURN(last_exception_id < PJ_MAX_EXCEPTION_ID-1, PJ_ETOOMANY); |
| 164 | |
Benny Prijono | 1185299 | 2006-03-18 12:28:03 +0000 | [diff] [blame] | 165 | *id = last_exception_id++; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 166 | return PJ_SUCCESS; |
| 167 | } |
| 168 | |
| 169 | PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) |
| 170 | { |
| 171 | return PJ_SUCCESS; |
| 172 | } |
| 173 | |
| 174 | PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id) |
| 175 | { |
| 176 | return ""; |
| 177 | } |
| 178 | |
| 179 | #endif /* PJ_HAS_EXCEPTION_NAMES */ |
| 180 | |
| 181 | |
| 182 | |