diff --git a/sys/src/libthread/channel.c b/sys/src/libthread/channel.c index 4e410aa94..6b736f7d9 100644 --- a/sys/src/libthread/channel.c +++ b/sys/src/libthread/channel.c @@ -339,15 +339,7 @@ nbsend(Channel *c, void *v) return runop(CHANSND, c, v, 1); } -static void -channelsize(Channel *c, int sz) -{ - if(c->e != sz){ - fprint(2, "expected channel with elements of size %d, got size %d\n", - sz, c->e); - abort(); - } -} +#define channelsize(c, sz) assert(c->e == sz) int sendul(Channel *c, ulong v) diff --git a/sys/src/libthread/debug.c b/sys/src/libthread/debug.c index 183869d18..cab101ca6 100644 --- a/sys/src/libthread/debug.c +++ b/sys/src/libthread/debug.c @@ -6,16 +6,13 @@ int _threaddebuglevel; void -_threaddebug(ulong flag, char *fmt, ...) +_threadprint(char *fmt, ...) { char buf[128]; va_list arg; Fmt f; Proc *p; - if((_threaddebuglevel&flag) == 0) - return; - fmtfdinit(&f, 2, buf, sizeof buf); p = _threadgetproc(); @@ -36,16 +33,6 @@ _threaddebug(ulong flag, char *fmt, ...) void _threadassert(char *s) { - char buf[256]; - int n; - Proc *p; - - p = _threadgetproc(); - if(p && p->thread) - n = sprint(buf, "%d.%d ", p->pid, p->thread->id); - else - n = 0; - snprint(buf+n, sizeof(buf)-n, "%s: assertion failed\n", s); - write(2, buf, strlen(buf)); + _threadprint("%s: assertion failed", s); abort(); } diff --git a/sys/src/libthread/main.c b/sys/src/libthread/main.c index faa08bf79..7f88c17bc 100644 --- a/sys/src/libthread/main.c +++ b/sys/src/libthread/main.c @@ -3,59 +3,41 @@ #include #include "threadimpl.h" -typedef struct Mainarg Mainarg; -struct Mainarg -{ - int argc; - char **argv; -}; - -int mainstacksize; -static jmp_buf _mainjmp; -static void mainlauncher(void*); extern void (*_sysfatal)(char*, va_list); extern void (*__assert)(char*); -static Proc **mainp; +int mainstacksize; + +static jmp_buf mainjmp; +static int mainargc; + +static void +mainlauncher(void *arg) +{ + threadmain(mainargc, arg); + threadexits("threadmain"); +} void main(int argc, char **argv) { - Mainarg *a; - Proc *p; - rfork(RFREND); - mainp = &p; //_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0; - _systhreadinit(); + _threadprocp = privalloc(); _qlockinit(_threadrendezvous); _sysfatal = _threadsysfatal; __assert = _threadassert; notify(_threadnote); if(mainstacksize == 0) mainstacksize = 8*1024; - - a = _threadmalloc(sizeof *a, 1); - a->argc = argc; - a->argv = argv; - - p = _newproc(mainlauncher, a, mainstacksize, "threadmain", 0, 0); - setjmp(_mainjmp); - _schedinit(p); + mainargc = argc; + _threadsetproc(_newproc(mainlauncher, argv, mainstacksize, "threadmain", 0, 0)); + setjmp(mainjmp); + _schedinit(); abort(); /* not reached */ } -static void -mainlauncher(void *arg) -{ - Mainarg *a; - - a = arg; - threadmain(a->argc, a->argv); - threadexits("threadmain"); -} - static void efork(Execargs *e) { @@ -93,8 +75,8 @@ _schedfork(Proc *p) switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT|p->rforkflag)){ case 0: - *mainp = p; /* write to stack, so local to proc */ - longjmp(_mainjmp, 1); + _threadsetproc(p); + longjmp(mainjmp, 1); default: return pid; } @@ -141,23 +123,3 @@ _schedexecwait(void) } threadexits("procexec"); } - -static Proc **procp; - -void -_systhreadinit(void) -{ - procp = privalloc(); -} - -Proc* -_threadgetproc(void) -{ - return *procp; -} - -void -_threadsetproc(Proc *p) -{ - *procp = p; -} diff --git a/sys/src/libthread/note.c b/sys/src/libthread/note.c index c04e3e1a2..fcbbf8c07 100644 --- a/sys/src/libthread/note.c +++ b/sys/src/libthread/note.c @@ -72,7 +72,7 @@ delayednotes(Proc *p, void *v) break; } if(i==NFN){ - _threaddebug(DBGNOTE, "Unhandled note %s, proc %p\n", n->s, p); + _threaddebug(DBGNOTE, "Unhandled note %s, proc %p", n->s, p); if(v != nil) noted(NDFLT); else if(strncmp(n->s, "sys:", 4)==0) @@ -94,7 +94,7 @@ _threadnote(void *v, char *s) noted(NDFLT); if(_threadexitsallstatus){ - _threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'\n", _threadexitsallstatus); + _threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'", _threadexitsallstatus); _exits(_threadexitsallstatus); } diff --git a/sys/src/libthread/sched.c b/sys/src/libthread/sched.c index 888d64cc2..6e7a6d57a 100644 --- a/sys/src/libthread/sched.c +++ b/sys/src/libthread/sched.c @@ -36,14 +36,13 @@ unlinkproc(Proc *p) } void -_schedinit(void *arg) +_schedinit(void) { Proc *p; Thread *t, **l; - p = arg; + p = _threadgetproc(); p->pid = getpid(); - _threadsetproc(p); while(setjmp(p->sched)) ; _threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus); @@ -164,7 +163,7 @@ Resched: _threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id); p->thread = t; if(t->moribund){ - _threaddebug(DBGSCHED, "%d.%d marked to die"); + _threaddebug(DBGSCHED, "%d.%d marked to die", t->proc->pid, t->id); goto Resched; } t->state = Running; diff --git a/sys/src/libthread/threadimpl.h b/sys/src/libthread/threadimpl.h index 6574a6201..b5006f914 100644 --- a/sys/src/libthread/threadimpl.h +++ b/sys/src/libthread/threadimpl.h @@ -157,15 +157,12 @@ int _schedexec(Execargs*); void _schedexecwait(void); void _schedexit(Proc*); int _schedfork(Proc*); -void _schedinit(void*); -void _systhreadinit(void); +void _schedinit(void); void _threadassert(char*); void _threadbreakrendez(void); -void _threaddebug(ulong, char*, ...); +void _threadprint(char*, ...); void _threadexitsall(char*); void _threadflagrendez(Thread*); -Proc* _threadgetproc(void); -void _threadsetproc(Proc*); void _threadinitstack(Thread*, void(*)(void*), void*); void* _threadmalloc(long, int); void _threadnote(void*, char*); @@ -173,6 +170,10 @@ void _threadready(Thread*); void* _threadrendezvous(void*, void*); void _threadsysfatal(char*, va_list); +Proc **_threadprocp; +#define _threadgetproc() (*_threadprocp) +#define _threadsetproc(p) (*_threadprocp = (p)) + extern int _threaddebuglevel; extern char* _threadexitsallstatus; extern Pqueue _threadpq; @@ -187,4 +188,7 @@ extern Rgrp _threadrgrp; #define DBGNOTE (1 << 20) #define DBGEXEC (1 << 21) +#pragma varargck argpos _threadprint 1 +#define _threaddebug(flag, ...) if((_threaddebuglevel&(flag))==0){}else _threadprint(__VA_ARGS__) + #define ioproc_arg(io, type) (va_arg((io)->arg, type))