ref: 5a807265a819206f8342ab3a23b940a0c75049fc
parent: 3fe3e370e3bcad21b61aec6cbf5d11a9398e805b
author: cinap_lenrek <[email protected]>
date: Wed Oct 13 13:08:26 EDT 2021
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.
--- a/sys/src/libthread/channel.c
+++ b/sys/src/libthread/channel.c
@@ -339,15 +339,7 @@
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)
--- a/sys/src/libthread/debug.c
+++ b/sys/src/libthread/debug.c
@@ -6,7 +6,7 @@
int _threaddebuglevel;
void
-_threaddebug(ulong flag, char *fmt, ...)
+_threadprint(char *fmt, ...)
{
char buf[128];
va_list arg;
@@ -13,9 +13,6 @@
Fmt f;
Proc *p;
- if((_threaddebuglevel&flag) == 0)
- return;
-
fmtfdinit(&f, 2, buf, sizeof buf);
p = _threadgetproc();
@@ -36,16 +33,6 @@
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();
}
--- a/sys/src/libthread/main.c
+++ b/sys/src/libthread/main.c
@@ -3,32 +3,28 @@
#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;
@@ -35,28 +31,14 @@
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)
{
char buf[ERRMAX];
@@ -93,8 +75,8 @@
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;
}
@@ -140,24 +122,4 @@
free(w);
}
threadexits("procexec");
-}
-
-static Proc **procp;
-
-void
-_systhreadinit(void)
-{
- procp = privalloc();
-}
-
-Proc*
-_threadgetproc(void)
-{
- return *procp;
-}
-
-void
-_threadsetproc(Proc *p)
-{
- *procp = p;
}
--- a/sys/src/libthread/note.c
+++ b/sys/src/libthread/note.c
@@ -72,7 +72,7 @@
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 @@
noted(NDFLT);
if(_threadexitsallstatus){
- _threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'\n", _threadexitsallstatus);
+ _threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'", _threadexitsallstatus);
_exits(_threadexitsallstatus);
}
--- a/sys/src/libthread/sched.c
+++ b/sys/src/libthread/sched.c
@@ -36,14 +36,13 @@
}
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 @@
_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;
--- a/sys/src/libthread/threadimpl.h
+++ b/sys/src/libthread/threadimpl.h
@@ -157,15 +157,12 @@
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* _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;
@@ -186,5 +187,8 @@
/* #define DBGKILL (1 << 19) */
#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))