libthread: fix debug prints, simplify
Do the debuglevel check before calling the print function for _threaddebug, by making it a macro. Do not waste cycles passing arguments. Generalize the _threaddebug function into _threadprint() and add a varargcheck pragma. This function can also be used from _threadassert(). Fix missing arguments in one case, fix trailing newlines in _threaddebug(). Make _threadgetproc()/_threadsetproc() a macro, just dereferencing Proc**_threadprocp. Simplify the mainjump, just call _threadsetproc() directly without that mainp dance. Remove the _schedinit() argument, it uses _threadgetproc() now. Get rid of Mainarg struct, just have a global variable for argc.
This commit is contained in:
parent
3fe3e370e3
commit
5a807265a8
6 changed files with 35 additions and 91 deletions
|
@ -339,15 +339,7 @@ nbsend(Channel *c, void *v)
|
||||||
return runop(CHANSND, c, v, 1);
|
return runop(CHANSND, c, v, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
#define channelsize(c, sz) assert(c->e == sz)
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int
|
int
|
||||||
sendul(Channel *c, ulong v)
|
sendul(Channel *c, ulong v)
|
||||||
|
|
|
@ -6,16 +6,13 @@
|
||||||
int _threaddebuglevel;
|
int _threaddebuglevel;
|
||||||
|
|
||||||
void
|
void
|
||||||
_threaddebug(ulong flag, char *fmt, ...)
|
_threadprint(char *fmt, ...)
|
||||||
{
|
{
|
||||||
char buf[128];
|
char buf[128];
|
||||||
va_list arg;
|
va_list arg;
|
||||||
Fmt f;
|
Fmt f;
|
||||||
Proc *p;
|
Proc *p;
|
||||||
|
|
||||||
if((_threaddebuglevel&flag) == 0)
|
|
||||||
return;
|
|
||||||
|
|
||||||
fmtfdinit(&f, 2, buf, sizeof buf);
|
fmtfdinit(&f, 2, buf, sizeof buf);
|
||||||
|
|
||||||
p = _threadgetproc();
|
p = _threadgetproc();
|
||||||
|
@ -36,16 +33,6 @@ _threaddebug(ulong flag, char *fmt, ...)
|
||||||
void
|
void
|
||||||
_threadassert(char *s)
|
_threadassert(char *s)
|
||||||
{
|
{
|
||||||
char buf[256];
|
_threadprint("%s: assertion failed", s);
|
||||||
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));
|
|
||||||
abort();
|
abort();
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,59 +3,41 @@
|
||||||
#include <thread.h>
|
#include <thread.h>
|
||||||
#include "threadimpl.h"
|
#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 (*_sysfatal)(char*, va_list);
|
||||||
extern void (*__assert)(char*);
|
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
|
void
|
||||||
main(int argc, char **argv)
|
main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
Mainarg *a;
|
|
||||||
Proc *p;
|
|
||||||
|
|
||||||
rfork(RFREND);
|
rfork(RFREND);
|
||||||
mainp = &p;
|
|
||||||
|
|
||||||
//_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0;
|
//_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0;
|
||||||
_systhreadinit();
|
_threadprocp = privalloc();
|
||||||
_qlockinit(_threadrendezvous);
|
_qlockinit(_threadrendezvous);
|
||||||
_sysfatal = _threadsysfatal;
|
_sysfatal = _threadsysfatal;
|
||||||
__assert = _threadassert;
|
__assert = _threadassert;
|
||||||
notify(_threadnote);
|
notify(_threadnote);
|
||||||
if(mainstacksize == 0)
|
if(mainstacksize == 0)
|
||||||
mainstacksize = 8*1024;
|
mainstacksize = 8*1024;
|
||||||
|
mainargc = argc;
|
||||||
a = _threadmalloc(sizeof *a, 1);
|
_threadsetproc(_newproc(mainlauncher, argv, mainstacksize, "threadmain", 0, 0));
|
||||||
a->argc = argc;
|
setjmp(mainjmp);
|
||||||
a->argv = argv;
|
_schedinit();
|
||||||
|
|
||||||
p = _newproc(mainlauncher, a, mainstacksize, "threadmain", 0, 0);
|
|
||||||
setjmp(_mainjmp);
|
|
||||||
_schedinit(p);
|
|
||||||
abort(); /* not reached */
|
abort(); /* not reached */
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
|
||||||
mainlauncher(void *arg)
|
|
||||||
{
|
|
||||||
Mainarg *a;
|
|
||||||
|
|
||||||
a = arg;
|
|
||||||
threadmain(a->argc, a->argv);
|
|
||||||
threadexits("threadmain");
|
|
||||||
}
|
|
||||||
|
|
||||||
static void
|
static void
|
||||||
efork(Execargs *e)
|
efork(Execargs *e)
|
||||||
{
|
{
|
||||||
|
@ -93,8 +75,8 @@ _schedfork(Proc *p)
|
||||||
|
|
||||||
switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT|p->rforkflag)){
|
switch(pid = rfork(RFPROC|RFMEM|RFNOWAIT|p->rforkflag)){
|
||||||
case 0:
|
case 0:
|
||||||
*mainp = p; /* write to stack, so local to proc */
|
_threadsetproc(p);
|
||||||
longjmp(_mainjmp, 1);
|
longjmp(mainjmp, 1);
|
||||||
default:
|
default:
|
||||||
return pid;
|
return pid;
|
||||||
}
|
}
|
||||||
|
@ -141,23 +123,3 @@ _schedexecwait(void)
|
||||||
}
|
}
|
||||||
threadexits("procexec");
|
threadexits("procexec");
|
||||||
}
|
}
|
||||||
|
|
||||||
static Proc **procp;
|
|
||||||
|
|
||||||
void
|
|
||||||
_systhreadinit(void)
|
|
||||||
{
|
|
||||||
procp = privalloc();
|
|
||||||
}
|
|
||||||
|
|
||||||
Proc*
|
|
||||||
_threadgetproc(void)
|
|
||||||
{
|
|
||||||
return *procp;
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
_threadsetproc(Proc *p)
|
|
||||||
{
|
|
||||||
*procp = p;
|
|
||||||
}
|
|
||||||
|
|
|
@ -72,7 +72,7 @@ delayednotes(Proc *p, void *v)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if(i==NFN){
|
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)
|
if(v != nil)
|
||||||
noted(NDFLT);
|
noted(NDFLT);
|
||||||
else if(strncmp(n->s, "sys:", 4)==0)
|
else if(strncmp(n->s, "sys:", 4)==0)
|
||||||
|
@ -94,7 +94,7 @@ _threadnote(void *v, char *s)
|
||||||
noted(NDFLT);
|
noted(NDFLT);
|
||||||
|
|
||||||
if(_threadexitsallstatus){
|
if(_threadexitsallstatus){
|
||||||
_threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'\n", _threadexitsallstatus);
|
_threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'", _threadexitsallstatus);
|
||||||
_exits(_threadexitsallstatus);
|
_exits(_threadexitsallstatus);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -36,14 +36,13 @@ unlinkproc(Proc *p)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
_schedinit(void *arg)
|
_schedinit(void)
|
||||||
{
|
{
|
||||||
Proc *p;
|
Proc *p;
|
||||||
Thread *t, **l;
|
Thread *t, **l;
|
||||||
|
|
||||||
p = arg;
|
p = _threadgetproc();
|
||||||
p->pid = getpid();
|
p->pid = getpid();
|
||||||
_threadsetproc(p);
|
|
||||||
while(setjmp(p->sched))
|
while(setjmp(p->sched))
|
||||||
;
|
;
|
||||||
_threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
|
_threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
|
||||||
|
@ -164,7 +163,7 @@ Resched:
|
||||||
_threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
|
_threaddebug(DBGSCHED, "running %d.%d", t->proc->pid, t->id);
|
||||||
p->thread = t;
|
p->thread = t;
|
||||||
if(t->moribund){
|
if(t->moribund){
|
||||||
_threaddebug(DBGSCHED, "%d.%d marked to die");
|
_threaddebug(DBGSCHED, "%d.%d marked to die", t->proc->pid, t->id);
|
||||||
goto Resched;
|
goto Resched;
|
||||||
}
|
}
|
||||||
t->state = Running;
|
t->state = Running;
|
||||||
|
|
|
@ -157,15 +157,12 @@ int _schedexec(Execargs*);
|
||||||
void _schedexecwait(void);
|
void _schedexecwait(void);
|
||||||
void _schedexit(Proc*);
|
void _schedexit(Proc*);
|
||||||
int _schedfork(Proc*);
|
int _schedfork(Proc*);
|
||||||
void _schedinit(void*);
|
void _schedinit(void);
|
||||||
void _systhreadinit(void);
|
|
||||||
void _threadassert(char*);
|
void _threadassert(char*);
|
||||||
void _threadbreakrendez(void);
|
void _threadbreakrendez(void);
|
||||||
void _threaddebug(ulong, char*, ...);
|
void _threadprint(char*, ...);
|
||||||
void _threadexitsall(char*);
|
void _threadexitsall(char*);
|
||||||
void _threadflagrendez(Thread*);
|
void _threadflagrendez(Thread*);
|
||||||
Proc* _threadgetproc(void);
|
|
||||||
void _threadsetproc(Proc*);
|
|
||||||
void _threadinitstack(Thread*, void(*)(void*), void*);
|
void _threadinitstack(Thread*, void(*)(void*), void*);
|
||||||
void* _threadmalloc(long, int);
|
void* _threadmalloc(long, int);
|
||||||
void _threadnote(void*, char*);
|
void _threadnote(void*, char*);
|
||||||
|
@ -173,6 +170,10 @@ void _threadready(Thread*);
|
||||||
void* _threadrendezvous(void*, void*);
|
void* _threadrendezvous(void*, void*);
|
||||||
void _threadsysfatal(char*, va_list);
|
void _threadsysfatal(char*, va_list);
|
||||||
|
|
||||||
|
Proc **_threadprocp;
|
||||||
|
#define _threadgetproc() (*_threadprocp)
|
||||||
|
#define _threadsetproc(p) (*_threadprocp = (p))
|
||||||
|
|
||||||
extern int _threaddebuglevel;
|
extern int _threaddebuglevel;
|
||||||
extern char* _threadexitsallstatus;
|
extern char* _threadexitsallstatus;
|
||||||
extern Pqueue _threadpq;
|
extern Pqueue _threadpq;
|
||||||
|
@ -187,4 +188,7 @@ extern Rgrp _threadrgrp;
|
||||||
#define DBGNOTE (1 << 20)
|
#define DBGNOTE (1 << 20)
|
||||||
#define DBGEXEC (1 << 21)
|
#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))
|
#define ioproc_arg(io, type) (va_arg((io)->arg, type))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue