@@ -74,6 +74,15 @@ static void jl_swap_fiber(jl_ucontext_t *lastt, jl_ucontext_t *t);
74
74
static JL_THREAD unw_cursor_t jl_basecursor ;
75
75
#endif
76
76
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
+
77
86
#ifdef COPY_STACKS
78
87
79
88
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)
323
332
jl_swap_fiber (lastt_ctx , & t -> ctx );
324
333
}
325
334
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 );
327
339
}
328
340
// TODO: mutex unlock the thread we just switched from
329
341
#ifdef ENABLE_TIMINGS
@@ -459,12 +471,13 @@ JL_DLLEXPORT jl_task_t *jl_new_task(jl_function_t *start, jl_value_t *completion
459
471
t -> copy_stack = 0 ;
460
472
if (ssize == 0 ) {
461
473
// 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
+ }
468
481
}
469
482
else {
470
483
// user requested dedicated stack of a certain size
@@ -922,7 +935,6 @@ void jl_init_root_task(void *stack_lo, void *stack_hi)
922
935
jl_ptls_t ptls = jl_get_ptls_states ();
923
936
ptls -> current_task = (jl_task_t * )jl_gc_alloc (ptls , sizeof (jl_task_t ),
924
937
jl_task_type );
925
- ptls -> current_task -> copy_stack = 0 ;
926
938
void * stack = stack_lo ;
927
939
size_t ssize = (char * )stack_hi - (char * )stack_lo ;
928
940
#ifndef _OS_WINDOWS_
@@ -931,8 +943,16 @@ void jl_init_root_task(void *stack_lo, void *stack_hi)
931
943
ssize += ROOT_TASK_STACK_ADJUSTMENT ; // sizeof stack is known exactly, but not where we are in that stack
932
944
}
933
945
#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
+ }
936
956
ptls -> current_task -> started = 1 ;
937
957
ptls -> current_task -> next = jl_nothing ;
938
958
ptls -> current_task -> queue = jl_nothing ;
@@ -955,7 +975,15 @@ void jl_init_root_task(void *stack_lo, void *stack_hi)
955
975
956
976
ptls -> root_task = ptls -> current_task ;
957
977
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
+ }
959
987
}
960
988
961
989
JL_DLLEXPORT int jl_is_task_started (jl_task_t * t )
0 commit comments