X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/1b96e9580714d7a677d10a8e286b3f8a2bddc6f5..4c819e9466a0573afae0f9787a4704a479517624:/src/error.c diff --git a/src/error.c b/src/error.c index 0ff31ee..e07a410 100644 --- a/src/error.c +++ b/src/error.c @@ -24,38 +24,14 @@ #include "cdecl.h" #include "cdecl-internal.h" - -#include - #include "errmsg.h" -#if USE_POSIX_THREADS -# include "thread-posix.h" -#elif USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS -# include "thread-stdc.h" -#elif USE_WINDOWS_THREADS -# include "thread-w32.h" -#else -static void *tls_key; -enum { tls_key_valid = 1 }; - -#define tls_key_init() ((void)0) -#define tls_get() tls_key -#define tls_set(a) (tls_key = (a), 1) -#endif - -struct err_state { - struct cdecl_error err; - size_t nstr; - char str[FLEXIBLE_ARRAY_MEMBER]; -}; - /* This pre-initialized error is reserved for dire out-of-memory conditions. */ static struct cdecl_error err_no_mem; static void set_err(unsigned code, struct cdecl_error *err) { - static const char errmsgs[] = STRTAB_INITIALIZER; + static const char _Alignas(1) errmsgs[] = STRTAB_INITIALIZER; switch (code) { case CDECL__ENOMEM: @@ -69,16 +45,43 @@ static void set_err(unsigned code, struct cdecl_error *err) err->str = _(&errmsgs[code]); } -static void initialize_cb(void) +static void init_once_cb(void) { #if ENABLE_NLS bindtextdomain(PACKAGE, LOCALEDIR); bindtextdomain("bison-runtime", BISON_LOCALEDIR); #endif set_err(CDECL__ENOMEM, &err_no_mem); - tls_key_init(); } +#if USE_POSIX_THREADS +# include "thread-posix.h" +#elif USE_ISOC_THREADS || USE_ISOC_AND_POSIX_THREADS +# include "thread-stdc.h" +#elif USE_WINDOWS_THREADS +# include "thread-w32.h" +#else +static void *tls_key; +enum { tls_key_valid = 1 }; + +#define tls_get() tls_key +#define tls_set(a) ((tls_key = (a)), 1) + +static int init_once(void) +{ + if (!err_no_mem.code) + init_once_cb(); + return 1; +} + +#endif + +struct err_state { + struct cdecl_error err; + size_t nstr; + char str[FLEXIBLE_ARRAY_MEMBER]; +}; + static void *alloc_err_state(void *old, size_t buf_size) { struct err_state *state; @@ -109,8 +112,7 @@ static struct err_state *get_err_state(void) { void *state; - gl_once_define(static, tls_initialized) - if (glthread_once(&tls_initialized, initialize_cb) || !tls_key_valid) + if (!init_once()) return NULL; if (!(state = tls_get()))