Skip to content

Commit

Permalink
Try to implement our own TLS on windows too since it does not seem to…
Browse files Browse the repository at this point in the history
… have a static TLS model either......
  • Loading branch information
yuyichao committed Jun 28, 2016
1 parent 76253f6 commit 09a0be0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/julia_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ STATIC_INLINE int8_t jl_gc_state_set(jl_tls_states_t *ptls,
}
#else // ifndef JULIA_ENABLE_THREADING
typedef jl_tls_states_t *(*jl_get_ptls_states_func)(void);
#ifndef _OS_DARWIN_
#if !defined(_OS_DARWIN_) && !defined(_OS_WINDOWS_)
JL_DLLEXPORT void jl_set_ptls_states_getter(jl_get_ptls_states_func f);
#endif
// Make sure jl_gc_state() is always a rvalue
Expand Down
45 changes: 40 additions & 5 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ extern "C" {
#include "threading.h"

#ifdef JULIA_ENABLE_THREADING
# ifdef _OS_DARWIN_
# if defined(_OS_DARWIN_)
// Mac doesn't seem to have static TLS model so the runtime TLS getter
// registration will only add overhead to TLS access. The `__thread` variables
// are emulated with `pthread_key_t` so it is actually faster to use it directly.
Expand Down Expand Up @@ -62,15 +62,50 @@ jl_get_ptls_states_func jl_get_ptls_states_getter(void)
// for codegen
return &jl_get_ptls_states_fast;
}
# elif defined(_OS_WINDOWS_)
// Apparently windows doesn't have a static TLS model (or one that can be
// reliably used from a shared library) either..... Use `TLSAlloc` instead.

static DWORD jl_tls_key;

// Put this here for now. We can move this out later if we find more use for it.
BOOLEAN WINAPI DllMain(IN HINSTANCE hDllHandle, IN DWORD nReason,
IN LPVOID Reserved)
{
switch (nReason) {
case DLL_PROCESS_ATTACH:
jl_tls_key = TlsAlloc();
assert(jl_tls_key != TLS_OUT_OF_INDEXES);
// Fall through
case DLL_THREAD_ATTACH:
TlsSetValue(jl_tls_key, calloc(1, sizeof(jl_tls_states_t)));
break;
case DLL_THREAD_DETACH:
free(TlsGetValue(jl_tls_key));
TlsSetValue(jl_tls_key, NULL);
break;
case DLL_PROCESS_DETACH:
free(TlsGetValue(jl_tls_key));
TlsFree(jl_tls_key);
break;
}
}

JL_DLLEXPORT JL_CONST_FUNC jl_tls_states_t *(jl_get_ptls_states)(void)
{
return TlsGetValue(jl_tls_key);

This comment has been minimized.

Copy link
@tkelman

tkelman Jun 29, 2016

Contributor

needs a cast

}

jl_get_ptls_states_func jl_get_ptls_states_getter(void)
{
// for codegen
return &jl_get_ptls_states;
}
# else
// fallback provided for embedding
static JL_CONST_FUNC jl_tls_states_t *jl_get_ptls_states_fallback(void)
{
# if !defined(_COMPILER_MICROSOFT_)
static __thread jl_tls_states_t tls_states;
# else
static __declspec(thread) jl_tls_states_t tls_states;
# endif
return &tls_states;
}
static jl_tls_states_t *jl_get_ptls_states_init(void);
Expand Down
8 changes: 2 additions & 6 deletions ui/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,10 @@
extern "C" {
#endif

#if defined(JULIA_ENABLE_THREADING) && !defined(_OS_DARWIN_)
#if defined(JULIA_ENABLE_THREADING) && !defined(_OS_DARWIN_) && !defined(_OS_WINDOWS_)
static JL_CONST_FUNC jl_tls_states_t *jl_get_ptls_states_static(void)
{
# if !defined(_COMPILER_MICROSOFT_)
static __attribute__((tls_model("local-exec"))) __thread jl_tls_states_t tls_states;
# else
static __declspec(thread) jl_tls_states_t tls_states;
# endif
return &tls_states;
}
#endif
Expand Down Expand Up @@ -660,7 +656,7 @@ int wmain(int argc, wchar_t *argv[], wchar_t *envp[])
argv[i] = (wchar_t*)arg;
}
#endif
#if defined(JULIA_ENABLE_THREADING) && !defined(_OS_DARWIN_)
#if defined(JULIA_ENABLE_THREADING) && !defined(_OS_DARWIN_) && !defined(_OS_WINDOWS_)
// We need to make sure this function is called before any reference to
// TLS variables. Since the compiler is free to move calls to
// `jl_get_ptls_states()` around, we should avoid referencing TLS
Expand Down

0 comments on commit 09a0be0

Please sign in to comment.