plan9fox/sys/src/libthread/note.c
cinap_lenrek 5a807265a8 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.
2021-10-13 17:08:26 +00:00

146 lines
2.2 KiB
C

#include <u.h>
#include <libc.h>
#include <thread.h>
#include "threadimpl.h"
int _threadnopasser;
#define NFN 33
#define ERRLEN 48
typedef struct Note Note;
struct Note
{
Lock inuse;
Proc *proc; /* recipient */
char s[ERRMAX]; /* arg2 */
};
static Note notes[128];
static Note *enotes = notes+nelem(notes);
static int (*onnote[NFN])(void*, char*);
static int onnotepid[NFN];
static Lock onnotelock;
int
threadnotify(int (*f)(void*, char*), int in)
{
int i, topid;
int (*from)(void*, char*), (*to)(void*, char*);
if(in){
from = nil;
to = f;
topid = _threadgetproc()->pid;
}else{
from = f;
to = nil;
topid = 0;
}
lock(&onnotelock);
for(i=0; i<NFN; i++)
if(onnote[i]==from){
onnote[i] = to;
onnotepid[i] = topid;
break;
}
unlock(&onnotelock);
return i<NFN;
}
static void
delayednotes(Proc *p, void *v)
{
int i;
Note *n;
char s[ERRMAX];
int (*fn)(void*, char*);
if(!p->pending)
return;
p->pending = 0;
for(n=notes; n<enotes; n++){
if(n->proc == p){
strcpy(s, n->s);
n->proc = nil;
unlock(&n->inuse);
for(i=0; i<NFN; i++){
if(onnotepid[i]!=p->pid || (fn = onnote[i])==nil)
continue;
if((*fn)(v, s))
break;
}
if(i==NFN){
_threaddebug(DBGNOTE, "Unhandled note %s, proc %p", n->s, p);
if(v != nil)
noted(NDFLT);
else if(strncmp(n->s, "sys:", 4)==0)
abort();
threadexitsall(n->s);
}
}
}
}
void
_threadnote(void *v, char *s)
{
Proc *p;
Note *n;
_threaddebug(DBGNOTE, "Got note %s", s);
if(strncmp(s, "sys:", 4) == 0)
noted(NDFLT);
if(_threadexitsallstatus){
_threaddebug(DBGNOTE, "Threadexitsallstatus = '%s'", _threadexitsallstatus);
_exits(_threadexitsallstatus);
}
if(strcmp(s, "threadint")==0)
noted(NCONT);
p = _threadgetproc();
if(p == nil)
noted(NDFLT);
for(n=notes; n<enotes; n++)
if(canlock(&n->inuse))
break;
if(n==enotes)
sysfatal("libthread: too many delayed notes");
utfecpy(n->s, n->s+ERRMAX, s);
n->proc = p;
p->pending = 1;
if(!p->splhi)
delayednotes(p, v);
noted(NCONT);
}
int
_procsplhi(void)
{
int s;
Proc *p;
p = _threadgetproc();
s = p->splhi;
p->splhi = 1;
return s;
}
void
_procsplx(int s)
{
Proc *p;
p = _threadgetproc();
p->splhi = s;
if(s)
return;
if(p->pending)
delayednotes(p, nil);
}