Skip to content

Commit

Permalink
Fix race cond. in thread init (_julia_init could read thread state be…
Browse files Browse the repository at this point in the history
…fore it was setup). Closes #14091
carnaval authored and yuyichao committed Nov 23, 2015

Verified

This commit was signed with the committer’s verified signature.
huangzhen1997 Joe Huang
1 parent 221ed6d commit 7afed16
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/threading.c
Original file line number Diff line number Diff line change
@@ -147,6 +147,8 @@ uint64_t *user_ticks;
uint64_t *join_ticks;
#endif

static uv_barrier_t thread_init_done;

// thread function: used by all except the main thread
void ti_threadfun(void *arg)
{
@@ -167,7 +169,7 @@ void ti_threadfun(void *arg)
while (ta->state == TI_THREAD_INIT)
cpu_pause();
cpu_lfence();

uv_barrier_wait(&thread_init_done);
// initialize this thread in the thread group
tg = ta->tg;
ti_threadgroup_initthread(tg, ti_tid);
@@ -289,6 +291,9 @@ void jl_start_threads(void)
jl_all_task_states[0].system_id = pthread_self();
#endif
targs = (ti_threadarg_t **)malloc((jl_n_threads - 1) * sizeof (ti_threadarg_t *));

uv_barrier_init(&thread_init_done, jl_n_threads);

for (i = 0; i < jl_n_threads - 1; ++i) {
targs[i] = (ti_threadarg_t *)malloc(sizeof (ti_threadarg_t));
targs[i]->state = TI_THREAD_INIT;
@@ -316,6 +321,8 @@ void jl_start_threads(void)
targs[i]->state = TI_THREAD_WORK;
}

uv_barrier_wait(&thread_init_done);

// free the argument array; the threads will free their arguments
free(targs);
}

0 comments on commit 7afed16

Please sign in to comment.