Skip to content

Commit

Permalink
add missing increment of nrunning for jl_adopt_thread
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Dec 13, 2023
1 parent 3495404 commit ab1dda2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
16 changes: 15 additions & 1 deletion src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static const int16_t sleeping_like_the_dead JL_UNUSED = 2;
// a running count of how many threads are currently not_sleeping
// plus a running count of the number of in-flight wake-ups
// n.b. this may temporarily exceed jl_n_threads
static _Atomic(int) nrunning = 1;
static _Atomic(int) nrunning = 0;

// invariant: No thread is ever asleep unless sleep_check_state is sleeping (or we have a wakeup signal pending).
// invariant: Any particular thread is not asleep unless that thread's sleep_check_state is sleeping.
Expand Down Expand Up @@ -201,6 +201,20 @@ void jl_threadfun(void *arg)
}



void jl_init_thread_scheduler(jl_ptls_t ptls) JL_NOTSAFEPOINT
{
uv_mutex_init(&ptls->sleep_lock);
uv_cond_init(&ptls->wake_signal);
// record that there is now another thread that may be used to schedule work
// we will decrement this again in scheduler_delete_thread, only slightly
// in advance of pthread_join (which hopefully itself also had been
// adopted by now and is included in nrunning too)
(void)jl_atomic_fetch_add_relaxed(&nrunning, 1);
// n.b. this is the only point in the code where we ignore the invariants on the ordering of nrunning
// since we are being initialized from foreign code, we could not necessarily have expected or predicted that to happen
}

int jl_running_under_rr(int recheck)
{
#ifdef _OS_LINUX_
Expand Down
5 changes: 2 additions & 3 deletions src/threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ int jl_all_tls_states_size;
static uv_cond_t cond;
// concurrent reads are permitted, using the same pattern as mtsmall_arraylist
// it is implemented separately because the API of direct jl_all_tls_states use is already widely prevalent
void jl_init_thread_scheduler(jl_ptls_t ptls) JL_NOTSAFEPOINT;

// return calling thread's ID
JL_DLLEXPORT int16_t jl_threadid(void)
Expand Down Expand Up @@ -379,9 +380,7 @@ jl_ptls_t jl_init_threadtls(int16_t tid)
ptls->bt_data = bt_data;
small_arraylist_new(&ptls->locks, 0);
jl_init_thread_heap(ptls);

uv_mutex_init(&ptls->sleep_lock);
uv_cond_init(&ptls->wake_signal);
jl_init_thread_scheduler(ptls);

uv_mutex_lock(&tls_lock);
if (tid == -1)
Expand Down

0 comments on commit ab1dda2

Please sign in to comment.