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:
cinap_lenrek 2021-10-13 17:08:26 +00:00
parent 3fe3e370e3
commit 5a807265a8
6 changed files with 35 additions and 91 deletions

View file

@ -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)

View file

@ -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();
}

View file

@ -3,59 +3,41 @@
#include <thread.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 (*__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;
}

View file

@ -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);
}

View file

@ -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;

View file

@ -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))