Skip to content

Commit

Permalink
Use C standard thread_local
Browse files Browse the repository at this point in the history
Signed-off-by: Cristian Le <[email protected]>
  • Loading branch information
LecrisUT committed Oct 27, 2023
1 parent 0a6de8c commit 3379e9f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
22 changes: 16 additions & 6 deletions include/gk_externs.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 8 additions & 8 deletions src/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ can be used to define other memory allocation routines.
#include <GKlib.h>

/* 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;


/*************************************************************************/
Expand Down

0 comments on commit 3379e9f

Please sign in to comment.