AmendHub

jcs

/

subtext

/

amendments

/

239

uthread: Crank stack sizes for each thread

Something having to do with QuickDraw operations puts a ton of stuff
on the stack momentarily, which was writing into our stack gaps with
3K stacks. I haven't been able to figure out what actually causes
it, but when the logger window is disabled, it never happens.
Move defines into uthread.c file so they can be tweaked without having
to recompile everything.

jcs made amendment 239 about 1 month ago
--- uthread.c Wed Aug 3 14:25:06 2022 +++ uthread.c Thu Aug 4 21:34:25 2022 @@ -23,6 +23,12 @@ #include "uthread.h" #include "util.h" +#define COORDINATOR_STACK_SIZE (1024UL * 100UL) +#define STACK_SIZE (1024UL * 50UL) +#define STACK_GAP 1024 +#define STACK_GAP_FILL 0xff +#define CANARY 0xdeadf00d + jmp_buf uthread_coord_env; static struct uthread uthreads[NUM_UTHREADS]; struct uthread *uthread_current = NULL; @@ -71,21 +77,29 @@ uthread_init(void) stacks_bottom = (unsigned long)main_stack_gap - (STACK_SIZE * NUM_UTHREADS); - - uthread_ApplLimit = stacks_bottom; // - (1024 * 5); + + if (stacks_bottom > stacks_top) + panic("stacks_bottom > stacks_top"); + + uthread_ApplLimit = stacks_bottom - (1024UL * 64); + if (uthread_ApplLimit < (unsigned long)ApplZone) + panic("stacks_bottom < ApplZone"); + SetApplLimit((Ptr)uthread_ApplLimit); if (MemError()) panic("Failed to SetApplLimit to %lu", uthread_ApplLimit); - /* color in our stack gap */ - memset(main_stack_gap, STACK_GAP_FILL, STACK_GAP); + /* color in our whole uthread stack space */ + memset((Ptr)uthread_ApplLimit, STACK_GAP_FILL, + (unsigned long)main_stack_gap + STACK_GAP - uthread_ApplLimit); + /* this is what we'll compare each gap to */ memset(uthread_stack_gap_fill, STACK_GAP_FILL, STACK_GAP); for (i = 0; i < NUM_UTHREADS; i++) { uthreads[i].id = i; uthreads[i].state = UTHREAD_STATE_DEAD; - uthreads[i].stack_loc = stacks_bottom + (STACK_SIZE * (i + 1)); - uthreads[i].stack_gap = uthreads[i].stack_loc - STACK_SIZE + 1; + uthreads[i].stack_loc = stacks_bottom + (STACK_SIZE * (i + 1)) - 2; + uthreads[i].stack_gap = uthreads[i].stack_loc - STACK_SIZE + 2; } uthread_verify(); @@ -104,7 +118,7 @@ uthread_add(void *func, void *arg) uthreads[i].arg = arg; uthreads[i].state = UTHREAD_STATE_SETUP; - /* color in stack gap */ + /* color in stack gap just to be sure */ memset((Ptr)uthreads[i].stack_gap, STACK_GAP_FILL, STACK_GAP); return &uthreads[i]; @@ -126,6 +140,8 @@ uthread_yield(void) if (uthread_current == NULL) panic("uthread_yield not from a thread!"); + uthread_verify(); + if ((uthread_current->stack_loc - _sp) > (STACK_SIZE / 2)) panic("thread %d stack growing too large " "[SP=0x%lx] [stack=0x%lx] [gap=0x%lx-0x%lx]", @@ -142,8 +158,6 @@ uthread_yield(void) if (magic != CANARY) panic("uthread stack canary dead!"); - - uthread_verify(); } void --- uthread.h Wed Aug 3 14:25:24 2022 +++ uthread.h Thu Aug 11 09:32:20 2022 @@ -19,12 +19,7 @@ #ifndef __UTHREAD_H__ #define __UTHREAD_H__ -#define NUM_UTHREADS 8 -#define COORDINATOR_STACK_SIZE (1024 * 10) -#define STACK_SIZE (1024 * 3) -#define STACK_GAP 255 -#define STACK_GAP_FILL 0xff -#define CANARY 0xdeadf00d +#define NUM_UTHREADS 6 enum { UTHREAD_STATE_DEAD,