Skip to content

Scheduler: Use the root task as a scheduler task (alternative 1) #57543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/julia_threads.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ typedef struct _jl_tls_states_t {
struct _jl_task_t *next_task;
struct _jl_task_t *previous_task;
struct _jl_task_t *root_task;
// The scheduler sets this to instruct the task switching code (jl_switch())
// to wait in the scheduler for another task.
int8_t wait_in_scheduler;
struct _jl_timing_block_t *timing_stack;
// This is the location of our copy_stack
void *stackbase;
Expand Down
13 changes: 12 additions & 1 deletion src/scheduler.c
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,18 @@ JL_DLLEXPORT jl_task_t *jl_task_get_next(jl_value_t *trypoptask, jl_value_t *q,

jl_cpu_pause();
jl_ptls_t ptls = ct->ptls;
if (sleep_check_after_threshold(&start_cycles) || (ptls->tid == jl_atomic_load_relaxed(&io_loop_tid) && (!jl_atomic_load_relaxed(&_threadedregion) || wait_empty))) {
if (sleep_check_after_threshold(&start_cycles) ||
(ptls->tid == jl_atomic_load_relaxed(&io_loop_tid) &&
(!jl_atomic_load_relaxed(&_threadedregion) || wait_empty))) {
// The place seems empty and this thread is on its way to sleeping;
// switch to the root task to do that and instruct the task switching
// code to wait for another task.
if (ct != ptls->root_task) {
ptls->wait_in_scheduler = 1;
jl_switchto(&ptls->root_task);
return ct;
}

// acquire sleep-check lock
assert(jl_atomic_load_relaxed(&ptls->sleep_check_state) == not_sleeping);
jl_atomic_store_relaxed(&ptls->sleep_check_state, sleeping);
Expand Down
16 changes: 15 additions & 1 deletion src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@ JL_DLLEXPORT jl_task_t *jl_get_next_task(void) JL_NOTSAFEPOINT
const char tsan_state_corruption[] = "TSAN state corrupted. Exiting HARD!\n";
#endif

// is `t` the root task?
static int is_root_task(jl_task_t *ct, jl_task_t *t)
{
return ct->ptls->root_task == t;
}

JL_NO_ASAN static void ctx_switch(jl_task_t *lastt)
{
jl_ptls_t ptls = lastt->ptls;
Expand All @@ -446,7 +452,8 @@ JL_NO_ASAN static void ctx_switch(jl_task_t *lastt)
}
#endif

int killed = jl_atomic_load_relaxed(&lastt->_state) != JL_TASK_STATE_RUNNABLE;
int killed = (jl_atomic_load_relaxed(&lastt->_state) != JL_TASK_STATE_RUNNABLE) &&
!is_root_task(t, lastt);
if (!t->ctx.started && !t->ctx.copy_stack) {
// may need to allocate the stack
if (t->ctx.stkbuf == NULL) {
Expand Down Expand Up @@ -663,6 +670,7 @@ JL_DLLEXPORT void jl_switch(void) JL_NOTSAFEPOINT_LEAVE JL_NOTSAFEPOINT_ENTER
{
jl_task_t *ct = jl_current_task;
jl_ptls_t ptls = ct->ptls;
switch_restart:
jl_task_t *t = ptls->next_task;
if (t == ct) {
return;
Expand Down Expand Up @@ -711,6 +719,12 @@ JL_DLLEXPORT void jl_switch(void) JL_NOTSAFEPOINT_LEAVE JL_NOTSAFEPOINT_ENTER
if (other_defer_signal && !defer_signal)
jl_sigint_safepoint(ptls);

if (ptls->wait_in_scheduler) {
ptls->wait_in_scheduler = 0;
jl_set_next_task(jl_task_get_next());
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vtjnash: how would we get the various arguments needed for jl_task_get_next here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That seems quite tedious. I think #57544 is probably the better direction to go

goto switch_restart;
}

JL_PROBE_RT_RUN_TASK(ct);
jl_gc_unsafe_leave(ptls, gc_state);
}
Expand Down