Skip to content

Commit 54aef6e

Browse files
committed
always run on original process stack in ALWAYS_COPY_STACKS mode
this makes it possible to work around #31104
1 parent 4dd91fa commit 54aef6e

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

src/task.c

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ static void jl_swap_fiber(jl_ucontext_t *lastt, jl_ucontext_t *t);
7474
static JL_THREAD unw_cursor_t jl_basecursor;
7575
#endif
7676

77+
#ifdef ALWAYS_COPY_STACKS
78+
# ifndef COPY_STACKS
79+
# error "ALWAYS_COPY_STACKS requires COPY_STACKS"
80+
# endif
81+
static int always_copy_stacks = 1;
82+
#else
83+
static int always_copy_stacks = 0;
84+
#endif
85+
7786
#ifdef COPY_STACKS
7887

7988
static void memcpy_a16(uint64_t *to, uint64_t *from, size_t nb)
@@ -323,7 +332,10 @@ static void ctx_switch(jl_ptls_t ptls, jl_task_t **pt)
323332
jl_swap_fiber(lastt_ctx, &t->ctx);
324333
}
325334
else {
326-
jl_start_fiber(lastt_ctx, &t->ctx);
335+
if (always_copy_stacks)
336+
jl_longjmp(ptls->base_ctx.uc_mcontext, 1);
337+
else
338+
jl_start_fiber(lastt_ctx, &t->ctx);
327339
}
328340
// TODO: mutex unlock the thread we just switched from
329341
#ifdef ENABLE_TIMINGS
@@ -459,12 +471,13 @@ JL_DLLEXPORT jl_task_t *jl_new_task(jl_function_t *start, jl_value_t *completion
459471
t->copy_stack = 0;
460472
if (ssize == 0) {
461473
// stack size unspecified; use default
462-
#if defined(COPY_STACKS) && defined(ALWAYS_COPY_STACKS)
463-
t->copy_stack = 1;
464-
t->bufsz = 0;
465-
#else
466-
t->bufsz = JL_STACK_SIZE;
467-
#endif
474+
if (always_copy_stacks) {
475+
t->copy_stack = 1;
476+
t->bufsz = 0;
477+
}
478+
else {
479+
t->bufsz = JL_STACK_SIZE;
480+
}
468481
}
469482
else {
470483
// user requested dedicated stack of a certain size
@@ -922,7 +935,6 @@ void jl_init_root_task(void *stack_lo, void *stack_hi)
922935
jl_ptls_t ptls = jl_get_ptls_states();
923936
ptls->current_task = (jl_task_t*)jl_gc_alloc(ptls, sizeof(jl_task_t),
924937
jl_task_type);
925-
ptls->current_task->copy_stack = 0;
926938
void *stack = stack_lo;
927939
size_t ssize = (char*)stack_hi - (char*)stack_lo;
928940
#ifndef _OS_WINDOWS_
@@ -931,8 +943,16 @@ void jl_init_root_task(void *stack_lo, void *stack_hi)
931943
ssize += ROOT_TASK_STACK_ADJUSTMENT; // sizeof stack is known exactly, but not where we are in that stack
932944
}
933945
#endif
934-
ptls->current_task->stkbuf = stack;
935-
ptls->current_task->bufsz = ssize;
946+
if (always_copy_stacks) {
947+
ptls->current_task->copy_stack = 1;
948+
ptls->current_task->stkbuf = NULL;
949+
ptls->current_task->bufsz = 0;
950+
}
951+
else {
952+
ptls->current_task->copy_stack = 0;
953+
ptls->current_task->stkbuf = stack;
954+
ptls->current_task->bufsz = ssize;
955+
}
936956
ptls->current_task->started = 1;
937957
ptls->current_task->next = jl_nothing;
938958
ptls->current_task->queue = jl_nothing;
@@ -955,7 +975,15 @@ void jl_init_root_task(void *stack_lo, void *stack_hi)
955975

956976
ptls->root_task = ptls->current_task;
957977

958-
jl_init_basefiber(JL_STACK_SIZE);
978+
if (always_copy_stacks) {
979+
ptls->stackbase = stack_hi;
980+
ptls->stacksize = ssize;
981+
if (jl_setjmp(ptls->base_ctx.uc_mcontext, 0))
982+
start_task();
983+
}
984+
else {
985+
jl_init_basefiber(JL_STACK_SIZE);
986+
}
959987
}
960988

961989
JL_DLLEXPORT int jl_is_task_started(jl_task_t *t)

0 commit comments

Comments
 (0)