Skip to content

Commit

Permalink
Merge pull request #50083 from topolarity/fasttls-semaphore
Browse files Browse the repository at this point in the history
Prevent registering static FASTTLS with multiple libjulias
  • Loading branch information
topolarity authored Jun 13, 2023
2 parents 9d839f9 + 97f445e commit 84fc130
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
9 changes: 7 additions & 2 deletions cli/loader_lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -527,8 +527,13 @@ __attribute__((constructor)) void jl_load_libjulia_internal(void) {
}
void *fptr = lookup_symbol(RTLD_DEFAULT, "jl_get_pgcstack_static");
void *(*key)(void) = lookup_symbol(RTLD_DEFAULT, "jl_pgcstack_addr_static");
if (fptr != NULL && key != NULL)
jl_pgcstack_setkey(fptr, key);
_Atomic(char) *semaphore = lookup_symbol(RTLD_DEFAULT, "jl_pgcstack_static_semaphore");
if (fptr != NULL && key != NULL && semaphore != NULL) {
char already_used = 0;
atomic_compare_exchange_strong(semaphore, &already_used, 1);
if (already_used == 0) // RMW succeeded - we have exclusive access
jl_pgcstack_setkey(fptr, key);
}
#endif

// jl_options must be initialized very early, in case an embedder sets some
Expand Down
8 changes: 8 additions & 0 deletions src/julia_fasttls.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
#ifndef JL_FASTTLS_H
#define JL_FASTTLS_H

#ifdef __cplusplus
#include <atomic>
#define _Atomic(T) std::atomic<T>
#else
#include <stdatomic.h>
#endif

// Thread-local storage access

#ifdef __cplusplus
Expand All @@ -25,6 +32,7 @@ typedef jl_gcframe_t **(jl_get_pgcstack_func)(void);
#if !defined(_OS_DARWIN_) && !defined(_OS_WINDOWS_)
#define JULIA_DEFINE_FAST_TLS \
static __attribute__((tls_model("local-exec"))) __thread jl_gcframe_t **jl_pgcstack_localexec; \
JL_DLLEXPORT _Atomic(char) jl_pgcstack_static_semaphore; \
JL_DLLEXPORT jl_gcframe_t **jl_get_pgcstack_static(void) \
{ \
return jl_pgcstack_localexec; \
Expand Down

0 comments on commit 84fc130

Please sign in to comment.