1 1544f909 2004-12-25 devnull #include "threadimpl.h"
3 1544f909 2004-12-25 devnull int _threaddebuglevel;
5 1544f909 2004-12-25 devnull static uint threadnproc;
6 1544f909 2004-12-25 devnull static uint threadnsysproc;
7 1544f909 2004-12-25 devnull static Lock threadnproclock;
8 1544f909 2004-12-25 devnull static Ref threadidref;
9 4dbefdd4 2004-12-27 devnull static Proc *threadmainproc;
11 4dbefdd4 2004-12-27 devnull static void addproc(Proc*);
12 4dbefdd4 2004-12-27 devnull static void delproc(Proc*);
13 1544f909 2004-12-25 devnull static void addthread(_Threadlist*, _Thread*);
14 1544f909 2004-12-25 devnull static void delthread(_Threadlist*, _Thread*);
15 1544f909 2004-12-25 devnull static void addthreadinproc(Proc*, _Thread*);
16 1544f909 2004-12-25 devnull static void delthreadinproc(Proc*, _Thread*);
17 1544f909 2004-12-25 devnull static void contextswitch(Context *from, Context *to);
18 73722a8b 2004-12-27 devnull static void scheduler(Proc*);
20 2c87dda8 2004-12-28 devnull static void
21 2c87dda8 2004-12-28 devnull _threaddebug(char *fmt, ...)
23 2c87dda8 2004-12-28 devnull va_list arg;
24 2c87dda8 2004-12-28 devnull char buf[128];
25 2c87dda8 2004-12-28 devnull _Thread *t;
27 af89fc10 2005-01-06 devnull static int fd = -1;
30 b8f742db 2005-01-11 devnull va_start(arg, fmt);
31 b8f742db 2005-01-11 devnull vfprint(1, fmt, arg);
32 b8f742db 2005-01-11 devnull va_end(arg);
35 af89fc10 2005-01-06 devnull if(fd < 0){
36 af89fc10 2005-01-06 devnull p = strrchr(argv0, '/');
40 af89fc10 2005-01-06 devnull p = argv0;
41 af89fc10 2005-01-06 devnull snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
42 af89fc10 2005-01-06 devnull if((fd = create(buf, OWRITE, 0666)) < 0)
43 af89fc10 2005-01-06 devnull fd = open("/dev/null", OWRITE);
46 2c87dda8 2004-12-28 devnull va_start(arg, fmt);
47 2c87dda8 2004-12-28 devnull vsnprint(buf, sizeof buf, fmt, arg);
48 2c87dda8 2004-12-28 devnull va_end(arg);
49 2c87dda8 2004-12-28 devnull t = proc()->thread;
51 af89fc10 2005-01-06 devnull fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
53 af89fc10 2005-01-06 devnull fprint(fd, "%d._: %s\n", getpid(), buf);
56 1544f909 2004-12-25 devnull static _Thread*
57 1544f909 2004-12-25 devnull getthreadnow(void)
59 1544f909 2004-12-25 devnull return proc()->thread;
61 1544f909 2004-12-25 devnull _Thread *(*threadnow)(void) = getthreadnow;
63 1544f909 2004-12-25 devnull static Proc*
64 1544f909 2004-12-25 devnull procalloc(void)
68 1544f909 2004-12-25 devnull p = malloc(sizeof *p);
69 73722a8b 2004-12-27 devnull if(p == nil)
70 73722a8b 2004-12-27 devnull sysfatal("procalloc malloc: %r");
71 1544f909 2004-12-25 devnull memset(p, 0, sizeof *p);
72 4dbefdd4 2004-12-27 devnull addproc(p);
73 1544f909 2004-12-25 devnull lock(&threadnproclock);
74 1544f909 2004-12-25 devnull threadnproc++;
75 1544f909 2004-12-25 devnull unlock(&threadnproclock);
76 1544f909 2004-12-25 devnull return p;
79 1544f909 2004-12-25 devnull static void
80 1544f909 2004-12-25 devnull threadstart(void *v)
82 1544f909 2004-12-25 devnull _Thread *t;
85 b8f742db 2005-01-11 devnull //print("threadstart %p\n", v);
86 1544f909 2004-12-25 devnull t->startfn(t->startarg);
87 b8f742db 2005-01-11 devnull //print("threadexits %p\n", v);
88 4dbefdd4 2004-12-27 devnull threadexits(nil);
89 b8f742db 2005-01-11 devnull //print("not reacehd\n");
92 1544f909 2004-12-25 devnull static _Thread*
93 1544f909 2004-12-25 devnull threadalloc(void (*fn)(void*), void *arg, uint stack)
95 1544f909 2004-12-25 devnull _Thread *t;
96 1544f909 2004-12-25 devnull sigset_t zero;
98 1544f909 2004-12-25 devnull /* allocate the task and stack together */
99 1544f909 2004-12-25 devnull t = malloc(sizeof *t+stack);
100 73722a8b 2004-12-27 devnull if(t == nil)
101 73722a8b 2004-12-27 devnull sysfatal("threadalloc malloc: %r");
102 1544f909 2004-12-25 devnull memset(t, 0, sizeof *t);
103 1544f909 2004-12-25 devnull t->stk = (uchar*)(t+1);
104 1544f909 2004-12-25 devnull t->stksize = stack;
105 1544f909 2004-12-25 devnull t->id = incref(&threadidref);
106 1544f909 2004-12-25 devnull t->startfn = fn;
107 1544f909 2004-12-25 devnull t->startarg = arg;
109 1544f909 2004-12-25 devnull /* do a reasonable initialization */
110 1544f909 2004-12-25 devnull memset(&t->context.uc, 0, sizeof t->context.uc);
111 1544f909 2004-12-25 devnull sigemptyset(&zero);
112 1544f909 2004-12-25 devnull sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
114 d54ead7f 2004-12-28 devnull /* must initialize with current context */
115 37cc72d3 2005-01-11 devnull if(getcontext(&t->context.uc) < 0)
116 37cc72d3 2005-01-11 devnull sysfatal("threadalloc getcontext: %r");
118 1544f909 2004-12-25 devnull /* call makecontext to do the real work. */
119 1544f909 2004-12-25 devnull /* leave a few words open on both ends */
120 1544f909 2004-12-25 devnull t->context.uc.uc_stack.ss_sp = t->stk+8;
121 2c87dda8 2004-12-28 devnull t->context.uc.uc_stack.ss_size = t->stksize-64;
122 37cc72d3 2005-01-11 devnull #ifdef __sun__ /* sigh */
123 37cc72d3 2005-01-11 devnull /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
124 2b3b1e89 2005-01-14 devnull t->context.uc.uc_stack.ss_sp =
125 2b3b1e89 2005-01-14 devnull (char*)t->context.uc.uc_stack.ss_sp
126 2b3b1e89 2005-01-14 devnull +t->context.uc.uc_stack.ss_size;
128 1544f909 2004-12-25 devnull makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
130 1544f909 2004-12-25 devnull return t;
133 1544f909 2004-12-25 devnull _Thread*
134 1544f909 2004-12-25 devnull _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
136 1544f909 2004-12-25 devnull _Thread *t;
138 1544f909 2004-12-25 devnull t = threadalloc(fn, arg, stack);
139 1544f909 2004-12-25 devnull t->proc = p;
140 1544f909 2004-12-25 devnull addthreadinproc(p, t);
141 1544f909 2004-12-25 devnull p->nthread++;
142 1544f909 2004-12-25 devnull _threadready(t);
143 1544f909 2004-12-25 devnull return t;
147 1544f909 2004-12-25 devnull threadcreate(void (*fn)(void*), void *arg, uint stack)
149 1544f909 2004-12-25 devnull _Thread *t;
151 1544f909 2004-12-25 devnull t = _threadcreate(proc(), fn, arg, stack);
152 1544f909 2004-12-25 devnull return t->id;
156 1544f909 2004-12-25 devnull proccreate(void (*fn)(void*), void *arg, uint stack)
158 1544f909 2004-12-25 devnull _Thread *t;
159 1544f909 2004-12-25 devnull Proc *p;
161 1544f909 2004-12-25 devnull p = procalloc();
162 1544f909 2004-12-25 devnull t = _threadcreate(p, fn, arg, stack);
163 1544f909 2004-12-25 devnull _procstart(p, scheduler);
164 1544f909 2004-12-25 devnull return t->id;
168 1544f909 2004-12-25 devnull _threadswitch(void)
170 1544f909 2004-12-25 devnull Proc *p;
172 c8b6342d 2005-01-13 devnull needstack(0);
173 1544f909 2004-12-25 devnull p = proc();
174 b8f742db 2005-01-11 devnull //print("threadswtch %p\n", p);
175 1544f909 2004-12-25 devnull contextswitch(&p->thread->context, &p->schedcontext);
179 1544f909 2004-12-25 devnull _threadready(_Thread *t)
181 1544f909 2004-12-25 devnull Proc *p;
183 1544f909 2004-12-25 devnull p = t->proc;
184 1544f909 2004-12-25 devnull lock(&p->lock);
185 a0a331aa 2005-01-06 devnull p->runrend.l = &p->lock;
186 1544f909 2004-12-25 devnull addthread(&p->runqueue, t);
187 8cbd854a 2004-12-27 devnull //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
188 8cbd854a 2004-12-27 devnull if(p != proc())
189 a0a331aa 2005-01-06 devnull _procwakeupandunlock(&p->runrend);
191 a0a331aa 2005-01-06 devnull unlock(&p->lock);
195 1544f909 2004-12-25 devnull threadyield(void)
198 8cbd854a 2004-12-27 devnull Proc *p;
200 8cbd854a 2004-12-27 devnull p = proc();
201 8cbd854a 2004-12-27 devnull n = p->nswitch;
202 8cbd854a 2004-12-27 devnull _threadready(p->thread);
203 1544f909 2004-12-25 devnull _threadswitch();
204 8cbd854a 2004-12-27 devnull return p->nswitch - n;
208 1544f909 2004-12-25 devnull threadexits(char *msg)
210 1544f909 2004-12-25 devnull Proc *p;
212 1544f909 2004-12-25 devnull p = proc();
213 4dbefdd4 2004-12-27 devnull if(msg == nil)
214 4dbefdd4 2004-12-27 devnull msg = "";
215 1544f909 2004-12-25 devnull utfecpy(p->msg, p->msg+sizeof p->msg, msg);
216 4dbefdd4 2004-12-27 devnull proc()->thread->exiting = 1;
217 4dbefdd4 2004-12-27 devnull _threadswitch();
220 1544f909 2004-12-25 devnull static void
221 1544f909 2004-12-25 devnull contextswitch(Context *from, Context *to)
223 1544f909 2004-12-25 devnull if(swapcontext(&from->uc, &to->uc) < 0){
224 1544f909 2004-12-25 devnull fprint(2, "swapcontext failed: %r\n");
225 1544f909 2004-12-25 devnull assert(0);
229 1544f909 2004-12-25 devnull static void
230 73722a8b 2004-12-27 devnull scheduler(Proc *p)
232 1544f909 2004-12-25 devnull _Thread *t;
234 1544f909 2004-12-25 devnull setproc(p);
235 2c87dda8 2004-12-28 devnull _threaddebug("scheduler enter");
236 b8f742db 2005-01-11 devnull // print("s %p\n", p);
237 1544f909 2004-12-25 devnull lock(&p->lock);
238 1544f909 2004-12-25 devnull for(;;){
239 1544f909 2004-12-25 devnull while((t = p->runqueue.head) == nil){
240 1544f909 2004-12-25 devnull if(p->nthread == 0)
241 1544f909 2004-12-25 devnull goto Out;
242 1544f909 2004-12-25 devnull p->runrend.l = &p->lock;
243 2c87dda8 2004-12-28 devnull _threaddebug("scheduler sleep");
244 1544f909 2004-12-25 devnull _procsleep(&p->runrend);
245 2c87dda8 2004-12-28 devnull _threaddebug("scheduler wake");
247 1544f909 2004-12-25 devnull delthread(&p->runqueue, t);
248 1544f909 2004-12-25 devnull unlock(&p->lock);
249 1544f909 2004-12-25 devnull p->thread = t;
250 8cbd854a 2004-12-27 devnull p->nswitch++;
251 2c87dda8 2004-12-28 devnull _threaddebug("run %d (%s)", t->id, t->name);
252 1544f909 2004-12-25 devnull contextswitch(&p->schedcontext, &t->context);
253 b8f742db 2005-01-11 devnull //print("back in scheduler\n");
254 1544f909 2004-12-25 devnull p->thread = nil;
255 1544f909 2004-12-25 devnull lock(&p->lock);
256 1544f909 2004-12-25 devnull if(t->exiting){
257 1544f909 2004-12-25 devnull delthreadinproc(p, t);
258 1544f909 2004-12-25 devnull p->nthread--;
259 b8f742db 2005-01-11 devnull //print("ntrhead %d\n", p->nthread);
260 1544f909 2004-12-25 devnull free(t);
265 2c87dda8 2004-12-28 devnull _threaddebug("scheduler exit");
266 4dbefdd4 2004-12-27 devnull delproc(p);
267 1544f909 2004-12-25 devnull lock(&threadnproclock);
268 1544f909 2004-12-25 devnull if(p->sysproc)
269 1544f909 2004-12-25 devnull --threadnsysproc;
270 1544f909 2004-12-25 devnull if(--threadnproc == threadnsysproc)
271 4dbefdd4 2004-12-27 devnull threadexitsall(p->msg);
272 1544f909 2004-12-25 devnull unlock(&threadnproclock);
273 1544f909 2004-12-25 devnull unlock(&p->lock);
274 1544f909 2004-12-25 devnull free(p);
275 1544f909 2004-12-25 devnull setproc(0);
279 1544f909 2004-12-25 devnull _threadsetsysproc(void)
281 1544f909 2004-12-25 devnull lock(&threadnproclock);
282 1544f909 2004-12-25 devnull if(++threadnsysproc == threadnproc)
283 1544f909 2004-12-25 devnull exit(0);
284 1544f909 2004-12-25 devnull unlock(&threadnproclock);
285 1544f909 2004-12-25 devnull proc()->sysproc = 1;
289 4dbefdd4 2004-12-27 devnull procdata(void)
291 4dbefdd4 2004-12-27 devnull return &proc()->udata;
294 4dbefdd4 2004-12-27 devnull extern Jmp *(*_notejmpbuf)(void);
295 4dbefdd4 2004-12-27 devnull static Jmp*
296 4dbefdd4 2004-12-27 devnull threadnotejmp(void)
298 4dbefdd4 2004-12-27 devnull return &proc()->sigjmp;
302 1544f909 2004-12-25 devnull * debugging
305 1544f909 2004-12-25 devnull threadsetname(char *fmt, ...)
307 1544f909 2004-12-25 devnull va_list arg;
308 1544f909 2004-12-25 devnull _Thread *t;
310 1544f909 2004-12-25 devnull t = proc()->thread;
311 1544f909 2004-12-25 devnull va_start(arg, fmt);
312 1544f909 2004-12-25 devnull vsnprint(t->name, sizeof t->name, fmt, arg);
313 1544f909 2004-12-25 devnull va_end(arg);
317 1544f909 2004-12-25 devnull threadsetstate(char *fmt, ...)
319 1544f909 2004-12-25 devnull va_list arg;
320 1544f909 2004-12-25 devnull _Thread *t;
322 1544f909 2004-12-25 devnull t = proc()->thread;
323 1544f909 2004-12-25 devnull va_start(arg, fmt);
324 1544f909 2004-12-25 devnull vsnprint(t->state, sizeof t->name, fmt, arg);
325 1544f909 2004-12-25 devnull va_end(arg);
329 c8b6342d 2005-01-13 devnull needstack(int n)
331 c8b6342d 2005-01-13 devnull _Thread *t;
333 c8b6342d 2005-01-13 devnull t = proc()->thread;
335 c8b6342d 2005-01-13 devnull if((char*)&t <= (char*)t->stk
336 c8b6342d 2005-01-13 devnull || (char*)&t - (char*)t->stk < 256+n){
337 c8b6342d 2005-01-13 devnull fprint(2, "thread stack overflow\n");
338 c8b6342d 2005-01-13 devnull abort();
343 1544f909 2004-12-25 devnull * locking
345 1544f909 2004-12-25 devnull static int
346 1544f909 2004-12-25 devnull threadqlock(QLock *l, int block, ulong pc)
348 b8f742db 2005-01-11 devnull //print("threadqlock %p\n", l);
349 1544f909 2004-12-25 devnull lock(&l->l);
350 1544f909 2004-12-25 devnull if(l->owner == nil){
351 1544f909 2004-12-25 devnull l->owner = (*threadnow)();
352 1544f909 2004-12-25 devnull //print("qlock %p @%#x by %p\n", l, pc, l->owner);
353 1544f909 2004-12-25 devnull unlock(&l->l);
354 1544f909 2004-12-25 devnull return 1;
356 1544f909 2004-12-25 devnull if(!block){
357 1544f909 2004-12-25 devnull unlock(&l->l);
358 1544f909 2004-12-25 devnull return 0;
360 1544f909 2004-12-25 devnull //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
361 1544f909 2004-12-25 devnull addthread(&l->waiting, (*threadnow)());
362 1544f909 2004-12-25 devnull unlock(&l->l);
364 1544f909 2004-12-25 devnull _threadswitch();
366 1544f909 2004-12-25 devnull if(l->owner != (*threadnow)()){
367 15cd8255 2005-01-04 devnull fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
368 15cd8255 2005-01-04 devnull argv0, pc, l->owner, (*threadnow)());
369 1544f909 2004-12-25 devnull abort();
371 1544f909 2004-12-25 devnull //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
372 1544f909 2004-12-25 devnull return 1;
375 1544f909 2004-12-25 devnull static void
376 1544f909 2004-12-25 devnull threadqunlock(QLock *l, ulong pc)
378 1544f909 2004-12-25 devnull lock(&l->l);
379 1544f909 2004-12-25 devnull //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
380 1aa9c533 2005-01-07 devnull if(l->owner == 0){
381 15cd8255 2005-01-04 devnull fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
382 15cd8255 2005-01-04 devnull argv0, pc, l->owner, (*threadnow)());
383 1aa9c533 2005-01-07 devnull abort();
385 1544f909 2004-12-25 devnull if((l->owner = l->waiting.head) != nil){
386 1544f909 2004-12-25 devnull delthread(&l->waiting, l->owner);
387 1544f909 2004-12-25 devnull _threadready(l->owner);
389 1544f909 2004-12-25 devnull unlock(&l->l);
392 1544f909 2004-12-25 devnull static int
393 1544f909 2004-12-25 devnull threadrlock(RWLock *l, int block, ulong pc)
395 1544f909 2004-12-25 devnull USED(pc);
397 1544f909 2004-12-25 devnull lock(&l->l);
398 1544f909 2004-12-25 devnull if(l->writer == nil && l->wwaiting.head == nil){
399 1544f909 2004-12-25 devnull l->readers++;
400 1544f909 2004-12-25 devnull unlock(&l->l);
401 1544f909 2004-12-25 devnull return 1;
403 1544f909 2004-12-25 devnull if(!block){
404 1544f909 2004-12-25 devnull unlock(&l->l);
405 1544f909 2004-12-25 devnull return 0;
407 1544f909 2004-12-25 devnull addthread(&l->rwaiting, (*threadnow)());
408 1544f909 2004-12-25 devnull unlock(&l->l);
409 1544f909 2004-12-25 devnull _threadswitch();
410 1544f909 2004-12-25 devnull return 1;
413 1544f909 2004-12-25 devnull static int
414 1544f909 2004-12-25 devnull threadwlock(RWLock *l, int block, ulong pc)
416 1544f909 2004-12-25 devnull USED(pc);
418 1544f909 2004-12-25 devnull lock(&l->l);
419 1544f909 2004-12-25 devnull if(l->writer == nil && l->readers == 0){
420 1544f909 2004-12-25 devnull l->writer = (*threadnow)();
421 1544f909 2004-12-25 devnull unlock(&l->l);
422 1544f909 2004-12-25 devnull return 1;
424 1544f909 2004-12-25 devnull if(!block){
425 1544f909 2004-12-25 devnull unlock(&l->l);
426 1544f909 2004-12-25 devnull return 0;
428 1544f909 2004-12-25 devnull addthread(&l->wwaiting, (*threadnow)());
429 1544f909 2004-12-25 devnull unlock(&l->l);
430 1544f909 2004-12-25 devnull _threadswitch();
431 1544f909 2004-12-25 devnull return 1;
434 1544f909 2004-12-25 devnull static void
435 1544f909 2004-12-25 devnull threadrunlock(RWLock *l, ulong pc)
437 1544f909 2004-12-25 devnull _Thread *t;
439 1544f909 2004-12-25 devnull USED(pc);
440 1544f909 2004-12-25 devnull lock(&l->l);
441 1544f909 2004-12-25 devnull --l->readers;
442 1544f909 2004-12-25 devnull if(l->readers == 0 && (t = l->wwaiting.head) != nil){
443 1544f909 2004-12-25 devnull delthread(&l->wwaiting, t);
444 1544f909 2004-12-25 devnull l->writer = t;
445 1544f909 2004-12-25 devnull _threadready(t);
447 1544f909 2004-12-25 devnull unlock(&l->l);
450 1544f909 2004-12-25 devnull static void
451 1544f909 2004-12-25 devnull threadwunlock(RWLock *l, ulong pc)
453 1544f909 2004-12-25 devnull _Thread *t;
455 1544f909 2004-12-25 devnull USED(pc);
456 1544f909 2004-12-25 devnull lock(&l->l);
457 1544f909 2004-12-25 devnull l->writer = nil;
458 1544f909 2004-12-25 devnull assert(l->readers == 0);
459 1544f909 2004-12-25 devnull while((t = l->rwaiting.head) != nil){
460 1544f909 2004-12-25 devnull delthread(&l->rwaiting, t);
461 1544f909 2004-12-25 devnull l->readers++;
462 1544f909 2004-12-25 devnull _threadready(t);
464 1544f909 2004-12-25 devnull if(l->readers == 0 && (t = l->wwaiting.head) != nil){
465 1544f909 2004-12-25 devnull delthread(&l->wwaiting, t);
466 1544f909 2004-12-25 devnull l->writer = t;
467 1544f909 2004-12-25 devnull _threadready(t);
469 1544f909 2004-12-25 devnull unlock(&l->l);
473 1544f909 2004-12-25 devnull * sleep and wakeup
475 1544f909 2004-12-25 devnull static void
476 1544f909 2004-12-25 devnull threadrsleep(Rendez *r, ulong pc)
478 1544f909 2004-12-25 devnull addthread(&r->waiting, proc()->thread);
479 1544f909 2004-12-25 devnull qunlock(r->l);
480 1544f909 2004-12-25 devnull _threadswitch();
481 1544f909 2004-12-25 devnull qlock(r->l);
484 1544f909 2004-12-25 devnull static int
485 1544f909 2004-12-25 devnull threadrwakeup(Rendez *r, int all, ulong pc)
488 1544f909 2004-12-25 devnull _Thread *t;
490 1544f909 2004-12-25 devnull for(i=0;; i++){
491 1544f909 2004-12-25 devnull if(i==1 && !all)
493 1544f909 2004-12-25 devnull if((t = r->waiting.head) == nil)
495 1544f909 2004-12-25 devnull delthread(&r->waiting, t);
496 1544f909 2004-12-25 devnull _threadready(t);
498 1544f909 2004-12-25 devnull return i;
502 4dbefdd4 2004-12-27 devnull * startup
505 4dbefdd4 2004-12-27 devnull static int threadargc;
506 4dbefdd4 2004-12-27 devnull static char **threadargv;
507 4dbefdd4 2004-12-27 devnull int mainstacksize;
509 4dbefdd4 2004-12-27 devnull static void
510 4dbefdd4 2004-12-27 devnull threadmainstart(void *v)
512 4dbefdd4 2004-12-27 devnull USED(v);
515 0b561695 2005-01-16 devnull * N.B. This call to proc() is a program's first call (indirectly) to a
516 0b561695 2005-01-16 devnull * pthreads function while executing on a non-pthreads-allocated
517 0b561695 2005-01-16 devnull * stack. If the pthreads implementation is using the stack pointer
518 0b561695 2005-01-16 devnull * to locate the per-thread data, then this call will blow up.
519 0b561695 2005-01-16 devnull * This means the pthread implementation is not suitable for
520 0b561695 2005-01-16 devnull * running under libthread. Time to write your own. Sorry.
522 4dbefdd4 2004-12-27 devnull threadmainproc = proc();
523 4dbefdd4 2004-12-27 devnull threadmain(threadargc, threadargv);
527 4dbefdd4 2004-12-27 devnull main(int argc, char **argv)
529 4dbefdd4 2004-12-27 devnull Proc *p;
531 1d2533d0 2004-12-28 devnull argv0 = argv[0];
533 2c87dda8 2004-12-28 devnull _threadsetupdaemonize();
535 4dbefdd4 2004-12-27 devnull threadargc = argc;
536 4dbefdd4 2004-12-27 devnull threadargv = argv;
539 4dbefdd4 2004-12-27 devnull * Install locking routines into C library.
541 4dbefdd4 2004-12-27 devnull _lock = _threadlock;
542 4dbefdd4 2004-12-27 devnull _unlock = _threadunlock;
543 4dbefdd4 2004-12-27 devnull _qlock = threadqlock;
544 4dbefdd4 2004-12-27 devnull _qunlock = threadqunlock;
545 4dbefdd4 2004-12-27 devnull _rlock = threadrlock;
546 4dbefdd4 2004-12-27 devnull _runlock = threadrunlock;
547 4dbefdd4 2004-12-27 devnull _wlock = threadwlock;
548 4dbefdd4 2004-12-27 devnull _wunlock = threadwunlock;
549 4dbefdd4 2004-12-27 devnull _rsleep = threadrsleep;
550 4dbefdd4 2004-12-27 devnull _rwakeup = threadrwakeup;
551 4dbefdd4 2004-12-27 devnull _notejmpbuf = threadnotejmp;
553 4dbefdd4 2004-12-27 devnull _pthreadinit();
554 4dbefdd4 2004-12-27 devnull p = procalloc();
555 4dbefdd4 2004-12-27 devnull _threadsetproc(p);
556 4dbefdd4 2004-12-27 devnull if(mainstacksize == 0)
557 4dbefdd4 2004-12-27 devnull mainstacksize = 65536;
558 4dbefdd4 2004-12-27 devnull _threadcreate(p, threadmainstart, nil, mainstacksize);
559 4dbefdd4 2004-12-27 devnull scheduler(p);
560 15cd8255 2005-01-04 devnull _threaddaemonize();
561 1d2533d0 2004-12-28 devnull _threadpexit();
562 15cd8255 2005-01-04 devnull return 0;
566 1544f909 2004-12-25 devnull * hooray for linked lists
568 1544f909 2004-12-25 devnull static void
569 1544f909 2004-12-25 devnull addthread(_Threadlist *l, _Thread *t)
571 1544f909 2004-12-25 devnull if(l->tail){
572 1544f909 2004-12-25 devnull l->tail->next = t;
573 1544f909 2004-12-25 devnull t->prev = l->tail;
575 1544f909 2004-12-25 devnull l->head = t;
576 1544f909 2004-12-25 devnull t->prev = nil;
578 1544f909 2004-12-25 devnull l->tail = t;
579 1544f909 2004-12-25 devnull t->next = nil;
582 1544f909 2004-12-25 devnull static void
583 1544f909 2004-12-25 devnull delthread(_Threadlist *l, _Thread *t)
585 1544f909 2004-12-25 devnull if(t->prev)
586 1544f909 2004-12-25 devnull t->prev->next = t->next;
588 1544f909 2004-12-25 devnull l->head = t->next;
589 1544f909 2004-12-25 devnull if(t->next)
590 1544f909 2004-12-25 devnull t->next->prev = t->prev;
592 1544f909 2004-12-25 devnull l->tail = t->prev;
595 1544f909 2004-12-25 devnull static void
596 1544f909 2004-12-25 devnull addthreadinproc(Proc *p, _Thread *t)
598 1544f909 2004-12-25 devnull _Threadlist *l;
600 1544f909 2004-12-25 devnull l = &p->allthreads;
601 1544f909 2004-12-25 devnull if(l->tail){
602 1544f909 2004-12-25 devnull l->tail->allnext = t;
603 1544f909 2004-12-25 devnull t->allprev = l->tail;
605 1544f909 2004-12-25 devnull l->head = t;
606 1544f909 2004-12-25 devnull t->allprev = nil;
608 1544f909 2004-12-25 devnull l->tail = t;
609 1544f909 2004-12-25 devnull t->allnext = nil;
612 1544f909 2004-12-25 devnull static void
613 1544f909 2004-12-25 devnull delthreadinproc(Proc *p, _Thread *t)
615 1544f909 2004-12-25 devnull _Threadlist *l;
617 1544f909 2004-12-25 devnull l = &p->allthreads;
618 1544f909 2004-12-25 devnull if(t->allprev)
619 1544f909 2004-12-25 devnull t->allprev->allnext = t->allnext;
621 1544f909 2004-12-25 devnull l->head = t->allnext;
622 1544f909 2004-12-25 devnull if(t->allnext)
623 1544f909 2004-12-25 devnull t->allnext->allprev = t->allprev;
625 1544f909 2004-12-25 devnull l->tail = t->allprev;
628 4dbefdd4 2004-12-27 devnull Proc *_threadprocs;
629 4dbefdd4 2004-12-27 devnull Lock _threadprocslock;
630 4dbefdd4 2004-12-27 devnull static Proc *_threadprocstail;
632 1544f909 2004-12-25 devnull static void
633 4dbefdd4 2004-12-27 devnull addproc(Proc *p)
635 4dbefdd4 2004-12-27 devnull lock(&_threadprocslock);
636 4dbefdd4 2004-12-27 devnull if(_threadprocstail){
637 4dbefdd4 2004-12-27 devnull _threadprocstail->next = p;
638 4dbefdd4 2004-12-27 devnull p->prev = _threadprocstail;
640 4dbefdd4 2004-12-27 devnull _threadprocs = p;
641 4dbefdd4 2004-12-27 devnull p->prev = nil;
643 4dbefdd4 2004-12-27 devnull _threadprocstail = p;
644 4dbefdd4 2004-12-27 devnull p->next = nil;
645 4dbefdd4 2004-12-27 devnull unlock(&_threadprocslock);
648 4dbefdd4 2004-12-27 devnull static void
649 4dbefdd4 2004-12-27 devnull delproc(Proc *p)
651 4dbefdd4 2004-12-27 devnull lock(&_threadprocslock);
652 4dbefdd4 2004-12-27 devnull if(p->prev)
653 4dbefdd4 2004-12-27 devnull p->prev->next = p->next;
655 4dbefdd4 2004-12-27 devnull _threadprocs = p->next;
656 4dbefdd4 2004-12-27 devnull if(p->next)
657 4dbefdd4 2004-12-27 devnull p->next->prev = p->prev;
659 4dbefdd4 2004-12-27 devnull _threadprocstail = p->prev;
660 4dbefdd4 2004-12-27 devnull unlock(&_threadprocslock);
664 15cd8255 2005-01-04 devnull * notify - for now just use the usual mechanisms
667 15cd8255 2005-01-04 devnull threadnotify(int (*f)(void*, char*), int in)
669 15cd8255 2005-01-04 devnull atnotify(f, in);