Skip to content

Commit

Permalink
Add MVM_THREAD_LOCAL to declare variables with thread local storage
Browse files Browse the repository at this point in the history
If the compiler provides this, it's likely faster than UV's API, which is
(basically) POSIX thread local storage, and requires a function call for
each "get". UV appears to rely on Win32 compilers supporting this. All
recent gcc and clang versions support it, and seemingly also the vendor
compilers on Solaris and AIX.

(Likely also VMS, given that it originated in the ia64 ABI, but I suspect
UV support for VMS won't happen any time soon.)

Initially, just use this if UV defined UV_THREAD_LOCAL, which seems only to
be the case on Win32.
  • Loading branch information
nwc10 committed Oct 31, 2020
1 parent 4cfde6e commit ac941c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/moar.c
Expand Up @@ -87,7 +87,9 @@ MVM_STATIC_INLINE MVMuint64 ptr_hash_64_to_64(MVMuint64 u) {
return (MVMuint64)u;
}

#ifndef MVM_THREAD_LOCAL
#ifdef MVM_THREAD_LOCAL
MVM_THREAD_LOCAL MVMThreadContext *MVM_running_threads_context;
#else
uv_key_t MVM_running_threads_context_key;

static void
Expand Down
16 changes: 15 additions & 1 deletion src/moar.h
Expand Up @@ -293,7 +293,21 @@ AO_t AO_fetch_compare_and_swap_emulation(volatile AO_t *addr, AO_t old_val, AO_t
#define MVM_store(addr, new) AO_store_full((volatile AO_t *)(addr), (AO_t)(new))
#define MVM_load(addr) AO_load_full((volatile AO_t *)(addr))

#ifndef MVM_THREAD_LOCAL
/* UV always defines this for Win32 but not Unix. Which is fine, as we can probe
* for it on Unix more easily than on Win32. */
#if !defined(MVM_THREAD_LOCAL) && defined(UV_THREAD_LOCAL)
#define MVM_THREAD_LOCAL UV_THREAD_LOCAL
#endif

#ifdef MVM_THREAD_LOCAL

extern MVM_THREAD_LOCAL MVMThreadContext *MVM_running_threads_context;

MVM_STATIC_INLINE MVMThreadContext *MVM_get_running_threads_context(void) {
return MVM_running_threads_context;
}

#else

/* Fallback to an implememtation using UV's APIs (pretty much pthreads) */
extern uv_key_t MVM_running_threads_context_key;
Expand Down

0 comments on commit ac941c0

Please sign in to comment.