From 4e882275f599479b5d7445feb225e480bce49304 Mon Sep 17 00:00:00 2001 From: Samuel Hym Date: Wed, 15 Feb 2023 16:47:18 +0100 Subject: [PATCH] Put error global variables into thread-local storage Global variable `error_buffer` is used to store a string that is returned to callers, so there is a race condition if dynamic linking is invoked from 2 OCaml domains in parallel Since the error message must be returned, a mutex cannot be used to prevent the race condition GNU libc uses that same solution: keep the last error in thread-local storage; so support for calling dlerror from a different thread than the one calling dlopen is not to be expected Co-authored-by: David Allsopp --- flexdll.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/flexdll.c b/flexdll.c index b19fe2a..a90a11f 100644 --- a/flexdll.c +++ b/flexdll.c @@ -47,8 +47,11 @@ typedef struct dlunit { } dlunit; typedef void *resolver(void*, const char*); -static int error = 0; -static char error_buffer[256]; +#ifdef _MSC_VER +#define __thread __declspec(thread) +#endif +static __thread int error = 0; +static __thread char error_buffer[256]; /* Emulate a low-level dlopen-like interface */