Skip to content

Commit

Permalink
Put error global variables into thread-local storage
Browse files Browse the repository at this point in the history
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 <david.allsopp@metastack.com>
  • Loading branch information
shym and dra27 committed Mar 3, 2023
1 parent f5ccd97 commit 4e88227
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions flexdll.c
Expand Up @@ -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 */

Expand Down

0 comments on commit 4e88227

Please sign in to comment.