Skip to content

Commit

Permalink
fix #8551 and make stack switch more efficient
Browse files Browse the repository at this point in the history
previously, each new stack would have a copy of a small portion of the
stack that started it. in small amounts, this was fine, but it is bad if
the user is starting many tasks. additionally, it wasted memory inside
every task to have a per-task base_ctx.

(cherry picked from commit ac13711)
  • Loading branch information
vtjnash authored and tkelman committed Dec 11, 2014
1 parent c1dfdca commit 06d01c2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -992,11 +992,12 @@ DLLEXPORT void jl_handle_stack_switch();

#ifdef COPY_STACKS
// initialize base context of root task
extern jl_jmp_buf jl_base_ctx;
#define JL_SET_STACK_BASE \
{ \
int __stk; \
jl_root_task->stackbase = (char*)&__stk; \
if (jl_setjmp(jl_root_task->base_ctx, 1)) { \
if (jl_setjmp(jl_base_ctx, 1)) { \
jl_handle_stack_switch(); \
} \
}
Expand Down Expand Up @@ -1145,7 +1146,6 @@ typedef struct _jl_task_t {
void *stackbase;
void *stack;
};
jl_jmp_buf base_ctx;
size_t bufsz;
void *stkbuf;
size_t ssize;
Expand Down
15 changes: 4 additions & 11 deletions src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ volatile int jl_in_stackwalk = 0;
#include <dlfcn.h> // for dladdr
#endif

jl_jmp_buf jl_base_ctx;

/* This probing code is derived from Douglas Jones' user thread library */

/* true if stack grows up, false if down */
Expand Down Expand Up @@ -211,11 +213,6 @@ static void switch_stack(jl_task_t *t, jl_jmp_buf *where)
restore_stack(t, where, NULL);
}
}

void jl_switch_stack(jl_task_t *t, jl_jmp_buf *where)
{
switch_stack(t, where);
}
#endif

static void ctx_switch(jl_task_t *t, jl_jmp_buf *where)
Expand Down Expand Up @@ -262,7 +259,7 @@ static void ctx_switch(jl_task_t *t, jl_jmp_buf *where)

#ifdef COPY_STACKS
jl_jmp_target = where;
jl_longjmp(lastt->base_ctx, 1);
jl_longjmp(jl_base_ctx, 1);
#else
jl_longjmp(*where, 1);
#endif
Expand Down Expand Up @@ -418,10 +415,6 @@ static void start_task(jl_task_t *t)
local_sp += sizeof(jl_gcframe_t);
local_sp += 12*sizeof(void*);
t->stackbase = (void*)(local_sp + _frame_offset);
if (jl_setjmp(t->base_ctx, 0)) {
// we get here to remove our data from the process stack
switch_stack(jl_current_task, jl_jmp_target);
}
#endif
res = jl_apply(t->start, NULL, 0);
JL_GC_POP();
Expand All @@ -431,7 +424,7 @@ static void start_task(jl_task_t *t)

DLLEXPORT void jl_handle_stack_switch()
{
jl_switch_stack(jl_current_task, jl_jmp_target);
switch_stack(jl_current_task, jl_jmp_target);
}

#ifndef COPY_STACKS
Expand Down

0 comments on commit 06d01c2

Please sign in to comment.