diff --git a/include/gk_externs.h b/include/gk_externs.h index 2c0fdd9..8338217 100644 --- a/include/gk_externs.h +++ b/include/gk_externs.h @@ -10,15 +10,25 @@ #ifndef _GK_EXTERNS_H_ #define _GK_EXTERNS_H_ +// Windows does not support _Thread_local. Use appropriate aliases +// Reference: https://stackoverflow.com/a/18298965 +#ifndef thread_local +#if __STDC_VERSION__ >= 201112 && !defined __STDC_NO_THREADS__ +#define thread_local _Thread_local +#elif defined _MSC_VER +#define thread_local __declspec(thread) +#elif defined __GNUC__ +#define thread_local __thread +#else +#error "Cannot define thread_local" +#endif +#endif -/************************************************************************* -* Extern variable definition. Hopefully, the __thread makes them thread-safe. -**************************************************************************/ #ifndef _GK_ERROR_C_ /* declared in error.c */ -extern __thread int gk_cur_jbufs; -extern __thread jmp_buf gk_jbufs[]; -extern __thread jmp_buf gk_jbuf; +extern thread_local int gk_cur_jbufs; +extern thread_local jmp_buf gk_jbufs[]; +extern thread_local jmp_buf gk_jbuf; #endif diff --git a/src/error.c b/src/error.c index e2a18cf..e053537 100644 --- a/src/error.c +++ b/src/error.c @@ -19,17 +19,17 @@ This file contains functions dealing with error reporting and termination /* These are the jmp_buf for the graceful exit in case of severe errors. Multiple buffers are defined to allow for recursive invokation. */ #define MAX_JBUFS 128 -__thread int gk_cur_jbufs=-1; -__thread jmp_buf gk_jbufs[MAX_JBUFS]; -__thread jmp_buf gk_jbuf; +thread_local int gk_cur_jbufs=-1; +thread_local jmp_buf gk_jbufs[MAX_JBUFS]; +thread_local jmp_buf gk_jbuf; typedef void (*gksighandler_t)(int); /* These are the holders of the old singal handlers for the trapped signals */ -static __thread gksighandler_t old_SIGMEM_handler; /* Custom signal */ -static __thread gksighandler_t old_SIGERR_handler; /* Custom signal */ -static __thread gksighandler_t old_SIGMEM_handlers[MAX_JBUFS]; /* Custom signal */ -static __thread gksighandler_t old_SIGERR_handlers[MAX_JBUFS]; /* Custom signal */ +static thread_local gksighandler_t old_SIGMEM_handler; /* Custom signal */ +static thread_local gksighandler_t old_SIGERR_handler; /* Custom signal */ +static thread_local gksighandler_t old_SIGMEM_handlers[MAX_JBUFS]; /* Custom signal */ +static thread_local gksighandler_t old_SIGERR_handlers[MAX_JBUFS]; /* Custom signal */ /* The following is used to control if the gk_errexit() will actually abort or not. There is always a single copy of this variable */ @@ -178,7 +178,7 @@ char *gk_strerror(int errnum) return strerror(errnum); #else #ifndef SUNOS - static __thread char buf[1024]; + static thread_local char buf[1024]; strerror_r(errnum, buf, 1024); diff --git a/src/memory.c b/src/memory.c index e6dc99c..616436d 100644 --- a/src/memory.c +++ b/src/memory.c @@ -16,7 +16,7 @@ can be used to define other memory allocation routines. #include /* This is for the global mcore that tracks all heap allocations */ -static __thread gk_mcore_t *gkmcore = NULL; +static thread_local gk_mcore_t *gkmcore = NULL; /*************************************************************************/