Fix small bugs.

This commit is contained in:
rsc 2004-05-11 17:51:27 +00:00
parent 2f2df5e02e
commit c4097c2951
14 changed files with 71 additions and 34 deletions

View file

@ -121,7 +121,7 @@ Channel* threadwaitchan(void);
int tprivalloc(void); int tprivalloc(void);
void tprivfree(int); void tprivfree(int);
void **tprivaddr(int); void **tprivaddr(int);
void yield(void); int yield(void);
long threadstack(void); long threadstack(void);

View file

@ -101,6 +101,8 @@ ls(char *s, int multi)
return 1; return 1;
} }
if(db->qid.type&QTDIR && dflag==0){ if(db->qid.type&QTDIR && dflag==0){
free(db);
db = nil;
output(); output();
fd = open(s, OREAD); fd = open(s, OREAD);
if(fd == -1) if(fd == -1)

View file

@ -71,7 +71,7 @@ void Vinit(void){
for(s=*env;*s && *s!='(' && *s!='=';s++); for(s=*env;*s && *s!='(' && *s!='=';s++);
switch(*s){ switch(*s){
case '\0': case '\0':
pfmt(err, "rc: odd environment %q?\n", *env); // pfmt(err, "rc: odd environment %q?\n", *env);
break; break;
case '=': case '=':
*s='\0'; *s='\0';

View file

@ -11,6 +11,7 @@ main(int argc, char *argv[])
{ {
int i; int i;
Waitmsg *w; Waitmsg *w;
vlong t0, t1;
long l; long l;
char *p; char *p;
char err[ERRMAX]; char err[ERRMAX];
@ -20,6 +21,7 @@ main(int argc, char *argv[])
exits("usage"); exits("usage");
} }
t0 = nsec();
switch(fork()){ switch(fork()){
case -1: case -1:
error("fork"); error("fork");
@ -37,8 +39,9 @@ main(int argc, char *argv[])
loop: loop:
w = wait(); w = wait();
t1 = nsec();
if(w == nil){ if(w == nil){
errstr(err, sizeof err); rerrstr(err, sizeof err);
if(strcmp(err, "interrupted") == 0) if(strcmp(err, "interrupted") == 0)
goto loop; goto loop;
error("wait"); error("wait");
@ -47,7 +50,7 @@ main(int argc, char *argv[])
add("%ld.%.2ldu", l/1000, (l%1000)/10); add("%ld.%.2ldu", l/1000, (l%1000)/10);
l = w->time[1]; l = w->time[1];
add("%ld.%.2lds", l/1000, (l%1000)/10); add("%ld.%.2lds", l/1000, (l%1000)/10);
l = w->time[2]; l = (t1-t0)/1000000;
add("%ld.%.2ldr", l/1000, (l%1000)/10); add("%ld.%.2ldr", l/1000, (l%1000)/10);
add("\t"); add("\t");
for(i=1; i<argc; i++){ for(i=1; i<argc; i++){

View file

@ -66,7 +66,7 @@ int
notify(void (*f)(void*, char*)) notify(void (*f)(void*, char*))
{ {
int i; int i;
struct sigaction sa; struct sigaction sa, osa;
_p9uproc(0); _p9uproc(0);
memset(&sa, 0, sizeof sa); memset(&sa, 0, sizeof sa);
@ -77,11 +77,22 @@ notify(void (*f)(void*, char*))
sa.sa_handler = notifysigf; sa.sa_handler = notifysigf;
} }
for(i=0; i<nelem(sigs); i++){ for(i=0; i<nelem(sigs); i++){
/*
* If someone has already installed a handler,
* It's probably some ld preload nonsense,
* like pct (a SIGVTALRM-based profiler).
* Leave it alone.
*/
sigaction(sigs[i].sig, nil, &osa);
if(osa.sa_handler != SIG_DFL)
continue;
sigemptyset(&sa.sa_mask);
sigaddset(&sa.sa_mask, i);
if(sigs[i].restart) if(sigs[i].restart)
sa.sa_flags |= SA_RESTART; sa.sa_flags |= SA_RESTART;
else else
sa.sa_flags &= ~SA_RESTART; sa.sa_flags &= ~SA_RESTART;
sigaction(sigs[i].sig, &sa, 0); sigaction(sigs[i].sig, &sa, nil);
} }
return 0; return 0;
} }

View file

@ -15,7 +15,7 @@ launcher386(void (*f)(void *arg), void *arg)
p = _threadgetproc(); p = _threadgetproc();
t = p->thread; t = p->thread;
_threadstacklimit(t->stk); _threadstacklimit(t->stk, t->stk+t->stksize);
(*f)(arg); (*f)(arg);
threadexits(nil); threadexits(nil);
@ -39,18 +39,19 @@ _threadinswitch(int enter)
USED(enter); USED(enter);
#ifdef USEVALGRIND #ifdef USEVALGRIND
if(enter) if(enter)
VALGRIND_SET_STACK_LIMIT(0, 0, 1);
else
VALGRIND_SET_STACK_LIMIT(0, 0, 0); VALGRIND_SET_STACK_LIMIT(0, 0, 0);
else
VALGRIND_SET_STACK_LIMIT(0, 0, 1);
#endif #endif
} }
void void
_threadstacklimit(void *addr) _threadstacklimit(void *bottom, void *top)
{ {
USED(addr); USED(bottom);
USED(top);
#ifdef USEVALGRIND #ifdef USEVALGRIND
VALGRIND_SET_STACK_LIMIT(1, addr, 0); VALGRIND_SET_STACK_LIMIT(1, bottom, top);
#endif #endif
} }

View file

@ -32,7 +32,7 @@ _threadinswitch(int enter)
} }
void void
_threadstacklimit(void *addr) _threadstacklimit(void *addr, void *addr2)
{ {
USED(addr); USED(addr);
} }

View file

@ -24,6 +24,7 @@ newthread(Proc *p, void (*f)(void *arg), void *arg, uint stacksize, char *name,
if(stacksize < 32) if(stacksize < 32)
sysfatal("bad stacksize %d", stacksize); sysfatal("bad stacksize %d", stacksize);
t = _threadmalloc(sizeof(Thread), 1); t = _threadmalloc(sizeof(Thread), 1);
t->lastfd = -1;
s = _threadmalloc(stacksize, 0); s = _threadmalloc(stacksize, 0);
t->stk = (uchar*)s; t->stk = (uchar*)s;
t->stksize = stacksize; t->stksize = stacksize;

View file

@ -2,12 +2,13 @@
#include <u.h> #include <u.h>
#include <libc.h> #include <libc.h>
#include <thread.h> #include <thread.h>
#include "threadimpl.h"
#include <errno.h> #include <errno.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#define debugpoll 0 #define debugpoll 0
static int noblocked[4096/32];
#ifdef __APPLE__ #ifdef __APPLE__
#include <sys/time.h> #include <sys/time.h>
@ -174,11 +175,15 @@ _threadfdwait(int fd, int rw, ulong pc)
struct { struct {
Channel c; Channel c;
Alt *qentry[2];
ulong x; ulong x;
} s; } s;
threadfdwaitsetup(); threadfdwaitsetup();
chaninit(&s.c, sizeof(ulong), 1); chaninit(&s.c, sizeof(ulong), 1);
s.c.qentry = (volatile Alt**)s.qentry;
s.c.nentry = 2;
memset(s.qentry, 0, sizeof s.qentry);
for(i=0; i<npoll; i++) for(i=0; i<npoll; i++)
if(pfd[i].fd == -1) if(pfd[i].fd == -1)
break; break;
@ -223,7 +228,25 @@ threadsleep(int ms)
void void
threadfdnoblock(int fd) threadfdnoblock(int fd)
{ {
Thread *t;
if(fd<0)
return;
if(fd < 8*sizeof(int)*nelem(noblocked)
&& (noblocked[fd/(8*sizeof(int))] & (1<<(fd%(8*sizeof(int))))))
return;
t = _threadgetproc()->thread;
if(t && t->lastfd == fd)
return;
fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0)|O_NONBLOCK); fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0)|O_NONBLOCK);
if(t)
t->lastfd = fd;
/* We could lock this but we're probably single-threaded
* and the worst that will happen is we'll run fcntl
* a few more times.
*/
noblocked[fd/(8*sizeof(int))] |= 1<<(fd%(8*sizeof(int)));
} }
long long

View file

@ -32,24 +32,12 @@ _threaddie(int x)
exit(_threadexitsallstatus[0] ? 1 : 0); exit(_threadexitsallstatus[0] ? 1 : 0);
} }
static void
_nop(int x)
{
USED(x);
}
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
Mainarg *a; Mainarg *a;
Proc *p; Proc *p;
signal(SIGTERM, _threaddie);
signal(SIGCHLD, _nop);
signal(SIGALRM, _nop);
// signal(SIGINFO, _threadstatus);
// rfork(RFREND);
//_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0; //_threaddebuglevel = (DBGSCHED|DBGCHAN|DBGREND)^~0;
_systhreadinit(); _systhreadinit();
_qlockinit(_threadrendezvous); _qlockinit(_threadrendezvous);

View file

@ -59,6 +59,7 @@ asm-OpenBSD-386.$O: asm-FreeBSD-386.s
# sorry # sorry
VG=`test -d /home/rsc/pub/valgrind-debian && echo -DUSEVALGRIND` VG=`test -d /home/rsc/pub/valgrind-debian && echo -DUSEVALGRIND`
# VG=
CFLAGS=$CFLAGS $VG CFLAGS=$CFLAGS $VG

View file

@ -171,7 +171,7 @@ needstack(int howmuch)
} }
} }
void int
_sched(void) _sched(void)
{ {
Proc *p; Proc *p;
@ -186,8 +186,8 @@ Resched:
// psstate(t->state), &t->sched, &p->sched); // psstate(t->state), &t->sched, &p->sched);
if(_setlabel(&t->sched)==0) if(_setlabel(&t->sched)==0)
_gotolabel(&p->sched); _gotolabel(&p->sched);
_threadstacklimit(t->stk); _threadstacklimit(t->stk, t->stk+t->stksize);
return; return p->nsched++;
}else{ }else{
t = runthread(p); t = runthread(p);
if(t == nil){ if(t == nil){
@ -277,10 +277,15 @@ _threadidle(void)
unlock(&p->readylock); unlock(&p->readylock);
} }
void int
yield(void) yield(void)
{ {
_sched(); Proc *p;
int nsched;
p = _threadgetproc();
nsched = p->nsched;
return _sched() - nsched;
} }
void void

View file

@ -46,7 +46,7 @@ _threadinswitch(int enter)
} }
void void
_threadstacklimit(void *addr) _threadstacklimit(void *addr, void *addr2)
{ {
USED(addr); USED(addr);
} }

View file

@ -99,6 +99,7 @@ struct Thread
ulong userpc; ulong userpc;
void* udata[NPRIV]; /* User per-thread data pointer */ void* udata[NPRIV]; /* User per-thread data pointer */
int lastfd;
}; };
struct Execargs struct Execargs
@ -143,6 +144,7 @@ struct Proc
Waitmsg *waitmsg; Waitmsg *waitmsg;
void* udata; /* User per-proc data pointer */ void* udata; /* User per-proc data pointer */
int nsched;
}; };
struct Pqueue { /* Proc queue */ struct Pqueue { /* Proc queue */
@ -169,7 +171,7 @@ void _freeproc(Proc*);
Proc* _newproc(void(*)(void*), void*, uint, char*, int, int); Proc* _newproc(void(*)(void*), void*, uint, char*, int, int);
int _procsplhi(void); int _procsplhi(void);
void _procsplx(int); void _procsplx(int);
void _sched(void); int _sched(void);
int _schedexec(Execargs*); int _schedexec(Execargs*);
void _schedexecwait(void); void _schedexecwait(void);
void _schedexit(Proc*); void _schedexit(Proc*);
@ -219,4 +221,4 @@ extern int _threadgetpid(void);
extern void _threadmemset(void*, int, int); extern void _threadmemset(void*, int, int);
extern void _threaddebugmemset(void*, int, int); extern void _threaddebugmemset(void*, int, int);
extern int _threadprocs; extern int _threadprocs;
extern void _threadstacklimit(void*); extern void _threadstacklimit(void*, void*);