1 1544f909 2004-12-25 devnull #include "u.h"
2 1544f909 2004-12-25 devnull #include "libc.h"
3 1544f909 2004-12-25 devnull #include "thread.h"
4 1544f909 2004-12-25 devnull #include "threadimpl.h"
6 1544f909 2004-12-25 devnull int _threaddebuglevel;
8 1544f909 2004-12-25 devnull static uint threadnproc;
9 1544f909 2004-12-25 devnull static uint threadnsysproc;
10 1544f909 2004-12-25 devnull static Lock threadnproclock;
11 1544f909 2004-12-25 devnull static Ref threadidref;
12 4dbefdd4 2004-12-27 devnull static Proc *threadmainproc;
14 4dbefdd4 2004-12-27 devnull static void addproc(Proc*);
15 4dbefdd4 2004-12-27 devnull static void delproc(Proc*);
16 1544f909 2004-12-25 devnull static void addthread(_Threadlist*, _Thread*);
17 1544f909 2004-12-25 devnull static void delthread(_Threadlist*, _Thread*);
18 1544f909 2004-12-25 devnull static void addthreadinproc(Proc*, _Thread*);
19 1544f909 2004-12-25 devnull static void delthreadinproc(Proc*, _Thread*);
20 1544f909 2004-12-25 devnull static void contextswitch(Context *from, Context *to);
21 73722a8b 2004-12-27 devnull static void scheduler(Proc*);
23 2c87dda8 2004-12-28 devnull static void
24 2c87dda8 2004-12-28 devnull _threaddebug(char *fmt, ...)
26 2c87dda8 2004-12-28 devnull va_list arg;
27 2c87dda8 2004-12-28 devnull char buf[128];
28 2c87dda8 2004-12-28 devnull _Thread *t;
32 2c87dda8 2004-12-28 devnull va_start(arg, fmt);
33 2c87dda8 2004-12-28 devnull vsnprint(buf, sizeof buf, fmt, arg);
34 2c87dda8 2004-12-28 devnull va_end(arg);
35 2c87dda8 2004-12-28 devnull t = proc()->thread;
37 2c87dda8 2004-12-28 devnull fprint(2, "%d.%d: %s\n", getpid(), t->id, buf);
39 2c87dda8 2004-12-28 devnull fprint(2, "%d._: %s\n", getpid(), buf);
42 1544f909 2004-12-25 devnull static _Thread*
43 1544f909 2004-12-25 devnull getthreadnow(void)
45 1544f909 2004-12-25 devnull return proc()->thread;
47 1544f909 2004-12-25 devnull _Thread *(*threadnow)(void) = getthreadnow;
49 1544f909 2004-12-25 devnull static Proc*
50 1544f909 2004-12-25 devnull procalloc(void)
54 1544f909 2004-12-25 devnull p = malloc(sizeof *p);
55 73722a8b 2004-12-27 devnull if(p == nil)
56 73722a8b 2004-12-27 devnull sysfatal("procalloc malloc: %r");
57 1544f909 2004-12-25 devnull memset(p, 0, sizeof *p);
58 4dbefdd4 2004-12-27 devnull addproc(p);
59 1544f909 2004-12-25 devnull lock(&threadnproclock);
60 1544f909 2004-12-25 devnull threadnproc++;
61 1544f909 2004-12-25 devnull unlock(&threadnproclock);
62 1544f909 2004-12-25 devnull return p;
65 1544f909 2004-12-25 devnull static void
66 1544f909 2004-12-25 devnull threadstart(void *v)
68 1544f909 2004-12-25 devnull _Thread *t;
71 1544f909 2004-12-25 devnull t->startfn(t->startarg);
72 2c87dda8 2004-12-28 devnull memset(&v, 0xff, 32); /* try to cut off stack traces */
73 4dbefdd4 2004-12-27 devnull threadexits(nil);
76 1544f909 2004-12-25 devnull static _Thread*
77 1544f909 2004-12-25 devnull threadalloc(void (*fn)(void*), void *arg, uint stack)
79 1544f909 2004-12-25 devnull _Thread *t;
80 1544f909 2004-12-25 devnull sigset_t zero;
82 1544f909 2004-12-25 devnull /* allocate the task and stack together */
83 1544f909 2004-12-25 devnull t = malloc(sizeof *t+stack);
84 73722a8b 2004-12-27 devnull if(t == nil)
85 73722a8b 2004-12-27 devnull sysfatal("threadalloc malloc: %r");
86 1544f909 2004-12-25 devnull memset(t, 0, sizeof *t);
87 1544f909 2004-12-25 devnull t->stk = (uchar*)(t+1);
88 1544f909 2004-12-25 devnull t->stksize = stack;
89 1544f909 2004-12-25 devnull t->id = incref(&threadidref);
90 1544f909 2004-12-25 devnull t->startfn = fn;
91 1544f909 2004-12-25 devnull t->startarg = arg;
93 1544f909 2004-12-25 devnull /* do a reasonable initialization */
94 1544f909 2004-12-25 devnull memset(&t->context.uc, 0, sizeof t->context.uc);
95 1544f909 2004-12-25 devnull sigemptyset(&zero);
96 1544f909 2004-12-25 devnull sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
98 1544f909 2004-12-25 devnull /* on Linux makecontext neglects floating point */
99 1544f909 2004-12-25 devnull getcontext(&t->context.uc);
101 1544f909 2004-12-25 devnull /* call makecontext to do the real work. */
102 1544f909 2004-12-25 devnull /* leave a few words open on both ends */
103 1544f909 2004-12-25 devnull t->context.uc.uc_stack.ss_sp = t->stk+8;
104 2c87dda8 2004-12-28 devnull t->context.uc.uc_stack.ss_size = t->stksize-64;
105 1544f909 2004-12-25 devnull makecontext(&t->context.uc, (void(*)())threadstart, 1, t);
107 1544f909 2004-12-25 devnull return t;
110 1544f909 2004-12-25 devnull _Thread*
111 1544f909 2004-12-25 devnull _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
113 1544f909 2004-12-25 devnull _Thread *t;
115 1544f909 2004-12-25 devnull t = threadalloc(fn, arg, stack);
116 1544f909 2004-12-25 devnull t->proc = p;
117 1544f909 2004-12-25 devnull addthreadinproc(p, t);
118 1544f909 2004-12-25 devnull p->nthread++;
119 1544f909 2004-12-25 devnull _threadready(t);
120 1544f909 2004-12-25 devnull return t;
124 1544f909 2004-12-25 devnull threadcreate(void (*fn)(void*), void *arg, uint stack)
126 1544f909 2004-12-25 devnull _Thread *t;
128 1544f909 2004-12-25 devnull t = _threadcreate(proc(), fn, arg, stack);
129 1544f909 2004-12-25 devnull return t->id;
133 1544f909 2004-12-25 devnull proccreate(void (*fn)(void*), void *arg, uint stack)
135 1544f909 2004-12-25 devnull _Thread *t;
136 1544f909 2004-12-25 devnull Proc *p;
138 1544f909 2004-12-25 devnull p = procalloc();
139 1544f909 2004-12-25 devnull t = _threadcreate(p, fn, arg, stack);
140 1544f909 2004-12-25 devnull _procstart(p, scheduler);
141 1544f909 2004-12-25 devnull return t->id;
145 1544f909 2004-12-25 devnull _threadswitch(void)
147 1544f909 2004-12-25 devnull Proc *p;
149 1544f909 2004-12-25 devnull p = proc();
150 1544f909 2004-12-25 devnull contextswitch(&p->thread->context, &p->schedcontext);
154 1544f909 2004-12-25 devnull _threadready(_Thread *t)
156 1544f909 2004-12-25 devnull Proc *p;
158 1544f909 2004-12-25 devnull p = t->proc;
159 1544f909 2004-12-25 devnull lock(&p->lock);
160 1544f909 2004-12-25 devnull addthread(&p->runqueue, t);
161 8cbd854a 2004-12-27 devnull //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
162 8cbd854a 2004-12-27 devnull if(p != proc())
163 8cbd854a 2004-12-27 devnull _procwakeup(&p->runrend);
164 1544f909 2004-12-25 devnull unlock(&p->lock);
168 1544f909 2004-12-25 devnull threadyield(void)
171 8cbd854a 2004-12-27 devnull Proc *p;
173 8cbd854a 2004-12-27 devnull p = proc();
174 8cbd854a 2004-12-27 devnull n = p->nswitch;
175 8cbd854a 2004-12-27 devnull _threadready(p->thread);
176 1544f909 2004-12-25 devnull _threadswitch();
177 8cbd854a 2004-12-27 devnull return p->nswitch - n;
181 1544f909 2004-12-25 devnull threadexits(char *msg)
183 1544f909 2004-12-25 devnull Proc *p;
185 1544f909 2004-12-25 devnull p = proc();
186 4dbefdd4 2004-12-27 devnull if(msg == nil)
187 4dbefdd4 2004-12-27 devnull msg = "";
188 1544f909 2004-12-25 devnull utfecpy(p->msg, p->msg+sizeof p->msg, msg);
189 4dbefdd4 2004-12-27 devnull proc()->thread->exiting = 1;
190 4dbefdd4 2004-12-27 devnull _threadswitch();
193 1544f909 2004-12-25 devnull static void
194 1544f909 2004-12-25 devnull contextswitch(Context *from, Context *to)
196 1544f909 2004-12-25 devnull if(swapcontext(&from->uc, &to->uc) < 0){
197 1544f909 2004-12-25 devnull fprint(2, "swapcontext failed: %r\n");
198 1544f909 2004-12-25 devnull assert(0);
202 1544f909 2004-12-25 devnull static void
203 73722a8b 2004-12-27 devnull scheduler(Proc *p)
205 1544f909 2004-12-25 devnull _Thread *t;
207 1544f909 2004-12-25 devnull setproc(p);
208 2c87dda8 2004-12-28 devnull _threaddebug("scheduler enter");
209 e1dc7e45 2004-12-27 devnull // print("s %p %d\n", p, gettid());
210 1544f909 2004-12-25 devnull lock(&p->lock);
211 1544f909 2004-12-25 devnull for(;;){
212 1544f909 2004-12-25 devnull while((t = p->runqueue.head) == nil){
213 1544f909 2004-12-25 devnull if(p->nthread == 0)
214 1544f909 2004-12-25 devnull goto Out;
215 1544f909 2004-12-25 devnull p->runrend.l = &p->lock;
216 2c87dda8 2004-12-28 devnull _threaddebug("scheduler sleep");
217 1544f909 2004-12-25 devnull _procsleep(&p->runrend);
218 2c87dda8 2004-12-28 devnull _threaddebug("scheduler wake");
220 1544f909 2004-12-25 devnull delthread(&p->runqueue, t);
221 1544f909 2004-12-25 devnull unlock(&p->lock);
222 1544f909 2004-12-25 devnull p->thread = t;
223 8cbd854a 2004-12-27 devnull p->nswitch++;
224 2c87dda8 2004-12-28 devnull _threaddebug("run %d (%s)", t->id, t->name);
225 1544f909 2004-12-25 devnull contextswitch(&p->schedcontext, &t->context);
226 1544f909 2004-12-25 devnull p->thread = nil;
227 1544f909 2004-12-25 devnull lock(&p->lock);
228 1544f909 2004-12-25 devnull if(t->exiting){
229 1544f909 2004-12-25 devnull delthreadinproc(p, t);
230 1544f909 2004-12-25 devnull p->nthread--;
231 1544f909 2004-12-25 devnull free(t);
236 2c87dda8 2004-12-28 devnull _threaddebug("scheduler exit");
237 4dbefdd4 2004-12-27 devnull delproc(p);
238 1544f909 2004-12-25 devnull lock(&threadnproclock);
239 1544f909 2004-12-25 devnull if(p->sysproc)
240 1544f909 2004-12-25 devnull --threadnsysproc;
241 1544f909 2004-12-25 devnull if(--threadnproc == threadnsysproc)
242 4dbefdd4 2004-12-27 devnull threadexitsall(p->msg);
243 1544f909 2004-12-25 devnull unlock(&threadnproclock);
244 1544f909 2004-12-25 devnull unlock(&p->lock);
245 1544f909 2004-12-25 devnull free(p);
246 1544f909 2004-12-25 devnull setproc(0);
250 1544f909 2004-12-25 devnull _threadsetsysproc(void)
252 1544f909 2004-12-25 devnull lock(&threadnproclock);
253 1544f909 2004-12-25 devnull if(++threadnsysproc == threadnproc)
254 1544f909 2004-12-25 devnull exit(0);
255 1544f909 2004-12-25 devnull unlock(&threadnproclock);
256 1544f909 2004-12-25 devnull proc()->sysproc = 1;
260 4dbefdd4 2004-12-27 devnull procdata(void)
262 4dbefdd4 2004-12-27 devnull return &proc()->udata;
265 4dbefdd4 2004-12-27 devnull extern Jmp *(*_notejmpbuf)(void);
266 4dbefdd4 2004-12-27 devnull static Jmp*
267 4dbefdd4 2004-12-27 devnull threadnotejmp(void)
269 4dbefdd4 2004-12-27 devnull return &proc()->sigjmp;
273 1544f909 2004-12-25 devnull * debugging
276 1544f909 2004-12-25 devnull threadsetname(char *fmt, ...)
278 1544f909 2004-12-25 devnull va_list arg;
279 1544f909 2004-12-25 devnull _Thread *t;
281 1544f909 2004-12-25 devnull t = proc()->thread;
282 1544f909 2004-12-25 devnull va_start(arg, fmt);
283 1544f909 2004-12-25 devnull vsnprint(t->name, sizeof t->name, fmt, arg);
284 1544f909 2004-12-25 devnull va_end(arg);
288 1544f909 2004-12-25 devnull threadsetstate(char *fmt, ...)
290 1544f909 2004-12-25 devnull va_list arg;
291 1544f909 2004-12-25 devnull _Thread *t;
293 1544f909 2004-12-25 devnull t = proc()->thread;
294 1544f909 2004-12-25 devnull va_start(arg, fmt);
295 1544f909 2004-12-25 devnull vsnprint(t->state, sizeof t->name, fmt, arg);
296 1544f909 2004-12-25 devnull va_end(arg);
300 1544f909 2004-12-25 devnull * locking
302 1544f909 2004-12-25 devnull static int
303 1544f909 2004-12-25 devnull threadqlock(QLock *l, int block, ulong pc)
305 1544f909 2004-12-25 devnull lock(&l->l);
306 1544f909 2004-12-25 devnull if(l->owner == nil){
307 1544f909 2004-12-25 devnull l->owner = (*threadnow)();
308 1544f909 2004-12-25 devnull //print("qlock %p @%#x by %p\n", l, pc, l->owner);
309 1544f909 2004-12-25 devnull unlock(&l->l);
310 1544f909 2004-12-25 devnull return 1;
312 1544f909 2004-12-25 devnull if(!block){
313 1544f909 2004-12-25 devnull unlock(&l->l);
314 1544f909 2004-12-25 devnull return 0;
316 1544f909 2004-12-25 devnull //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
317 1544f909 2004-12-25 devnull addthread(&l->waiting, (*threadnow)());
318 1544f909 2004-12-25 devnull unlock(&l->l);
320 1544f909 2004-12-25 devnull _threadswitch();
322 1544f909 2004-12-25 devnull if(l->owner != (*threadnow)()){
323 1544f909 2004-12-25 devnull fprint(2, "qlock pc=0x%lux owner=%p self=%p oops\n", pc, l->owner, (*threadnow)());
324 1544f909 2004-12-25 devnull abort();
326 1544f909 2004-12-25 devnull //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
327 1544f909 2004-12-25 devnull return 1;
330 1544f909 2004-12-25 devnull static void
331 1544f909 2004-12-25 devnull threadqunlock(QLock *l, ulong pc)
333 1544f909 2004-12-25 devnull lock(&l->l);
334 1544f909 2004-12-25 devnull //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
335 1544f909 2004-12-25 devnull if(l->owner == nil){
336 1544f909 2004-12-25 devnull fprint(2, "qunlock pc=0x%lux owner=%p self=%p oops\n",
337 1544f909 2004-12-25 devnull pc, l->owner, (*threadnow)());
338 1544f909 2004-12-25 devnull abort();
340 1544f909 2004-12-25 devnull if((l->owner = l->waiting.head) != nil){
341 1544f909 2004-12-25 devnull delthread(&l->waiting, l->owner);
342 1544f909 2004-12-25 devnull _threadready(l->owner);
344 1544f909 2004-12-25 devnull unlock(&l->l);
347 1544f909 2004-12-25 devnull static int
348 1544f909 2004-12-25 devnull threadrlock(RWLock *l, int block, ulong pc)
350 1544f909 2004-12-25 devnull USED(pc);
352 1544f909 2004-12-25 devnull lock(&l->l);
353 1544f909 2004-12-25 devnull if(l->writer == nil && l->wwaiting.head == nil){
354 1544f909 2004-12-25 devnull l->readers++;
355 1544f909 2004-12-25 devnull unlock(&l->l);
356 1544f909 2004-12-25 devnull return 1;
358 1544f909 2004-12-25 devnull if(!block){
359 1544f909 2004-12-25 devnull unlock(&l->l);
360 1544f909 2004-12-25 devnull return 0;
362 1544f909 2004-12-25 devnull addthread(&l->rwaiting, (*threadnow)());
363 1544f909 2004-12-25 devnull unlock(&l->l);
364 1544f909 2004-12-25 devnull _threadswitch();
365 1544f909 2004-12-25 devnull return 1;
368 1544f909 2004-12-25 devnull static int
369 1544f909 2004-12-25 devnull threadwlock(RWLock *l, int block, ulong pc)
371 1544f909 2004-12-25 devnull USED(pc);
373 1544f909 2004-12-25 devnull lock(&l->l);
374 1544f909 2004-12-25 devnull if(l->writer == nil && l->readers == 0){
375 1544f909 2004-12-25 devnull l->writer = (*threadnow)();
376 1544f909 2004-12-25 devnull unlock(&l->l);
377 1544f909 2004-12-25 devnull return 1;
379 1544f909 2004-12-25 devnull if(!block){
380 1544f909 2004-12-25 devnull unlock(&l->l);
381 1544f909 2004-12-25 devnull return 0;
383 1544f909 2004-12-25 devnull addthread(&l->wwaiting, (*threadnow)());
384 1544f909 2004-12-25 devnull unlock(&l->l);
385 1544f909 2004-12-25 devnull _threadswitch();
386 1544f909 2004-12-25 devnull return 1;
389 1544f909 2004-12-25 devnull static void
390 1544f909 2004-12-25 devnull threadrunlock(RWLock *l, ulong pc)
392 1544f909 2004-12-25 devnull _Thread *t;
394 1544f909 2004-12-25 devnull USED(pc);
395 1544f909 2004-12-25 devnull lock(&l->l);
396 1544f909 2004-12-25 devnull --l->readers;
397 1544f909 2004-12-25 devnull if(l->readers == 0 && (t = l->wwaiting.head) != nil){
398 1544f909 2004-12-25 devnull delthread(&l->wwaiting, t);
399 1544f909 2004-12-25 devnull l->writer = t;
400 1544f909 2004-12-25 devnull _threadready(t);
402 1544f909 2004-12-25 devnull unlock(&l->l);
405 1544f909 2004-12-25 devnull static void
406 1544f909 2004-12-25 devnull threadwunlock(RWLock *l, ulong pc)
408 1544f909 2004-12-25 devnull _Thread *t;
410 1544f909 2004-12-25 devnull USED(pc);
411 1544f909 2004-12-25 devnull lock(&l->l);
412 1544f909 2004-12-25 devnull l->writer = nil;
413 1544f909 2004-12-25 devnull assert(l->readers == 0);
414 1544f909 2004-12-25 devnull while((t = l->rwaiting.head) != nil){
415 1544f909 2004-12-25 devnull delthread(&l->rwaiting, t);
416 1544f909 2004-12-25 devnull l->readers++;
417 1544f909 2004-12-25 devnull _threadready(t);
419 1544f909 2004-12-25 devnull if(l->readers == 0 && (t = l->wwaiting.head) != nil){
420 1544f909 2004-12-25 devnull delthread(&l->wwaiting, t);
421 1544f909 2004-12-25 devnull l->writer = t;
422 1544f909 2004-12-25 devnull _threadready(t);
424 1544f909 2004-12-25 devnull unlock(&l->l);
428 1544f909 2004-12-25 devnull * sleep and wakeup
430 1544f909 2004-12-25 devnull static void
431 1544f909 2004-12-25 devnull threadrsleep(Rendez *r, ulong pc)
433 1544f909 2004-12-25 devnull addthread(&r->waiting, proc()->thread);
434 1544f909 2004-12-25 devnull qunlock(r->l);
435 1544f909 2004-12-25 devnull _threadswitch();
436 1544f909 2004-12-25 devnull qlock(r->l);
439 1544f909 2004-12-25 devnull static int
440 1544f909 2004-12-25 devnull threadrwakeup(Rendez *r, int all, ulong pc)
443 1544f909 2004-12-25 devnull _Thread *t;
445 1544f909 2004-12-25 devnull for(i=0;; i++){
446 1544f909 2004-12-25 devnull if(i==1 && !all)
448 1544f909 2004-12-25 devnull if((t = r->waiting.head) == nil)
450 1544f909 2004-12-25 devnull delthread(&r->waiting, t);
451 1544f909 2004-12-25 devnull _threadready(t);
453 1544f909 2004-12-25 devnull return i;
457 4dbefdd4 2004-12-27 devnull * startup
460 4dbefdd4 2004-12-27 devnull static int threadargc;
461 4dbefdd4 2004-12-27 devnull static char **threadargv;
462 4dbefdd4 2004-12-27 devnull int mainstacksize;
464 4dbefdd4 2004-12-27 devnull static void
465 4dbefdd4 2004-12-27 devnull threadmainstart(void *v)
467 4dbefdd4 2004-12-27 devnull USED(v);
468 4dbefdd4 2004-12-27 devnull threadmainproc = proc();
469 4dbefdd4 2004-12-27 devnull threadmain(threadargc, threadargv);
473 2c87dda8 2004-12-28 devnull threadlinklibrary(void)
478 4dbefdd4 2004-12-27 devnull main(int argc, char **argv)
480 4dbefdd4 2004-12-27 devnull Proc *p;
482 2c87dda8 2004-12-28 devnull _threadsetupdaemonize();
484 4dbefdd4 2004-12-27 devnull threadargc = argc;
485 4dbefdd4 2004-12-27 devnull threadargv = argv;
488 4dbefdd4 2004-12-27 devnull * Install locking routines into C library.
490 4dbefdd4 2004-12-27 devnull _lock = _threadlock;
491 4dbefdd4 2004-12-27 devnull _unlock = _threadunlock;
492 4dbefdd4 2004-12-27 devnull _qlock = threadqlock;
493 4dbefdd4 2004-12-27 devnull _qunlock = threadqunlock;
494 4dbefdd4 2004-12-27 devnull _rlock = threadrlock;
495 4dbefdd4 2004-12-27 devnull _runlock = threadrunlock;
496 4dbefdd4 2004-12-27 devnull _wlock = threadwlock;
497 4dbefdd4 2004-12-27 devnull _wunlock = threadwunlock;
498 4dbefdd4 2004-12-27 devnull _rsleep = threadrsleep;
499 4dbefdd4 2004-12-27 devnull _rwakeup = threadrwakeup;
500 4dbefdd4 2004-12-27 devnull _notejmpbuf = threadnotejmp;
502 4dbefdd4 2004-12-27 devnull _pthreadinit();
503 4dbefdd4 2004-12-27 devnull p = procalloc();
504 4dbefdd4 2004-12-27 devnull _threadsetproc(p);
505 4dbefdd4 2004-12-27 devnull if(mainstacksize == 0)
506 4dbefdd4 2004-12-27 devnull mainstacksize = 65536;
507 4dbefdd4 2004-12-27 devnull _threadcreate(p, threadmainstart, nil, mainstacksize);
508 4dbefdd4 2004-12-27 devnull scheduler(p);
509 4dbefdd4 2004-12-27 devnull return 0; /* not reached */
513 1544f909 2004-12-25 devnull * hooray for linked lists
515 1544f909 2004-12-25 devnull static void
516 1544f909 2004-12-25 devnull addthread(_Threadlist *l, _Thread *t)
518 1544f909 2004-12-25 devnull if(l->tail){
519 1544f909 2004-12-25 devnull l->tail->next = t;
520 1544f909 2004-12-25 devnull t->prev = l->tail;
522 1544f909 2004-12-25 devnull l->head = t;
523 1544f909 2004-12-25 devnull t->prev = nil;
525 1544f909 2004-12-25 devnull l->tail = t;
526 1544f909 2004-12-25 devnull t->next = nil;
529 1544f909 2004-12-25 devnull static void
530 1544f909 2004-12-25 devnull delthread(_Threadlist *l, _Thread *t)
532 1544f909 2004-12-25 devnull if(t->prev)
533 1544f909 2004-12-25 devnull t->prev->next = t->next;
535 1544f909 2004-12-25 devnull l->head = t->next;
536 1544f909 2004-12-25 devnull if(t->next)
537 1544f909 2004-12-25 devnull t->next->prev = t->prev;
539 1544f909 2004-12-25 devnull l->tail = t->prev;
542 1544f909 2004-12-25 devnull static void
543 1544f909 2004-12-25 devnull addthreadinproc(Proc *p, _Thread *t)
545 1544f909 2004-12-25 devnull _Threadlist *l;
547 1544f909 2004-12-25 devnull l = &p->allthreads;
548 1544f909 2004-12-25 devnull if(l->tail){
549 1544f909 2004-12-25 devnull l->tail->allnext = t;
550 1544f909 2004-12-25 devnull t->allprev = l->tail;
552 1544f909 2004-12-25 devnull l->head = t;
553 1544f909 2004-12-25 devnull t->allprev = nil;
555 1544f909 2004-12-25 devnull l->tail = t;
556 1544f909 2004-12-25 devnull t->allnext = nil;
559 1544f909 2004-12-25 devnull static void
560 1544f909 2004-12-25 devnull delthreadinproc(Proc *p, _Thread *t)
562 1544f909 2004-12-25 devnull _Threadlist *l;
564 1544f909 2004-12-25 devnull l = &p->allthreads;
565 1544f909 2004-12-25 devnull if(t->allprev)
566 1544f909 2004-12-25 devnull t->allprev->allnext = t->allnext;
568 1544f909 2004-12-25 devnull l->head = t->allnext;
569 1544f909 2004-12-25 devnull if(t->allnext)
570 1544f909 2004-12-25 devnull t->allnext->allprev = t->allprev;
572 1544f909 2004-12-25 devnull l->tail = t->allprev;
575 4dbefdd4 2004-12-27 devnull Proc *_threadprocs;
576 4dbefdd4 2004-12-27 devnull Lock _threadprocslock;
577 4dbefdd4 2004-12-27 devnull static Proc *_threadprocstail;
579 1544f909 2004-12-25 devnull static void
580 4dbefdd4 2004-12-27 devnull addproc(Proc *p)
582 4dbefdd4 2004-12-27 devnull lock(&_threadprocslock);
583 4dbefdd4 2004-12-27 devnull if(_threadprocstail){
584 4dbefdd4 2004-12-27 devnull _threadprocstail->next = p;
585 4dbefdd4 2004-12-27 devnull p->prev = _threadprocstail;
587 4dbefdd4 2004-12-27 devnull _threadprocs = p;
588 4dbefdd4 2004-12-27 devnull p->prev = nil;
590 4dbefdd4 2004-12-27 devnull _threadprocstail = p;
591 4dbefdd4 2004-12-27 devnull p->next = nil;
592 4dbefdd4 2004-12-27 devnull unlock(&_threadprocslock);
595 4dbefdd4 2004-12-27 devnull static void
596 4dbefdd4 2004-12-27 devnull delproc(Proc *p)
598 4dbefdd4 2004-12-27 devnull lock(&_threadprocslock);
599 4dbefdd4 2004-12-27 devnull if(p->prev)
600 4dbefdd4 2004-12-27 devnull p->prev->next = p->next;
602 4dbefdd4 2004-12-27 devnull _threadprocs = p->next;
603 4dbefdd4 2004-12-27 devnull if(p->next)
604 4dbefdd4 2004-12-27 devnull p->next->prev = p->prev;
606 4dbefdd4 2004-12-27 devnull _threadprocstail = p->prev;
607 4dbefdd4 2004-12-27 devnull unlock(&_threadprocslock);