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 1fdb4a54 2005-01-23 devnull static void procscheduler(Proc*);
19 6dde87f8 2005-09-26 devnull static int threadinfo(void*, char*);
21 2c87dda8 2004-12-28 devnull static void
22 2c87dda8 2004-12-28 devnull _threaddebug(char *fmt, ...)
24 2c87dda8 2004-12-28 devnull va_list arg;
25 2c87dda8 2004-12-28 devnull char buf[128];
26 2c87dda8 2004-12-28 devnull _Thread *t;
28 af89fc10 2005-01-06 devnull static int fd = -1;
30 5b092791 2005-09-26 devnull if(_threaddebuglevel == 0)
33 af89fc10 2005-01-06 devnull if(fd < 0){
34 af89fc10 2005-01-06 devnull p = strrchr(argv0, '/');
38 af89fc10 2005-01-06 devnull p = argv0;
39 af89fc10 2005-01-06 devnull snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
40 af89fc10 2005-01-06 devnull if((fd = create(buf, OWRITE, 0666)) < 0)
41 af89fc10 2005-01-06 devnull fd = open("/dev/null", OWRITE);
44 2c87dda8 2004-12-28 devnull va_start(arg, fmt);
45 2c87dda8 2004-12-28 devnull vsnprint(buf, sizeof buf, fmt, arg);
46 2c87dda8 2004-12-28 devnull va_end(arg);
47 2c87dda8 2004-12-28 devnull t = proc()->thread;
49 af89fc10 2005-01-06 devnull fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
51 af89fc10 2005-01-06 devnull fprint(fd, "%d._: %s\n", getpid(), buf);
54 1544f909 2004-12-25 devnull static _Thread*
55 1544f909 2004-12-25 devnull getthreadnow(void)
57 1544f909 2004-12-25 devnull return proc()->thread;
59 1544f909 2004-12-25 devnull _Thread *(*threadnow)(void) = getthreadnow;
61 1544f909 2004-12-25 devnull static Proc*
62 1544f909 2004-12-25 devnull procalloc(void)
66 1544f909 2004-12-25 devnull p = malloc(sizeof *p);
67 73722a8b 2004-12-27 devnull if(p == nil)
68 73722a8b 2004-12-27 devnull sysfatal("procalloc malloc: %r");
69 1544f909 2004-12-25 devnull memset(p, 0, sizeof *p);
70 4dbefdd4 2004-12-27 devnull addproc(p);
71 1544f909 2004-12-25 devnull lock(&threadnproclock);
72 1544f909 2004-12-25 devnull threadnproc++;
73 1544f909 2004-12-25 devnull unlock(&threadnproclock);
74 1544f909 2004-12-25 devnull return p;
77 1544f909 2004-12-25 devnull static void
78 86502504 2005-01-18 devnull threadstart(uint y, uint x)
80 1544f909 2004-12-25 devnull _Thread *t;
83 86502504 2005-01-18 devnull z = x<<16; /* hide undefined 32-bit shift from 32-bit compilers */
84 86502504 2005-01-18 devnull z <<= 16;
86 86502504 2005-01-18 devnull t = (_Thread*)z;
88 b8f742db 2005-01-11 devnull //print("threadstart %p\n", v);
89 1544f909 2004-12-25 devnull t->startfn(t->startarg);
90 b8f742db 2005-01-11 devnull //print("threadexits %p\n", v);
91 4dbefdd4 2004-12-27 devnull threadexits(nil);
92 b8f742db 2005-01-11 devnull //print("not reacehd\n");
95 1544f909 2004-12-25 devnull static _Thread*
96 1544f909 2004-12-25 devnull threadalloc(void (*fn)(void*), void *arg, uint stack)
98 1544f909 2004-12-25 devnull _Thread *t;
99 1544f909 2004-12-25 devnull sigset_t zero;
100 86502504 2005-01-18 devnull uint x, y;
101 86502504 2005-01-18 devnull ulong z;
103 1544f909 2004-12-25 devnull /* allocate the task and stack together */
104 1544f909 2004-12-25 devnull t = malloc(sizeof *t+stack);
105 73722a8b 2004-12-27 devnull if(t == nil)
106 73722a8b 2004-12-27 devnull sysfatal("threadalloc malloc: %r");
107 1544f909 2004-12-25 devnull memset(t, 0, sizeof *t);
108 1544f909 2004-12-25 devnull t->stk = (uchar*)(t+1);
109 1544f909 2004-12-25 devnull t->stksize = stack;
110 1544f909 2004-12-25 devnull t->id = incref(&threadidref);
111 1544f909 2004-12-25 devnull t->startfn = fn;
112 1544f909 2004-12-25 devnull t->startarg = arg;
114 1544f909 2004-12-25 devnull /* do a reasonable initialization */
115 1544f909 2004-12-25 devnull memset(&t->context.uc, 0, sizeof t->context.uc);
116 1544f909 2004-12-25 devnull sigemptyset(&zero);
117 1544f909 2004-12-25 devnull sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
119 d54ead7f 2004-12-28 devnull /* must initialize with current context */
120 37cc72d3 2005-01-11 devnull if(getcontext(&t->context.uc) < 0)
121 37cc72d3 2005-01-11 devnull sysfatal("threadalloc getcontext: %r");
123 1544f909 2004-12-25 devnull /* call makecontext to do the real work. */
124 1544f909 2004-12-25 devnull /* leave a few words open on both ends */
125 1544f909 2004-12-25 devnull t->context.uc.uc_stack.ss_sp = t->stk+8;
126 2c87dda8 2004-12-28 devnull t->context.uc.uc_stack.ss_size = t->stksize-64;
127 d08f4066 2005-07-27 devnull #if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */
128 37cc72d3 2005-01-11 devnull /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
129 2b3b1e89 2005-01-14 devnull t->context.uc.uc_stack.ss_sp =
130 2b3b1e89 2005-01-14 devnull (char*)t->context.uc.uc_stack.ss_sp
131 2b3b1e89 2005-01-14 devnull +t->context.uc.uc_stack.ss_size;
134 86502504 2005-01-18 devnull * All this magic is because you have to pass makecontext a
135 86502504 2005-01-18 devnull * function that takes some number of word-sized variables,
136 86502504 2005-01-18 devnull * and on 64-bit machines pointers are bigger than words.
138 86502504 2005-01-18 devnull z = (ulong)t;
140 86502504 2005-01-18 devnull z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
141 86502504 2005-01-18 devnull x = z>>16;
142 2b604081 2005-05-07 devnull makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
144 1544f909 2004-12-25 devnull return t;
147 1544f909 2004-12-25 devnull _Thread*
148 1544f909 2004-12-25 devnull _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
150 1544f909 2004-12-25 devnull _Thread *t;
152 1544f909 2004-12-25 devnull t = threadalloc(fn, arg, stack);
153 1544f909 2004-12-25 devnull t->proc = p;
154 1544f909 2004-12-25 devnull addthreadinproc(p, t);
155 1544f909 2004-12-25 devnull p->nthread++;
156 1544f909 2004-12-25 devnull _threadready(t);
157 1544f909 2004-12-25 devnull return t;
161 1544f909 2004-12-25 devnull threadcreate(void (*fn)(void*), void *arg, uint stack)
163 1544f909 2004-12-25 devnull _Thread *t;
165 1544f909 2004-12-25 devnull t = _threadcreate(proc(), fn, arg, stack);
166 1544f909 2004-12-25 devnull return t->id;
170 1544f909 2004-12-25 devnull proccreate(void (*fn)(void*), void *arg, uint stack)
173 1544f909 2004-12-25 devnull _Thread *t;
174 1544f909 2004-12-25 devnull Proc *p;
176 1544f909 2004-12-25 devnull p = procalloc();
177 1544f909 2004-12-25 devnull t = _threadcreate(p, fn, arg, stack);
178 803292c6 2005-01-18 devnull id = t->id; /* t might be freed after _procstart */
179 1fdb4a54 2005-01-23 devnull _procstart(p, procscheduler);
180 803292c6 2005-01-18 devnull return id;
184 1544f909 2004-12-25 devnull _threadswitch(void)
186 1544f909 2004-12-25 devnull Proc *p;
188 c8b6342d 2005-01-13 devnull needstack(0);
189 1544f909 2004-12-25 devnull p = proc();
190 b8f742db 2005-01-11 devnull //print("threadswtch %p\n", p);
191 1544f909 2004-12-25 devnull contextswitch(&p->thread->context, &p->schedcontext);
195 1544f909 2004-12-25 devnull _threadready(_Thread *t)
197 1544f909 2004-12-25 devnull Proc *p;
199 1544f909 2004-12-25 devnull p = t->proc;
200 1544f909 2004-12-25 devnull lock(&p->lock);
201 a0a331aa 2005-01-06 devnull p->runrend.l = &p->lock;
202 1544f909 2004-12-25 devnull addthread(&p->runqueue, t);
203 8cbd854a 2004-12-27 devnull //print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
204 8cbd854a 2004-12-27 devnull if(p != proc())
205 a0a331aa 2005-01-06 devnull _procwakeupandunlock(&p->runrend);
207 a0a331aa 2005-01-06 devnull unlock(&p->lock);
211 1544f909 2004-12-25 devnull threadyield(void)
214 8cbd854a 2004-12-27 devnull Proc *p;
216 8cbd854a 2004-12-27 devnull p = proc();
217 8cbd854a 2004-12-27 devnull n = p->nswitch;
218 8cbd854a 2004-12-27 devnull _threadready(p->thread);
219 1544f909 2004-12-25 devnull _threadswitch();
220 8cbd854a 2004-12-27 devnull return p->nswitch - n;
224 1544f909 2004-12-25 devnull threadexits(char *msg)
226 1544f909 2004-12-25 devnull Proc *p;
228 1544f909 2004-12-25 devnull p = proc();
229 4dbefdd4 2004-12-27 devnull if(msg == nil)
230 4dbefdd4 2004-12-27 devnull msg = "";
231 1544f909 2004-12-25 devnull utfecpy(p->msg, p->msg+sizeof p->msg, msg);
232 4dbefdd4 2004-12-27 devnull proc()->thread->exiting = 1;
233 4dbefdd4 2004-12-27 devnull _threadswitch();
236 1544f909 2004-12-25 devnull static void
237 1544f909 2004-12-25 devnull contextswitch(Context *from, Context *to)
239 1544f909 2004-12-25 devnull if(swapcontext(&from->uc, &to->uc) < 0){
240 1544f909 2004-12-25 devnull fprint(2, "swapcontext failed: %r\n");
241 1544f909 2004-12-25 devnull assert(0);
245 1544f909 2004-12-25 devnull static void
246 1fdb4a54 2005-01-23 devnull procscheduler(Proc *p)
248 1544f909 2004-12-25 devnull _Thread *t;
250 1544f909 2004-12-25 devnull setproc(p);
251 2c87dda8 2004-12-28 devnull _threaddebug("scheduler enter");
252 b8f742db 2005-01-11 devnull // print("s %p\n", p);
253 1544f909 2004-12-25 devnull lock(&p->lock);
254 1544f909 2004-12-25 devnull for(;;){
255 1544f909 2004-12-25 devnull while((t = p->runqueue.head) == nil){
256 1544f909 2004-12-25 devnull if(p->nthread == 0)
257 1544f909 2004-12-25 devnull goto Out;
258 1544f909 2004-12-25 devnull p->runrend.l = &p->lock;
259 2c87dda8 2004-12-28 devnull _threaddebug("scheduler sleep");
260 1544f909 2004-12-25 devnull _procsleep(&p->runrend);
261 2c87dda8 2004-12-28 devnull _threaddebug("scheduler wake");
263 1544f909 2004-12-25 devnull delthread(&p->runqueue, t);
264 1544f909 2004-12-25 devnull unlock(&p->lock);
265 1544f909 2004-12-25 devnull p->thread = t;
266 8cbd854a 2004-12-27 devnull p->nswitch++;
267 2c87dda8 2004-12-28 devnull _threaddebug("run %d (%s)", t->id, t->name);
268 1544f909 2004-12-25 devnull contextswitch(&p->schedcontext, &t->context);
269 b8f742db 2005-01-11 devnull //print("back in scheduler\n");
270 1544f909 2004-12-25 devnull p->thread = nil;
271 1544f909 2004-12-25 devnull lock(&p->lock);
272 1544f909 2004-12-25 devnull if(t->exiting){
273 1544f909 2004-12-25 devnull delthreadinproc(p, t);
274 1544f909 2004-12-25 devnull p->nthread--;
275 b8f742db 2005-01-11 devnull //print("ntrhead %d\n", p->nthread);
276 1544f909 2004-12-25 devnull free(t);
281 2c87dda8 2004-12-28 devnull _threaddebug("scheduler exit");
282 8ee6ad4d 2005-02-14 devnull if(p->mainproc){
284 8ee6ad4d 2005-02-14 devnull * Stupid bug - on Linux 2.6 and maybe elsewhere,
285 8ee6ad4d 2005-02-14 devnull * if the main thread exits then the others keep running
286 8ee6ad4d 2005-02-14 devnull * but the process shows up as a zombie in ps and is not
287 8ee6ad4d 2005-02-14 devnull * attachable with ptrace. We'll just sit around pretending
288 8ee6ad4d 2005-02-14 devnull * to be a system proc instead of exiting.
290 8ee6ad4d 2005-02-14 devnull _threaddaemonize();
291 8ee6ad4d 2005-02-14 devnull lock(&threadnproclock);
292 8ee6ad4d 2005-02-14 devnull if(++threadnsysproc == threadnproc)
293 8ee6ad4d 2005-02-14 devnull threadexitsall(p->msg);
294 8ee6ad4d 2005-02-14 devnull p->sysproc = 1;
295 8ee6ad4d 2005-02-14 devnull unlock(&threadnproclock);
297 8ee6ad4d 2005-02-14 devnull sleep(1000);
300 4dbefdd4 2004-12-27 devnull delproc(p);
301 1544f909 2004-12-25 devnull lock(&threadnproclock);
302 1544f909 2004-12-25 devnull if(p->sysproc)
303 1544f909 2004-12-25 devnull --threadnsysproc;
304 1544f909 2004-12-25 devnull if(--threadnproc == threadnsysproc)
305 4dbefdd4 2004-12-27 devnull threadexitsall(p->msg);
306 1544f909 2004-12-25 devnull unlock(&threadnproclock);
307 1544f909 2004-12-25 devnull unlock(&p->lock);
308 1544f909 2004-12-25 devnull free(p);
312 1544f909 2004-12-25 devnull _threadsetsysproc(void)
314 1544f909 2004-12-25 devnull lock(&threadnproclock);
315 1544f909 2004-12-25 devnull if(++threadnsysproc == threadnproc)
316 8ee6ad4d 2005-02-14 devnull threadexitsall(nil);
317 1544f909 2004-12-25 devnull unlock(&threadnproclock);
318 1544f909 2004-12-25 devnull proc()->sysproc = 1;
322 4dbefdd4 2004-12-27 devnull procdata(void)
324 4dbefdd4 2004-12-27 devnull return &proc()->udata;
328 8dd8a81f 2005-03-18 devnull threaddata(void)
330 8dd8a81f 2005-03-18 devnull return &proc()->thread->udata;
333 4dbefdd4 2004-12-27 devnull extern Jmp *(*_notejmpbuf)(void);
334 4dbefdd4 2004-12-27 devnull static Jmp*
335 4dbefdd4 2004-12-27 devnull threadnotejmp(void)
337 4dbefdd4 2004-12-27 devnull return &proc()->sigjmp;
341 1544f909 2004-12-25 devnull * debugging
344 1544f909 2004-12-25 devnull threadsetname(char *fmt, ...)
346 1544f909 2004-12-25 devnull va_list arg;
347 1544f909 2004-12-25 devnull _Thread *t;
349 1544f909 2004-12-25 devnull t = proc()->thread;
350 1544f909 2004-12-25 devnull va_start(arg, fmt);
351 1544f909 2004-12-25 devnull vsnprint(t->name, sizeof t->name, fmt, arg);
352 1544f909 2004-12-25 devnull va_end(arg);
356 515b6e5b 2005-01-18 devnull threadgetname(void)
358 515b6e5b 2005-01-18 devnull return proc()->thread->name;
362 1544f909 2004-12-25 devnull threadsetstate(char *fmt, ...)
364 1544f909 2004-12-25 devnull va_list arg;
365 1544f909 2004-12-25 devnull _Thread *t;
367 1544f909 2004-12-25 devnull t = proc()->thread;
368 1544f909 2004-12-25 devnull va_start(arg, fmt);
369 1544f909 2004-12-25 devnull vsnprint(t->state, sizeof t->name, fmt, arg);
370 1544f909 2004-12-25 devnull va_end(arg);
374 c8b6342d 2005-01-13 devnull needstack(int n)
376 c8b6342d 2005-01-13 devnull _Thread *t;
378 c8b6342d 2005-01-13 devnull t = proc()->thread;
380 c8b6342d 2005-01-13 devnull if((char*)&t <= (char*)t->stk
381 c8b6342d 2005-01-13 devnull || (char*)&t - (char*)t->stk < 256+n){
382 1fdb4a54 2005-01-23 devnull fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
383 c8b6342d 2005-01-13 devnull abort();
388 1544f909 2004-12-25 devnull * locking
390 1544f909 2004-12-25 devnull static int
391 1544f909 2004-12-25 devnull threadqlock(QLock *l, int block, ulong pc)
393 b8f742db 2005-01-11 devnull //print("threadqlock %p\n", l);
394 1544f909 2004-12-25 devnull lock(&l->l);
395 1544f909 2004-12-25 devnull if(l->owner == nil){
396 1544f909 2004-12-25 devnull l->owner = (*threadnow)();
397 1544f909 2004-12-25 devnull //print("qlock %p @%#x by %p\n", l, pc, l->owner);
398 1544f909 2004-12-25 devnull unlock(&l->l);
399 1544f909 2004-12-25 devnull return 1;
401 1544f909 2004-12-25 devnull if(!block){
402 1544f909 2004-12-25 devnull unlock(&l->l);
403 1544f909 2004-12-25 devnull return 0;
405 1544f909 2004-12-25 devnull //print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
406 1544f909 2004-12-25 devnull addthread(&l->waiting, (*threadnow)());
407 1544f909 2004-12-25 devnull unlock(&l->l);
409 1544f909 2004-12-25 devnull _threadswitch();
411 1544f909 2004-12-25 devnull if(l->owner != (*threadnow)()){
412 15cd8255 2005-01-04 devnull fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
413 15cd8255 2005-01-04 devnull argv0, pc, l->owner, (*threadnow)());
414 1544f909 2004-12-25 devnull abort();
416 1544f909 2004-12-25 devnull //print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
417 1544f909 2004-12-25 devnull return 1;
420 1544f909 2004-12-25 devnull static void
421 1544f909 2004-12-25 devnull threadqunlock(QLock *l, ulong pc)
423 80b8842f 2005-02-15 devnull _Thread *ready;
425 1544f909 2004-12-25 devnull lock(&l->l);
426 1544f909 2004-12-25 devnull //print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner);
427 1aa9c533 2005-01-07 devnull if(l->owner == 0){
428 15cd8255 2005-01-04 devnull fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
429 15cd8255 2005-01-04 devnull argv0, pc, l->owner, (*threadnow)());
430 1aa9c533 2005-01-07 devnull abort();
432 80b8842f 2005-02-15 devnull if((l->owner = ready = l->waiting.head) != nil)
433 1544f909 2004-12-25 devnull delthread(&l->waiting, l->owner);
435 80b8842f 2005-02-15 devnull * N.B. Cannot call _threadready() before unlocking l->l,
436 80b8842f 2005-02-15 devnull * because the thread we are readying might:
437 80b8842f 2005-02-15 devnull * - be in another proc
438 80b8842f 2005-02-15 devnull * - start running immediately
439 80b8842f 2005-02-15 devnull * - and free l before we get a chance to run again
441 1544f909 2004-12-25 devnull unlock(&l->l);
442 80b8842f 2005-02-15 devnull if(ready)
443 80b8842f 2005-02-15 devnull _threadready(l->owner);
446 1544f909 2004-12-25 devnull static int
447 1544f909 2004-12-25 devnull threadrlock(RWLock *l, int block, ulong pc)
449 1544f909 2004-12-25 devnull USED(pc);
451 1544f909 2004-12-25 devnull lock(&l->l);
452 1544f909 2004-12-25 devnull if(l->writer == nil && l->wwaiting.head == nil){
453 1544f909 2004-12-25 devnull l->readers++;
454 1544f909 2004-12-25 devnull unlock(&l->l);
455 1544f909 2004-12-25 devnull return 1;
457 1544f909 2004-12-25 devnull if(!block){
458 1544f909 2004-12-25 devnull unlock(&l->l);
459 1544f909 2004-12-25 devnull return 0;
461 1544f909 2004-12-25 devnull addthread(&l->rwaiting, (*threadnow)());
462 1544f909 2004-12-25 devnull unlock(&l->l);
463 1544f909 2004-12-25 devnull _threadswitch();
464 1544f909 2004-12-25 devnull return 1;
467 1544f909 2004-12-25 devnull static int
468 1544f909 2004-12-25 devnull threadwlock(RWLock *l, int block, ulong pc)
470 1544f909 2004-12-25 devnull USED(pc);
472 1544f909 2004-12-25 devnull lock(&l->l);
473 1544f909 2004-12-25 devnull if(l->writer == nil && l->readers == 0){
474 1544f909 2004-12-25 devnull l->writer = (*threadnow)();
475 1544f909 2004-12-25 devnull unlock(&l->l);
476 1544f909 2004-12-25 devnull return 1;
478 1544f909 2004-12-25 devnull if(!block){
479 1544f909 2004-12-25 devnull unlock(&l->l);
480 1544f909 2004-12-25 devnull return 0;
482 1544f909 2004-12-25 devnull addthread(&l->wwaiting, (*threadnow)());
483 1544f909 2004-12-25 devnull unlock(&l->l);
484 1544f909 2004-12-25 devnull _threadswitch();
485 1544f909 2004-12-25 devnull return 1;
488 1544f909 2004-12-25 devnull static void
489 1544f909 2004-12-25 devnull threadrunlock(RWLock *l, ulong pc)
491 1544f909 2004-12-25 devnull _Thread *t;
493 1544f909 2004-12-25 devnull USED(pc);
494 80b8842f 2005-02-15 devnull t = nil;
495 1544f909 2004-12-25 devnull lock(&l->l);
496 1544f909 2004-12-25 devnull --l->readers;
497 1544f909 2004-12-25 devnull if(l->readers == 0 && (t = l->wwaiting.head) != nil){
498 1544f909 2004-12-25 devnull delthread(&l->wwaiting, t);
499 1544f909 2004-12-25 devnull l->writer = t;
501 1544f909 2004-12-25 devnull unlock(&l->l);
503 80b8842f 2005-02-15 devnull _threadready(t);
507 1544f909 2004-12-25 devnull static void
508 1544f909 2004-12-25 devnull threadwunlock(RWLock *l, ulong pc)
510 1544f909 2004-12-25 devnull _Thread *t;
512 1544f909 2004-12-25 devnull USED(pc);
513 1544f909 2004-12-25 devnull lock(&l->l);
514 1544f909 2004-12-25 devnull l->writer = nil;
515 1544f909 2004-12-25 devnull assert(l->readers == 0);
516 1544f909 2004-12-25 devnull while((t = l->rwaiting.head) != nil){
517 1544f909 2004-12-25 devnull delthread(&l->rwaiting, t);
518 1544f909 2004-12-25 devnull l->readers++;
519 1544f909 2004-12-25 devnull _threadready(t);
521 80b8842f 2005-02-15 devnull t = nil;
522 1544f909 2004-12-25 devnull if(l->readers == 0 && (t = l->wwaiting.head) != nil){
523 1544f909 2004-12-25 devnull delthread(&l->wwaiting, t);
524 1544f909 2004-12-25 devnull l->writer = t;
526 1544f909 2004-12-25 devnull unlock(&l->l);
528 80b8842f 2005-02-15 devnull _threadready(t);
532 1544f909 2004-12-25 devnull * sleep and wakeup
534 1544f909 2004-12-25 devnull static void
535 1544f909 2004-12-25 devnull threadrsleep(Rendez *r, ulong pc)
537 1544f909 2004-12-25 devnull addthread(&r->waiting, proc()->thread);
538 1544f909 2004-12-25 devnull qunlock(r->l);
539 1544f909 2004-12-25 devnull _threadswitch();
540 1544f909 2004-12-25 devnull qlock(r->l);
543 1544f909 2004-12-25 devnull static int
544 1544f909 2004-12-25 devnull threadrwakeup(Rendez *r, int all, ulong pc)
547 1544f909 2004-12-25 devnull _Thread *t;
549 1544f909 2004-12-25 devnull for(i=0;; i++){
550 1544f909 2004-12-25 devnull if(i==1 && !all)
552 1544f909 2004-12-25 devnull if((t = r->waiting.head) == nil)
554 1544f909 2004-12-25 devnull delthread(&r->waiting, t);
555 1544f909 2004-12-25 devnull _threadready(t);
557 1544f909 2004-12-25 devnull return i;
561 4dbefdd4 2004-12-27 devnull * startup
564 4dbefdd4 2004-12-27 devnull static int threadargc;
565 4dbefdd4 2004-12-27 devnull static char **threadargv;
566 4dbefdd4 2004-12-27 devnull int mainstacksize;
568 4dbefdd4 2004-12-27 devnull static void
569 4dbefdd4 2004-12-27 devnull threadmainstart(void *v)
571 4dbefdd4 2004-12-27 devnull USED(v);
574 0b561695 2005-01-16 devnull * N.B. This call to proc() is a program's first call (indirectly) to a
575 0b561695 2005-01-16 devnull * pthreads function while executing on a non-pthreads-allocated
576 0b561695 2005-01-16 devnull * stack. If the pthreads implementation is using the stack pointer
577 0b561695 2005-01-16 devnull * to locate the per-thread data, then this call will blow up.
578 0b561695 2005-01-16 devnull * This means the pthread implementation is not suitable for
579 0b561695 2005-01-16 devnull * running under libthread. Time to write your own. Sorry.
581 4dbefdd4 2004-12-27 devnull threadmainproc = proc();
582 4dbefdd4 2004-12-27 devnull threadmain(threadargc, threadargv);
586 4dbefdd4 2004-12-27 devnull main(int argc, char **argv)
588 4dbefdd4 2004-12-27 devnull Proc *p;
590 1d2533d0 2004-12-28 devnull argv0 = argv[0];
592 2c87dda8 2004-12-28 devnull _threadsetupdaemonize();
594 4dbefdd4 2004-12-27 devnull threadargc = argc;
595 4dbefdd4 2004-12-27 devnull threadargv = argv;
598 4dbefdd4 2004-12-27 devnull * Install locking routines into C library.
600 4dbefdd4 2004-12-27 devnull _lock = _threadlock;
601 4dbefdd4 2004-12-27 devnull _unlock = _threadunlock;
602 4dbefdd4 2004-12-27 devnull _qlock = threadqlock;
603 4dbefdd4 2004-12-27 devnull _qunlock = threadqunlock;
604 4dbefdd4 2004-12-27 devnull _rlock = threadrlock;
605 4dbefdd4 2004-12-27 devnull _runlock = threadrunlock;
606 4dbefdd4 2004-12-27 devnull _wlock = threadwlock;
607 4dbefdd4 2004-12-27 devnull _wunlock = threadwunlock;
608 4dbefdd4 2004-12-27 devnull _rsleep = threadrsleep;
609 4dbefdd4 2004-12-27 devnull _rwakeup = threadrwakeup;
610 4dbefdd4 2004-12-27 devnull _notejmpbuf = threadnotejmp;
612 4dbefdd4 2004-12-27 devnull _pthreadinit();
613 4dbefdd4 2004-12-27 devnull p = procalloc();
614 8ee6ad4d 2005-02-14 devnull p->mainproc = 1;
615 4dbefdd4 2004-12-27 devnull _threadsetproc(p);
616 4dbefdd4 2004-12-27 devnull if(mainstacksize == 0)
617 a0e8d02d 2005-01-23 devnull mainstacksize = 256*1024;
618 6dde87f8 2005-09-26 devnull atnotify(threadinfo, 1);
619 4dbefdd4 2004-12-27 devnull _threadcreate(p, threadmainstart, nil, mainstacksize);
620 1fdb4a54 2005-01-23 devnull procscheduler(p);
621 d7688cce 2005-02-16 devnull sysfatal("procscheduler returned in threadmain!");
622 8ee6ad4d 2005-02-14 devnull /* does not return */
623 15cd8255 2005-01-04 devnull return 0;
627 1544f909 2004-12-25 devnull * hooray for linked lists
629 1544f909 2004-12-25 devnull static void
630 1544f909 2004-12-25 devnull addthread(_Threadlist *l, _Thread *t)
632 1544f909 2004-12-25 devnull if(l->tail){
633 1544f909 2004-12-25 devnull l->tail->next = t;
634 1544f909 2004-12-25 devnull t->prev = l->tail;
636 1544f909 2004-12-25 devnull l->head = t;
637 1544f909 2004-12-25 devnull t->prev = nil;
639 1544f909 2004-12-25 devnull l->tail = t;
640 1544f909 2004-12-25 devnull t->next = nil;
643 1544f909 2004-12-25 devnull static void
644 1544f909 2004-12-25 devnull delthread(_Threadlist *l, _Thread *t)
646 1544f909 2004-12-25 devnull if(t->prev)
647 1544f909 2004-12-25 devnull t->prev->next = t->next;
649 1544f909 2004-12-25 devnull l->head = t->next;
650 1544f909 2004-12-25 devnull if(t->next)
651 1544f909 2004-12-25 devnull t->next->prev = t->prev;
653 1544f909 2004-12-25 devnull l->tail = t->prev;
656 1544f909 2004-12-25 devnull static void
657 1544f909 2004-12-25 devnull addthreadinproc(Proc *p, _Thread *t)
659 1544f909 2004-12-25 devnull _Threadlist *l;
661 1544f909 2004-12-25 devnull l = &p->allthreads;
662 1544f909 2004-12-25 devnull if(l->tail){
663 1544f909 2004-12-25 devnull l->tail->allnext = t;
664 1544f909 2004-12-25 devnull t->allprev = l->tail;
666 1544f909 2004-12-25 devnull l->head = t;
667 1544f909 2004-12-25 devnull t->allprev = nil;
669 1544f909 2004-12-25 devnull l->tail = t;
670 1544f909 2004-12-25 devnull t->allnext = nil;
673 1544f909 2004-12-25 devnull static void
674 1544f909 2004-12-25 devnull delthreadinproc(Proc *p, _Thread *t)
676 1544f909 2004-12-25 devnull _Threadlist *l;
678 1544f909 2004-12-25 devnull l = &p->allthreads;
679 1544f909 2004-12-25 devnull if(t->allprev)
680 1544f909 2004-12-25 devnull t->allprev->allnext = t->allnext;
682 1544f909 2004-12-25 devnull l->head = t->allnext;
683 1544f909 2004-12-25 devnull if(t->allnext)
684 1544f909 2004-12-25 devnull t->allnext->allprev = t->allprev;
686 1544f909 2004-12-25 devnull l->tail = t->allprev;
689 4dbefdd4 2004-12-27 devnull Proc *_threadprocs;
690 4dbefdd4 2004-12-27 devnull Lock _threadprocslock;
691 4dbefdd4 2004-12-27 devnull static Proc *_threadprocstail;
693 1544f909 2004-12-25 devnull static void
694 4dbefdd4 2004-12-27 devnull addproc(Proc *p)
696 4dbefdd4 2004-12-27 devnull lock(&_threadprocslock);
697 4dbefdd4 2004-12-27 devnull if(_threadprocstail){
698 4dbefdd4 2004-12-27 devnull _threadprocstail->next = p;
699 4dbefdd4 2004-12-27 devnull p->prev = _threadprocstail;
701 4dbefdd4 2004-12-27 devnull _threadprocs = p;
702 4dbefdd4 2004-12-27 devnull p->prev = nil;
704 4dbefdd4 2004-12-27 devnull _threadprocstail = p;
705 4dbefdd4 2004-12-27 devnull p->next = nil;
706 4dbefdd4 2004-12-27 devnull unlock(&_threadprocslock);
709 4dbefdd4 2004-12-27 devnull static void
710 4dbefdd4 2004-12-27 devnull delproc(Proc *p)
712 4dbefdd4 2004-12-27 devnull lock(&_threadprocslock);
713 4dbefdd4 2004-12-27 devnull if(p->prev)
714 4dbefdd4 2004-12-27 devnull p->prev->next = p->next;
716 4dbefdd4 2004-12-27 devnull _threadprocs = p->next;
717 4dbefdd4 2004-12-27 devnull if(p->next)
718 4dbefdd4 2004-12-27 devnull p->next->prev = p->prev;
720 4dbefdd4 2004-12-27 devnull _threadprocstail = p->prev;
721 4dbefdd4 2004-12-27 devnull unlock(&_threadprocslock);
725 15cd8255 2005-01-04 devnull * notify - for now just use the usual mechanisms
728 15cd8255 2005-01-04 devnull threadnotify(int (*f)(void*, char*), int in)
730 15cd8255 2005-01-04 devnull atnotify(f, in);
733 6dde87f8 2005-09-26 devnull static int
734 6dde87f8 2005-09-26 devnull onrunqueue(Proc *p, _Thread *t)
736 6dde87f8 2005-09-26 devnull _Thread *tt;
738 6dde87f8 2005-09-26 devnull for(tt=p->runqueue.head; tt; tt=tt->next)
739 6dde87f8 2005-09-26 devnull if(tt == t)
740 6dde87f8 2005-09-26 devnull return 1;
741 6dde87f8 2005-09-26 devnull return 0;
745 6dde87f8 2005-09-26 devnull * print state - called from SIGINFO
747 6dde87f8 2005-09-26 devnull static int
748 6dde87f8 2005-09-26 devnull threadinfo(void *v, char *s)
750 6dde87f8 2005-09-26 devnull Proc *p;
751 6dde87f8 2005-09-26 devnull _Thread *t;
753 6dde87f8 2005-09-26 devnull if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
754 6dde87f8 2005-09-26 devnull return 0;
756 6dde87f8 2005-09-26 devnull for(p=_threadprocs; p; p=p->next){
757 6dde87f8 2005-09-26 devnull fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
758 6dde87f8 2005-09-26 devnull p->sysproc ? " (sysproc)": "");
759 6dde87f8 2005-09-26 devnull for(t=p->allthreads.head; t; t=t->allnext){
760 6dde87f8 2005-09-26 devnull fprint(2, "\tthread %d %s: %s %s\n",
762 6dde87f8 2005-09-26 devnull t == p->thread ? "Running" :
763 6dde87f8 2005-09-26 devnull onrunqueue(p, t) ? "Ready" : "Sleeping",
764 6dde87f8 2005-09-26 devnull t->state, t->name);
767 6dde87f8 2005-09-26 devnull return 1;