1
/* $Id: except.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#include <pj/except.h>
22
#include <pj/assert.h>
25
#include <pj/string.h>
27
static long thread_local_id = -1;
29
#if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0
30
static const char *exception_id_names[PJ_MAX_EXCEPTION_ID];
33
* Start from 1 (not 0)!!!
34
* Exception 0 is reserved for normal path of setjmp()!!!
36
static int last_exception_id = 1;
37
#endif /* PJ_HAS_EXCEPTION_NAMES */
40
#if !defined(PJ_EXCEPTION_USE_WIN32_SEH) || PJ_EXCEPTION_USE_WIN32_SEH==0
41
PJ_DEF(void) pj_throw_exception_(int exception_id)
43
struct pj_exception_state_t *handler;
45
handler = (struct pj_exception_state_t*)
46
pj_thread_local_get(thread_local_id);
47
if (handler == NULL) {
48
PJ_LOG(1,("except.c", "!!!FATAL: unhandled exception %s!\n",
49
pj_exception_id_name(exception_id)));
50
pj_assert(handler != NULL);
51
/* This will crash the system! */
53
pj_pop_exception_handler_(handler);
54
pj_longjmp(handler->state, exception_id);
57
static void exception_cleanup(void)
59
if (thread_local_id != -1) {
60
pj_thread_local_free(thread_local_id);
64
#if defined(PJ_HAS_EXCEPTION_NAMES) && PJ_HAS_EXCEPTION_NAMES != 0
67
for (i=0; i<PJ_MAX_EXCEPTION_ID; ++i)
68
exception_id_names[i] = NULL;
71
last_exception_id = 1;
75
PJ_DEF(void) pj_push_exception_handler_(struct pj_exception_state_t *rec)
77
struct pj_exception_state_t *parent_handler = NULL;
79
if (thread_local_id == -1) {
80
pj_thread_local_alloc(&thread_local_id);
81
pj_assert(thread_local_id != -1);
82
pj_atexit(&exception_cleanup);
84
parent_handler = (struct pj_exception_state_t *)
85
pj_thread_local_get(thread_local_id);
86
rec->prev = parent_handler;
87
pj_thread_local_set(thread_local_id, rec);
90
PJ_DEF(void) pj_pop_exception_handler_(struct pj_exception_state_t *rec)
92
struct pj_exception_state_t *handler;
94
handler = (struct pj_exception_state_t *)
95
pj_thread_local_get(thread_local_id);
96
if (handler && handler==rec) {
97
pj_thread_local_set(thread_local_id, handler->prev);
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)
108
pj_enter_critical_section();
111
* Start from 1 (not 0)!!!
112
* Exception 0 is reserved for normal path of setjmp()!!!
114
for (i=1; i<PJ_MAX_EXCEPTION_ID; ++i) {
115
if (exception_id_names[i] == NULL) {
116
exception_id_names[i] = name;
118
pj_leave_critical_section();
123
pj_leave_critical_section();
127
PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id )
130
* Start from 1 (not 0)!!!
131
* Exception 0 is reserved for normal path of setjmp()!!!
133
PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, PJ_EINVAL);
135
pj_enter_critical_section();
136
exception_id_names[id] = NULL;
137
pj_leave_critical_section();
143
PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id)
145
static char unknown_name[32];
148
* Start from 1 (not 0)!!!
149
* Exception 0 is reserved for normal path of setjmp()!!!
151
PJ_ASSERT_RETURN(id>0 && id<PJ_MAX_EXCEPTION_ID, "<Invalid ID>");
153
if (exception_id_names[id] == NULL) {
154
pj_ansi_snprintf(unknown_name, sizeof(unknown_name),
159
return exception_id_names[id];
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)
166
PJ_ASSERT_RETURN(last_exception_id < PJ_MAX_EXCEPTION_ID-1, PJ_ETOOMANY);
168
*id = last_exception_id++;
172
PJ_DEF(pj_status_t) pj_exception_id_free( pj_exception_id_t id )
177
PJ_DEF(const char*) pj_exception_id_name(pj_exception_id_t id)
182
#endif /* PJ_HAS_EXCEPTION_NAMES */