Skip to content

Commit

Permalink
Make sure the tls callback is always initialized before threads starts.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Nov 22, 2015
1 parent 8f7c0f3 commit c8ae160
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,10 @@ static void jl_resolve_sysimg_location(JL_IMAGE_SEARCH rel)

void _julia_init(JL_IMAGE_SEARCH rel)
{
#ifdef JULIA_ENABLE_THREADING
// Make sure we finalize the tls callback before starting any threads.
jl_get_ptls_states_getter();
#endif
libsupport_init();
jl_io_loop = uv_default_loop(); // this loop will internal events (spawning process etc.),
// best to call this first, since it also initializes libuv
Expand Down
16 changes: 14 additions & 2 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,32 @@ static JL_CONST_FUNC jl_tls_states_t *jl_get_ptls_states_fallback(void)
# endif
return &tls_states;
}
static jl_get_ptls_states_func jl_tls_states_cb = jl_get_ptls_states_fallback;
static jl_tls_states_t *jl_get_ptls_states_init(void);
static jl_get_ptls_states_func jl_tls_states_cb = jl_get_ptls_states_init;
static jl_tls_states_t *jl_get_ptls_states_init(void)
{
// This is clearly not thread safe but should be fine since we
// make sure the tls states callback is finalized before adding
// multiple threads
jl_tls_states_cb = jl_get_ptls_states_fallback;
return jl_get_ptls_states_fallback();
}
DLLEXPORT JL_CONST_FUNC jl_tls_states_t *(jl_get_ptls_states)(void)
{
return (*jl_tls_states_cb)();
}
DLLEXPORT void jl_set_ptls_states_getter(jl_get_ptls_states_func f)
{
// only allow setting this once
if (f && jl_tls_states_cb == jl_get_ptls_states_fallback) {
if (f && f != jl_get_ptls_states_init &&
jl_tls_states_cb == jl_get_ptls_states_init) {
jl_tls_states_cb = f;
}
}
jl_get_ptls_states_func jl_get_ptls_states_getter(void)
{
if (jl_tls_states_cb == jl_get_ptls_states_init)
jl_get_ptls_states_init();
// for codegen
return jl_tls_states_cb;
}
Expand Down

0 comments on commit c8ae160

Please sign in to comment.