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 df970459 2006-06-26 devnull static int onlist(_Threadlist*, _Thread*);
16 1544f909 2004-12-25 devnull static void addthreadinproc(Proc*, _Thread*);
17 1544f909 2004-12-25 devnull static void delthreadinproc(Proc*, _Thread*);
18 1544f909 2004-12-25 devnull static void contextswitch(Context *from, Context *to);
19 1fdb4a54 2005-01-23 devnull static void procscheduler(Proc*);
20 6dde87f8 2005-09-26 devnull static int threadinfo(void*, char*);
22 2c87dda8 2004-12-28 devnull static void
23 2c87dda8 2004-12-28 devnull _threaddebug(char *fmt, ...)
25 2c87dda8 2004-12-28 devnull va_list arg;
26 2c87dda8 2004-12-28 devnull char buf[128];
27 2c87dda8 2004-12-28 devnull _Thread *t;
29 af89fc10 2005-01-06 devnull static int fd = -1;
31 5b092791 2005-09-26 devnull if(_threaddebuglevel == 0)
34 af89fc10 2005-01-06 devnull if(fd < 0){
35 af89fc10 2005-01-06 devnull p = strrchr(argv0, '/');
39 af89fc10 2005-01-06 devnull p = argv0;
40 af89fc10 2005-01-06 devnull snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
41 af89fc10 2005-01-06 devnull if((fd = create(buf, OWRITE, 0666)) < 0)
42 af89fc10 2005-01-06 devnull fd = open("/dev/null", OWRITE);
43 d93cc14e 2006-06-12 devnull if(fd >= 0 && fd != 2){
44 d93cc14e 2006-06-12 devnull dup(fd, 2);
45 d93cc14e 2006-06-12 devnull close(fd);
50 2c87dda8 2004-12-28 devnull va_start(arg, fmt);
51 2c87dda8 2004-12-28 devnull vsnprint(buf, sizeof buf, fmt, arg);
52 2c87dda8 2004-12-28 devnull va_end(arg);
53 2c87dda8 2004-12-28 devnull t = proc()->thread;
55 af89fc10 2005-01-06 devnull fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
57 af89fc10 2005-01-06 devnull fprint(fd, "%d._: %s\n", getpid(), buf);
60 1544f909 2004-12-25 devnull static _Thread*
61 1544f909 2004-12-25 devnull getthreadnow(void)
63 1544f909 2004-12-25 devnull return proc()->thread;
65 1544f909 2004-12-25 devnull _Thread *(*threadnow)(void) = getthreadnow;
67 1544f909 2004-12-25 devnull static Proc*
68 1544f909 2004-12-25 devnull procalloc(void)
72 1544f909 2004-12-25 devnull p = malloc(sizeof *p);
73 73722a8b 2004-12-27 devnull if(p == nil)
74 73722a8b 2004-12-27 devnull sysfatal("procalloc malloc: %r");
75 1544f909 2004-12-25 devnull memset(p, 0, sizeof *p);
76 4dbefdd4 2004-12-27 devnull addproc(p);
77 1544f909 2004-12-25 devnull lock(&threadnproclock);
78 1544f909 2004-12-25 devnull threadnproc++;
79 1544f909 2004-12-25 devnull unlock(&threadnproclock);
80 1544f909 2004-12-25 devnull return p;
83 1544f909 2004-12-25 devnull static void
84 86502504 2005-01-18 devnull threadstart(uint y, uint x)
86 1544f909 2004-12-25 devnull _Thread *t;
89 df1ee4e1 2008-06-01 rsc z = (ulong)x << 16; /* hide undefined 32-bit shift from 32-bit compilers */
90 86502504 2005-01-18 devnull z <<= 16;
92 86502504 2005-01-18 devnull t = (_Thread*)z;
94 cbeb0b26 2006-04-01 devnull /*print("threadstart %p\n", v); */
95 1544f909 2004-12-25 devnull t->startfn(t->startarg);
96 cbeb0b26 2006-04-01 devnull /*print("threadexits %p\n", v); */
97 4dbefdd4 2004-12-27 devnull threadexits(nil);
98 cbeb0b26 2006-04-01 devnull /*print("not reacehd\n"); */
101 1544f909 2004-12-25 devnull static _Thread*
102 1544f909 2004-12-25 devnull threadalloc(void (*fn)(void*), void *arg, uint stack)
104 1544f909 2004-12-25 devnull _Thread *t;
105 1544f909 2004-12-25 devnull sigset_t zero;
106 86502504 2005-01-18 devnull uint x, y;
107 86502504 2005-01-18 devnull ulong z;
109 1544f909 2004-12-25 devnull /* allocate the task and stack together */
110 1544f909 2004-12-25 devnull t = malloc(sizeof *t+stack);
111 73722a8b 2004-12-27 devnull if(t == nil)
112 73722a8b 2004-12-27 devnull sysfatal("threadalloc malloc: %r");
113 1544f909 2004-12-25 devnull memset(t, 0, sizeof *t);
114 1544f909 2004-12-25 devnull t->stk = (uchar*)(t+1);
115 1544f909 2004-12-25 devnull t->stksize = stack;
116 1544f909 2004-12-25 devnull t->id = incref(&threadidref);
117 1544f909 2004-12-25 devnull t->startfn = fn;
118 1544f909 2004-12-25 devnull t->startarg = arg;
120 1544f909 2004-12-25 devnull /* do a reasonable initialization */
121 1544f909 2004-12-25 devnull memset(&t->context.uc, 0, sizeof t->context.uc);
122 1544f909 2004-12-25 devnull sigemptyset(&zero);
123 1544f909 2004-12-25 devnull sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
125 d54ead7f 2004-12-28 devnull /* must initialize with current context */
126 37cc72d3 2005-01-11 devnull if(getcontext(&t->context.uc) < 0)
127 37cc72d3 2005-01-11 devnull sysfatal("threadalloc getcontext: %r");
129 1544f909 2004-12-25 devnull /* call makecontext to do the real work. */
130 1544f909 2004-12-25 devnull /* leave a few words open on both ends */
131 0ac67058 2008-12-23 rsc t->context.uc.uc_stack.ss_sp = (void*)(t->stk+8);
132 2c87dda8 2004-12-28 devnull t->context.uc.uc_stack.ss_size = t->stksize-64;
133 d08f4066 2005-07-27 devnull #if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */
134 37cc72d3 2005-01-11 devnull /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
135 2b3b1e89 2005-01-14 devnull t->context.uc.uc_stack.ss_sp =
136 2b3b1e89 2005-01-14 devnull (char*)t->context.uc.uc_stack.ss_sp
137 2b3b1e89 2005-01-14 devnull +t->context.uc.uc_stack.ss_size;
140 86502504 2005-01-18 devnull * All this magic is because you have to pass makecontext a
141 86502504 2005-01-18 devnull * function that takes some number of word-sized variables,
142 86502504 2005-01-18 devnull * and on 64-bit machines pointers are bigger than words.
144 86502504 2005-01-18 devnull z = (ulong)t;
146 86502504 2005-01-18 devnull z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
147 86502504 2005-01-18 devnull x = z>>16;
148 2b604081 2005-05-07 devnull makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
150 1544f909 2004-12-25 devnull return t;
153 1544f909 2004-12-25 devnull _Thread*
154 1544f909 2004-12-25 devnull _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
156 1544f909 2004-12-25 devnull _Thread *t;
158 0829f75b 2009-08-18 rsc /* defend against bad C libraries */
159 0829f75b 2009-08-18 rsc if(stack < (256<<10))
160 0829f75b 2009-08-18 rsc stack = 256<<10;
162 1544f909 2004-12-25 devnull t = threadalloc(fn, arg, stack);
163 1544f909 2004-12-25 devnull t->proc = p;
164 1544f909 2004-12-25 devnull addthreadinproc(p, t);
165 1544f909 2004-12-25 devnull p->nthread++;
166 1544f909 2004-12-25 devnull _threadready(t);
167 1544f909 2004-12-25 devnull return t;
171 1544f909 2004-12-25 devnull threadcreate(void (*fn)(void*), void *arg, uint stack)
173 1544f909 2004-12-25 devnull _Thread *t;
175 1544f909 2004-12-25 devnull t = _threadcreate(proc(), fn, arg, stack);
176 1544f909 2004-12-25 devnull return t->id;
180 1544f909 2004-12-25 devnull proccreate(void (*fn)(void*), void *arg, uint stack)
183 1544f909 2004-12-25 devnull _Thread *t;
184 1544f909 2004-12-25 devnull Proc *p;
186 1544f909 2004-12-25 devnull p = procalloc();
187 1544f909 2004-12-25 devnull t = _threadcreate(p, fn, arg, stack);
188 803292c6 2005-01-18 devnull id = t->id; /* t might be freed after _procstart */
189 1fdb4a54 2005-01-23 devnull _procstart(p, procscheduler);
190 803292c6 2005-01-18 devnull return id;
194 1544f909 2004-12-25 devnull _threadswitch(void)
196 1544f909 2004-12-25 devnull Proc *p;
198 c8b6342d 2005-01-13 devnull needstack(0);
199 1544f909 2004-12-25 devnull p = proc();
200 cbeb0b26 2006-04-01 devnull /*print("threadswtch %p\n", p); */
201 1544f909 2004-12-25 devnull contextswitch(&p->thread->context, &p->schedcontext);
205 1544f909 2004-12-25 devnull _threadready(_Thread *t)
207 1544f909 2004-12-25 devnull Proc *p;
209 1544f909 2004-12-25 devnull p = t->proc;
210 1544f909 2004-12-25 devnull lock(&p->lock);
211 a0a331aa 2005-01-06 devnull p->runrend.l = &p->lock;
212 1544f909 2004-12-25 devnull addthread(&p->runqueue, t);
213 cbeb0b26 2006-04-01 devnull /*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
214 8cbd854a 2004-12-27 devnull if(p != proc())
215 a0a331aa 2005-01-06 devnull _procwakeupandunlock(&p->runrend);
217 a0a331aa 2005-01-06 devnull unlock(&p->lock);
221 615e0f9f 2006-02-07 devnull threadidle(void)
224 615e0f9f 2006-02-07 devnull Proc *p;
226 615e0f9f 2006-02-07 devnull p = proc();
227 615e0f9f 2006-02-07 devnull n = p->nswitch;
228 615e0f9f 2006-02-07 devnull lock(&p->lock);
229 615e0f9f 2006-02-07 devnull p->runrend.l = &p->lock;
230 615e0f9f 2006-02-07 devnull addthread(&p->idlequeue, p->thread);
231 615e0f9f 2006-02-07 devnull unlock(&p->lock);
232 615e0f9f 2006-02-07 devnull _threadswitch();
233 615e0f9f 2006-02-07 devnull return p->nswitch - n;
237 1544f909 2004-12-25 devnull threadyield(void)
240 8cbd854a 2004-12-27 devnull Proc *p;
242 8cbd854a 2004-12-27 devnull p = proc();
243 8cbd854a 2004-12-27 devnull n = p->nswitch;
244 8cbd854a 2004-12-27 devnull _threadready(p->thread);
245 1544f909 2004-12-25 devnull _threadswitch();
246 8cbd854a 2004-12-27 devnull return p->nswitch - n;
250 1544f909 2004-12-25 devnull threadexits(char *msg)
252 1544f909 2004-12-25 devnull Proc *p;
254 1544f909 2004-12-25 devnull p = proc();
255 4dbefdd4 2004-12-27 devnull if(msg == nil)
256 4dbefdd4 2004-12-27 devnull msg = "";
257 1544f909 2004-12-25 devnull utfecpy(p->msg, p->msg+sizeof p->msg, msg);
258 4dbefdd4 2004-12-27 devnull proc()->thread->exiting = 1;
259 4dbefdd4 2004-12-27 devnull _threadswitch();
263 df970459 2006-06-26 devnull threadpin(void)
265 df970459 2006-06-26 devnull Proc *p;
267 df970459 2006-06-26 devnull p = proc();
268 df970459 2006-06-26 devnull if(p->pinthread){
269 df970459 2006-06-26 devnull fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
270 df970459 2006-06-26 devnull assert(0);
272 df970459 2006-06-26 devnull p->pinthread = p->thread;
276 df970459 2006-06-26 devnull threadunpin(void)
278 df970459 2006-06-26 devnull Proc *p;
280 df970459 2006-06-26 devnull p = proc();
281 df970459 2006-06-26 devnull if(p->pinthread != p->thread){
282 df970459 2006-06-26 devnull fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
283 df970459 2006-06-26 devnull assert(0);
285 df970459 2006-06-26 devnull p->pinthread = nil;
289 d19769ae 2008-10-26 rsc threadsysfatal(char *fmt, va_list arg)
291 69d1259c 2008-07-20 devnull char buf[256];
293 69d1259c 2008-07-20 devnull vseprint(buf, buf+sizeof(buf), fmt, arg);
294 69d1259c 2008-07-20 devnull __fixargv0();
295 69d1259c 2008-07-20 devnull fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
296 69d1259c 2008-07-20 devnull threadexitsall(buf);
299 1544f909 2004-12-25 devnull static void
300 1544f909 2004-12-25 devnull contextswitch(Context *from, Context *to)
302 1544f909 2004-12-25 devnull if(swapcontext(&from->uc, &to->uc) < 0){
303 1544f909 2004-12-25 devnull fprint(2, "swapcontext failed: %r\n");
304 1544f909 2004-12-25 devnull assert(0);
308 1544f909 2004-12-25 devnull static void
309 1fdb4a54 2005-01-23 devnull procscheduler(Proc *p)
311 1544f909 2004-12-25 devnull _Thread *t;
313 1544f909 2004-12-25 devnull setproc(p);
314 2c87dda8 2004-12-28 devnull _threaddebug("scheduler enter");
315 cbeb0b26 2006-04-01 devnull /* print("s %p\n", p); */
316 1544f909 2004-12-25 devnull lock(&p->lock);
317 1544f909 2004-12-25 devnull for(;;){
318 df970459 2006-06-26 devnull if((t = p->pinthread) != nil){
319 df970459 2006-06-26 devnull while(!onlist(&p->runqueue, t)){
320 df970459 2006-06-26 devnull p->runrend.l = &p->lock;
321 df970459 2006-06-26 devnull _threaddebug("scheduler sleep (pin)");
322 df970459 2006-06-26 devnull _procsleep(&p->runrend);
323 df970459 2006-06-26 devnull _threaddebug("scheduler wake (pin)");
326 1544f909 2004-12-25 devnull while((t = p->runqueue.head) == nil){
327 1544f909 2004-12-25 devnull if(p->nthread == 0)
328 1544f909 2004-12-25 devnull goto Out;
329 615e0f9f 2006-02-07 devnull if((t = p->idlequeue.head) != nil){
331 615e0f9f 2006-02-07 devnull * Run all the idling threads once.
333 615e0f9f 2006-02-07 devnull while((t = p->idlequeue.head) != nil){
334 615e0f9f 2006-02-07 devnull delthread(&p->idlequeue, t);
335 615e0f9f 2006-02-07 devnull addthread(&p->runqueue, t);
337 615e0f9f 2006-02-07 devnull continue;
339 1544f909 2004-12-25 devnull p->runrend.l = &p->lock;
340 2c87dda8 2004-12-28 devnull _threaddebug("scheduler sleep");
341 1544f909 2004-12-25 devnull _procsleep(&p->runrend);
342 2c87dda8 2004-12-28 devnull _threaddebug("scheduler wake");
344 df970459 2006-06-26 devnull if(p->pinthread && p->pinthread != t)
345 df970459 2006-06-26 devnull fprint(2, "p->pinthread %p t %p\n", p->pinthread, t);
346 df970459 2006-06-26 devnull assert(p->pinthread == nil || p->pinthread == t);
347 1544f909 2004-12-25 devnull delthread(&p->runqueue, t);
348 1544f909 2004-12-25 devnull unlock(&p->lock);
349 1544f909 2004-12-25 devnull p->thread = t;
350 8cbd854a 2004-12-27 devnull p->nswitch++;
351 2c87dda8 2004-12-28 devnull _threaddebug("run %d (%s)", t->id, t->name);
352 1544f909 2004-12-25 devnull contextswitch(&p->schedcontext, &t->context);
353 cbeb0b26 2006-04-01 devnull /*print("back in scheduler\n"); */
354 1544f909 2004-12-25 devnull p->thread = nil;
355 1544f909 2004-12-25 devnull lock(&p->lock);
356 1544f909 2004-12-25 devnull if(t->exiting){
357 1544f909 2004-12-25 devnull delthreadinproc(p, t);
358 1544f909 2004-12-25 devnull p->nthread--;
359 cbeb0b26 2006-04-01 devnull /*print("nthread %d\n", p->nthread); */
360 1544f909 2004-12-25 devnull free(t);
365 2c87dda8 2004-12-28 devnull _threaddebug("scheduler exit");
366 8ee6ad4d 2005-02-14 devnull if(p->mainproc){
368 8ee6ad4d 2005-02-14 devnull * Stupid bug - on Linux 2.6 and maybe elsewhere,
369 8ee6ad4d 2005-02-14 devnull * if the main thread exits then the others keep running
370 8ee6ad4d 2005-02-14 devnull * but the process shows up as a zombie in ps and is not
371 8ee6ad4d 2005-02-14 devnull * attachable with ptrace. We'll just sit around pretending
372 8ee6ad4d 2005-02-14 devnull * to be a system proc instead of exiting.
374 8ee6ad4d 2005-02-14 devnull _threaddaemonize();
375 8ee6ad4d 2005-02-14 devnull lock(&threadnproclock);
376 8ee6ad4d 2005-02-14 devnull if(++threadnsysproc == threadnproc)
377 8ee6ad4d 2005-02-14 devnull threadexitsall(p->msg);
378 8ee6ad4d 2005-02-14 devnull p->sysproc = 1;
379 8ee6ad4d 2005-02-14 devnull unlock(&threadnproclock);
381 8ee6ad4d 2005-02-14 devnull sleep(1000);
384 4dbefdd4 2004-12-27 devnull delproc(p);
385 1544f909 2004-12-25 devnull lock(&threadnproclock);
386 1544f909 2004-12-25 devnull if(p->sysproc)
387 1544f909 2004-12-25 devnull --threadnsysproc;
388 1544f909 2004-12-25 devnull if(--threadnproc == threadnsysproc)
389 4dbefdd4 2004-12-27 devnull threadexitsall(p->msg);
390 1544f909 2004-12-25 devnull unlock(&threadnproclock);
391 1544f909 2004-12-25 devnull unlock(&p->lock);
392 d93cc14e 2006-06-12 devnull _threadsetproc(nil);
393 1544f909 2004-12-25 devnull free(p);
397 1544f909 2004-12-25 devnull _threadsetsysproc(void)
399 1544f909 2004-12-25 devnull lock(&threadnproclock);
400 1544f909 2004-12-25 devnull if(++threadnsysproc == threadnproc)
401 8ee6ad4d 2005-02-14 devnull threadexitsall(nil);
402 1544f909 2004-12-25 devnull unlock(&threadnproclock);
403 1544f909 2004-12-25 devnull proc()->sysproc = 1;
407 4dbefdd4 2004-12-27 devnull procdata(void)
409 4dbefdd4 2004-12-27 devnull return &proc()->udata;
413 8dd8a81f 2005-03-18 devnull threaddata(void)
415 8dd8a81f 2005-03-18 devnull return &proc()->thread->udata;
418 4dbefdd4 2004-12-27 devnull extern Jmp *(*_notejmpbuf)(void);
419 4dbefdd4 2004-12-27 devnull static Jmp*
420 4dbefdd4 2004-12-27 devnull threadnotejmp(void)
422 4dbefdd4 2004-12-27 devnull return &proc()->sigjmp;
426 1544f909 2004-12-25 devnull * debugging
429 1544f909 2004-12-25 devnull threadsetname(char *fmt, ...)
431 1544f909 2004-12-25 devnull va_list arg;
432 1544f909 2004-12-25 devnull _Thread *t;
434 1544f909 2004-12-25 devnull t = proc()->thread;
435 1544f909 2004-12-25 devnull va_start(arg, fmt);
436 1544f909 2004-12-25 devnull vsnprint(t->name, sizeof t->name, fmt, arg);
437 1544f909 2004-12-25 devnull va_end(arg);
441 515b6e5b 2005-01-18 devnull threadgetname(void)
443 515b6e5b 2005-01-18 devnull return proc()->thread->name;
447 1544f909 2004-12-25 devnull threadsetstate(char *fmt, ...)
449 1544f909 2004-12-25 devnull va_list arg;
450 1544f909 2004-12-25 devnull _Thread *t;
452 1544f909 2004-12-25 devnull t = proc()->thread;
453 1544f909 2004-12-25 devnull va_start(arg, fmt);
454 1544f909 2004-12-25 devnull vsnprint(t->state, sizeof t->name, fmt, arg);
455 1544f909 2004-12-25 devnull va_end(arg);
459 9eda38e5 2006-02-05 devnull threadid(void)
461 9eda38e5 2006-02-05 devnull _Thread *t;
463 9eda38e5 2006-02-05 devnull t = proc()->thread;
464 9eda38e5 2006-02-05 devnull return t->id;
468 c8b6342d 2005-01-13 devnull needstack(int n)
470 c8b6342d 2005-01-13 devnull _Thread *t;
472 c8b6342d 2005-01-13 devnull t = proc()->thread;
474 c8b6342d 2005-01-13 devnull if((char*)&t <= (char*)t->stk
475 c8b6342d 2005-01-13 devnull || (char*)&t - (char*)t->stk < 256+n){
476 1fdb4a54 2005-01-23 devnull fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
477 c8b6342d 2005-01-13 devnull abort();
482 faf1fb6c 2008-07-09 rsc singlethreaded(void)
484 faf1fb6c 2008-07-09 rsc return threadnproc == 1 && _threadprocs->nthread == 1;
488 1544f909 2004-12-25 devnull * locking
490 1544f909 2004-12-25 devnull static int
491 1544f909 2004-12-25 devnull threadqlock(QLock *l, int block, ulong pc)
493 cbeb0b26 2006-04-01 devnull /*print("threadqlock %p\n", l); */
494 1544f909 2004-12-25 devnull lock(&l->l);
495 1544f909 2004-12-25 devnull if(l->owner == nil){
496 1544f909 2004-12-25 devnull l->owner = (*threadnow)();
497 cbeb0b26 2006-04-01 devnull /*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
498 1544f909 2004-12-25 devnull unlock(&l->l);
499 1544f909 2004-12-25 devnull return 1;
501 1544f909 2004-12-25 devnull if(!block){
502 1544f909 2004-12-25 devnull unlock(&l->l);
503 1544f909 2004-12-25 devnull return 0;
506 faf1fb6c 2008-07-09 rsc if(singlethreaded()){
507 faf1fb6c 2008-07-09 rsc fprint(2, "qlock deadlock\n");
511 cbeb0b26 2006-04-01 devnull /*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
512 1544f909 2004-12-25 devnull addthread(&l->waiting, (*threadnow)());
513 1544f909 2004-12-25 devnull unlock(&l->l);
515 1544f909 2004-12-25 devnull _threadswitch();
517 1544f909 2004-12-25 devnull if(l->owner != (*threadnow)()){
518 15cd8255 2005-01-04 devnull fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
519 15cd8255 2005-01-04 devnull argv0, pc, l->owner, (*threadnow)());
520 1544f909 2004-12-25 devnull abort();
522 cbeb0b26 2006-04-01 devnull /*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
523 1544f909 2004-12-25 devnull return 1;
526 1544f909 2004-12-25 devnull static void
527 1544f909 2004-12-25 devnull threadqunlock(QLock *l, ulong pc)
529 80b8842f 2005-02-15 devnull _Thread *ready;
531 1544f909 2004-12-25 devnull lock(&l->l);
532 cbeb0b26 2006-04-01 devnull /*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
533 1aa9c533 2005-01-07 devnull if(l->owner == 0){
534 15cd8255 2005-01-04 devnull fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
535 15cd8255 2005-01-04 devnull argv0, pc, l->owner, (*threadnow)());
536 1aa9c533 2005-01-07 devnull abort();
538 80b8842f 2005-02-15 devnull if((l->owner = ready = l->waiting.head) != nil)
539 1544f909 2004-12-25 devnull delthread(&l->waiting, l->owner);
541 80b8842f 2005-02-15 devnull * N.B. Cannot call _threadready() before unlocking l->l,
542 80b8842f 2005-02-15 devnull * because the thread we are readying might:
543 80b8842f 2005-02-15 devnull * - be in another proc
544 80b8842f 2005-02-15 devnull * - start running immediately
545 80b8842f 2005-02-15 devnull * - and free l before we get a chance to run again
547 1544f909 2004-12-25 devnull unlock(&l->l);
548 80b8842f 2005-02-15 devnull if(ready)
549 80b8842f 2005-02-15 devnull _threadready(l->owner);
552 1544f909 2004-12-25 devnull static int
553 1544f909 2004-12-25 devnull threadrlock(RWLock *l, int block, ulong pc)
555 1544f909 2004-12-25 devnull USED(pc);
557 1544f909 2004-12-25 devnull lock(&l->l);
558 1544f909 2004-12-25 devnull if(l->writer == nil && l->wwaiting.head == nil){
559 1544f909 2004-12-25 devnull l->readers++;
560 1544f909 2004-12-25 devnull unlock(&l->l);
561 1544f909 2004-12-25 devnull return 1;
563 1544f909 2004-12-25 devnull if(!block){
564 1544f909 2004-12-25 devnull unlock(&l->l);
565 1544f909 2004-12-25 devnull return 0;
567 faf1fb6c 2008-07-09 rsc if(singlethreaded()){
568 faf1fb6c 2008-07-09 rsc fprint(2, "rlock deadlock\n");
571 1544f909 2004-12-25 devnull addthread(&l->rwaiting, (*threadnow)());
572 1544f909 2004-12-25 devnull unlock(&l->l);
573 1544f909 2004-12-25 devnull _threadswitch();
574 1544f909 2004-12-25 devnull return 1;
577 1544f909 2004-12-25 devnull static int
578 1544f909 2004-12-25 devnull threadwlock(RWLock *l, int block, ulong pc)
580 1544f909 2004-12-25 devnull USED(pc);
582 1544f909 2004-12-25 devnull lock(&l->l);
583 1544f909 2004-12-25 devnull if(l->writer == nil && l->readers == 0){
584 1544f909 2004-12-25 devnull l->writer = (*threadnow)();
585 1544f909 2004-12-25 devnull unlock(&l->l);
586 1544f909 2004-12-25 devnull return 1;
588 1544f909 2004-12-25 devnull if(!block){
589 1544f909 2004-12-25 devnull unlock(&l->l);
590 1544f909 2004-12-25 devnull return 0;
592 faf1fb6c 2008-07-09 rsc if(singlethreaded()){
593 faf1fb6c 2008-07-09 rsc fprint(2, "wlock deadlock\n");
596 1544f909 2004-12-25 devnull addthread(&l->wwaiting, (*threadnow)());
597 1544f909 2004-12-25 devnull unlock(&l->l);
598 1544f909 2004-12-25 devnull _threadswitch();
599 1544f909 2004-12-25 devnull return 1;
602 1544f909 2004-12-25 devnull static void
603 1544f909 2004-12-25 devnull threadrunlock(RWLock *l, ulong pc)
605 1544f909 2004-12-25 devnull _Thread *t;
607 1544f909 2004-12-25 devnull USED(pc);
608 80b8842f 2005-02-15 devnull t = nil;
609 1544f909 2004-12-25 devnull lock(&l->l);
610 1544f909 2004-12-25 devnull --l->readers;
611 1544f909 2004-12-25 devnull if(l->readers == 0 && (t = l->wwaiting.head) != nil){
612 1544f909 2004-12-25 devnull delthread(&l->wwaiting, t);
613 1544f909 2004-12-25 devnull l->writer = t;
615 1544f909 2004-12-25 devnull unlock(&l->l);
617 80b8842f 2005-02-15 devnull _threadready(t);
621 1544f909 2004-12-25 devnull static void
622 1544f909 2004-12-25 devnull threadwunlock(RWLock *l, ulong pc)
624 1544f909 2004-12-25 devnull _Thread *t;
626 1544f909 2004-12-25 devnull USED(pc);
627 1544f909 2004-12-25 devnull lock(&l->l);
628 1544f909 2004-12-25 devnull l->writer = nil;
629 1544f909 2004-12-25 devnull assert(l->readers == 0);
630 1544f909 2004-12-25 devnull while((t = l->rwaiting.head) != nil){
631 1544f909 2004-12-25 devnull delthread(&l->rwaiting, t);
632 1544f909 2004-12-25 devnull l->readers++;
633 1544f909 2004-12-25 devnull _threadready(t);
635 80b8842f 2005-02-15 devnull t = nil;
636 1544f909 2004-12-25 devnull if(l->readers == 0 && (t = l->wwaiting.head) != nil){
637 1544f909 2004-12-25 devnull delthread(&l->wwaiting, t);
638 1544f909 2004-12-25 devnull l->writer = t;
640 1544f909 2004-12-25 devnull unlock(&l->l);
642 80b8842f 2005-02-15 devnull _threadready(t);
646 1544f909 2004-12-25 devnull * sleep and wakeup
648 1544f909 2004-12-25 devnull static void
649 1544f909 2004-12-25 devnull threadrsleep(Rendez *r, ulong pc)
651 faf1fb6c 2008-07-09 rsc if(singlethreaded()){
652 faf1fb6c 2008-07-09 rsc fprint(2, "rsleep deadlock\n");
655 1544f909 2004-12-25 devnull addthread(&r->waiting, proc()->thread);
656 1544f909 2004-12-25 devnull qunlock(r->l);
657 1544f909 2004-12-25 devnull _threadswitch();
658 1544f909 2004-12-25 devnull qlock(r->l);
661 1544f909 2004-12-25 devnull static int
662 1544f909 2004-12-25 devnull threadrwakeup(Rendez *r, int all, ulong pc)
665 1544f909 2004-12-25 devnull _Thread *t;
667 1544f909 2004-12-25 devnull for(i=0;; i++){
668 1544f909 2004-12-25 devnull if(i==1 && !all)
670 1544f909 2004-12-25 devnull if((t = r->waiting.head) == nil)
672 1544f909 2004-12-25 devnull delthread(&r->waiting, t);
673 1544f909 2004-12-25 devnull _threadready(t);
675 1544f909 2004-12-25 devnull return i;
679 4dbefdd4 2004-12-27 devnull * startup
682 4dbefdd4 2004-12-27 devnull static int threadargc;
683 4dbefdd4 2004-12-27 devnull static char **threadargv;
684 4dbefdd4 2004-12-27 devnull int mainstacksize;
685 d93cc14e 2006-06-12 devnull extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
686 4dbefdd4 2004-12-27 devnull static void
687 4dbefdd4 2004-12-27 devnull threadmainstart(void *v)
689 4dbefdd4 2004-12-27 devnull USED(v);
692 0b561695 2005-01-16 devnull * N.B. This call to proc() is a program's first call (indirectly) to a
693 0b561695 2005-01-16 devnull * pthreads function while executing on a non-pthreads-allocated
694 0b561695 2005-01-16 devnull * stack. If the pthreads implementation is using the stack pointer
695 0b561695 2005-01-16 devnull * to locate the per-thread data, then this call will blow up.
696 0b561695 2005-01-16 devnull * This means the pthread implementation is not suitable for
697 0b561695 2005-01-16 devnull * running under libthread. Time to write your own. Sorry.
699 d93cc14e 2006-06-12 devnull _p9usepwlibrary = 0;
700 4dbefdd4 2004-12-27 devnull threadmainproc = proc();
701 4dbefdd4 2004-12-27 devnull threadmain(threadargc, threadargv);
704 d19769ae 2008-10-26 rsc extern void (*_sysfatal)(char*, va_list);
707 4dbefdd4 2004-12-27 devnull main(int argc, char **argv)
709 4dbefdd4 2004-12-27 devnull Proc *p;
711 1d2533d0 2004-12-28 devnull argv0 = argv[0];
713 615e0f9f 2006-02-07 devnull if(getenv("NOLIBTHREADDAEMONIZE") == nil)
714 615e0f9f 2006-02-07 devnull _threadsetupdaemonize();
716 4dbefdd4 2004-12-27 devnull threadargc = argc;
717 4dbefdd4 2004-12-27 devnull threadargv = argv;
720 4dbefdd4 2004-12-27 devnull * Install locking routines into C library.
722 4dbefdd4 2004-12-27 devnull _lock = _threadlock;
723 4dbefdd4 2004-12-27 devnull _unlock = _threadunlock;
724 4dbefdd4 2004-12-27 devnull _qlock = threadqlock;
725 4dbefdd4 2004-12-27 devnull _qunlock = threadqunlock;
726 4dbefdd4 2004-12-27 devnull _rlock = threadrlock;
727 4dbefdd4 2004-12-27 devnull _runlock = threadrunlock;
728 4dbefdd4 2004-12-27 devnull _wlock = threadwlock;
729 4dbefdd4 2004-12-27 devnull _wunlock = threadwunlock;
730 4dbefdd4 2004-12-27 devnull _rsleep = threadrsleep;
731 4dbefdd4 2004-12-27 devnull _rwakeup = threadrwakeup;
732 4dbefdd4 2004-12-27 devnull _notejmpbuf = threadnotejmp;
733 df970459 2006-06-26 devnull _pin = threadpin;
734 df970459 2006-06-26 devnull _unpin = threadunpin;
735 69d1259c 2008-07-20 devnull _sysfatal = threadsysfatal;
737 4dbefdd4 2004-12-27 devnull _pthreadinit();
738 4dbefdd4 2004-12-27 devnull p = procalloc();
739 8ee6ad4d 2005-02-14 devnull p->mainproc = 1;
740 4dbefdd4 2004-12-27 devnull _threadsetproc(p);
741 4dbefdd4 2004-12-27 devnull if(mainstacksize == 0)
742 a0e8d02d 2005-01-23 devnull mainstacksize = 256*1024;
743 af6e459f 2006-04-21 devnull atnotify(threadinfo, 1);
744 4dbefdd4 2004-12-27 devnull _threadcreate(p, threadmainstart, nil, mainstacksize);
745 1fdb4a54 2005-01-23 devnull procscheduler(p);
746 d7688cce 2005-02-16 devnull sysfatal("procscheduler returned in threadmain!");
747 8ee6ad4d 2005-02-14 devnull /* does not return */
748 15cd8255 2005-01-04 devnull return 0;
752 1544f909 2004-12-25 devnull * hooray for linked lists
754 1544f909 2004-12-25 devnull static void
755 1544f909 2004-12-25 devnull addthread(_Threadlist *l, _Thread *t)
757 1544f909 2004-12-25 devnull if(l->tail){
758 1544f909 2004-12-25 devnull l->tail->next = t;
759 1544f909 2004-12-25 devnull t->prev = l->tail;
761 1544f909 2004-12-25 devnull l->head = t;
762 1544f909 2004-12-25 devnull t->prev = nil;
764 1544f909 2004-12-25 devnull l->tail = t;
765 1544f909 2004-12-25 devnull t->next = nil;
768 1544f909 2004-12-25 devnull static void
769 1544f909 2004-12-25 devnull delthread(_Threadlist *l, _Thread *t)
771 1544f909 2004-12-25 devnull if(t->prev)
772 1544f909 2004-12-25 devnull t->prev->next = t->next;
774 1544f909 2004-12-25 devnull l->head = t->next;
775 1544f909 2004-12-25 devnull if(t->next)
776 1544f909 2004-12-25 devnull t->next->prev = t->prev;
778 1544f909 2004-12-25 devnull l->tail = t->prev;
781 df970459 2006-06-26 devnull /* inefficient but rarely used */
782 df970459 2006-06-26 devnull static int
783 df970459 2006-06-26 devnull onlist(_Threadlist *l, _Thread *t)
785 df970459 2006-06-26 devnull _Thread *tt;
787 df970459 2006-06-26 devnull for(tt = l->head; tt; tt=tt->next)
788 df970459 2006-06-26 devnull if(tt == t)
789 df970459 2006-06-26 devnull return 1;
790 df970459 2006-06-26 devnull return 0;
793 1544f909 2004-12-25 devnull static void
794 1544f909 2004-12-25 devnull addthreadinproc(Proc *p, _Thread *t)
796 1544f909 2004-12-25 devnull _Threadlist *l;
798 1544f909 2004-12-25 devnull l = &p->allthreads;
799 1544f909 2004-12-25 devnull if(l->tail){
800 1544f909 2004-12-25 devnull l->tail->allnext = t;
801 1544f909 2004-12-25 devnull t->allprev = l->tail;
803 1544f909 2004-12-25 devnull l->head = t;
804 1544f909 2004-12-25 devnull t->allprev = nil;
806 1544f909 2004-12-25 devnull l->tail = t;
807 1544f909 2004-12-25 devnull t->allnext = nil;
810 1544f909 2004-12-25 devnull static void
811 1544f909 2004-12-25 devnull delthreadinproc(Proc *p, _Thread *t)
813 1544f909 2004-12-25 devnull _Threadlist *l;
815 1544f909 2004-12-25 devnull l = &p->allthreads;
816 1544f909 2004-12-25 devnull if(t->allprev)
817 1544f909 2004-12-25 devnull t->allprev->allnext = t->allnext;
819 1544f909 2004-12-25 devnull l->head = t->allnext;
820 1544f909 2004-12-25 devnull if(t->allnext)
821 1544f909 2004-12-25 devnull t->allnext->allprev = t->allprev;
823 1544f909 2004-12-25 devnull l->tail = t->allprev;
826 4dbefdd4 2004-12-27 devnull Proc *_threadprocs;
827 4dbefdd4 2004-12-27 devnull Lock _threadprocslock;
828 4dbefdd4 2004-12-27 devnull static Proc *_threadprocstail;
830 1544f909 2004-12-25 devnull static void
831 4dbefdd4 2004-12-27 devnull addproc(Proc *p)
833 4dbefdd4 2004-12-27 devnull lock(&_threadprocslock);
834 4dbefdd4 2004-12-27 devnull if(_threadprocstail){
835 4dbefdd4 2004-12-27 devnull _threadprocstail->next = p;
836 4dbefdd4 2004-12-27 devnull p->prev = _threadprocstail;
838 4dbefdd4 2004-12-27 devnull _threadprocs = p;
839 4dbefdd4 2004-12-27 devnull p->prev = nil;
841 4dbefdd4 2004-12-27 devnull _threadprocstail = p;
842 4dbefdd4 2004-12-27 devnull p->next = nil;
843 4dbefdd4 2004-12-27 devnull unlock(&_threadprocslock);
846 4dbefdd4 2004-12-27 devnull static void
847 4dbefdd4 2004-12-27 devnull delproc(Proc *p)
849 4dbefdd4 2004-12-27 devnull lock(&_threadprocslock);
850 4dbefdd4 2004-12-27 devnull if(p->prev)
851 4dbefdd4 2004-12-27 devnull p->prev->next = p->next;
853 4dbefdd4 2004-12-27 devnull _threadprocs = p->next;
854 4dbefdd4 2004-12-27 devnull if(p->next)
855 4dbefdd4 2004-12-27 devnull p->next->prev = p->prev;
857 4dbefdd4 2004-12-27 devnull _threadprocstail = p->prev;
858 4dbefdd4 2004-12-27 devnull unlock(&_threadprocslock);
862 15cd8255 2005-01-04 devnull * notify - for now just use the usual mechanisms
865 15cd8255 2005-01-04 devnull threadnotify(int (*f)(void*, char*), int in)
867 15cd8255 2005-01-04 devnull atnotify(f, in);
870 6dde87f8 2005-09-26 devnull static int
871 6dde87f8 2005-09-26 devnull onrunqueue(Proc *p, _Thread *t)
873 6dde87f8 2005-09-26 devnull _Thread *tt;
875 6dde87f8 2005-09-26 devnull for(tt=p->runqueue.head; tt; tt=tt->next)
876 6dde87f8 2005-09-26 devnull if(tt == t)
877 6dde87f8 2005-09-26 devnull return 1;
878 6dde87f8 2005-09-26 devnull return 0;
882 6dde87f8 2005-09-26 devnull * print state - called from SIGINFO
884 6dde87f8 2005-09-26 devnull static int
885 6dde87f8 2005-09-26 devnull threadinfo(void *v, char *s)
887 6dde87f8 2005-09-26 devnull Proc *p;
888 6dde87f8 2005-09-26 devnull _Thread *t;
890 6dde87f8 2005-09-26 devnull if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
891 6dde87f8 2005-09-26 devnull return 0;
893 6dde87f8 2005-09-26 devnull for(p=_threadprocs; p; p=p->next){
894 6dde87f8 2005-09-26 devnull fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
895 6dde87f8 2005-09-26 devnull p->sysproc ? " (sysproc)": "");
896 6dde87f8 2005-09-26 devnull for(t=p->allthreads.head; t; t=t->allnext){
897 6dde87f8 2005-09-26 devnull fprint(2, "\tthread %d %s: %s %s\n",
899 6dde87f8 2005-09-26 devnull t == p->thread ? "Running" :
900 6dde87f8 2005-09-26 devnull onrunqueue(p, t) ? "Ready" : "Sleeping",
901 6dde87f8 2005-09-26 devnull t->state, t->name);
904 6dde87f8 2005-09-26 devnull return 1;