2004-12-25 21:56:33 +00:00
|
|
|
#include "threadimpl.h"
|
|
|
|
|
|
2020-12-30 00:15:37 -05:00
|
|
|
int _threaddebuglevel = 0;
|
2004-12-25 21:56:33 +00:00
|
|
|
|
|
|
|
|
static uint threadnproc;
|
|
|
|
|
static uint threadnsysproc;
|
|
|
|
|
static Lock threadnproclock;
|
|
|
|
|
static Ref threadidref;
|
2004-12-27 16:52:26 +00:00
|
|
|
static Proc *threadmainproc;
|
2020-05-17 08:24:54 -04:00
|
|
|
static int pthreadperthread;
|
2004-12-25 21:56:33 +00:00
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
static void addproc(Proc*);
|
|
|
|
|
static void delproc(Proc*);
|
2004-12-25 21:56:33 +00:00
|
|
|
static void addthread(_Threadlist*, _Thread*);
|
|
|
|
|
static void delthread(_Threadlist*, _Thread*);
|
2006-06-26 05:47:59 +00:00
|
|
|
static int onlist(_Threadlist*, _Thread*);
|
2004-12-25 21:56:33 +00:00
|
|
|
static void addthreadinproc(Proc*, _Thread*);
|
|
|
|
|
static void delthreadinproc(Proc*, _Thread*);
|
|
|
|
|
static void contextswitch(Context *from, Context *to);
|
2020-01-08 22:47:51 -05:00
|
|
|
static void procmain(Proc*);
|
2005-01-23 22:57:53 +00:00
|
|
|
static void procscheduler(Proc*);
|
2005-09-26 12:05:26 +00:00
|
|
|
static int threadinfo(void*, char*);
|
2020-12-30 00:15:37 -05:00
|
|
|
static void pthreadscheduler(Proc *p);
|
|
|
|
|
static void pthreadsleepschedlocked(Proc *p, _Thread *t);
|
|
|
|
|
static void pthreadwakeupschedlocked(Proc *p, _Thread *self, _Thread *t);
|
|
|
|
|
static _Thread* procnext(Proc*, _Thread*);
|
2004-12-25 21:56:33 +00:00
|
|
|
|
2004-12-28 01:35:38 +00:00
|
|
|
static void
|
2020-12-30 00:15:37 -05:00
|
|
|
_threaddebug(_Thread *t, char *fmt, ...)
|
2004-12-28 01:35:38 +00:00
|
|
|
{
|
|
|
|
|
va_list arg;
|
|
|
|
|
char buf[128];
|
2005-01-06 23:07:19 +00:00
|
|
|
char *p;
|
|
|
|
|
static int fd = -1;
|
2004-12-28 01:35:38 +00:00
|
|
|
|
2005-09-26 11:37:49 +00:00
|
|
|
if(_threaddebuglevel == 0)
|
|
|
|
|
return;
|
2005-01-11 17:43:53 +00:00
|
|
|
|
2005-01-06 23:07:19 +00:00
|
|
|
if(fd < 0){
|
|
|
|
|
p = strrchr(argv0, '/');
|
|
|
|
|
if(p)
|
|
|
|
|
p++;
|
|
|
|
|
else
|
|
|
|
|
p = argv0;
|
|
|
|
|
snprint(buf, sizeof buf, "/tmp/%s.tlog", p);
|
|
|
|
|
if((fd = create(buf, OWRITE, 0666)) < 0)
|
|
|
|
|
fd = open("/dev/null", OWRITE);
|
2006-06-12 17:20:42 +00:00
|
|
|
if(fd >= 0 && fd != 2){
|
|
|
|
|
dup(fd, 2);
|
|
|
|
|
close(fd);
|
|
|
|
|
fd = 2;
|
|
|
|
|
}
|
2005-01-06 23:07:19 +00:00
|
|
|
}
|
2004-12-28 01:35:38 +00:00
|
|
|
|
|
|
|
|
va_start(arg, fmt);
|
|
|
|
|
vsnprint(buf, sizeof buf, fmt, arg);
|
|
|
|
|
va_end(arg);
|
2020-12-30 00:15:37 -05:00
|
|
|
if(t == nil)
|
|
|
|
|
t = proc()->thread;
|
2004-12-28 01:35:38 +00:00
|
|
|
if(t)
|
2020-05-17 22:31:38 -04:00
|
|
|
fprint(fd, "%p %d.%d: %s\n", proc(), getpid(), t->id, buf);
|
2004-12-28 01:35:38 +00:00
|
|
|
else
|
2020-05-17 22:31:38 -04:00
|
|
|
fprint(fd, "%p %d._: %s\n", proc(), getpid(), buf);
|
2004-12-28 01:35:38 +00:00
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
static _Thread*
|
|
|
|
|
getthreadnow(void)
|
|
|
|
|
{
|
|
|
|
|
return proc()->thread;
|
|
|
|
|
}
|
|
|
|
|
_Thread *(*threadnow)(void) = getthreadnow;
|
|
|
|
|
|
|
|
|
|
static Proc*
|
|
|
|
|
procalloc(void)
|
|
|
|
|
{
|
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
|
|
p = malloc(sizeof *p);
|
2004-12-27 03:49:03 +00:00
|
|
|
if(p == nil)
|
|
|
|
|
sysfatal("procalloc malloc: %r");
|
2004-12-25 21:56:33 +00:00
|
|
|
memset(p, 0, sizeof *p);
|
2004-12-27 16:52:26 +00:00
|
|
|
addproc(p);
|
2004-12-25 21:56:33 +00:00
|
|
|
lock(&threadnproclock);
|
|
|
|
|
threadnproc++;
|
|
|
|
|
unlock(&threadnproclock);
|
|
|
|
|
return p;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2005-01-18 05:57:35 +00:00
|
|
|
threadstart(uint y, uint x)
|
2004-12-25 21:56:33 +00:00
|
|
|
{
|
|
|
|
|
_Thread *t;
|
2005-01-18 05:57:35 +00:00
|
|
|
ulong z;
|
|
|
|
|
|
2011-10-13 23:57:54 -04:00
|
|
|
//print("threadstart\n");
|
2008-06-01 08:41:51 -04:00
|
|
|
z = (ulong)x << 16; /* hide undefined 32-bit shift from 32-bit compilers */
|
2005-01-18 05:57:35 +00:00
|
|
|
z <<= 16;
|
|
|
|
|
z |= y;
|
|
|
|
|
t = (_Thread*)z;
|
2004-12-25 21:56:33 +00:00
|
|
|
|
2011-08-02 14:28:04 -04:00
|
|
|
//print("threadstart sp=%p arg=%p startfn=%p t=%p\n", &t, t, t->startfn, t->startarg);
|
2004-12-25 21:56:33 +00:00
|
|
|
t->startfn(t->startarg);
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("threadexits %p\n", v); */
|
2004-12-27 16:52:26 +00:00
|
|
|
threadexits(nil);
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("not reacehd\n"); */
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static _Thread*
|
|
|
|
|
threadalloc(void (*fn)(void*), void *arg, uint stack)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
sigset_t zero;
|
2005-01-18 05:57:35 +00:00
|
|
|
uint x, y;
|
|
|
|
|
ulong z;
|
2004-12-25 21:56:33 +00:00
|
|
|
|
|
|
|
|
/* allocate the task and stack together */
|
2020-01-14 12:40:09 -05:00
|
|
|
t = malloc(sizeof *t);
|
2004-12-27 03:49:03 +00:00
|
|
|
if(t == nil)
|
|
|
|
|
sysfatal("threadalloc malloc: %r");
|
2004-12-25 21:56:33 +00:00
|
|
|
memset(t, 0, sizeof *t);
|
|
|
|
|
t->id = incref(&threadidref);
|
2011-08-02 14:28:04 -04:00
|
|
|
//print("fn=%p arg=%p\n", fn, arg);
|
2004-12-25 21:56:33 +00:00
|
|
|
t->startfn = fn;
|
|
|
|
|
t->startarg = arg;
|
2011-08-02 14:28:04 -04:00
|
|
|
//print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
|
2004-12-25 21:56:33 +00:00
|
|
|
|
|
|
|
|
/* do a reasonable initialization */
|
2020-01-08 22:47:51 -05:00
|
|
|
if(stack == 0)
|
|
|
|
|
return t;
|
2020-01-14 12:40:09 -05:00
|
|
|
t->stk = _threadstkalloc(stack);
|
|
|
|
|
if(t->stk == nil)
|
|
|
|
|
sysfatal("threadalloc malloc stack: %r");
|
2020-01-08 22:47:51 -05:00
|
|
|
t->stksize = stack;
|
2004-12-25 21:56:33 +00:00
|
|
|
memset(&t->context.uc, 0, sizeof t->context.uc);
|
|
|
|
|
sigemptyset(&zero);
|
|
|
|
|
sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
|
2011-08-02 14:28:04 -04:00
|
|
|
//print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
|
2004-12-25 21:56:33 +00:00
|
|
|
|
2004-12-28 04:20:39 +00:00
|
|
|
/* must initialize with current context */
|
2005-01-11 21:28:00 +00:00
|
|
|
if(getcontext(&t->context.uc) < 0)
|
|
|
|
|
sysfatal("threadalloc getcontext: %r");
|
2011-08-02 14:28:04 -04:00
|
|
|
//print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
|
2004-12-25 21:56:33 +00:00
|
|
|
|
2020-01-24 13:09:55 -05:00
|
|
|
/*
|
|
|
|
|
* Call makecontext to do the real work.
|
|
|
|
|
* To avoid various mistakes on other system software,
|
|
|
|
|
* debuggers, and so on, don't get too close to both
|
|
|
|
|
* ends of the stack. Just staying away is much easier
|
|
|
|
|
* than debugging everything (outside our control)
|
|
|
|
|
* that has off-by-one errors.
|
|
|
|
|
*/
|
|
|
|
|
t->context.uc.uc_stack.ss_sp = (void*)(t->stk+64);
|
|
|
|
|
t->context.uc.uc_stack.ss_size = t->stksize-2*64;
|
2005-07-27 13:06:50 +00:00
|
|
|
#if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */
|
2005-01-11 21:28:00 +00:00
|
|
|
/* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
|
2020-01-10 14:44:21 +00:00
|
|
|
t->context.uc.uc_stack.ss_sp =
|
2005-01-14 18:11:21 +00:00
|
|
|
(char*)t->context.uc.uc_stack.ss_sp
|
|
|
|
|
+t->context.uc.uc_stack.ss_size;
|
2005-01-11 21:28:00 +00:00
|
|
|
#endif
|
2005-01-18 05:57:35 +00:00
|
|
|
/*
|
|
|
|
|
* All this magic is because you have to pass makecontext a
|
|
|
|
|
* function that takes some number of word-sized variables,
|
|
|
|
|
* and on 64-bit machines pointers are bigger than words.
|
|
|
|
|
*/
|
2011-08-02 14:28:04 -04:00
|
|
|
//print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn);
|
2005-01-18 05:57:35 +00:00
|
|
|
z = (ulong)t;
|
|
|
|
|
y = z;
|
|
|
|
|
z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
|
|
|
|
|
x = z>>16;
|
2005-05-07 22:42:14 +00:00
|
|
|
makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x);
|
2004-12-25 21:56:33 +00:00
|
|
|
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_Thread*
|
|
|
|
|
_threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
2009-08-17 17:29:44 -07:00
|
|
|
/* defend against bad C libraries */
|
|
|
|
|
if(stack < (256<<10))
|
|
|
|
|
stack = 256<<10;
|
2020-01-10 14:44:21 +00:00
|
|
|
|
2020-05-17 08:24:54 -04:00
|
|
|
if(p->nthread == 0 || pthreadperthread)
|
2020-01-08 22:47:51 -05:00
|
|
|
stack = 0; // not using it
|
2004-12-25 21:56:33 +00:00
|
|
|
t = threadalloc(fn, arg, stack);
|
|
|
|
|
t->proc = p;
|
2020-12-30 00:15:37 -05:00
|
|
|
if(pthreadperthread) {
|
|
|
|
|
if(p->nthread != 0)
|
|
|
|
|
_threadpthreadstart(p, t);
|
|
|
|
|
else
|
|
|
|
|
t->mainthread = 1;
|
|
|
|
|
} else {
|
|
|
|
|
if(p->nthread == 0)
|
|
|
|
|
p->thread0 = t;
|
|
|
|
|
}
|
2004-12-25 21:56:33 +00:00
|
|
|
p->nthread++;
|
2020-05-17 08:24:54 -04:00
|
|
|
addthreadinproc(p, t);
|
2004-12-25 21:56:33 +00:00
|
|
|
_threadready(t);
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
threadcreate(void (*fn)(void*), void *arg, uint stack)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
t = _threadcreate(proc(), fn, arg, stack);
|
2020-12-30 00:15:37 -05:00
|
|
|
_threaddebug(nil, "threadcreate %d", t->id);
|
2004-12-25 21:56:33 +00:00
|
|
|
return t->id;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
proccreate(void (*fn)(void*), void *arg, uint stack)
|
|
|
|
|
{
|
2005-01-18 20:53:12 +00:00
|
|
|
int id;
|
2004-12-25 21:56:33 +00:00
|
|
|
_Thread *t;
|
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
|
|
p = procalloc();
|
|
|
|
|
t = _threadcreate(p, fn, arg, stack);
|
2005-01-18 20:53:12 +00:00
|
|
|
id = t->id; /* t might be freed after _procstart */
|
2020-12-30 00:15:37 -05:00
|
|
|
_threaddebug(t, "proccreate %p", p);
|
2020-01-08 22:47:51 -05:00
|
|
|
_procstart(p, procmain);
|
2005-01-18 20:53:12 +00:00
|
|
|
return id;
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_threadswitch(void)
|
|
|
|
|
{
|
|
|
|
|
Proc *p;
|
|
|
|
|
|
2005-01-13 04:49:19 +00:00
|
|
|
needstack(0);
|
2004-12-25 21:56:33 +00:00
|
|
|
p = proc();
|
2020-05-17 08:24:54 -04:00
|
|
|
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("threadswtch %p\n", p); */
|
2020-05-17 08:24:54 -04:00
|
|
|
|
2020-12-30 00:15:37 -05:00
|
|
|
if(pthreadperthread)
|
|
|
|
|
pthreadscheduler(p);
|
|
|
|
|
else if(p->thread == p->thread0)
|
2020-01-08 22:47:51 -05:00
|
|
|
procscheduler(p);
|
|
|
|
|
else
|
|
|
|
|
contextswitch(&p->thread->context, &p->schedcontext);
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
_threadready(_Thread *t)
|
|
|
|
|
{
|
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
|
|
p = t->proc;
|
|
|
|
|
lock(&p->lock);
|
2005-01-06 23:43:42 +00:00
|
|
|
p->runrend.l = &p->lock;
|
2004-12-25 21:56:33 +00:00
|
|
|
addthread(&p->runqueue, t);
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid); */
|
2004-12-27 19:11:33 +00:00
|
|
|
if(p != proc())
|
2005-01-06 23:43:42 +00:00
|
|
|
_procwakeupandunlock(&p->runrend);
|
|
|
|
|
else
|
|
|
|
|
unlock(&p->lock);
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
2006-02-07 17:02:05 +00:00
|
|
|
int
|
|
|
|
|
threadidle(void)
|
|
|
|
|
{
|
|
|
|
|
int n;
|
|
|
|
|
Proc *p;
|
2020-01-10 14:44:21 +00:00
|
|
|
|
2006-02-07 17:02:05 +00:00
|
|
|
p = proc();
|
|
|
|
|
n = p->nswitch;
|
|
|
|
|
lock(&p->lock);
|
|
|
|
|
p->runrend.l = &p->lock;
|
|
|
|
|
addthread(&p->idlequeue, p->thread);
|
|
|
|
|
unlock(&p->lock);
|
|
|
|
|
_threadswitch();
|
|
|
|
|
return p->nswitch - n;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-27 19:11:33 +00:00
|
|
|
int
|
2004-12-25 21:56:33 +00:00
|
|
|
threadyield(void)
|
|
|
|
|
{
|
2004-12-27 19:11:33 +00:00
|
|
|
int n;
|
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
|
|
p = proc();
|
|
|
|
|
n = p->nswitch;
|
|
|
|
|
_threadready(p->thread);
|
2004-12-25 21:56:33 +00:00
|
|
|
_threadswitch();
|
2004-12-27 19:11:33 +00:00
|
|
|
return p->nswitch - n;
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
threadexits(char *msg)
|
|
|
|
|
{
|
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
|
|
p = proc();
|
2004-12-27 16:52:26 +00:00
|
|
|
if(msg == nil)
|
|
|
|
|
msg = "";
|
2004-12-25 21:56:33 +00:00
|
|
|
utfecpy(p->msg, p->msg+sizeof p->msg, msg);
|
2004-12-27 16:52:26 +00:00
|
|
|
proc()->thread->exiting = 1;
|
|
|
|
|
_threadswitch();
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
2006-06-26 05:47:59 +00:00
|
|
|
void
|
|
|
|
|
threadpin(void)
|
|
|
|
|
{
|
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
|
|
p = proc();
|
|
|
|
|
if(p->pinthread){
|
|
|
|
|
fprint(2, "already pinning a thread - %p %p\n", p->pinthread, p->thread);
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
p->pinthread = p->thread;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
threadunpin(void)
|
|
|
|
|
{
|
|
|
|
|
Proc *p;
|
|
|
|
|
|
|
|
|
|
p = proc();
|
|
|
|
|
if(p->pinthread != p->thread){
|
|
|
|
|
fprint(2, "wrong pinthread - %p %p\n", p->pinthread, p->thread);
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
p->pinthread = nil;
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-20 03:11:05 -04:00
|
|
|
void
|
2008-10-26 17:01:59 -04:00
|
|
|
threadsysfatal(char *fmt, va_list arg)
|
2008-07-20 03:11:05 -04:00
|
|
|
{
|
|
|
|
|
char buf[256];
|
|
|
|
|
|
|
|
|
|
vseprint(buf, buf+sizeof(buf), fmt, arg);
|
|
|
|
|
__fixargv0();
|
|
|
|
|
fprint(2, "%s: %s\n", argv0 ? argv0 : "<prog>", buf);
|
|
|
|
|
threadexitsall(buf);
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
static void
|
|
|
|
|
contextswitch(Context *from, Context *to)
|
|
|
|
|
{
|
|
|
|
|
if(swapcontext(&from->uc, &to->uc) < 0){
|
|
|
|
|
fprint(2, "swapcontext failed: %r\n");
|
|
|
|
|
assert(0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-08 22:47:51 -05:00
|
|
|
static void
|
|
|
|
|
procmain(Proc *p)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
_threadsetproc(p);
|
|
|
|
|
|
|
|
|
|
/* take out first thread to run on system stack */
|
|
|
|
|
t = p->runqueue.head;
|
|
|
|
|
delthread(&p->runqueue, t);
|
|
|
|
|
memset(&t->context.uc, 0, sizeof t->context.uc);
|
|
|
|
|
|
|
|
|
|
/* run it */
|
|
|
|
|
p->thread = t;
|
|
|
|
|
t->startfn(t->startarg);
|
|
|
|
|
if(p->nthread != 0)
|
|
|
|
|
threadexits(nil);
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-17 08:24:54 -04:00
|
|
|
void
|
|
|
|
|
_threadpthreadmain(Proc *p, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
_threadsetproc(p);
|
2020-12-30 00:15:37 -05:00
|
|
|
lock(&p->lock);
|
|
|
|
|
pthreadsleepschedlocked(p, t);
|
|
|
|
|
unlock(&p->lock);
|
|
|
|
|
_threaddebug(nil, "startfn");
|
2020-05-17 08:24:54 -04:00
|
|
|
t->startfn(t->startarg);
|
|
|
|
|
threadexits(nil);
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
static void
|
2005-01-23 22:57:53 +00:00
|
|
|
procscheduler(Proc *p)
|
2004-12-25 21:56:33 +00:00
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
2020-12-30 00:15:37 -05:00
|
|
|
_threaddebug(nil, "scheduler enter");
|
2011-10-13 23:57:54 -04:00
|
|
|
//print("s %p\n", p);
|
2020-12-30 00:15:37 -05:00
|
|
|
for(;;) {
|
|
|
|
|
/* Finish running current thread. */
|
|
|
|
|
lock(&p->lock);
|
|
|
|
|
t = p->thread;
|
|
|
|
|
p->thread = nil;
|
|
|
|
|
if(t->exiting){
|
|
|
|
|
delthreadinproc(p, t);
|
|
|
|
|
p->nthread--;
|
|
|
|
|
/*print("nthread %d\n", p->nthread); */
|
|
|
|
|
_threadstkfree(t->stk, t->stksize);
|
|
|
|
|
/*
|
|
|
|
|
* Cannot free p->thread0 yet: it is used for the
|
|
|
|
|
* context switches back to the scheduler.
|
|
|
|
|
* Instead, we will free it at the end of this function.
|
|
|
|
|
* But all the other threads can be freed now.
|
|
|
|
|
*/
|
|
|
|
|
if(t != p->thread0)
|
|
|
|
|
free(t);
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
2020-12-30 00:15:37 -05:00
|
|
|
|
|
|
|
|
/* Pick next thread. */
|
|
|
|
|
t = procnext(p, nil);
|
|
|
|
|
if(t == nil)
|
|
|
|
|
break;
|
|
|
|
|
_threaddebug(nil, "run %d (%s)", t->id, t->name);
|
|
|
|
|
//print("run %p %p %p %p\n", t, *(uintptr*)(t->context.uc.mc.sp), t->context.uc.mc.di, t->context.uc.mc.si);
|
2004-12-25 21:56:33 +00:00
|
|
|
unlock(&p->lock);
|
2020-12-30 00:15:37 -05:00
|
|
|
|
|
|
|
|
/* Switch to next thread. */
|
2020-05-17 08:24:54 -04:00
|
|
|
if(t == p->thread0)
|
2020-01-08 22:47:51 -05:00
|
|
|
return;
|
2020-12-30 00:15:37 -05:00
|
|
|
contextswitch(&p->schedcontext, &t->context);
|
|
|
|
|
|
|
|
|
|
_threaddebug(nil, "back in scheduler");
|
|
|
|
|
/*print("back in scheduler\n"); */
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-30 00:15:37 -05:00
|
|
|
/* No more threads in proc. Clean up. */
|
|
|
|
|
_threaddebug(nil, "scheduler exit");
|
2005-02-14 18:58:56 +00:00
|
|
|
if(p->mainproc){
|
|
|
|
|
/*
|
|
|
|
|
* Stupid bug - on Linux 2.6 and maybe elsewhere,
|
|
|
|
|
* if the main thread exits then the others keep running
|
|
|
|
|
* but the process shows up as a zombie in ps and is not
|
|
|
|
|
* attachable with ptrace. We'll just sit around pretending
|
|
|
|
|
* to be a system proc instead of exiting.
|
|
|
|
|
*/
|
|
|
|
|
_threaddaemonize();
|
|
|
|
|
lock(&threadnproclock);
|
|
|
|
|
if(++threadnsysproc == threadnproc)
|
|
|
|
|
threadexitsall(p->msg);
|
|
|
|
|
p->sysproc = 1;
|
|
|
|
|
unlock(&threadnproclock);
|
|
|
|
|
for(;;)
|
|
|
|
|
sleep(1000);
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
delproc(p);
|
2004-12-25 21:56:33 +00:00
|
|
|
lock(&threadnproclock);
|
|
|
|
|
if(p->sysproc)
|
|
|
|
|
--threadnsysproc;
|
|
|
|
|
if(--threadnproc == threadnsysproc)
|
2004-12-27 16:52:26 +00:00
|
|
|
threadexitsall(p->msg);
|
2004-12-25 21:56:33 +00:00
|
|
|
unlock(&threadnproclock);
|
|
|
|
|
unlock(&p->lock);
|
2006-06-12 17:20:42 +00:00
|
|
|
_threadsetproc(nil);
|
2020-12-15 00:05:17 -05:00
|
|
|
free(p->thread0);
|
2004-12-25 21:56:33 +00:00
|
|
|
free(p);
|
2020-01-13 23:26:26 -05:00
|
|
|
_threadpexit();
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
2020-12-30 00:15:37 -05:00
|
|
|
static void
|
|
|
|
|
pthreadsleepschedlocked(Proc *p, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
_threaddebug(t, "pthreadsleepsched %p %d", p, t->id);;
|
|
|
|
|
t->schedrend.l = &p->lock;
|
|
|
|
|
while(p->schedthread != t)
|
|
|
|
|
_procsleep(&t->schedrend);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
pthreadwakeupschedlocked(Proc *p, _Thread *self, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
_threaddebug(self, "pthreadwakeupschedlocked %p %d", p, t->id);;
|
|
|
|
|
t->schedrend.l = &p->schedlock;
|
|
|
|
|
p->schedthread = t;
|
|
|
|
|
_procwakeup(&t->schedrend);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
pthreadscheduler(Proc *p)
|
|
|
|
|
{
|
|
|
|
|
_Thread *self, *t;
|
|
|
|
|
|
|
|
|
|
_threaddebug(nil, "scheduler");
|
|
|
|
|
lock(&p->lock);
|
|
|
|
|
self = p->thread;
|
|
|
|
|
p->thread = nil;
|
|
|
|
|
_threaddebug(self, "pausing");
|
|
|
|
|
|
|
|
|
|
if(self->exiting) {
|
|
|
|
|
_threaddebug(self, "exiting");
|
|
|
|
|
delthreadinproc(p, self);
|
|
|
|
|
p->nthread--;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
t = procnext(p, self);
|
|
|
|
|
if(t != nil) {
|
|
|
|
|
pthreadwakeupschedlocked(p, self, t);
|
|
|
|
|
if(!self->exiting) {
|
|
|
|
|
pthreadsleepschedlocked(p, self);
|
|
|
|
|
_threaddebug(nil, "resume %d", self->id);
|
|
|
|
|
unlock(&p->lock);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(t == nil) {
|
|
|
|
|
/* Tear down proc bookkeeping. Wait to free p. */
|
|
|
|
|
delproc(p);
|
|
|
|
|
lock(&threadnproclock);
|
|
|
|
|
if(p->sysproc)
|
|
|
|
|
--threadnsysproc;
|
|
|
|
|
if(--threadnproc == threadnsysproc)
|
|
|
|
|
threadexitsall(p->msg);
|
|
|
|
|
unlock(&threadnproclock);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Tear down pthread. */
|
|
|
|
|
if(self->mainthread && p->mainproc) {
|
|
|
|
|
_threaddaemonize();
|
|
|
|
|
_threaddebug(self, "sleeper");
|
|
|
|
|
unlock(&p->lock);
|
|
|
|
|
/*
|
|
|
|
|
* Avoid bugs with main pthread exiting.
|
|
|
|
|
* When all procs are gone, threadexitsall above will happen.
|
|
|
|
|
*/
|
|
|
|
|
for(;;)
|
|
|
|
|
sleep(60*60*1000);
|
|
|
|
|
}
|
|
|
|
|
_threadsetproc(nil);
|
|
|
|
|
free(self);
|
|
|
|
|
unlock(&p->lock);
|
|
|
|
|
if(t == nil)
|
|
|
|
|
free(p);
|
|
|
|
|
_threadpexit();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static _Thread*
|
|
|
|
|
procnext(Proc *p, _Thread *self)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
if((t = p->pinthread) != nil){
|
|
|
|
|
while(!onlist(&p->runqueue, t)){
|
|
|
|
|
p->runrend.l = &p->lock;
|
|
|
|
|
_threaddebug(self, "scheduler sleep (pin)");
|
|
|
|
|
_procsleep(&p->runrend);
|
|
|
|
|
_threaddebug(self, "scheduler wake (pin)");
|
|
|
|
|
}
|
|
|
|
|
} else
|
|
|
|
|
while((t = p->runqueue.head) == nil){
|
|
|
|
|
if(p->nthread == 0)
|
|
|
|
|
return nil;
|
|
|
|
|
if((t = p->idlequeue.head) != nil){
|
|
|
|
|
/*
|
|
|
|
|
* Run all the idling threads once.
|
|
|
|
|
*/
|
|
|
|
|
while((t = p->idlequeue.head) != nil){
|
|
|
|
|
delthread(&p->idlequeue, t);
|
|
|
|
|
addthread(&p->runqueue, t);
|
|
|
|
|
}
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
p->runrend.l = &p->lock;
|
|
|
|
|
_threaddebug(self, "scheduler sleep");
|
|
|
|
|
_procsleep(&p->runrend);
|
|
|
|
|
_threaddebug(self, "scheduler wake");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(p->pinthread && p->pinthread != t)
|
|
|
|
|
fprint(2, "p->pinthread %p t %p\n", p->pinthread, t);
|
|
|
|
|
assert(p->pinthread == nil || p->pinthread == t);
|
|
|
|
|
delthread(&p->runqueue, t);
|
|
|
|
|
|
|
|
|
|
p->thread = t;
|
|
|
|
|
p->nswitch++;
|
|
|
|
|
return t;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
void
|
|
|
|
|
_threadsetsysproc(void)
|
|
|
|
|
{
|
|
|
|
|
lock(&threadnproclock);
|
|
|
|
|
if(++threadnsysproc == threadnproc)
|
2005-02-14 18:58:56 +00:00
|
|
|
threadexitsall(nil);
|
2004-12-25 21:56:33 +00:00
|
|
|
unlock(&threadnproclock);
|
|
|
|
|
proc()->sysproc = 1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
void**
|
|
|
|
|
procdata(void)
|
|
|
|
|
{
|
|
|
|
|
return &proc()->udata;
|
|
|
|
|
}
|
|
|
|
|
|
2005-03-18 18:56:17 +00:00
|
|
|
void**
|
|
|
|
|
threaddata(void)
|
|
|
|
|
{
|
|
|
|
|
return &proc()->thread->udata;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
extern Jmp *(*_notejmpbuf)(void);
|
|
|
|
|
static Jmp*
|
|
|
|
|
threadnotejmp(void)
|
|
|
|
|
{
|
|
|
|
|
return &proc()->sigjmp;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
/*
|
|
|
|
|
* debugging
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
threadsetname(char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list arg;
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
t = proc()->thread;
|
|
|
|
|
va_start(arg, fmt);
|
|
|
|
|
vsnprint(t->name, sizeof t->name, fmt, arg);
|
|
|
|
|
va_end(arg);
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-18 20:17:12 +00:00
|
|
|
char*
|
|
|
|
|
threadgetname(void)
|
|
|
|
|
{
|
|
|
|
|
return proc()->thread->name;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
void
|
|
|
|
|
threadsetstate(char *fmt, ...)
|
|
|
|
|
{
|
|
|
|
|
va_list arg;
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
t = proc()->thread;
|
|
|
|
|
va_start(arg, fmt);
|
|
|
|
|
vsnprint(t->state, sizeof t->name, fmt, arg);
|
|
|
|
|
va_end(arg);
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-05 17:50:09 +00:00
|
|
|
int
|
|
|
|
|
threadid(void)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
2020-01-10 14:44:21 +00:00
|
|
|
|
2006-02-05 17:50:09 +00:00
|
|
|
t = proc()->thread;
|
|
|
|
|
return t->id;
|
|
|
|
|
}
|
|
|
|
|
|
2005-01-13 04:49:19 +00:00
|
|
|
void
|
|
|
|
|
needstack(int n)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
t = proc()->thread;
|
2020-01-14 12:40:09 -05:00
|
|
|
if(t->stk == nil)
|
|
|
|
|
return;
|
2005-01-13 04:49:19 +00:00
|
|
|
|
|
|
|
|
if((char*)&t <= (char*)t->stk
|
|
|
|
|
|| (char*)&t - (char*)t->stk < 256+n){
|
2005-01-23 22:57:53 +00:00
|
|
|
fprint(2, "thread stack overflow: &t=%p tstk=%p n=%d\n", &t, t->stk, 256+n);
|
2005-01-13 04:49:19 +00:00
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2008-07-09 11:41:14 -04:00
|
|
|
static int
|
|
|
|
|
singlethreaded(void)
|
|
|
|
|
{
|
|
|
|
|
return threadnproc == 1 && _threadprocs->nthread == 1;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
/*
|
|
|
|
|
* locking
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
threadqlock(QLock *l, int block, ulong pc)
|
|
|
|
|
{
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("threadqlock %p\n", l); */
|
2004-12-25 21:56:33 +00:00
|
|
|
lock(&l->l);
|
|
|
|
|
if(l->owner == nil){
|
|
|
|
|
l->owner = (*threadnow)();
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("qlock %p @%#x by %p\n", l, pc, l->owner); */
|
2020-05-17 22:31:38 -04:00
|
|
|
if(l->owner == nil) {
|
|
|
|
|
fprint(2, "%s: qlock uncontended owner=nil oops\n", argv0);
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2004-12-25 21:56:33 +00:00
|
|
|
unlock(&l->l);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(!block){
|
|
|
|
|
unlock(&l->l);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-07-09 11:41:14 -04:00
|
|
|
|
|
|
|
|
if(singlethreaded()){
|
|
|
|
|
fprint(2, "qlock deadlock\n");
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)()); */
|
2004-12-25 21:56:33 +00:00
|
|
|
addthread(&l->waiting, (*threadnow)());
|
|
|
|
|
unlock(&l->l);
|
|
|
|
|
|
|
|
|
|
_threadswitch();
|
|
|
|
|
|
|
|
|
|
if(l->owner != (*threadnow)()){
|
2005-01-04 22:22:18 +00:00
|
|
|
fprint(2, "%s: qlock pc=0x%lux owner=%p self=%p oops\n",
|
|
|
|
|
argv0, pc, l->owner, (*threadnow)());
|
2004-12-25 21:56:33 +00:00
|
|
|
abort();
|
|
|
|
|
}
|
2020-05-17 22:31:38 -04:00
|
|
|
if(l->owner == nil) {
|
|
|
|
|
fprint(2, "%s: qlock threadswitch owner=nil oops\n", argv0);
|
|
|
|
|
abort();
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)()); */
|
2004-12-25 21:56:33 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
threadqunlock(QLock *l, ulong pc)
|
|
|
|
|
{
|
2005-02-15 18:08:28 +00:00
|
|
|
_Thread *ready;
|
2020-01-10 14:44:21 +00:00
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
lock(&l->l);
|
2006-04-01 19:24:03 +00:00
|
|
|
/*print("qlock unlock %p @%#x by %p (owner %p)\n", l, pc, (*threadnow)(), l->owner); */
|
2005-01-07 20:52:07 +00:00
|
|
|
if(l->owner == 0){
|
2005-01-04 22:22:18 +00:00
|
|
|
fprint(2, "%s: qunlock pc=0x%lux owner=%p self=%p oops\n",
|
|
|
|
|
argv0, pc, l->owner, (*threadnow)());
|
2005-01-07 20:52:07 +00:00
|
|
|
abort();
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
2005-02-15 18:08:28 +00:00
|
|
|
if((l->owner = ready = l->waiting.head) != nil)
|
2004-12-25 21:56:33 +00:00
|
|
|
delthread(&l->waiting, l->owner);
|
2005-02-15 18:08:28 +00:00
|
|
|
/*
|
|
|
|
|
* N.B. Cannot call _threadready() before unlocking l->l,
|
|
|
|
|
* because the thread we are readying might:
|
|
|
|
|
* - be in another proc
|
|
|
|
|
* - start running immediately
|
|
|
|
|
* - and free l before we get a chance to run again
|
|
|
|
|
*/
|
2004-12-25 21:56:33 +00:00
|
|
|
unlock(&l->l);
|
2005-02-15 18:08:28 +00:00
|
|
|
if(ready)
|
|
|
|
|
_threadready(l->owner);
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
threadrlock(RWLock *l, int block, ulong pc)
|
|
|
|
|
{
|
|
|
|
|
USED(pc);
|
|
|
|
|
|
|
|
|
|
lock(&l->l);
|
|
|
|
|
if(l->writer == nil && l->wwaiting.head == nil){
|
|
|
|
|
l->readers++;
|
|
|
|
|
unlock(&l->l);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(!block){
|
|
|
|
|
unlock(&l->l);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-07-09 11:41:14 -04:00
|
|
|
if(singlethreaded()){
|
|
|
|
|
fprint(2, "rlock deadlock\n");
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2004-12-25 21:56:33 +00:00
|
|
|
addthread(&l->rwaiting, (*threadnow)());
|
|
|
|
|
unlock(&l->l);
|
|
|
|
|
_threadswitch();
|
2020-01-10 14:44:21 +00:00
|
|
|
return 1;
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
threadwlock(RWLock *l, int block, ulong pc)
|
|
|
|
|
{
|
|
|
|
|
USED(pc);
|
|
|
|
|
|
|
|
|
|
lock(&l->l);
|
|
|
|
|
if(l->writer == nil && l->readers == 0){
|
|
|
|
|
l->writer = (*threadnow)();
|
|
|
|
|
unlock(&l->l);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
if(!block){
|
|
|
|
|
unlock(&l->l);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2008-07-09 11:41:14 -04:00
|
|
|
if(singlethreaded()){
|
|
|
|
|
fprint(2, "wlock deadlock\n");
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2004-12-25 21:56:33 +00:00
|
|
|
addthread(&l->wwaiting, (*threadnow)());
|
|
|
|
|
unlock(&l->l);
|
|
|
|
|
_threadswitch();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
threadrunlock(RWLock *l, ulong pc)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
USED(pc);
|
2005-02-15 18:08:28 +00:00
|
|
|
t = nil;
|
2004-12-25 21:56:33 +00:00
|
|
|
lock(&l->l);
|
|
|
|
|
--l->readers;
|
|
|
|
|
if(l->readers == 0 && (t = l->wwaiting.head) != nil){
|
|
|
|
|
delthread(&l->wwaiting, t);
|
|
|
|
|
l->writer = t;
|
|
|
|
|
}
|
|
|
|
|
unlock(&l->l);
|
2005-02-15 18:08:28 +00:00
|
|
|
if(t)
|
|
|
|
|
_threadready(t);
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
threadwunlock(RWLock *l, ulong pc)
|
|
|
|
|
{
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
USED(pc);
|
|
|
|
|
lock(&l->l);
|
|
|
|
|
l->writer = nil;
|
|
|
|
|
assert(l->readers == 0);
|
|
|
|
|
while((t = l->rwaiting.head) != nil){
|
|
|
|
|
delthread(&l->rwaiting, t);
|
|
|
|
|
l->readers++;
|
|
|
|
|
_threadready(t);
|
|
|
|
|
}
|
2005-02-15 18:08:28 +00:00
|
|
|
t = nil;
|
2004-12-25 21:56:33 +00:00
|
|
|
if(l->readers == 0 && (t = l->wwaiting.head) != nil){
|
|
|
|
|
delthread(&l->wwaiting, t);
|
|
|
|
|
l->writer = t;
|
|
|
|
|
}
|
|
|
|
|
unlock(&l->l);
|
2005-02-15 18:08:28 +00:00
|
|
|
if(t)
|
|
|
|
|
_threadready(t);
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* sleep and wakeup
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
threadrsleep(Rendez *r, ulong pc)
|
|
|
|
|
{
|
2008-07-09 11:41:14 -04:00
|
|
|
if(singlethreaded()){
|
|
|
|
|
fprint(2, "rsleep deadlock\n");
|
|
|
|
|
abort();
|
|
|
|
|
}
|
2004-12-25 21:56:33 +00:00
|
|
|
addthread(&r->waiting, proc()->thread);
|
|
|
|
|
qunlock(r->l);
|
|
|
|
|
_threadswitch();
|
|
|
|
|
qlock(r->l);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
threadrwakeup(Rendez *r, int all, ulong pc)
|
|
|
|
|
{
|
|
|
|
|
int i;
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
2020-12-30 00:15:37 -05:00
|
|
|
_threaddebug(nil, "rwakeup %p %d", r, all);
|
2004-12-25 21:56:33 +00:00
|
|
|
for(i=0;; i++){
|
|
|
|
|
if(i==1 && !all)
|
|
|
|
|
break;
|
|
|
|
|
if((t = r->waiting.head) == nil)
|
|
|
|
|
break;
|
2020-12-30 00:15:37 -05:00
|
|
|
_threaddebug(nil, "rwakeup %p %d -> wake %d", r, all, t->id);
|
2004-12-25 21:56:33 +00:00
|
|
|
delthread(&r->waiting, t);
|
2020-01-10 14:44:21 +00:00
|
|
|
_threadready(t);
|
2020-12-30 00:15:37 -05:00
|
|
|
_threaddebug(nil, "rwakeup %p %d -> loop", r, all);
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
2020-12-30 00:15:37 -05:00
|
|
|
_threaddebug(nil, "rwakeup %p %d -> total %d", r, all, i);
|
2004-12-25 21:56:33 +00:00
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
/*
|
|
|
|
|
* startup
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
static int threadargc;
|
|
|
|
|
static char **threadargv;
|
|
|
|
|
int mainstacksize;
|
2006-06-12 17:20:42 +00:00
|
|
|
extern int _p9usepwlibrary; /* getgrgid etc. smash the stack - tell _p9dir just say no */
|
2004-12-27 16:52:26 +00:00
|
|
|
static void
|
|
|
|
|
threadmainstart(void *v)
|
|
|
|
|
{
|
|
|
|
|
USED(v);
|
2005-01-16 21:31:21 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* N.B. This call to proc() is a program's first call (indirectly) to a
|
|
|
|
|
* pthreads function while executing on a non-pthreads-allocated
|
|
|
|
|
* stack. If the pthreads implementation is using the stack pointer
|
|
|
|
|
* to locate the per-thread data, then this call will blow up.
|
|
|
|
|
* This means the pthread implementation is not suitable for
|
|
|
|
|
* running under libthread. Time to write your own. Sorry.
|
|
|
|
|
*/
|
2006-06-12 17:20:42 +00:00
|
|
|
_p9usepwlibrary = 0;
|
2004-12-27 16:52:26 +00:00
|
|
|
threadmainproc = proc();
|
|
|
|
|
threadmain(threadargc, threadargv);
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-26 17:01:59 -04:00
|
|
|
extern void (*_sysfatal)(char*, va_list);
|
2008-07-20 03:11:05 -04:00
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
int
|
|
|
|
|
main(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
Proc *p;
|
2020-12-30 00:15:37 -05:00
|
|
|
_Thread *t;
|
2020-05-17 08:24:54 -04:00
|
|
|
char *opts;
|
2004-12-27 16:52:26 +00:00
|
|
|
|
2004-12-28 22:36:24 +00:00
|
|
|
argv0 = argv[0];
|
|
|
|
|
|
2020-05-17 08:24:54 -04:00
|
|
|
opts = getenv("LIBTHREAD");
|
|
|
|
|
if(opts == nil)
|
|
|
|
|
opts = "";
|
|
|
|
|
|
|
|
|
|
pthreadperthread = (strstr(opts, "pthreadperthread") != nil);
|
|
|
|
|
#ifdef PLAN9PORT_ASAN
|
|
|
|
|
// ASAN can't deal with the coroutine stack switches.
|
|
|
|
|
// In theory it has support for informing it about stack switches,
|
|
|
|
|
// but even with those calls added it can't deal with things
|
|
|
|
|
// like fork or exit from a coroutine stack.
|
|
|
|
|
// Easier to just run in pthread-per-thread mode.
|
|
|
|
|
pthreadperthread = 1;
|
|
|
|
|
#endif
|
2020-12-30 00:10:11 -05:00
|
|
|
if(threadmaybackground() && strstr(opts, "nodaemon") == nil && getenv("NOLIBTHREADDAEMONIZE") == nil)
|
2006-02-07 17:02:05 +00:00
|
|
|
_threadsetupdaemonize();
|
2004-12-28 01:35:38 +00:00
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
threadargc = argc;
|
|
|
|
|
threadargv = argv;
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Install locking routines into C library.
|
|
|
|
|
*/
|
|
|
|
|
_lock = _threadlock;
|
|
|
|
|
_unlock = _threadunlock;
|
|
|
|
|
_qlock = threadqlock;
|
|
|
|
|
_qunlock = threadqunlock;
|
|
|
|
|
_rlock = threadrlock;
|
|
|
|
|
_runlock = threadrunlock;
|
|
|
|
|
_wlock = threadwlock;
|
|
|
|
|
_wunlock = threadwunlock;
|
|
|
|
|
_rsleep = threadrsleep;
|
|
|
|
|
_rwakeup = threadrwakeup;
|
|
|
|
|
_notejmpbuf = threadnotejmp;
|
2006-06-26 05:47:59 +00:00
|
|
|
_pin = threadpin;
|
|
|
|
|
_unpin = threadunpin;
|
2008-07-20 03:11:05 -04:00
|
|
|
_sysfatal = threadsysfatal;
|
2004-12-27 16:52:26 +00:00
|
|
|
|
|
|
|
|
_pthreadinit();
|
|
|
|
|
p = procalloc();
|
2005-02-14 18:58:56 +00:00
|
|
|
p->mainproc = 1;
|
2004-12-27 16:52:26 +00:00
|
|
|
_threadsetproc(p);
|
|
|
|
|
if(mainstacksize == 0)
|
2005-01-23 16:02:04 +00:00
|
|
|
mainstacksize = 256*1024;
|
2006-04-21 03:58:52 +00:00
|
|
|
atnotify(threadinfo, 1);
|
2020-12-30 00:15:37 -05:00
|
|
|
t = _threadcreate(p, threadmainstart, nil, mainstacksize);
|
|
|
|
|
t->mainthread = 1;
|
2020-01-08 22:47:51 -05:00
|
|
|
procmain(p);
|
2005-02-16 17:15:56 +00:00
|
|
|
sysfatal("procscheduler returned in threadmain!");
|
2005-02-14 18:58:56 +00:00
|
|
|
/* does not return */
|
2005-01-04 22:22:18 +00:00
|
|
|
return 0;
|
2004-12-27 16:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
/*
|
|
|
|
|
* hooray for linked lists
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
addthread(_Threadlist *l, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
if(l->tail){
|
|
|
|
|
l->tail->next = t;
|
|
|
|
|
t->prev = l->tail;
|
|
|
|
|
}else{
|
|
|
|
|
l->head = t;
|
|
|
|
|
t->prev = nil;
|
|
|
|
|
}
|
|
|
|
|
l->tail = t;
|
|
|
|
|
t->next = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
delthread(_Threadlist *l, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
if(t->prev)
|
|
|
|
|
t->prev->next = t->next;
|
|
|
|
|
else
|
|
|
|
|
l->head = t->next;
|
|
|
|
|
if(t->next)
|
|
|
|
|
t->next->prev = t->prev;
|
|
|
|
|
else
|
|
|
|
|
l->tail = t->prev;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-26 05:47:59 +00:00
|
|
|
/* inefficient but rarely used */
|
|
|
|
|
static int
|
|
|
|
|
onlist(_Threadlist *l, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
_Thread *tt;
|
|
|
|
|
|
|
|
|
|
for(tt = l->head; tt; tt=tt->next)
|
|
|
|
|
if(tt == t)
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-25 21:56:33 +00:00
|
|
|
static void
|
|
|
|
|
addthreadinproc(Proc *p, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
_Threadlist *l;
|
|
|
|
|
|
|
|
|
|
l = &p->allthreads;
|
|
|
|
|
if(l->tail){
|
|
|
|
|
l->tail->allnext = t;
|
|
|
|
|
t->allprev = l->tail;
|
|
|
|
|
}else{
|
|
|
|
|
l->head = t;
|
|
|
|
|
t->allprev = nil;
|
|
|
|
|
}
|
|
|
|
|
l->tail = t;
|
|
|
|
|
t->allnext = nil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
delthreadinproc(Proc *p, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
_Threadlist *l;
|
|
|
|
|
|
|
|
|
|
l = &p->allthreads;
|
|
|
|
|
if(t->allprev)
|
|
|
|
|
t->allprev->allnext = t->allnext;
|
|
|
|
|
else
|
|
|
|
|
l->head = t->allnext;
|
|
|
|
|
if(t->allnext)
|
|
|
|
|
t->allnext->allprev = t->allprev;
|
|
|
|
|
else
|
|
|
|
|
l->tail = t->allprev;
|
|
|
|
|
}
|
|
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
Proc *_threadprocs;
|
|
|
|
|
Lock _threadprocslock;
|
|
|
|
|
static Proc *_threadprocstail;
|
2004-12-25 21:56:33 +00:00
|
|
|
|
|
|
|
|
static void
|
2004-12-27 16:52:26 +00:00
|
|
|
addproc(Proc *p)
|
2004-12-27 00:13:48 +00:00
|
|
|
{
|
2004-12-27 16:52:26 +00:00
|
|
|
lock(&_threadprocslock);
|
|
|
|
|
if(_threadprocstail){
|
|
|
|
|
_threadprocstail->next = p;
|
|
|
|
|
p->prev = _threadprocstail;
|
|
|
|
|
}else{
|
|
|
|
|
_threadprocs = p;
|
|
|
|
|
p->prev = nil;
|
|
|
|
|
}
|
|
|
|
|
_threadprocstail = p;
|
|
|
|
|
p->next = nil;
|
|
|
|
|
unlock(&_threadprocslock);
|
2004-12-27 00:13:48 +00:00
|
|
|
}
|
|
|
|
|
|
2004-12-27 16:52:26 +00:00
|
|
|
static void
|
|
|
|
|
delproc(Proc *p)
|
2004-12-25 21:56:33 +00:00
|
|
|
{
|
2004-12-27 16:52:26 +00:00
|
|
|
lock(&_threadprocslock);
|
|
|
|
|
if(p->prev)
|
|
|
|
|
p->prev->next = p->next;
|
|
|
|
|
else
|
|
|
|
|
_threadprocs = p->next;
|
|
|
|
|
if(p->next)
|
|
|
|
|
p->next->prev = p->prev;
|
|
|
|
|
else
|
|
|
|
|
_threadprocstail = p->prev;
|
|
|
|
|
unlock(&_threadprocslock);
|
2004-12-25 21:56:33 +00:00
|
|
|
}
|
2005-01-04 22:22:18 +00:00
|
|
|
|
2020-01-10 14:44:21 +00:00
|
|
|
/*
|
2005-01-04 22:22:18 +00:00
|
|
|
* notify - for now just use the usual mechanisms
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
threadnotify(int (*f)(void*, char*), int in)
|
|
|
|
|
{
|
|
|
|
|
atnotify(f, in);
|
|
|
|
|
}
|
2005-09-26 12:05:26 +00:00
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
onrunqueue(Proc *p, _Thread *t)
|
|
|
|
|
{
|
|
|
|
|
_Thread *tt;
|
|
|
|
|
|
|
|
|
|
for(tt=p->runqueue.head; tt; tt=tt->next)
|
|
|
|
|
if(tt == t)
|
|
|
|
|
return 1;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* print state - called from SIGINFO
|
|
|
|
|
*/
|
|
|
|
|
static int
|
|
|
|
|
threadinfo(void *v, char *s)
|
|
|
|
|
{
|
|
|
|
|
Proc *p;
|
|
|
|
|
_Thread *t;
|
|
|
|
|
|
|
|
|
|
if(strcmp(s, "quit") != 0 && strcmp(s, "sys: status request") != 0)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
for(p=_threadprocs; p; p=p->next){
|
|
|
|
|
fprint(2, "proc %p %s%s\n", (void*)p->osprocid, p->msg,
|
|
|
|
|
p->sysproc ? " (sysproc)": "");
|
|
|
|
|
for(t=p->allthreads.head; t; t=t->allnext){
|
2020-01-10 14:44:21 +00:00
|
|
|
fprint(2, "\tthread %d %s: %s %s\n",
|
|
|
|
|
t->id,
|
|
|
|
|
t == p->thread ? "Running" :
|
2005-09-26 12:05:26 +00:00
|
|
|
onrunqueue(p, t) ? "Ready" : "Sleeping",
|
|
|
|
|
t->state, t->name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|