plan9port/src/libthread/thread.c

733 lines
13 KiB
C
Raw Normal View History

2004-12-25 21:56:33 +00:00
#include "threadimpl.h"
int _threaddebuglevel;
static uint threadnproc;
static uint threadnsysproc;
static Lock threadnproclock;
static Ref threadidref;
2004-12-27 16:52:26 +00:00
static Proc *threadmainproc;
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*);
static void addthreadinproc(Proc*, _Thread*);
static void delthreadinproc(Proc*, _Thread*);
static void contextswitch(Context *from, Context *to);
static void procscheduler(Proc*);
2004-12-25 21:56:33 +00:00
2004-12-28 01:35:38 +00:00
static void
_threaddebug(char *fmt, ...)
{
va_list arg;
char buf[128];
_Thread *t;
2005-01-06 23:07:19 +00:00
char *p;
static int fd = -1;
2004-12-28 01:35:38 +00:00
2005-01-06 23:07:19 +00:00
return;
2005-01-11 17:43:53 +00:00
va_start(arg, fmt);
vfprint(1, fmt, arg);
va_end(arg);
return;
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);
}
2004-12-28 01:35:38 +00:00
va_start(arg, fmt);
vsnprint(buf, sizeof buf, fmt, arg);
va_end(arg);
t = proc()->thread;
if(t)
2005-01-06 23:07:19 +00:00
fprint(fd, "%d.%d: %s\n", getpid(), t->id, buf);
2004-12-28 01:35:38 +00:00
else
2005-01-06 23:07:19 +00:00
fprint(fd, "%d._: %s\n", 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;
z = x<<16; /* hide undefined 32-bit shift from 32-bit compilers */
z <<= 16;
z |= y;
t = (_Thread*)z;
2004-12-25 21:56:33 +00:00
2005-01-11 17:43:53 +00:00
//print("threadstart %p\n", v);
2004-12-25 21:56:33 +00:00
t->startfn(t->startarg);
2005-01-11 17:43:53 +00:00
//print("threadexits %p\n", v);
2004-12-27 16:52:26 +00:00
threadexits(nil);
2005-01-11 17:43:53 +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 */
t = malloc(sizeof *t+stack);
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->stk = (uchar*)(t+1);
t->stksize = stack;
t->id = incref(&threadidref);
t->startfn = fn;
t->startarg = arg;
/* do a reasonable initialization */
memset(&t->context.uc, 0, sizeof t->context.uc);
sigemptyset(&zero);
sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask);
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");
2004-12-25 21:56:33 +00:00
/* call makecontext to do the real work. */
/* leave a few words open on both ends */
t->context.uc.uc_stack.ss_sp = t->stk+8;
2004-12-28 01:35:38 +00:00
t->context.uc.uc_stack.ss_size = t->stksize-64;
2005-01-11 21:28:00 +00:00
#ifdef __sun__ /* sigh */
/* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */
2005-01-14 18:11:21 +00:00
t->context.uc.uc_stack.ss_sp =
(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.
*/
z = (ulong)t;
y = z;
z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */
x = z>>16;
makecontext(&t->context.uc, (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;
t = threadalloc(fn, arg, stack);
t->proc = p;
addthreadinproc(p, t);
p->nthread++;
_threadready(t);
return t;
}
int
threadcreate(void (*fn)(void*), void *arg, uint stack)
{
_Thread *t;
t = _threadcreate(proc(), fn, arg, stack);
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 */
_procstart(p, procscheduler);
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();
2005-01-11 17:43:53 +00:00
//print("threadswtch %p\n", p);
2004-12-25 21:56:33 +00:00
contextswitch(&p->thread->context, &p->schedcontext);
}
void
_threadready(_Thread *t)
{
Proc *p;
p = t->proc;
lock(&p->lock);
p->runrend.l = &p->lock;
2004-12-25 21:56:33 +00:00
addthread(&p->runqueue, t);
2004-12-27 19:11:33 +00:00
//print("%d wake for job %d->%d\n", time(0), getpid(), p->osprocid);
if(p != proc())
_procwakeupandunlock(&p->runrend);
else
unlock(&p->lock);
2004-12-25 21:56:33 +00:00
}
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
}
static void
contextswitch(Context *from, Context *to)
{
if(swapcontext(&from->uc, &to->uc) < 0){
fprint(2, "swapcontext failed: %r\n");
assert(0);
}
}
static void
procscheduler(Proc *p)
2004-12-25 21:56:33 +00:00
{
_Thread *t;
setproc(p);
2004-12-28 01:35:38 +00:00
_threaddebug("scheduler enter");
2005-01-11 17:43:53 +00:00
// print("s %p\n", p);
2004-12-25 21:56:33 +00:00
lock(&p->lock);
for(;;){
while((t = p->runqueue.head) == nil){
if(p->nthread == 0)
goto Out;
p->runrend.l = &p->lock;
2004-12-28 01:35:38 +00:00
_threaddebug("scheduler sleep");
2004-12-25 21:56:33 +00:00
_procsleep(&p->runrend);
2004-12-28 01:35:38 +00:00
_threaddebug("scheduler wake");
2004-12-25 21:56:33 +00:00
}
delthread(&p->runqueue, t);
unlock(&p->lock);
p->thread = t;
2004-12-27 19:11:33 +00:00
p->nswitch++;
2004-12-28 01:35:38 +00:00
_threaddebug("run %d (%s)", t->id, t->name);
2004-12-25 21:56:33 +00:00
contextswitch(&p->schedcontext, &t->context);
2005-01-11 17:43:53 +00:00
//print("back in scheduler\n");
2004-12-25 21:56:33 +00:00
p->thread = nil;
lock(&p->lock);
if(t->exiting){
delthreadinproc(p, t);
p->nthread--;
2005-01-11 17:43:53 +00:00
//print("ntrhead %d\n", p->nthread);
2004-12-25 21:56:33 +00:00
free(t);
}
}
Out:
2004-12-28 01:35:38 +00:00
_threaddebug("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);
free(p);
}
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);
}
2005-01-13 04:49:19 +00:00
void
needstack(int n)
{
_Thread *t;
t = proc()->thread;
if((char*)&t <= (char*)t->stk
|| (char*)&t - (char*)t->stk < 256+n){
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();
}
}
2004-12-25 21:56:33 +00:00
/*
* locking
*/
static int
threadqlock(QLock *l, int block, ulong pc)
{
2005-01-11 17:43:53 +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)();
//print("qlock %p @%#x by %p\n", l, pc, l->owner);
unlock(&l->l);
return 1;
}
if(!block){
unlock(&l->l);
return 0;
}
//print("qsleep %p @%#x by %p\n", l, pc, (*threadnow)());
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();
}
//print("qlock wakeup %p @%#x by %p\n", l, pc, (*threadnow)());
return 1;
}
static void
threadqunlock(QLock *l, ulong pc)
{
2005-02-15 18:08:28 +00:00
_Thread *ready;
2004-12-25 21:56:33 +00:00
lock(&l->l);
//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;
}
addthread(&l->rwaiting, (*threadnow)());
unlock(&l->l);
_threadswitch();
return 1;
}
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;
}
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)
{
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;
for(i=0;; i++){
if(i==1 && !all)
break;
if((t = r->waiting.head) == nil)
break;
delthread(&r->waiting, t);
_threadready(t);
}
return i;
}
2004-12-27 16:52:26 +00:00
/*
* startup
*/
static int threadargc;
static char **threadargv;
int mainstacksize;
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.
*/
2004-12-27 16:52:26 +00:00
threadmainproc = proc();
threadmain(threadargc, threadargv);
}
int
main(int argc, char **argv)
{
Proc *p;
argv0 = argv[0];
2004-12-28 01:35:38 +00:00
_threadsetupdaemonize();
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;
_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)
mainstacksize = 256*1024;
2004-12-27 16:52:26 +00:00
_threadcreate(p, threadmainstart, nil, mainstacksize);
procscheduler(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;
}
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 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 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
/*
* notify - for now just use the usual mechanisms
*/
void
threadnotify(int (*f)(void*, char*), int in)
{
atnotify(f, in);
}