Skip to content

Commit 91faf4c

Browse files
committed
update exception.h, raii.h, cthread.c and exception.c
1 parent 19b369b commit 91faf4c

File tree

4 files changed

+74
-59
lines changed

4 files changed

+74
-59
lines changed

include/exception.h

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@
114114
#elif __APPLE__ && __MACH__
115115
#define thread_local __thread
116116
#else /* C11 and newer define thread_local in threads.h */
117+
#undef HAS_C11_THREADS
118+
#define HAS_C11_THREADS 1
117119
#include <threads.h>
118120
#endif
119121
#elif defined(__cplusplus) /* Compiling as C++ Language */
@@ -127,6 +129,8 @@
127129
#endif
128130
#else /* In C++ >= 11, thread_local in a builtin keyword */
129131
/* Don't do anything */
132+
#undef HAS_C11_THREADS
133+
#define HAS_C11_THREADS 1
130134
#endif
131135
#endif
132136
#endif
@@ -215,7 +219,24 @@ enum {
215219
#define rethrow() \
216220
ex_throw(ex_err.ex, ex_err.file, ex_err.line, ex_err.function, ex_err.panic)
217221

222+
#define ex_throw_loc(E, F, L, C) \
223+
do \
224+
{ \
225+
C_API const char EX_NAME(E)[]; \
226+
ex_throw(EX_NAME(E), F, L, C, NULL); \
227+
} while (0)
228+
229+
/* An macro that stops the ordinary flow of control and begins panicking,
230+
throws an exception of given message. */
231+
#define raii_panic(message) \
232+
do \
233+
{ \
234+
C_API const char EX_NAME(panic)[]; \
235+
ex_throw(EX_NAME(panic), __FILE__, __LINE__, __FUNCTION__, (message)); \
236+
} while (0)
237+
218238
#ifdef _WIN32
239+
#define throw(E) raii_panic(EX_STR(E))
219240
#define ex_signal_block(ctrl) \
220241
CRITICAL_SECTION ctrl##__FUNCTION__; \
221242
InitializeCriticalSection(&ctrl##__FUNCTION__); \
@@ -225,29 +246,31 @@ enum {
225246
LeaveCriticalSection(&ctrl##__FUNCTION__); \
226247
DeleteCriticalSection(&ctrl##__FUNCTION__);
227248

228-
#define ex_try \
229-
{ \
230-
/* local context */ \
231-
ex_context_t ex_err; \
232-
if (!ex_context) \
233-
ex_init(); \
234-
ex_err.next = ex_context; \
235-
ex_err.stack = 0; \
236-
ex_err.ex = 0; \
237-
ex_err.unstack = 0; \
238-
/* global context updated */ \
239-
ex_context = &ex_err; \
240-
/* save jump location */ \
241-
ex_err.state = ex_setjmp(ex_err.buf); \
242-
__try \
243-
{ \
249+
#define ex_try \
250+
{ \
251+
if (!exception_signal_set) \
252+
ex_signal_setup(); \
253+
/* local context */ \
254+
ex_context_t ex_err; \
255+
if (!ex_context) \
256+
ex_init(); \
257+
ex_err.next = ex_context; \
258+
ex_err.stack = 0; \
259+
ex_err.ex = 0; \
260+
ex_err.unstack = 0; \
261+
/* global context updated */ \
262+
ex_context = &ex_err; \
263+
/* save jump location */ \
264+
ex_err.state = ex_setjmp(ex_err.buf); \
265+
__try \
266+
{ \
244267
if (ex_err.state == ex_try_st) {
245268

246-
#define ex_catch(E) \
247-
} \
248-
} __except(catch_seh(E, GetExceptionCode(), GetExceptionInformation())) { \
269+
#define ex_catch(E) \
270+
} \
271+
} __except(catch_seh(EX_STR(E), GetExceptionCode(), GetExceptionInformation())) { \
249272
if (ex_err.state == ex_throw_st) { \
250-
EX_MAKE(); \
273+
EX_MAKE(); \
251274
ex_err.state = ex_catch_st;
252275

253276
#define ex_catch_any \
@@ -298,23 +321,27 @@ enum {
298321
#define ex_signal_unblock(ctrl) \
299322
pthread_sigmask(SIG_SETMASK, &ctrl_all##__FUNCTION__, NULL);
300323

301-
#define ex_try \
302-
{ \
303-
/* local context */ \
304-
ex_context_t ex_err; \
305-
if (!ex_context) \
306-
ex_init(); \
307-
ex_err.next = ex_context; \
308-
ex_err.stack = 0; \
309-
ex_err.ex = 0; \
310-
ex_err.unstack = 0; \
311-
/* global context updated */ \
312-
ex_context = &ex_err; \
313-
/* save jump location */ \
314-
ex_err.state = ex_setjmp(ex_err.buf); \
315-
if (ex_err.state == ex_try_st) \
316-
{ \
317-
{
324+
#define throw(E) \
325+
ex_throw_loc(E, __FILE__, __LINE__, __FUNCTION__)
326+
#define ex_try \
327+
{ \
328+
if (!exception_signal_set) \
329+
ex_signal_setup(); \
330+
/* local context */ \
331+
ex_context_t ex_err; \
332+
if (!ex_context) \
333+
ex_init(); \
334+
ex_err.next = ex_context; \
335+
ex_err.stack = 0; \
336+
ex_err.ex = 0; \
337+
ex_err.unstack = 0; \
338+
/* global context updated */ \
339+
ex_context = &ex_err; \
340+
/* save jump location */ \
341+
ex_err.state = ex_setjmp(ex_err.buf); \
342+
if (ex_err.state == ex_try_st) \
343+
{ \
344+
{
318345

319346
#define ex_catch_any \
320347
} \
@@ -357,32 +384,13 @@ enum {
357384
} \
358385
if (ex_err.state == ex_throw_st) \
359386
{ \
360-
C_API const char EX_NAME(E)[]; \
387+
C_API const char EX_NAME(E)[]; \
361388
if (ex_err.ex == EX_NAME(E)) \
362389
{ \
363390
EX_MAKE(); \
364391
ex_err.state = ex_catch_st;
365392
#endif
366393

367-
#define ex_throw_loc(E, F, L, C) \
368-
do \
369-
{ \
370-
C_API const char EX_NAME(E)[]; \
371-
ex_throw(EX_NAME(E), F, L, C, NULL); \
372-
} while (0)
373-
374-
#define throw(E) \
375-
ex_throw_loc(E, __FILE__, __LINE__, __FUNCTION__)
376-
377-
/* An macro that stops the ordinary flow of control and begins panicking,
378-
throws an exception of given message. */
379-
#define raii_panic(message) \
380-
do \
381-
{ \
382-
C_API const char EX_NAME(panic)[]; \
383-
ex_throw(EX_NAME(panic), __FILE__, __LINE__, __FUNCTION__, (message)); \
384-
} while (0)
385-
386394
/* types
387395
*/
388396

include/raii.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@
2727
#include "exception.h"
2828
#include <stdio.h>
2929
#include <time.h>
30+
31+
#ifndef HAS_C11_THREADS
32+
#include "cthread.h"
33+
#endif
34+
3035
#ifdef __cplusplus
3136
extern "C" {
3237
#endif
@@ -265,7 +270,7 @@ execution begins when current `guard` scope exits or panic/throw. */
265270
#define _defer(func, ptr) raii_recover_by(_$##__FUNCTION__, func, ptr)
266271

267272
/* Compare `err` to scoped error condition, will mark exception handled, if `true`. */
268-
#define _recover(err) raii_catch_by(raii_init()->arena, err)
273+
#define _recover(err) raii_is_caught(raii_init()->arena, err)
269274

270275
/* Compare `err` to scoped error condition, will mark exception handled, if `true`. */
271276
#define _is_caught(err) raii_is_caught(raii_init()->arena, err)

src/cthread.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ freely, subject to the following restrictions:
2424
SPDX-License-Identifier: Zlib
2525
*/
2626

27+
#ifndef HAS_C11_THREADS
2728
#include "cthread.h"
2829
#include <stdlib.h>
2930

@@ -1071,3 +1072,4 @@ void call_once(once_flag *flag, void (*func)(void))
10711072
#ifdef __cplusplus
10721073
}
10731074
#endif
1075+
#endif /* HAS_C11_THREADS */

src/exception.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ int catch_seh(const char *exception, DWORD code, struct _EXCEPTION_POINTERS *ep)
272272
int i;
273273

274274
for (i = 0; i < max_ex_sig; i++) {
275-
if (ex_sig[i].ex == exception) {
275+
if (ex_sig[i].ex == exception || is_str_eq(ctx->panic, exception)) {
276276
ctx->state = ex_throw_st;
277277
ctx->ex = ex_sig[i].ex;
278278
ctx->file = "unknown";

0 commit comments

Comments
 (0)