AmendHub

Download

jcs

/

subtext

/

uthread.c

 

(View History)

jcs   uthread: Crank stack sizes for each thread Latest amendment: 239 on 2022-08-11

1 /*
2 * Copyright (c) 2021 joshua stein <jcs@jcs.org>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #include <stdarg.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <setjmp.h>
22
23 #include "uthread.h"
24 #include "util.h"
25
26 #define COORDINATOR_STACK_SIZE (1024UL * 100UL)
27 #define STACK_SIZE (1024UL * 50UL)
28 #define STACK_GAP 1024
29 #define STACK_GAP_FILL 0xff
30 #define CANARY 0xdeadf00d
31
32 jmp_buf uthread_coord_env;
33 static struct uthread uthreads[NUM_UTHREADS];
34 struct uthread *uthread_current = NULL;
35 static Ptr main_stack_gap = NULL;
36 static char uthread_stack_gap_fill[STACK_GAP];
37 unsigned long uthread_ApplLimit = 0;
38
39 void uthread_begin(struct uthread *uthread);
40
41 /*
42 * | A5 world |
43 * |-------------------| 0x26234E CurStackBase (stacks_top)
44 * | main stack | COORDINATOR_STACK_SIZE
45 * |-------------------| 0x260F4E
46 * | main stack gap | STACK_GAP
47 * |-------------------| 0x260C00 (main_stack_gap, upward)
48 * : (other threads) :
49 * |-------------------| 0x25DC00 ((STACK_SIZE - STACK_GAP) downward)
50 * | uthread 1 stack |
51 * | |
52 * |-------------------|
53 * | uthread 1 gap | 0x25D001 (STACK_GAP upward)
54 * |-------------------| 0x25D000 (downward)
55 * | uthread 0 stack |
56 * | |
57 * |-------------------|
58 * | uthread 0 gap | 0x25C401 (STACK_GAP upward)
59 * |-------------------| 0x25C400 ApplLimit (stacks_bottom)
60 * | |
61 * | heap |
62 * | |
63 * |-------------------| 0x16F5D8 ApplZone
64 */
65 void
66 uthread_init(void)
67 {
68 unsigned long stacks_top, stacks_bottom;
69 short i;
70
71 stacks_top = (unsigned long)CurStackBase;
72 main_stack_gap = (Ptr)(stacks_top - COORDINATOR_STACK_SIZE -
73 STACK_GAP);
74 /* align uthread stacks */
75 main_stack_gap = (Ptr)((unsigned long)main_stack_gap -
76 ((unsigned long)main_stack_gap % 1024));
77
78 stacks_bottom = (unsigned long)main_stack_gap -
79 (STACK_SIZE * NUM_UTHREADS);
80
81 if (stacks_bottom > stacks_top)
82 panic("stacks_bottom > stacks_top");
83
84 uthread_ApplLimit = stacks_bottom - (1024UL * 64);
85 if (uthread_ApplLimit < (unsigned long)ApplZone)
86 panic("stacks_bottom < ApplZone");
87
88 SetApplLimit((Ptr)uthread_ApplLimit);
89 if (MemError())
90 panic("Failed to SetApplLimit to %lu", uthread_ApplLimit);
91
92 /* color in our whole uthread stack space */
93 memset((Ptr)uthread_ApplLimit, STACK_GAP_FILL,
94 (unsigned long)main_stack_gap + STACK_GAP - uthread_ApplLimit);
95 /* this is what we'll compare each gap to */
96 memset(uthread_stack_gap_fill, STACK_GAP_FILL, STACK_GAP);
97
98 for (i = 0; i < NUM_UTHREADS; i++) {
99 uthreads[i].id = i;
100 uthreads[i].state = UTHREAD_STATE_DEAD;
101 uthreads[i].stack_loc = stacks_bottom + (STACK_SIZE * (i + 1)) - 2;
102 uthreads[i].stack_gap = uthreads[i].stack_loc - STACK_SIZE + 2;
103 }
104
105 uthread_verify();
106 }
107
108 struct uthread *
109 uthread_add(void *func, void *arg)
110 {
111 short i;
112
113 for (i = 0; i < NUM_UTHREADS; i++) {
114 if (uthreads[i].state != UTHREAD_STATE_DEAD)
115 continue;
116
117 uthreads[i].func = func;
118 uthreads[i].arg = arg;
119 uthreads[i].state = UTHREAD_STATE_SETUP;
120
121 /* color in stack gap just to be sure */
122 memset((Ptr)uthreads[i].stack_gap, STACK_GAP_FILL, STACK_GAP);
123
124 return &uthreads[i];
125 }
126
127 return NULL;
128 }
129
130 void
131 uthread_yield(void)
132 {
133 volatile long magic = CANARY;
134 register unsigned long _sp = 0;
135
136 asm {
137 move.l a7, _sp
138 };
139
140 if (uthread_current == NULL)
141 panic("uthread_yield not from a thread!");
142
143 uthread_verify();
144
145 if ((uthread_current->stack_loc - _sp) > (STACK_SIZE / 2))
146 panic("thread %d stack growing too large "
147 "[SP=0x%lx] [stack=0x%lx] [gap=0x%lx-0x%lx]",
148 uthread_current->id, _sp, uthread_current->stack_loc,
149 uthread_current->stack_gap,
150 uthread_current->stack_gap + STACK_GAP);
151
152 if (uthread_current->state != UTHREAD_STATE_SLEEPING)
153 uthread_current->state = UTHREAD_STATE_YIELDING;
154
155 if (setjmp(uthread_current->env) == 0)
156 longjmp(uthread_coord_env, UTHREAD_SETJMP_YIELDED);
157 /* will not return */
158
159 if (magic != CANARY)
160 panic("uthread stack canary dead!");
161 }
162
163 void
164 uthread_msleep(unsigned long millis)
165 {
166 /* Ticks is in 1/60 of a second, but it's easier to think in millis */
167 unsigned long ts = millis / ((double)1000 / (double)60);
168
169 uthread_current->state = UTHREAD_STATE_SLEEPING;
170 uthread_current->sleeping_until = Ticks + ts;
171 uthread_yield();
172 }
173
174 void
175 uthread_begin(struct uthread *uthread)
176 {
177 register unsigned long stack_loc = uthread->stack_loc;
178 asm {
179 move.l stack_loc, a7
180 };
181
182 uthread->func(uthread, uthread->arg);
183
184 uthread_verify();
185
186 /* uthread variable is probably trashed at this point */
187 if (uthread_current->state == UTHREAD_STATE_REPEAT)
188 uthread_current->state = UTHREAD_STATE_SETUP;
189 else
190 uthread_current->state = UTHREAD_STATE_DEAD;
191
192 longjmp(uthread_coord_env, UTHREAD_SETJMP_YIELDED);
193 }
194
195 void
196 uthread_coordinate(void)
197 {
198 short i, n;
199 unsigned char *gap;
200
201 for (i = 0; i < NUM_UTHREADS; i++) {
202 if (uthreads[i].state == UTHREAD_STATE_DEAD)
203 continue;
204 if (uthreads[i].state == UTHREAD_STATE_SLEEPING &&
205 Ticks < uthreads[i].sleeping_until)
206 continue;
207
208 switch (setjmp(uthread_coord_env)) {
209 case UTHREAD_SETJMP_DEFAULT:
210 uthread_current = &uthreads[i];
211
212 switch (uthread_current->state) {
213 case UTHREAD_STATE_SETUP:
214 uthread_current->state = UTHREAD_STATE_RUNNING;
215 uthread_begin(uthread_current);
216 break;
217 case UTHREAD_STATE_YIELDING:
218 case UTHREAD_STATE_SLEEPING:
219 uthread_current->state = UTHREAD_STATE_RUNNING;
220 longjmp(uthread_current->env, UTHREAD_SETJMP_YIELDED);
221 break;
222 }
223 break;
224 case UTHREAD_SETJMP_YIELDED:
225 break;
226 default:
227 panic("unknown uthread state");
228 }
229 }
230
231 uthread_current = NULL;
232 uthread_verify();
233 }
234
235 void
236 uthread_verify(void)
237 {
238 short i;
239 unsigned char *gap;
240
241 gap = (unsigned char *)main_stack_gap;
242 if (memcmp(gap, uthread_stack_gap_fill, STACK_GAP) != 0)
243 panic("coordinator spilled into stack gap");
244
245 for (i = 0; i < NUM_UTHREADS; i++) {
246 if (uthreads[i].state == UTHREAD_STATE_DEAD)
247 continue;
248
249 gap = (unsigned char *)(uthreads[i].stack_gap);
250 if (memcmp(gap, uthread_stack_gap_fill, STACK_GAP) != 0)
251 panic("thread %d spilled into stack gap", i);
252 }
253 }