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