Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 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> |
| 24 | |
| 25 | static long thread_local_id = -1; |
| 26 | |
| 27 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 28 | static const char *exception_id_names[PJ_MAX_EXCEPTION_ID]; |
| 29 | #else |
| 30 | /* |
| 31 | * Start from 1 (not 0)!!! |
| 32 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 33 | */ |
| 34 | static int last_exception_id = 1; |
| 35 | #endif /* PJ_HAS_EXCEPTION_NAMES */ |
| 36 | |
| 37 | |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 38 | #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] | 39 | PJ_DEF(void) pj_throw_exception_(int exception_id) |
| 40 | { |
| 41 | struct pj_exception_state_t *handler; |
| 42 | |
| 43 | handler = pj_thread_local_get(thread_local_id); |
| 44 | if (handler == NULL) { |
| 45 | PJ_LOG(1,("except.c", "!!!FATAL: unhandled exception %d!\n", exception_id)); |
| 46 | pj_assert(handler != NULL); |
| 47 | /* This will crash the system! */ |
| 48 | } |
| 49 | pj_longjmp(handler->state, exception_id); |
| 50 | } |
| 51 | |
| 52 | PJ_DEF(void) pj_push_exception_handler_(struct pj_exception_state_t *rec) |
| 53 | { |
| 54 | struct pj_exception_state_t *parent_handler = NULL; |
| 55 | |
| 56 | if (thread_local_id == -1) { |
| 57 | pj_thread_local_alloc(&thread_local_id); |
| 58 | pj_assert(thread_local_id != -1); |
| 59 | } |
| 60 | parent_handler = pj_thread_local_get(thread_local_id); |
| 61 | rec->prev = parent_handler; |
| 62 | pj_thread_local_set(thread_local_id, rec); |
| 63 | } |
| 64 | |
| 65 | PJ_DEF(void) pj_pop_exception_handler_(void) |
| 66 | { |
| 67 | struct pj_exception_state_t *handler; |
| 68 | |
| 69 | handler = pj_thread_local_get(thread_local_id); |
| 70 | pj_assert(handler != NULL); |
| 71 | pj_thread_local_set(thread_local_id, handler->prev); |
| 72 | } |
Benny Prijono | d0d44f5 | 2005-11-21 16:57:02 +0000 | [diff] [blame] | 73 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 74 | |
| 75 | #if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0 |
| 76 | PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, |
| 77 | pj_exception_id_t *id) |
| 78 | { |
| 79 | unsigned i; |
| 80 | |
| 81 | pj_enter_critical_section(); |
| 82 | |
| 83 | /* |
| 84 | * Start from 1 (not 0)!!! |
| 85 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 86 | */ |
| 87 | for (i=1; i<PJ_MAX_EXCEPTION_ID; ++i) { |
| 88 | if (exception_id_names[i] == NULL) { |
| 89 | exception_id_names[i] = name; |
| 90 | *id = i; |
| 91 | pj_leave_critical_section(); |
| 92 | return PJ_SUCCESS; |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | pj_leave_critical_section(); |
| 97 | return PJ_ETOOMANY; |
| 98 | } |
| 99 | |
| 100 | PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) |
| 101 | { |
| 102 | /* |
| 103 | * Start from 1 (not 0)!!! |
| 104 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 105 | */ |
| 106 | PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, PJ_EINVAL); |
| 107 | |
| 108 | pj_enter_critical_section(); |
| 109 | exception_id_names[id] = NULL; |
| 110 | pj_leave_critical_section(); |
| 111 | |
| 112 | return PJ_SUCCESS; |
| 113 | |
| 114 | } |
| 115 | |
| 116 | PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id) |
| 117 | { |
| 118 | /* |
| 119 | * Start from 1 (not 0)!!! |
| 120 | * Exception 0 is reserved for normal path of setjmp()!!! |
| 121 | */ |
| 122 | PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, "<Invalid ID>"); |
| 123 | |
| 124 | if (exception_id_names[id] == NULL) |
| 125 | return "<Unallocated ID>"; |
| 126 | |
| 127 | return exception_id_names[id]; |
| 128 | } |
| 129 | |
| 130 | #else /* PJ_HAS_EXCEPTION_NAMES */ |
| 131 | PJ_DEF(pj_status_t) pj_exception_id_alloc( const char *name, |
| 132 | pj_exception_id_t *id) |
| 133 | { |
| 134 | PJ_ASSERT_RETURN(last_exception_id < PJ_MAX_EXCEPTION_ID-1, PJ_ETOOMANY); |
| 135 | |
Benny Prijono | 1185299 | 2006-03-18 12:28:03 +0000 | [diff] [blame] | 136 | *id = last_exception_id++; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 137 | return PJ_SUCCESS; |
| 138 | } |
| 139 | |
| 140 | PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id ) |
| 141 | { |
| 142 | return PJ_SUCCESS; |
| 143 | } |
| 144 | |
| 145 | PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id) |
| 146 | { |
| 147 | return ""; |
| 148 | } |
| 149 | |
| 150 | #endif /* PJ_HAS_EXCEPTION_NAMES */ |
| 151 | |
| 152 | |
| 153 | |