From 5b37d9126474864b5299426e27b2af37fcc96dd0 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 30 Dec 2020 00:06:35 -0500 Subject: libthread: delete NetBSD special case I added a direct call from thread.c to pthread.c's _threadpthreadstart in May, and no one has complained about NetBSD being broken. So probably no one is using this on NetBSD at all. Make pthread the only option. --- src/libthread/NetBSD-386-asm.s | 7 - src/libthread/NetBSD-power-asm.s | 16 -- src/libthread/NetBSD.c | 437 --------------------------------------- src/libthread/sysofiles.sh | 9 +- 4 files changed, 4 insertions(+), 465 deletions(-) delete mode 100644 src/libthread/NetBSD-386-asm.s delete mode 100644 src/libthread/NetBSD-power-asm.s delete mode 100644 src/libthread/NetBSD.c (limited to 'src/libthread') diff --git a/src/libthread/NetBSD-386-asm.s b/src/libthread/NetBSD-386-asm.s deleted file mode 100644 index 197f12b5..00000000 --- a/src/libthread/NetBSD-386-asm.s +++ /dev/null @@ -1,7 +0,0 @@ -.globl _tas -_tas: - movl $0xCAFEBABE, %eax - movl 4(%esp), %ecx - xchgl %eax, 0(%ecx) - ret - diff --git a/src/libthread/NetBSD-power-asm.s b/src/libthread/NetBSD-power-asm.s deleted file mode 100644 index d6e21c15..00000000 --- a/src/libthread/NetBSD-power-asm.s +++ /dev/null @@ -1,16 +0,0 @@ - .globl _tas -_tas: - li %r0, 0 - mr %r4, %r3 - lis %r5, 0xcafe - ori %r5, %r5, 0xbabe -1: - lwarx %r3, %r0, %r4 - cmpwi %r3, 0 - bne 2f - stwcx. %r5, %r0, %r4 - bne- 1b -2: - sync - blr - diff --git a/src/libthread/NetBSD.c b/src/libthread/NetBSD.c deleted file mode 100644 index 2b14146b..00000000 --- a/src/libthread/NetBSD.c +++ /dev/null @@ -1,437 +0,0 @@ -#include "threadimpl.h" - -#undef exits -#undef _exits - -static int -timefmt(Fmt *fmt) -{ - static char *mon[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", - "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; - vlong ns; - Tm tm; - ns = nsec(); - tm = *localtime(time(0)); - return fmtprint(fmt, "%s %2d %02d:%02d:%02d.%03d", - mon[tm.mon], tm.mday, tm.hour, tm.min, tm.sec, - (int)(ns%1000000000)/1000000); -} - -/* - * spin locks - */ -extern int _tas(int*); - -void -_threadunlock(Lock *l, ulong pc) -{ - USED(pc); - - l->held = 0; -} - -int -_threadlock(Lock *l, int block, ulong pc) -{ - int i; -static int first=1; -if(first) {first=0; fmtinstall('\001', timefmt);} - - USED(pc); - - /* once fast */ - if(!_tas(&l->held)) - return 1; - if(!block) - return 0; - - /* a thousand times pretty fast */ - for(i=0; i<1000; i++){ - if(!_tas(&l->held)) - return 1; - sched_yield(); - } - /* now increasingly slow */ - for(i=0; i<10; i++){ - if(!_tas(&l->held)) - return 1; - usleep(1); - } -fprint(2, "%\001 %s: lock loop1 %p from %lux\n", argv0, l, pc); - for(i=0; i<10; i++){ - if(!_tas(&l->held)) - return 1; - usleep(10); - } -fprint(2, "%\001 %s: lock loop2 %p from %lux\n", argv0, l, pc); - for(i=0; i<10; i++){ - if(!_tas(&l->held)) - return 1; - usleep(100); - } -fprint(2, "%\001 %s: lock loop3 %p from %lux\n", argv0, l, pc); - for(i=0; i<10; i++){ - if(!_tas(&l->held)) - return 1; - usleep(1000); - } -fprint(2, "%\001 %s: lock loop4 %p from %lux\n", argv0, l, pc); - for(i=0; i<10; i++){ - if(!_tas(&l->held)) - return 1; - usleep(10*1000); - } -fprint(2, "%\001 %s: lock loop5 %p from %lux\n", argv0, l, pc); - for(i=0; i<1000; i++){ - if(!_tas(&l->held)) - return 1; - usleep(100*1000); - } -fprint(2, "%\001 %s: lock loop6 %p from %lux\n", argv0, l, pc); - /* take your time */ - while(_tas(&l->held)) - usleep(1000*1000); - return 1; -} - -/* - * sleep and wakeup - */ -static void -ign(int x) -{ - USED(x); -} - -static void /*__attribute__((constructor))*/ -ignusr1(int restart) -{ - struct sigaction sa; - - memset(&sa, 0, sizeof sa); - sa.sa_handler = ign; - sigemptyset(&sa.sa_mask); - sigaddset(&sa.sa_mask, SIGUSR1); - if(restart) - sa.sa_flags = SA_RESTART; - sigaction(SIGUSR1, &sa, nil); -} - -void -_procsleep(_Procrendez *r) -{ - sigset_t mask; - - /* - * Go to sleep. - * - * Block USR1, set the handler to interrupt system calls, - * unlock the vouslock so our waker can wake us, - * and then suspend. - */ -again: - r->asleep = 1; - r->pid = getpid(); - - sigprocmask(SIG_SETMASK, nil, &mask); - sigaddset(&mask, SIGUSR1); - sigprocmask(SIG_SETMASK, &mask, nil); - ignusr1(0); - unlock(r->l); - sigdelset(&mask, SIGUSR1); - sigsuspend(&mask); - - /* - * We're awake. Make USR1 not interrupt system calls. - */ - lock(r->l); - ignusr1(1); - if(r->asleep && r->pid == getpid()){ - /* Didn't really wake up - signal from something else */ - goto again; - } -} - -void -_procwakeupandunlock(_Procrendez *r) -{ - int pid; - - pid = 0; - if(r->asleep){ - r->asleep = 0; - assert(r->pid >= 1); - pid = r->pid; - } - assert(r->l); - unlock(r->l); - if(pid) - kill(pid, SIGUSR1); -} - -/* - * process creation and exit - */ -typedef struct Stackfree Stackfree; -struct Stackfree -{ - Stackfree *next; - int pid; - int pid1; -}; -static Lock stacklock; -static Stackfree *stackfree; - -static void -delayfreestack(uchar *stk, int pid, int pid1) -{ - Stackfree *sf; - - sf = (Stackfree*)stk; - sf->pid = pid; - sf->pid1 = pid1; - lock(&stacklock); - sf->next = stackfree; - stackfree = sf; - unlock(&stacklock); -} - -static void -dofreestacks(void) -{ - Stackfree *sf, *last, *next; - - if(stackfree==nil || !canlock(&stacklock)) - return; - - for(last=nil,sf=stackfree; sf; last=sf,sf=next){ - next = sf->next; - if(sf->pid >= 1 && kill(sf->pid, 0) < 0 && errno == ESRCH) - if(sf->pid1 >= 1 && kill(sf->pid1, 0) < 0 && errno == ESRCH){ - free(sf); - if(last) - last->next = next; - else - stackfree = next; - sf = last; - } - } - unlock(&stacklock); -} - -static int -startprocfn(void *v) -{ - void **a; - uchar *stk; - void (*fn)(void*); - Proc *p; - int pid0, pid1; - - a = (void**)v; - fn = a[0]; - p = a[1]; - stk = a[2]; - pid0 = (int)a[4]; - pid1 = getpid(); - free(a); - p->osprocid = pid1; - - (*fn)(p); - - delayfreestack(stk, pid0, pid1); - _exit(0); - return 0; -} - -/* - * indirect through here so that parent need not wait for child zombie - * - * slight race - if child exits and then another process starts before we - * manage to exit, we'll be running on a freed stack. - */ -static int -trampnowait(void *v) -{ - void **a; - int *kidpid; - - a = (void*)v; - kidpid = a[3]; - a[4] = (void*)getpid(); - *kidpid = clone(startprocfn, a[2]+65536-512, CLONE_VM|CLONE_FILES, a); - _exit(0); - return 0; -} - -void -_procstart(Proc *p, void (*fn)(Proc*)) -{ - void **a; - uchar *stk; - int pid, kidpid, status; - - dofreestacks(); - a = malloc(5*sizeof a[0]); - if(a == nil) - sysfatal("_procstart malloc: %r"); - stk = malloc(65536); - if(stk == nil) - sysfatal("_procstart malloc stack: %r"); - - a[0] = fn; - a[1] = p; - a[2] = stk; - a[3] = &kidpid; - kidpid = -1; - - pid = clone(trampnowait, stk+65536-16, CLONE_VM|CLONE_FILES, a); - if(pid > 0) - if(wait4(pid, &status, __WALL, 0) < 0) - fprint(2, "ffork wait4: %r\n"); - if(pid < 0 || kidpid < 0){ - fprint(2, "_procstart clone: %r\n"); - abort(); - } -} - -static char *threadexitsmsg; -void -sigusr2handler(int s) -{ -/* fprint(2, "%d usr2 %d\n", time(0), getpid()); */ - if(threadexitsmsg) - _exits(threadexitsmsg); -} - -void -threadexitsall(char *msg) -{ - static int pid[1024]; - int i, npid, mypid; - Proc *p; - - if(msg == nil) - msg = ""; - - /* - * Only one guy, ever, gets to run this. - * If two guys do it, inevitably they end up - * tripping over each other in the underlying - * C library exit() implementation, which is - * trying to run the atexit handlers and apparently - * not thread safe. This has been observed on - * both Linux and OpenBSD. Sigh. - */ - { - static Lock onelock; - if(!canlock(&onelock)) - _exits(threadexitsmsg); - threadexitsmsg = msg; - } - - mypid = getpid(); - lock(&_threadprocslock); - npid = 0; - for(p=_threadprocs; p; p=p->next) - if(p->osprocid != mypid && p->osprocid >= 1) - pid[npid++] = p->osprocid; - for(i=0; ipid == pid) - return p; - if(p->pid == 0){ - print("found 0 at %d (h=%d)\n", (i+h)%nelem(perproc), h); - break; - } - } - fprint(2, "myperproc %d (%s): cannot find self\n", pid, argv0); - abort(); - return nil; -} - -static Perproc* -newperproc(void) -{ - int i, pid, h; - Perproc *p; - - lock(&perlock); - pid = getpid(); - h = pid%nelem(perproc); - for(i=0; ipid == pid || p->pid == -1 || p->pid == 0){ - p->pid = pid; - unlock(&perlock); - return p; - } - } - fprint(2, "newperproc %d: out of procs\n", pid); - abort(); - return nil; -} - -Proc* -_threadproc(void) -{ - return myperproc()->proc; -} - -void -_threadsetproc(Proc *p) -{ - Perproc *pp; - - if(p) - p->osprocid = getpid(); - pp = newperproc(); - pp->proc = p; - if(p == nil) - pp->pid = -1; -} - -void -_pthreadinit(void) -{ - signal(SIGUSR2, sigusr2handler); -} - -void -_threadpexit(void) -{ - _exit(0); -} diff --git a/src/libthread/sysofiles.sh b/src/libthread/sysofiles.sh index 833afbe0..cf9e0234 100644 --- a/src/libthread/sysofiles.sh +++ b/src/libthread/sysofiles.sh @@ -2,15 +2,14 @@ test -f $PLAN9/config && . $PLAN9/config +echo pthread.o + case "$SYSNAME" in -NetBSD) - echo ${SYSNAME}-${OBJTYPE}-asm.o $SYSNAME.o stkmalloc.o - ;; OpenBSD) - echo pthread.o stkmmap.o + echo stkmmap.o ;; *) - echo pthread.o stkmalloc.o + echo stkmalloc.o esac # Various libc don't supply swapcontext, makecontext, so we do. -- cgit v1.2.3 From b3a20a96eb2b91a5b0b8a8fb506e20a2fb50ebe8 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 30 Dec 2020 00:10:11 -0500 Subject: libthread: add threadmaybackground Programs that want to background themselves now need to define threadmaybackground returning 1. This avoids a confusing (to people and debuggers) extra parent process for all the threaded programs that will never want to background themselves. --- src/libthread/bg.c | 7 +++++++ src/libthread/daemonize.c | 12 ++++++------ src/libthread/mkfile | 1 + src/libthread/thread.c | 2 +- 4 files changed, 15 insertions(+), 7 deletions(-) create mode 100644 src/libthread/bg.c (limited to 'src/libthread') diff --git a/src/libthread/bg.c b/src/libthread/bg.c new file mode 100644 index 00000000..2edbc0e4 --- /dev/null +++ b/src/libthread/bg.c @@ -0,0 +1,7 @@ +#include "threadimpl.h" + +int +threadmaybackground(void) +{ + return 0; +} diff --git a/src/libthread/daemonize.c b/src/libthread/daemonize.c index 387d1527..f994ffe1 100644 --- a/src/libthread/daemonize.c +++ b/src/libthread/daemonize.c @@ -8,7 +8,7 @@ #undef wait static int sigpid; -static int threadpassfd; +static int threadpassfd = -1; static int gotsigchld; static void @@ -163,9 +163,9 @@ _threadsetupdaemonize(void) void _threaddaemonize(void) { - if(threadpassfd >= 0){ - write(threadpassfd, "0", 1); - close(threadpassfd); - threadpassfd = -1; - } + if(threadpassfd < 0) + sysfatal("threads in main proc exited w/o threadmaybackground"); + write(threadpassfd, "0", 1); + close(threadpassfd); + threadpassfd = -1; } diff --git a/src/libthread/mkfile b/src/libthread/mkfile index 8a77a316..eca4f4df 100644 --- a/src/libthread/mkfile +++ b/src/libthread/mkfile @@ -4,6 +4,7 @@ SYSOFILES=`{sh ./sysofiles.sh} LIB=libthread.a OFILES=\ $SYSOFILES\ + bg.$O\ channel.$O\ daemonize.$O\ exec.$O\ diff --git a/src/libthread/thread.c b/src/libthread/thread.c index 65e65194..7151e875 100644 --- a/src/libthread/thread.c +++ b/src/libthread/thread.c @@ -844,7 +844,7 @@ main(int argc, char **argv) // Easier to just run in pthread-per-thread mode. pthreadperthread = 1; #endif - if(strstr(opts, "nodaemon") == nil && getenv("NOLIBTHREADDAEMONIZE") == nil) + if(threadmaybackground() && strstr(opts, "nodaemon") == nil && getenv("NOLIBTHREADDAEMONIZE") == nil) _threadsetupdaemonize(); threadargc = argc; -- cgit v1.2.3 From b73633b1b4e9d3dbd680edf900b2b53befbf5a9a Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 30 Dec 2020 00:15:37 -0500 Subject: libthread: fix pthreadperthread bugs --- src/libthread/thread.c | 293 ++++++++++++++++++++++++++++----------------- src/libthread/threadimpl.h | 1 + 2 files changed, 187 insertions(+), 107 deletions(-) (limited to 'src/libthread') diff --git a/src/libthread/thread.c b/src/libthread/thread.c index 7151e875..94173ebc 100644 --- a/src/libthread/thread.c +++ b/src/libthread/thread.c @@ -1,6 +1,6 @@ #include "threadimpl.h" -int _threaddebuglevel; +int _threaddebuglevel = 0; static uint threadnproc; static uint threadnsysproc; @@ -20,13 +20,16 @@ static void contextswitch(Context *from, Context *to); static void procmain(Proc*); static void procscheduler(Proc*); static int threadinfo(void*, char*); +static void pthreadscheduler(Proc *p); +static void pthreadsleepschedlocked(Proc *p, _Thread *t); +static void pthreadwakeupschedlocked(Proc *p, _Thread *self, _Thread *t); +static _Thread* procnext(Proc*, _Thread*); static void -_threaddebug(char *fmt, ...) +_threaddebug(_Thread *t, char *fmt, ...) { va_list arg; char buf[128]; - _Thread *t; char *p; static int fd = -1; @@ -52,7 +55,8 @@ _threaddebug(char *fmt, ...) va_start(arg, fmt); vsnprint(buf, sizeof buf, fmt, arg); va_end(arg); - t = proc()->thread; + if(t == nil) + t = proc()->thread; if(t) fprint(fd, "%p %d.%d: %s\n", proc(), getpid(), t->id, buf); else @@ -181,10 +185,15 @@ _threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack) stack = 0; // not using it t = threadalloc(fn, arg, stack); t->proc = p; - if(p->nthread == 0) - p->thread0 = t; - else if(pthreadperthread) - _threadpthreadstart(p, t); + if(pthreadperthread) { + if(p->nthread != 0) + _threadpthreadstart(p, t); + else + t->mainthread = 1; + } else { + if(p->nthread == 0) + p->thread0 = t; + } p->nthread++; addthreadinproc(p, t); _threadready(t); @@ -197,6 +206,7 @@ threadcreate(void (*fn)(void*), void *arg, uint stack) _Thread *t; t = _threadcreate(proc(), fn, arg, stack); + _threaddebug(nil, "threadcreate %d", t->id); return t->id; } @@ -210,41 +220,11 @@ proccreate(void (*fn)(void*), void *arg, uint stack) p = procalloc(); t = _threadcreate(p, fn, arg, stack); id = t->id; /* t might be freed after _procstart */ + _threaddebug(t, "proccreate %p", p); _procstart(p, procmain); return id; } -// For pthreadperthread mode, procswitch flips -// between the threads. -static void -procswitch(Proc *p, _Thread *from, _Thread *to) -{ - _threaddebug("procswitch %p %d %d", p, from?from->id:-1, to?to->id:-1); - lock(&p->schedlock); - from->schedrend.l = &p->schedlock; - if(to) { - p->schedthread = to; - to->schedrend.l = &p->schedlock; - _threaddebug("procswitch wakeup %p %d", p, to->id); - _procwakeup(&to->schedrend); - } - if(p->schedthread != from) { - if(from->exiting) { - unlock(&p->schedlock); - _threadpexit(); - _threaddebug("procswitch exit wakeup!!!\n"); - } - while(p->schedthread != from) { - _threaddebug("procswitch sleep %p %d", p, from->id); - _procsleep(&from->schedrend); - _threaddebug("procswitch awake %p %d", p, from->id); - } - if(p->schedthread != from) - sysfatal("_procswitch %p %p oops", p->schedthread, from); - } - unlock(&p->schedlock); -} - void _threadswitch(void) { @@ -255,10 +235,10 @@ _threadswitch(void) /*print("threadswtch %p\n", p); */ - if(p->thread == p->thread0) + if(pthreadperthread) + pthreadscheduler(p); + else if(p->thread == p->thread0) procscheduler(p); - else if(pthreadperthread) - procswitch(p, p->thread, p->thread0); else contextswitch(&p->thread->context, &p->schedcontext); } @@ -390,7 +370,10 @@ void _threadpthreadmain(Proc *p, _Thread *t) { _threadsetproc(p); - procswitch(p, t, nil); + lock(&p->lock); + pthreadsleepschedlocked(p, t); + unlock(&p->lock); + _threaddebug(nil, "startfn"); t->startfn(t->startarg); threadexits(nil); } @@ -400,76 +383,47 @@ procscheduler(Proc *p) { _Thread *t; - _threaddebug("scheduler enter"); + _threaddebug(nil, "scheduler enter"); //print("s %p\n", p); -Top: - lock(&p->lock); - t = p->thread; - p->thread = nil; - if(t->exiting){ - delthreadinproc(p, t); - p->nthread--; -/*print("nthread %d\n", p->nthread); */ - _threadstkfree(t->stk, t->stksize); - /* - * Cannot free p->thread0 yet: it is used for the - * context switches back to the scheduler. - * Instead, we will free it at the end of this function. - * But all the other threads can be freed now. - */ - if(t != p->thread0) - free(t); - } - - for(;;){ - if((t = p->pinthread) != nil){ - while(!onlist(&p->runqueue, t)){ - p->runrend.l = &p->lock; - _threaddebug("scheduler sleep (pin)"); - _procsleep(&p->runrend); - _threaddebug("scheduler wake (pin)"); - } - }else - while((t = p->runqueue.head) == nil){ - if(p->nthread == 0) - goto Out; - if((t = p->idlequeue.head) != nil){ - /* - * Run all the idling threads once. - */ - while((t = p->idlequeue.head) != nil){ - delthread(&p->idlequeue, t); - addthread(&p->runqueue, t); - } - continue; - } - p->runrend.l = &p->lock; - _threaddebug("scheduler sleep"); - _procsleep(&p->runrend); - _threaddebug("scheduler wake"); + for(;;) { + /* Finish running current thread. */ + lock(&p->lock); + t = p->thread; + p->thread = nil; + if(t->exiting){ + delthreadinproc(p, t); + p->nthread--; + /*print("nthread %d\n", p->nthread); */ + _threadstkfree(t->stk, t->stksize); + /* + * Cannot free p->thread0 yet: it is used for the + * context switches back to the scheduler. + * Instead, we will free it at the end of this function. + * But all the other threads can be freed now. + */ + if(t != p->thread0) + free(t); } - if(p->pinthread && p->pinthread != t) - fprint(2, "p->pinthread %p t %p\n", p->pinthread, t); - assert(p->pinthread == nil || p->pinthread == t); - delthread(&p->runqueue, t); + + /* Pick next thread. */ + t = procnext(p, nil); + if(t == nil) + break; + _threaddebug(nil, "run %d (%s)", t->id, t->name); + //print("run %p %p %p %p\n", t, *(uintptr*)(t->context.uc.mc.sp), t->context.uc.mc.di, t->context.uc.mc.si); unlock(&p->lock); - p->thread = t; - p->nswitch++; - _threaddebug("run %d (%s)", t->id, t->name); -//print("run %p %p %p %p\n", t, *(uintptr*)(t->context.uc.mc.sp), t->context.uc.mc.di, t->context.uc.mc.si); + + /* Switch to next thread. */ if(t == p->thread0) return; - if(pthreadperthread) - procswitch(p, p->thread0, t); - else - contextswitch(&p->schedcontext, &t->context); - _threaddebug("back in scheduler"); -/*print("back in scheduler\n"); */ - goto Top; + contextswitch(&p->schedcontext, &t->context); + + _threaddebug(nil, "back in scheduler"); + /*print("back in scheduler\n"); */ } -Out: - _threaddebug("scheduler exit"); + /* No more threads in proc. Clean up. */ + _threaddebug(nil, "scheduler exit"); if(p->mainproc){ /* * Stupid bug - on Linux 2.6 and maybe elsewhere, @@ -502,6 +456,125 @@ Out: _threadpexit(); } +static void +pthreadsleepschedlocked(Proc *p, _Thread *t) +{ + _threaddebug(t, "pthreadsleepsched %p %d", p, t->id);; + t->schedrend.l = &p->lock; + while(p->schedthread != t) + _procsleep(&t->schedrend); +} + +static void +pthreadwakeupschedlocked(Proc *p, _Thread *self, _Thread *t) +{ + _threaddebug(self, "pthreadwakeupschedlocked %p %d", p, t->id);; + t->schedrend.l = &p->schedlock; + p->schedthread = t; + _procwakeup(&t->schedrend); +} + +static void +pthreadscheduler(Proc *p) +{ + _Thread *self, *t; + + _threaddebug(nil, "scheduler"); + lock(&p->lock); + self = p->thread; + p->thread = nil; + _threaddebug(self, "pausing"); + + if(self->exiting) { + _threaddebug(self, "exiting"); + delthreadinproc(p, self); + p->nthread--; + } + + t = procnext(p, self); + if(t != nil) { + pthreadwakeupschedlocked(p, self, t); + if(!self->exiting) { + pthreadsleepschedlocked(p, self); + _threaddebug(nil, "resume %d", self->id); + unlock(&p->lock); + return; + } + } + + if(t == nil) { + /* Tear down proc bookkeeping. Wait to free p. */ + delproc(p); + lock(&threadnproclock); + if(p->sysproc) + --threadnsysproc; + if(--threadnproc == threadnsysproc) + threadexitsall(p->msg); + unlock(&threadnproclock); + } + + /* Tear down pthread. */ + if(self->mainthread && p->mainproc) { + _threaddaemonize(); + _threaddebug(self, "sleeper"); + unlock(&p->lock); + /* + * Avoid bugs with main pthread exiting. + * When all procs are gone, threadexitsall above will happen. + */ + for(;;) + sleep(60*60*1000); + } + _threadsetproc(nil); + free(self); + unlock(&p->lock); + if(t == nil) + free(p); + _threadpexit(); +} + +static _Thread* +procnext(Proc *p, _Thread *self) +{ + _Thread *t; + + if((t = p->pinthread) != nil){ + while(!onlist(&p->runqueue, t)){ + p->runrend.l = &p->lock; + _threaddebug(self, "scheduler sleep (pin)"); + _procsleep(&p->runrend); + _threaddebug(self, "scheduler wake (pin)"); + } + } else + while((t = p->runqueue.head) == nil){ + if(p->nthread == 0) + return nil; + if((t = p->idlequeue.head) != nil){ + /* + * Run all the idling threads once. + */ + while((t = p->idlequeue.head) != nil){ + delthread(&p->idlequeue, t); + addthread(&p->runqueue, t); + } + continue; + } + p->runrend.l = &p->lock; + _threaddebug(self, "scheduler sleep"); + _procsleep(&p->runrend); + _threaddebug(self, "scheduler wake"); + } + + if(p->pinthread && p->pinthread != t) + fprint(2, "p->pinthread %p t %p\n", p->pinthread, t); + assert(p->pinthread == nil || p->pinthread == t); + delthread(&p->runqueue, t); + + p->thread = t; + p->nswitch++; + return t; +} + void _threadsetsysproc(void) { @@ -784,14 +857,18 @@ threadrwakeup(Rendez *r, int all, ulong pc) int i; _Thread *t; + _threaddebug(nil, "rwakeup %p %d", r, all); for(i=0;; i++){ if(i==1 && !all) break; if((t = r->waiting.head) == nil) break; + _threaddebug(nil, "rwakeup %p %d -> wake %d", r, all, t->id); delthread(&r->waiting, t); _threadready(t); + _threaddebug(nil, "rwakeup %p %d -> loop", r, all); } + _threaddebug(nil, "rwakeup %p %d -> total %d", r, all, i); return i; } @@ -827,6 +904,7 @@ int main(int argc, char **argv) { Proc *p; + _Thread *t; char *opts; argv0 = argv[0]; @@ -875,7 +953,8 @@ main(int argc, char **argv) if(mainstacksize == 0) mainstacksize = 256*1024; atnotify(threadinfo, 1); - _threadcreate(p, threadmainstart, nil, mainstacksize); + t = _threadcreate(p, threadmainstart, nil, mainstacksize); + t->mainthread = 1; procmain(p); sysfatal("procscheduler returned in threadmain!"); /* does not return */ diff --git a/src/libthread/threadimpl.h b/src/libthread/threadimpl.h index 8d22a161..14646031 100644 --- a/src/libthread/threadimpl.h +++ b/src/libthread/threadimpl.h @@ -97,6 +97,7 @@ struct _Thread uchar *stk; uint stksize; int exiting; + int mainthread; Proc *proc; char name[256]; char state[256]; -- cgit v1.2.3 From e68f07d46f5f168dc2076286627279540bf1f99e Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 30 Dec 2020 00:16:10 -0500 Subject: libthread: make pthreadperthread the default --- src/libthread/thread.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/libthread') diff --git a/src/libthread/thread.c b/src/libthread/thread.c index 94173ebc..0c764000 100644 --- a/src/libthread/thread.c +++ b/src/libthread/thread.c @@ -7,7 +7,7 @@ static uint threadnsysproc; static Lock threadnproclock; static Ref threadidref; static Proc *threadmainproc; -static int pthreadperthread; +static int pthreadperthread = 1; static void addproc(Proc*); static void delproc(Proc*); -- cgit v1.2.3 From 18571208068d5fe2f0bf7b4e980525a7f577c503 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 30 Dec 2020 07:18:30 -0500 Subject: libthread: simplify Now that everything uses pthreads and pthreadperthread, can delete various conditionals, all the custom context code, and so on. Also update documents. Fixes #355. --- src/libthread/386-ucontext.c | 22 -- src/libthread/386-ucontext.h | 119 --------- src/libthread/COPYING.SPARC64-CONTEXT | 458 ---------------------------------- src/libthread/COPYRIGHT | 9 - src/libthread/Linux-arm-asm.s | 41 --- src/libthread/Linux-sparc64-context.S | 135 ---------- src/libthread/OpenBSD-386-asm.s | 45 ---- src/libthread/OpenBSD-power-asm.S | 73 ------ src/libthread/OpenBSD-x86_64-asm.S | 44 ---- src/libthread/arm-ucontext.c | 24 -- src/libthread/mkfile | 3 +- src/libthread/power-ucontext.c | 26 -- src/libthread/power-ucontext.h | 36 --- src/libthread/sparc64-ucontext.c | 49 ---- src/libthread/stkmalloc.c | 13 - src/libthread/stkmmap.c | 25 -- src/libthread/sysofiles.sh | 27 -- src/libthread/thread.c | 217 +--------------- src/libthread/threadimpl.h | 44 ---- src/libthread/x86_64-ucontext.c | 28 --- src/libthread/x86_64-ucontext.h | 42 ---- 21 files changed, 11 insertions(+), 1469 deletions(-) delete mode 100644 src/libthread/386-ucontext.c delete mode 100644 src/libthread/386-ucontext.h delete mode 100644 src/libthread/COPYING.SPARC64-CONTEXT delete mode 100644 src/libthread/Linux-arm-asm.s delete mode 100644 src/libthread/Linux-sparc64-context.S delete mode 100644 src/libthread/OpenBSD-386-asm.s delete mode 100644 src/libthread/OpenBSD-power-asm.S delete mode 100644 src/libthread/OpenBSD-x86_64-asm.S delete mode 100644 src/libthread/arm-ucontext.c delete mode 100644 src/libthread/power-ucontext.c delete mode 100644 src/libthread/power-ucontext.h delete mode 100644 src/libthread/sparc64-ucontext.c delete mode 100644 src/libthread/stkmalloc.c delete mode 100644 src/libthread/stkmmap.c delete mode 100644 src/libthread/sysofiles.sh delete mode 100644 src/libthread/x86_64-ucontext.c delete mode 100644 src/libthread/x86_64-ucontext.h (limited to 'src/libthread') diff --git a/src/libthread/386-ucontext.c b/src/libthread/386-ucontext.c deleted file mode 100644 index 3afa9513..00000000 --- a/src/libthread/386-ucontext.c +++ /dev/null @@ -1,22 +0,0 @@ -#include "threadimpl.h" - -void -makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) -{ - int *sp; - - sp = USPALIGN(ucp, 4); - sp -= argc; - memmove(sp, &argc+1, argc*sizeof(int)); - *--sp = 0; /* return address */ - ucp->uc_mcontext.mc_eip = (long)func; - ucp->uc_mcontext.mc_esp = (int)sp; -} - -int -swapcontext(ucontext_t *oucp, ucontext_t *ucp) -{ - if(getcontext(oucp) == 0) - setcontext(ucp); - return 0; -} diff --git a/src/libthread/386-ucontext.h b/src/libthread/386-ucontext.h deleted file mode 100644 index b1ee81b3..00000000 --- a/src/libthread/386-ucontext.h +++ /dev/null @@ -1,119 +0,0 @@ -#define setcontext(u) setmcontext(&(u)->uc_mcontext) -#define getcontext(u) getmcontext(&(u)->uc_mcontext) -typedef struct mcontext mcontext_t; -typedef struct ucontext ucontext_t; - -extern int swapcontext(ucontext_t*, ucontext_t*); -extern void makecontext(ucontext_t*, void(*)(), int, ...); -extern int getmcontext(mcontext_t*); -extern void setmcontext(mcontext_t*); - -/*- - * Copyright (c) 1999 Marcel Moolenaar - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer - * in this position and unchanged. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD: src/sys/sys/ucontext.h,v 1.4 1999/10/11 20:33:17 luoqi Exp $ - */ - -/* #include */ - -/*- - * Copyright (c) 1999 Marcel Moolenaar - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer - * in this position and unchanged. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * 3. The name of the author may not be used to endorse or promote products - * derived from this software without specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR - * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. - * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, - * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF - * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - * - * $FreeBSD: src/sys/i386/include/ucontext.h,v 1.4 1999/10/11 20:33:09 luoqi Exp $ - */ - -struct mcontext { - /* - * The first 20 fields must match the definition of - * sigcontext. So that we can support sigcontext - * and ucontext_t at the same time. - */ - int mc_onstack; /* XXX - sigcontext compat. */ - int mc_gs; - int mc_fs; - int mc_es; - int mc_ds; - int mc_edi; - int mc_esi; - int mc_ebp; - int mc_isp; - int mc_ebx; - int mc_edx; - int mc_ecx; - int mc_eax; - int mc_trapno; - int mc_err; - int mc_eip; - int mc_cs; - int mc_eflags; - int mc_esp; /* machine state */ - int mc_ss; - - int mc_fpregs[28]; /* env87 + fpacc87 + u_long */ - int __spare__[17]; -}; - -struct ucontext { - /* - * Keep the order of the first two fields. Also, - * keep them the first two fields in the structure. - * This way we can have a union with struct - * sigcontext and ucontext_t. This allows us to - * support them both at the same time. - * note: the union is not defined, though. - */ - sigset_t uc_sigmask; - mcontext_t uc_mcontext; - - struct __ucontext *uc_link; - stack_t uc_stack; - int __spare__[8]; -}; diff --git a/src/libthread/COPYING.SPARC64-CONTEXT b/src/libthread/COPYING.SPARC64-CONTEXT deleted file mode 100644 index 3b204400..00000000 --- a/src/libthread/COPYING.SPARC64-CONTEXT +++ /dev/null @@ -1,458 +0,0 @@ - GNU LESSER GENERAL PUBLIC LICENSE - Version 2.1, February 1999 - - Copyright (C) 1991, 1999 Free Software Foundation, Inc. - 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - Everyone is permitted to copy and distribute verbatim copies - of this license document, but changing it is not allowed. - -[This is the first released version of the Lesser GPL. It also counts - as the successor of the GNU Library Public License, version 2, hence - the version number 2.1.] - - Preamble - - The licenses for most software are designed to take away your -freedom to share and change it. By contrast, the GNU General Public -Licenses are intended to guarantee your freedom to share and change -free software--to make sure the software is free for all its users. - - This license, the Lesser General Public License, applies to some -specially designated software packages--typically libraries--of the -Free Software Foundation and other authors who decide to use it. You -can use it too, but we suggest you first think carefully about whether -this license or the ordinary General Public License is the better -strategy to use in any particular case, based on the explanations below. - - When we speak of free software, we are referring to freedom of use, -not price. Our General Public Licenses are designed to make sure that -you have the freedom to distribute copies of free software (and charge -for this service if you wish); that you receive source code or can get -it if you want it; that you can change the software and use pieces of -it in new free programs; and that you are informed that you can do -these things. - - To protect your rights, we need to make restrictions that forbid -distributors to deny you these rights or to ask you to surrender these -rights. These restrictions translate to certain responsibilities for -you if you distribute copies of the library or if you modify it. - - For example, if you distribute copies of the library, whether gratis -or for a fee, you must give the recipients all the rights that we gave -you. You must make sure that they, too, receive or can get the source -code. If you link other code with the library, you must provide -complete object files to the recipients, so that they can relink them -with the library after making changes to the library and recompiling -it. And you must show them these terms so they know their rights. - - We protect your rights with a two-step method: (1) we copyright the -library, and (2) we offer you this license, which gives you legal -permission to copy, distribute and/or modify the library. - - To protect each distributor, we want to make it very clear that -there is no warranty for the free library. Also, if the library is -modified by someone else and passed on, the recipients should know -that what they have is not the original version, so that the original -author's reputation will not be affected by problems that might be -introduced by others. - - Finally, software patents pose a constant threat to the existence of -any free program. We wish to make sure that a company cannot -effectively restrict the users of a free program by obtaining a -restrictive license from a patent holder. Therefore, we insist that -any patent license obtained for a version of the library must be -consistent with the full freedom of use specified in this license. - - Most GNU software, including some libraries, is covered by the -ordinary GNU General Public License. This license, the GNU Lesser -General Public License, applies to certain designated libraries, and -is quite different from the ordinary General Public License. We use -this license for certain libraries in order to permit linking those -libraries into non-free programs. - - When a program is linked with a library, whether statically or using -a shared library, the combination of the two is legally speaking a -combined work, a derivative of the original library. The ordinary -General Public License therefore permits such linking only if the -entire combination fits its criteria of freedom. The Lesser General -Public License permits more lax criteria for linking other code with -the library. - - We call this license the "Lesser" General Public License because it -does Less to protect the user's freedom than the ordinary General -Public License. It also provides other free software developers Less -of an advantage over competing non-free programs. These disadvantages -are the reason we use the ordinary General Public License for many -libraries. However, the Lesser license provides advantages in certain -special circumstances. - - For example, on rare occasions, there may be a special need to -encourage the widest possible use of a certain library, so that it becomes -a de-facto standard. To achieve this, non-free programs must be -allowed to use the library. A more frequent case is that a free -library does the same job as widely used non-free libraries. In this -case, there is little to gain by limiting the free library to free -software only, so we use the Lesser General Public License. - - In other cases, permission to use a particular library in non-free -programs enables a greater number of people to use a large body of -free software. For example, permission to use the GNU C Library in -non-free programs enables many more people to use the whole GNU -operating system, as well as its variant, the GNU/Linux operating -system. - - Although the Lesser General Public License is Less protective of the -users' freedom, it does ensure that the user of a program that is -linked with the Library has the freedom and the wherewithal to run -that program using a modified version of the Library. - - The precise terms and conditions for copying, distribution and -modification follow. Pay close attention to the difference between a -"work based on the library" and a "work that uses the library". The -former contains code derived from the library, whereas the latter must -be combined with the library in order to run. - - GNU LESSER GENERAL PUBLIC LICENSE - TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION - - 0. This License Agreement applies to any software library or other -program which contains a notice placed by the copyright holder or -other authorized party saying it may be distributed under the terms of -this Lesser General Public License (also called "this License"). -Each licensee is addressed as "you". - - A "library" means a collection of software functions and/or data -prepared so as to be conveniently linked with application programs -(which use some of those functions and data) to form executables. - - The "Library", below, refers to any such software library or work -which has been distributed under these terms. A "work based on the -Library" means either the Library or any derivative work under -copyright law: that is to say, a work containing the Library or a -portion of it, either verbatim or with modifications and/or translated -straightforwardly into another language. (Hereinafter, translation is -included without limitation in the term "modification".) - - "Source code" for a work means the preferred form of the work for -making modifications to it. For a library, complete source code means -all the source code for all modules it contains, plus any associated -interface definition files, plus the scripts used to control compilation -and installation of the library. - - Activities other than copying, distribution and modification are not -covered by this License; they are outside its scope. The act of -running a program using the Library is not restricted, and output from -such a program is covered only if its contents constitute a work based -on the Library (independent of the use of the Library in a tool for -writing it). Whether that is true depends on what the Library does -and what the program that uses the Library does. - - 1. You may copy and distribute verbatim copies of the Library's -complete source code as you receive it, in any medium, provided that -you conspicuously and appropriately publish on each copy an -appropriate copyright notice and disclaimer of warranty; keep intact -all the notices that refer to this License and to the absence of any -warranty; and distribute a copy of this License along with the -Library. - - You may charge a fee for the physical act of transferring a copy, -and you may at your option offer warranty protection in exchange for a -fee. - - 2. You may modify your copy or copies of the Library or any portion -of it, thus forming a work based on the Library, and copy and -distribute such modifications or work under the terms of Section 1 -above, provided that you also meet all of these conditions: - - a) The modified work must itself be a software library. - - b) You must cause the files modified to carry prominent notices - stating that you changed the files and the date of any change. - - c) You must cause the whole of the work to be licensed at no - charge to all third parties under the terms of this License. - - d) If a facility in the modified Library refers to a function or a - table of data to be supplied by an application program that uses - the facility, other than as an argument passed when the facility - is invoked, then you must make a good faith effort to ensure that, - in the event an application does not supply such function or - table, the facility still operates, and performs whatever part of - its purpose remains meaningful. - - (For example, a function in a library to compute square roots has - a purpose that is entirely well-defined independent of the - application. Therefore, Subsection 2d requires that any - application-supplied function or table used by this function must - be optional: if the application does not supply it, the square - root function must still compute square roots.) - -These requirements apply to the modified work as a whole. If -identifiable sections of that work are not derived from the Library, -and can be reasonably considered independent and separate works in -themselves, then this License, and its terms, do not apply to those -sections when you distribute them as separate works. But when you -distribute the same sections as part of a whole which is a work based -on the Library, the distribution of the whole must be on the terms of -this License, whose permissions for other licensees extend to the -entire whole, and thus to each and every part regardless of who wrote -it. - -Thus, it is not the intent of this section to claim rights or contest -your rights to work written entirely by you; rather, the intent is to -exercise the right to control the distribution of derivative or -collective works based on the Library. - -In addition, mere aggregation of another work not based on the Library -with the Library (or with a work based on the Library) on a volume of -a storage or distribution medium does not bring the other work under -the scope of this License. - - 3. You may opt to apply the terms of the ordinary GNU General Public -License instead of this License to a given copy of the Library. To do -this, you must alter all the notices that refer to this License, so -that they refer to the ordinary GNU General Public License, version 2, -instead of to this License. (If a newer version than version 2 of the -ordinary GNU General Public License has appeared, then you can specify -that version instead if you wish.) Do not make any other change in -these notices. - - Once this change is made in a given copy, it is irreversible for -that copy, so the ordinary GNU General Public License applies to all -subsequent copies and derivative works made from that copy. - - This option is useful when you wish to copy part of the code of -the Library into a program that is not a library. - - 4. You may copy and distribute the Library (or a portion or -derivative of it, under Section 2) in object code or executable form -under the terms of Sections 1 and 2 above provided that you accompany -it with the complete corresponding machine-readable source code, which -must be distributed under the terms of Sections 1 and 2 above on a -medium customarily used for software interchange. - - If distribution of object code is made by offering access to copy -from a designated place, then offering equivalent access to copy the -source code from the same place satisfies the requirement to -distribute the source code, even though third parties are not -compelled to copy the source along with the object code. - - 5. A program that contains no derivative of any portion of the -Library, but is designed to work with the Library by being compiled or -linked with it, is called a "work that uses the Library". Such a -work, in isolation, is not a derivative work of the Library, and -therefore falls outside the scope of this License. - - However, linking a "work that uses the Library" with the Library -creates an executable that is a derivative of the Library (because it -contains portions of the Library), rather than a "work that uses the -library". The executable is therefore covered by this License. -Section 6 states terms for distribution of such executables. - - When a "work that uses the Library" uses material from a header file -that is part of the Library, the object code for the work may be a -derivative work of the Library even though the source code is not. -Whether this is true is especially significant if the work can be -linked without the Library, or if the work is itself a library. The -threshold for this to be true is not precisely defined by law. - - If such an object file uses only numerical parameters, data -structure layouts and accessors, and small macros and small inline -functions (ten lines or less in length), then the use of the object -file is unrestricted, regardless of whether it is legally a derivative -work. (Executables containing this object code plus portions of the -Library will still fall under Section 6.) - - Otherwise, if the work is a derivative of the Library, you may -distribute the object code for the work under the terms of Section 6. -Any executables containing that work also fall under Section 6, -whether or not they are linked directly with the Library itself. - - 6. As an exception to the Sections above, you may also combine or -link a "work that uses the Library" with the Library to produce a -work containing portions of the Library, and distribute that work -under terms of your choice, provided that the terms permit -modification of the work for the customer's own use and reverse -engineering for debugging such modifications. - - You must give prominent notice with each copy of the work that the -Library is used in it and that the Library and its use are covered by -this License. You must supply a copy of this License. If the work -during execution displays copyright notices, you must include the -copyright notice for the Library among them, as well as a reference -directing the user to the copy of this License. Also, you must do one -of these things: - - a) Accompany the work with the complete corresponding - machine-readable source code for the Library including whatever - changes were used in the work (which must be distributed under - Sections 1 and 2 above); and, if the work is an executable linked - with the Library, with the complete machine-readable "work that - uses the Library", as object code and/or source code, so that the - user can modify the Library and then relink to produce a modified - executable containing the modified Library. (It is understood - that the user who changes the contents of definitions files in the - Library will not necessarily be able to recompile the application - to use the modified definitions.) - - b) Use a suitable shared library mechanism for linking with the - Library. A suitable mechanism is one that (1) uses at run time a - copy of the library already present on the user's computer system, - rather than copying library functions into the executable, and (2) - will operate properly with a modified version of the library, if - the user installs one, as long as the modified version is - interface-compatible with the version that the work was made with. - - c) Accompany the work with a written offer, valid for at - least three years, to give the same user the materials - specified in Subsection 6a, above, for a charge no more - than the cost of performing this distribution. - - d) If distribution of the work is made by offering access to copy - from a designated place, offer equivalent access to copy the above - specified materials from the same place. - - e) Verify that the user has already received a copy of these - materials or that you have already sent this user a copy. - - For an executable, the required form of the "work that uses the -Library" must include any data and utility programs needed for -reproducing the executable from it. However, as a special exception, -the materials to be distributed need not include anything that is -normally distributed (in either source or binary form) with the major -components (compiler, kernel, and so on) of the operating system on -which the executable runs, unless that component itself accompanies -the executable. - - It may happen that this requirement contradicts the license -restrictions of other proprietary libraries that do not normally -accompany the operating system. Such a contradiction means you cannot -use both them and the Library together in an executable that you -distribute. - - 7. You may place library facilities that are a work based on the -Library side-by-side in a single library together with other library -facilities not covered by this License, and distribute such a combined -library, provided that the separate distribution of the work based on -the Library and of the other library facilities is otherwise -permitted, and provided that you do these two things: - - a) Accompany the combined library with a copy of the same work - based on the Library, uncombined with any other library - facilities. This must be distributed under the terms of the - Sections above. - - b) Give prominent notice with the combined library of the fact - that part of it is a work based on the Library, and explaining - where to find the accompanying uncombined form of the same work. - - 8. You may not copy, modify, sublicense, link with, or distribute -the Library except as expressly provided under this License. Any -attempt otherwise to copy, modify, sublicense, link with, or -distribute the Library is void, and will automatically terminate your -rights under this License. However, parties who have received copies, -or rights, from you under this License will not have their licenses -terminated so long as such parties remain in full compliance. - - 9. You are not required to accept this License, since you have not -signed it. However, nothing else grants you permission to modify or -distribute the Library or its derivative works. These actions are -prohibited by law if you do not accept this License. Therefore, by -modifying or distributing the Library (or any work based on the -Library), you indicate your acceptance of this License to do so, and -all its terms and conditions for copying, distributing or modifying -the Library or works based on it. - - 10. Each time you redistribute the Library (or any work based on the -Library), the recipient automatically receives a license from the -original licensor to copy, distribute, link with or modify the Library -subject to these terms and conditions. You may not impose any further -restrictions on the recipients' exercise of the rights granted herein. -You are not responsible for enforcing compliance by third parties with -this License. - - 11. If, as a consequence of a court judgment or allegation of patent -infringement or for any other reason (not limited to patent issues), -conditions are imposed on you (whether by court order, agreement or -otherwise) that contradict the conditions of this License, they do not -excuse you from the conditions of this License. If you cannot -distribute so as to satisfy simultaneously your obligations under this -License and any other pertinent obligations, then as a consequence you -may not distribute the Library at all. For example, if a patent -license would not permit royalty-free redistribution of the Library by -all those who receive copies directly or indirectly through you, then -the only way you could satisfy both it and this License would be to -refrain entirely from distribution of the Library. - -If any portion of this section is held invalid or unenforceable under any -particular circumstance, the balance of the section is intended to apply, -and the section as a whole is intended to apply in other circumstances. - -It is not the purpose of this section to induce you to infringe any -patents or other property right claims or to contest validity of any -such claims; this section has the sole purpose of protecting the -integrity of the free software distribution system which is -implemented by public license practices. Many people have made -generous contributions to the wide range of software distributed -through that system in reliance on consistent application of that -system; it is up to the author/donor to decide if he or she is willing -to distribute software through any other system and a licensee cannot -impose that choice. - -This section is intended to make thoroughly clear what is believed to -be a consequence of the rest of this License. - - 12. If the distribution and/or use of the Library is restricted in -certain countries either by patents or by copyrighted interfaces, the -original copyright holder who places the Library under this License may add -an explicit geographical distribution limitation excluding those countries, -so that distribution is permitted only in or among countries not thus -excluded. In such case, this License incorporates the limitation as if -written in the body of this License. - - 13. The Free Software Foundation may publish revised and/or new -versions of the Lesser General Public License from time to time. -Such new versions will be similar in spirit to the present version, -but may differ in detail to address new problems or concerns. - -Each version is given a distinguishing version number. If the Library -specifies a version number of this License which applies to it and -"any later version", you have the option of following the terms and -conditions either of that version or of any later version published by -the Free Software Foundation. If the Library does not specify a -license version number, you may choose any version ever published by -the Free Software Foundation. - - 14. If you wish to incorporate parts of the Library into other free -programs whose distribution conditions are incompatible with these, -write to the author to ask for permission. For software which is -copyrighted by the Free Software Foundation, write to the Free -Software Foundation; we sometimes make exceptions for this. Our -decision will be guided by the two goals of preserving the free status -of all derivatives of our free software and of promoting the sharing -and reuse of software generally. - - NO WARRANTY - - 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO -WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. -EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR -OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY -KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR -PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE -LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME -THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. - - 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN -WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY -AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU -FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR -CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE -LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING -RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A -FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF -SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH -DAMAGES. - - END OF TERMS AND CONDITIONS diff --git a/src/libthread/COPYRIGHT b/src/libthread/COPYRIGHT index d0820965..212b96bc 100644 --- a/src/libthread/COPYRIGHT +++ b/src/libthread/COPYRIGHT @@ -42,12 +42,3 @@ Contains parts of an earlier library that has: * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE. */ - -=== - -The above notices do *NOT* apply to Linux-sparc64-context.S -or to sparc64-ucontext.c. Those are functions from -the GNU C library and are provided for systems that use the GNU C -library but somehow are missing those functions. They are -distributed under the Lesser GPL; see COPYING.SPARC64-CONTEXT. - diff --git a/src/libthread/Linux-arm-asm.s b/src/libthread/Linux-arm-asm.s deleted file mode 100644 index 9bd54f8a..00000000 --- a/src/libthread/Linux-arm-asm.s +++ /dev/null @@ -1,41 +0,0 @@ -.globl mygetmcontext -mygetmcontext: - str r1, [r0,#4] - str r2, [r0,#8] - str r3, [r0,#12] - str r4, [r0,#16] - str r5, [r0,#20] - str r6, [r0,#24] - str r7, [r0,#28] - str r8, [r0,#32] - str r9, [r0,#36] - str r10, [r0,#40] - str r11, [r0,#44] - str r12, [r0,#48] - str r13, [r0,#52] - str r14, [r0,#56] - /* store 1 as r0-to-restore */ - mov r1, #1 - str r1, [r0] - /* return 0 */ - mov r0, #0 - mov pc, lr - -.globl mysetmcontext -mysetmcontext: - ldr r1, [r0,#4] - ldr r2, [r0,#8] - ldr r3, [r0,#12] - ldr r4, [r0,#16] - ldr r5, [r0,#20] - ldr r6, [r0,#24] - ldr r7, [r0,#28] - ldr r8, [r0,#32] - ldr r9, [r0,#36] - ldr r10, [r0,#40] - ldr r11, [r0,#44] - ldr r12, [r0,#48] - ldr r13, [r0,#52] - ldr r14, [r0,#56] - ldr r0, [r0] - mov pc, lr diff --git a/src/libthread/Linux-sparc64-context.S b/src/libthread/Linux-sparc64-context.S deleted file mode 100644 index 1cc38391..00000000 --- a/src/libthread/Linux-sparc64-context.S +++ /dev/null @@ -1,135 +0,0 @@ -/* Copyright (C) 2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Jakub Jelinek . - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -/* Constants shared between setcontext() and getcontext(). Don't - install this header file. */ - - -#define UC_LINK 0 -#define __UC_SIGMASK 16 -#define UC_M_PC 40 -#define UC_M_NPC 48 -#define UC_SIGMASK 536 -#define SIGMASK_WORDS 16 - -/* Copyright (C) 1997, 2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Richard Henderson (rth@tamu.edu). - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#define ENTRY(x) x: -#define END(x) - -/*#include "ucontext_i.h" (up above) */ - -/* int getcontext(ucontext_t *); */ - -ENTRY(getcontext) - - ldx [%o0 + UC_LINK], %o1 /* Preserve uc_link field, the - trap clears it. */ - ta 0x6e -1: - ldx [%o0 + UC_M_PC], %o2 - ldx [%o0 + UC_M_NPC], %o3 - ldx [%o0 + __UC_SIGMASK], %o4 - stx %o1, [%o0 + UC_LINK] - add %o2, 2f - 1b, %o2 - stx %o2, [%o0 + UC_M_PC] - add %o3, 2f - 1b, %o3 - stx %o3, [%o0 + UC_M_NPC] -#if SIGMASK_WORDS == 16 - stx %o4, [%o0 + UC_SIGMASK] - stx %g0, [%o0 + UC_SIGMASK + 8] - stx %g0, [%o0 + UC_SIGMASK + 16] - stx %g0, [%o0 + UC_SIGMASK + 24] - stx %g0, [%o0 + UC_SIGMASK + 32] - stx %g0, [%o0 + UC_SIGMASK + 40] - stx %g0, [%o0 + UC_SIGMASK + 48] - stx %g0, [%o0 + UC_SIGMASK + 56] - stx %g0, [%o0 + UC_SIGMASK + 64] - stx %g0, [%o0 + UC_SIGMASK + 72] - stx %g0, [%o0 + UC_SIGMASK + 80] - stx %g0, [%o0 + UC_SIGMASK + 88] - stx %g0, [%o0 + UC_SIGMASK + 96] - stx %g0, [%o0 + UC_SIGMASK + 104] - stx %g0, [%o0 + UC_SIGMASK + 112] - stx %g0, [%o0 + UC_SIGMASK + 120] -#else -# error Adjust getcontext -#endif -2: - retl - clr %o0 - -END(getcontext) - -/* Copyright (C) 1997, 2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Richard Henderson (rth@tamu.edu). - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -/* -#include -#include "ucontext_i.h" -*/ - -/* int setcontext(ucontext_t *ctx); */ -.weak setcontext -ENTRY(setcontext) - - mov 1, %o1 - -/* int __setcontext(ucontext_t *ctx, int restoremask); */ -ENTRY(__setcontext) - - ldx [%o0 + UC_SIGMASK], %o2 - stx %o2, [%o0 + __UC_SIGMASK] - ta 0x6f - -END(__setcontext) - diff --git a/src/libthread/OpenBSD-386-asm.s b/src/libthread/OpenBSD-386-asm.s deleted file mode 100644 index ed18d2f0..00000000 --- a/src/libthread/OpenBSD-386-asm.s +++ /dev/null @@ -1,45 +0,0 @@ -.globl getmcontext -getmcontext: - movl 4(%esp), %eax - - movl %fs, 8(%eax) - movl %es, 12(%eax) - movl %ds, 16(%eax) - movl %ss, 76(%eax) - movl %edi, 20(%eax) - movl %esi, 24(%eax) - movl %ebp, 28(%eax) - movl %ebx, 36(%eax) - movl %edx, 40(%eax) - movl %ecx, 44(%eax) - - movl $1, 48(%eax) /* %eax */ - movl (%esp), %ecx /* %eip */ - movl %ecx, 60(%eax) - leal 4(%esp), %ecx /* %esp */ - movl %ecx, 72(%eax) - - movl 44(%eax), %ecx /* restore %ecx */ - movl $0, %eax - ret - -.globl setmcontext -setmcontext: - movl 4(%esp), %eax - - movl 8(%eax), %fs - movl 12(%eax), %es - movl 16(%eax), %ds - movl 76(%eax), %ss - movl 20(%eax), %edi - movl 24(%eax), %esi - movl 28(%eax), %ebp - movl 36(%eax), %ebx - movl 40(%eax), %edx - movl 44(%eax), %ecx - - movl 72(%eax), %esp - pushl 60(%eax) /* new %eip */ - movl 48(%eax), %eax - ret - diff --git a/src/libthread/OpenBSD-power-asm.S b/src/libthread/OpenBSD-power-asm.S deleted file mode 100644 index 36035eb5..00000000 --- a/src/libthread/OpenBSD-power-asm.S +++ /dev/null @@ -1,73 +0,0 @@ -ENTRY(_getmcontext) /* xxx: instruction scheduling */ - mflr %r0 - mfcr %r5 - mfctr %r6 - mfxer %r7 - stw %r0, 0*4(%r3) - stw %r5, 1*4(%r3) - stw %r6, 2*4(%r3) - stw %r7, 3*4(%r3) - - stw %r1, 4*4(%r3) - stw %r2, 5*4(%r3) - li %r5, 1 /* return value for setmcontext */ - stw %r5, 6*4(%r3) - - stw %r13, (0+7)*4(%r3) /* callee-save GPRs */ - stw %r14, (1+7)*4(%r3) /* xxx: block move */ - stw %r15, (2+7)*4(%r3) - stw %r16, (3+7)*4(%r3) - stw %r17, (4+7)*4(%r3) - stw %r18, (5+7)*4(%r3) - stw %r19, (6+7)*4(%r3) - stw %r20, (7+7)*4(%r3) - stw %r21, (8+7)*4(%r3) - stw %r22, (9+7)*4(%r3) - stw %r23, (10+7)*4(%r3) - stw %r24, (11+7)*4(%r3) - stw %r25, (12+7)*4(%r3) - stw %r26, (13+7)*4(%r3) - stw %r27, (14+7)*4(%r3) - stw %r28, (15+7)*4(%r3) - stw %r29, (16+7)*4(%r3) - stw %r30, (17+7)*4(%r3) - stw %r31, (18+7)*4(%r3) - - li %r3, 0 /* return */ - blr - -ENTRY(_setmcontext) - lwz %r13, (0+7)*4(%r3) /* callee-save GPRs */ - lwz %r14, (1+7)*4(%r3) /* xxx: block move */ - lwz %r15, (2+7)*4(%r3) - lwz %r16, (3+7)*4(%r3) - lwz %r17, (4+7)*4(%r3) - lwz %r18, (5+7)*4(%r3) - lwz %r19, (6+7)*4(%r3) - lwz %r20, (7+7)*4(%r3) - lwz %r21, (8+7)*4(%r3) - lwz %r22, (9+7)*4(%r3) - lwz %r23, (10+7)*4(%r3) - lwz %r24, (11+7)*4(%r3) - lwz %r25, (12+7)*4(%r3) - lwz %r26, (13+7)*4(%r3) - lwz %r27, (14+7)*4(%r3) - lwz %r28, (15+7)*4(%r3) - lwz %r29, (16+7)*4(%r3) - lwz %r30, (17+7)*4(%r3) - lwz %r31, (18+7)*4(%r3) - - lwz %r1, 4*4(%r3) - lwz %r2, 5*4(%r3) - - lwz %r0, 0*4(%r3) - mtlr %r0 - lwz %r0, 1*4(%r3) - mtcr %r0 /* mtcrf 0xFF, %r0 */ - lwz %r0, 2*4(%r3) - mtctr %r0 - lwz %r0, 3*4(%r3) - mtxer %r0 - - lwz %r3, 6*4(%r3) - blr diff --git a/src/libthread/OpenBSD-x86_64-asm.S b/src/libthread/OpenBSD-x86_64-asm.S deleted file mode 100644 index e982cdef..00000000 --- a/src/libthread/OpenBSD-x86_64-asm.S +++ /dev/null @@ -1,44 +0,0 @@ -.text -.align 8 - -.globl libthread_getmcontext -libthread_getmcontext: - movq $1, 0*8(%rdi) // rax - movq %rbx, 1*8(%rdi) - movq %rcx, 2*8(%rdi) - movq %rdx, 3*8(%rdi) - movq %rsi, 4*8(%rdi) - movq %rdi, 5*8(%rdi) - movq %rbp, 6*8(%rdi) - movq %rsp, 7*8(%rdi) - movq %r8, 8*8(%rdi) - movq %r9, 9*8(%rdi) - movq %r10, 10*8(%rdi) - movq %r11, 11*8(%rdi) - movq %r12, 12*8(%rdi) - movq %r13, 13*8(%rdi) - movq %r14, 14*8(%rdi) - movq %r15, 15*8(%rdi) - movq $0, %rax - ret - -.globl libthread_setmcontext -libthread_setmcontext: - movq 0*8(%rdi), %rax - movq 1*8(%rdi), %rbx - movq 2*8(%rdi), %rcx - movq 3*8(%rdi), %rdx - movq 4*8(%rdi), %rsi - // %rdi later - movq 6*8(%rdi), %rbp - movq 7*8(%rdi), %rsp - movq 8*8(%rdi), %r8 - movq 9*8(%rdi), %r9 - movq 10*8(%rdi), %r10 - movq 11*8(%rdi), %r11 - movq 12*8(%rdi), %r12 - movq 13*8(%rdi), %r13 - movq 14*8(%rdi), %r14 - movq 15*8(%rdi), %r15 - movq 5*8(%rdi), %rdi - ret diff --git a/src/libthread/arm-ucontext.c b/src/libthread/arm-ucontext.c deleted file mode 100644 index 512ca973..00000000 --- a/src/libthread/arm-ucontext.c +++ /dev/null @@ -1,24 +0,0 @@ -#include "threadimpl.h" - -void -makecontext(ucontext_t *uc, void (*fn)(void), int argc, ...) -{ - int i, *sp; - va_list arg; - - sp = USPALIGN(uc, 4); - va_start(arg, argc); - for(i=0; i<4 && iuc_mcontext.arm_r0)[i] = va_arg(arg, uint); - va_end(arg); - uc->uc_mcontext.arm_sp = (uint)sp; - uc->uc_mcontext.arm_lr = (uint)fn; -} - -int -swapcontext(ucontext_t *oucp, const ucontext_t *ucp) -{ - if(getcontext(oucp) == 0) - setcontext(ucp); - return 0; -} diff --git a/src/libthread/mkfile b/src/libthread/mkfile index eca4f4df..40941f43 100644 --- a/src/libthread/mkfile +++ b/src/libthread/mkfile @@ -1,15 +1,14 @@ <$PLAN9/src/mkhdr -SYSOFILES=`{sh ./sysofiles.sh} LIB=libthread.a OFILES=\ - $SYSOFILES\ bg.$O\ channel.$O\ daemonize.$O\ exec.$O\ ioproc.$O\ iorw.$O\ + pthread.$O\ ref.$O\ thread.$O\ wait.$O\ diff --git a/src/libthread/power-ucontext.c b/src/libthread/power-ucontext.c deleted file mode 100644 index 32a8e931..00000000 --- a/src/libthread/power-ucontext.c +++ /dev/null @@ -1,26 +0,0 @@ -#include "threadimpl.h" - -void -makecontext(ucontext_t *ucp, void (*func)(void), int argc, ...) -{ - ulong *sp, *tos; - va_list arg; - - if(argc != 2) - sysfatal("libthread: makecontext misused"); - sp = USPALIGN(ucp, 16); - ucp->mc.pc = (long)func; - ucp->mc.sp = (long)sp; - va_start(arg, argc); - ucp->mc.r3 = va_arg(arg, long); - ucp->mc.r4 = va_arg(arg, long); - va_end(arg); -} - -int -swapcontext(ucontext_t *oucp, ucontext_t *ucp) -{ - if(getcontext(oucp) == 0) - setcontext(ucp); - return 0; -} diff --git a/src/libthread/power-ucontext.h b/src/libthread/power-ucontext.h deleted file mode 100644 index 1985d98d..00000000 --- a/src/libthread/power-ucontext.h +++ /dev/null @@ -1,36 +0,0 @@ -#define setcontext(u) _setmcontext(&(u)->mc) -#define getcontext(u) _getmcontext(&(u)->mc) -typedef struct mcontext mcontext_t; -typedef struct ucontext ucontext_t; -struct mcontext -{ - ulong pc; /* lr */ - ulong cr; /* mfcr */ - ulong ctr; /* mfcr */ - ulong xer; /* mfcr */ - ulong sp; /* callee saved: r1 */ - ulong toc; /* callee saved: r2 */ - ulong r3; /* first arg to function, return register: r3 */ - ulong gpr[19]; /* callee saved: r13-r31 */ -/* -// XXX: currently do not save vector registers or floating-point state -// ulong pad; -// uvlong fpr[18]; / * callee saved: f14-f31 * / -// ulong vr[4*12]; / * callee saved: v20-v31, 256-bits each * / -*/ -}; - -struct ucontext -{ - struct { - void *ss_sp; - uint ss_size; - } uc_stack; - sigset_t uc_sigmask; - mcontext_t mc; -}; - -void makecontext(ucontext_t*, void(*)(void), int, ...); -int swapcontext(ucontext_t*, ucontext_t*); -int _getmcontext(mcontext_t*); -void _setmcontext(mcontext_t*); diff --git a/src/libthread/sparc64-ucontext.c b/src/libthread/sparc64-ucontext.c deleted file mode 100644 index e4800c19..00000000 --- a/src/libthread/sparc64-ucontext.c +++ /dev/null @@ -1,49 +0,0 @@ -/* Copyright (C) 2001 Free Software Foundation, Inc. - This file is part of the GNU C Library. - Contributed by Jakub Jelinek . - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ - -#include - -#define UC_M_PC 40 -#define UC_M_NPC 48 - -extern int __getcontext (ucontext_t *ucp); -extern int __setcontext (const ucontext_t *ucp, int restoremask); - -int -swapcontext (ucontext_t *oucp, const ucontext_t *ucp) -{ - extern void __swapcontext_ret (void); - /* Save the current machine context to oucp. */ - __getcontext (oucp); - /* Modify oucp to skip the __setcontext call on reactivation. */ - *(long*)((char*)oucp+UC_M_PC) = (long)__swapcontext_ret; - *(long*)((char*)oucp+UC_M_NPC) = (long)__swapcontext_ret + 4; - /* Restore the machine context in ucp. */ - __setcontext (ucp, 1); - return 0; -} - -asm (" \n\ - .text \n\ - .type __swapcontext_ret, #function \n\ -__swapcontext_ret: \n\ - return %i7 + 8 \n\ - clr %o0 \n\ - .size __swapcontext_ret, .-__swapcontext_ret \n\ - "); diff --git a/src/libthread/stkmalloc.c b/src/libthread/stkmalloc.c deleted file mode 100644 index 083aea1b..00000000 --- a/src/libthread/stkmalloc.c +++ /dev/null @@ -1,13 +0,0 @@ -#include "threadimpl.h" - -void* -_threadstkalloc(int n) -{ - return malloc(n); -} - -void -_threadstkfree(void *v, int n) -{ - free(v); -} diff --git a/src/libthread/stkmmap.c b/src/libthread/stkmmap.c deleted file mode 100644 index f4a24630..00000000 --- a/src/libthread/stkmmap.c +++ /dev/null @@ -1,25 +0,0 @@ -#include -#include -#include "threadimpl.h" - -#ifndef MAP_STACK -#define MAP_STACK 0 -#endif - -void* -_threadstkalloc(int n) -{ - void *p; - - p = mmap(nil, n, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON|MAP_STACK, -1, 0); - if(p == (void*)-1) - return nil; - return p; -} - -void -_threadstkfree(void *v, int n) -{ - if(n > 0) - munmap(v, n); -} diff --git a/src/libthread/sysofiles.sh b/src/libthread/sysofiles.sh deleted file mode 100644 index cf9e0234..00000000 --- a/src/libthread/sysofiles.sh +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh - -test -f $PLAN9/config && . $PLAN9/config - -echo pthread.o - -case "$SYSNAME" in -OpenBSD) - echo stkmmap.o - ;; -*) - echo stkmalloc.o -esac - -# Various libc don't supply swapcontext, makecontext, so we do. -case "$SYSNAME-$OBJTYPE" in -Linux-arm | Linux-sparc64 | NetBSD-arm | OpenBSD-386 | OpenBSD-power | OpenBSD-x86_64) - echo $OBJTYPE-ucontext.o - ;; -esac - -# A few libc don't supply setcontext, getcontext, so we do. -case "$SYSNAME-$OBJTYPE" in -Linux-arm | Linux-sparc64 | OpenBSD-386 | OpenBSD-power | OpenBSD-x86_64) - echo $SYSNAME-$OBJTYPE-asm.o - ;; -esac diff --git a/src/libthread/thread.c b/src/libthread/thread.c index 0c764000..d72bf896 100644 --- a/src/libthread/thread.c +++ b/src/libthread/thread.c @@ -7,7 +7,6 @@ static uint threadnsysproc; static Lock threadnproclock; static Ref threadidref; static Proc *threadmainproc; -static int pthreadperthread = 1; static void addproc(Proc*); static void delproc(Proc*); @@ -16,9 +15,7 @@ static void delthread(_Threadlist*, _Thread*); static int onlist(_Threadlist*, _Thread*); static void addthreadinproc(Proc*, _Thread*); static void delthreadinproc(Proc*, _Thread*); -static void contextswitch(Context *from, Context *to); static void procmain(Proc*); -static void procscheduler(Proc*); static int threadinfo(void*, char*); static void pthreadscheduler(Proc *p); static void pthreadsleepschedlocked(Proc *p, _Thread *t); @@ -86,114 +83,24 @@ procalloc(void) return p; } -static void -threadstart(uint y, uint x) -{ - _Thread *t; - ulong z; - -//print("threadstart\n"); - z = (ulong)x << 16; /* hide undefined 32-bit shift from 32-bit compilers */ - z <<= 16; - z |= y; - t = (_Thread*)z; - -//print("threadstart sp=%p arg=%p startfn=%p t=%p\n", &t, t, t->startfn, t->startarg); - t->startfn(t->startarg); -/*print("threadexits %p\n", v); */ - threadexits(nil); -/*print("not reacehd\n"); */ -} - -static _Thread* -threadalloc(void (*fn)(void*), void *arg, uint stack) +_Thread* +_threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack) { _Thread *t; - sigset_t zero; - uint x, y; - ulong z; - /* allocate the task and stack together */ + USED(stack); t = malloc(sizeof *t); if(t == nil) - sysfatal("threadalloc malloc: %r"); + sysfatal("threadcreate malloc: %r"); memset(t, 0, sizeof *t); t->id = incref(&threadidref); -//print("fn=%p arg=%p\n", fn, arg); t->startfn = fn; t->startarg = arg; -//print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn); - - /* do a reasonable initialization */ - if(stack == 0) - return t; - t->stk = _threadstkalloc(stack); - if(t->stk == nil) - sysfatal("threadalloc malloc stack: %r"); - t->stksize = stack; - memset(&t->context.uc, 0, sizeof t->context.uc); - sigemptyset(&zero); - sigprocmask(SIG_BLOCK, &zero, &t->context.uc.uc_sigmask); -//print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn); - - /* must initialize with current context */ - if(getcontext(&t->context.uc) < 0) - sysfatal("threadalloc getcontext: %r"); -//print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn); - - /* - * Call makecontext to do the real work. - * To avoid various mistakes on other system software, - * debuggers, and so on, don't get too close to both - * ends of the stack. Just staying away is much easier - * than debugging everything (outside our control) - * that has off-by-one errors. - */ - t->context.uc.uc_stack.ss_sp = (void*)(t->stk+64); - t->context.uc.uc_stack.ss_size = t->stksize-2*64; -#if defined(__sun__) && !defined(__MAKECONTEXT_V2_SOURCE) /* sigh */ - /* can avoid this with __MAKECONTEXT_V2_SOURCE but only on SunOS 5.9 */ - t->context.uc.uc_stack.ss_sp = - (char*)t->context.uc.uc_stack.ss_sp - +t->context.uc.uc_stack.ss_size; -#endif - /* - * All this magic is because you have to pass makecontext a - * function that takes some number of word-sized variables, - * and on 64-bit machines pointers are bigger than words. - */ -//print("makecontext sp=%p t=%p startfn=%p\n", (char*)t->stk+t->stksize, t, t->startfn); - z = (ulong)t; - y = z; - z >>= 16; /* hide undefined 32-bit shift from 32-bit compilers */ - x = z>>16; - makecontext(&t->context.uc, (void(*)(void))threadstart, 2, y, x); - - return t; -} - -_Thread* -_threadcreate(Proc *p, void (*fn)(void*), void *arg, uint stack) -{ - _Thread *t; - - /* defend against bad C libraries */ - if(stack < (256<<10)) - stack = 256<<10; - - if(p->nthread == 0 || pthreadperthread) - stack = 0; // not using it - t = threadalloc(fn, arg, stack); t->proc = p; - if(pthreadperthread) { - if(p->nthread != 0) - _threadpthreadstart(p, t); - else - t->mainthread = 1; - } else { - if(p->nthread == 0) - p->thread0 = t; - } + if(p->nthread != 0) + _threadpthreadstart(p, t); + else + t->mainthread = 1; p->nthread++; addthreadinproc(p, t); _threadready(t); @@ -232,15 +139,8 @@ _threadswitch(void) needstack(0); p = proc(); - /*print("threadswtch %p\n", p); */ - - if(pthreadperthread) - pthreadscheduler(p); - else if(p->thread == p->thread0) - procscheduler(p); - else - contextswitch(&p->thread->context, &p->schedcontext); + pthreadscheduler(p); } void @@ -338,15 +238,6 @@ threadsysfatal(char *fmt, va_list arg) threadexitsall(buf); } -static void -contextswitch(Context *from, Context *to) -{ - if(swapcontext(&from->uc, &to->uc) < 0){ - fprint(2, "swapcontext failed: %r\n"); - assert(0); - } -} - static void procmain(Proc *p) { @@ -357,7 +248,6 @@ procmain(Proc *p) /* take out first thread to run on system stack */ t = p->runqueue.head; delthread(&p->runqueue, t); - memset(&t->context.uc, 0, sizeof t->context.uc); /* run it */ p->thread = t; @@ -378,84 +268,6 @@ _threadpthreadmain(Proc *p, _Thread *t) threadexits(nil); } -static void -procscheduler(Proc *p) -{ - _Thread *t; - - _threaddebug(nil, "scheduler enter"); -//print("s %p\n", p); - for(;;) { - /* Finish running current thread. */ - lock(&p->lock); - t = p->thread; - p->thread = nil; - if(t->exiting){ - delthreadinproc(p, t); - p->nthread--; - /*print("nthread %d\n", p->nthread); */ - _threadstkfree(t->stk, t->stksize); - /* - * Cannot free p->thread0 yet: it is used for the - * context switches back to the scheduler. - * Instead, we will free it at the end of this function. - * But all the other threads can be freed now. - */ - if(t != p->thread0) - free(t); - } - - /* Pick next thread. */ - t = procnext(p, nil); - if(t == nil) - break; - _threaddebug(nil, "run %d (%s)", t->id, t->name); - //print("run %p %p %p %p\n", t, *(uintptr*)(t->context.uc.mc.sp), t->context.uc.mc.di, t->context.uc.mc.si); - unlock(&p->lock); - - /* Switch to next thread. */ - if(t == p->thread0) - return; - contextswitch(&p->schedcontext, &t->context); - - _threaddebug(nil, "back in scheduler"); - /*print("back in scheduler\n"); */ - } - - /* No more threads in proc. Clean up. */ - _threaddebug(nil, "scheduler exit"); - if(p->mainproc){ - /* - * Stupid bug - on Linux 2.6 and maybe elsewhere, - * if the main thread exits then the others keep running - * but the process shows up as a zombie in ps and is not - * attachable with ptrace. We'll just sit around pretending - * to be a system proc instead of exiting. - */ - _threaddaemonize(); - lock(&threadnproclock); - if(++threadnsysproc == threadnproc) - threadexitsall(p->msg); - p->sysproc = 1; - unlock(&threadnproclock); - for(;;) - sleep(1000); - } - - delproc(p); - lock(&threadnproclock); - if(p->sysproc) - --threadnsysproc; - if(--threadnproc == threadnsysproc) - threadexitsall(p->msg); - unlock(&threadnproclock); - unlock(&p->lock); - _threadsetproc(nil); - free(p->thread0); - free(p); - _threadpexit(); -} - static void pthreadsleepschedlocked(Proc *p, _Thread *t) { @@ -913,15 +725,6 @@ main(int argc, char **argv) if(opts == nil) opts = ""; - pthreadperthread = (strstr(opts, "pthreadperthread") != nil); -#ifdef PLAN9PORT_ASAN - // ASAN can't deal with the coroutine stack switches. - // In theory it has support for informing it about stack switches, - // but even with those calls added it can't deal with things - // like fork or exit from a coroutine stack. - // Easier to just run in pthread-per-thread mode. - pthreadperthread = 1; -#endif if(threadmaybackground() && strstr(opts, "nodaemon") == nil && getenv("NOLIBTHREADDAEMONIZE") == nil) _threadsetupdaemonize(); @@ -956,7 +759,7 @@ main(int argc, char **argv) t = _threadcreate(p, threadmainstart, nil, mainstacksize); t->mainthread = 1; procmain(p); - sysfatal("procscheduler returned in threadmain!"); + sysfatal("procmain returned in libthread"); /* does not return */ return 0; } diff --git a/src/libthread/threadimpl.h b/src/libthread/threadimpl.h index 14646031..9eddba21 100644 --- a/src/libthread/threadimpl.h +++ b/src/libthread/threadimpl.h @@ -9,36 +9,11 @@ # if defined(__APPLE__) # define _XOPEN_SOURCE /* for Snow Leopard */ # endif -# include #endif #include #include "libc.h" #include "thread.h" -#if defined(__OpenBSD__) -# define mcontext libthread_mcontext -# define mcontext_t libthread_mcontext_t -# define ucontext libthread_ucontext -# define ucontext_t libthread_ucontext_t -# if defined __i386__ -# include "386-ucontext.h" -# elif defined __amd64__ -# include "x86_64-ucontext.h" -# else -# include "power-ucontext.h" -# endif -extern pid_t rfork_thread(int, void*, int(*)(void*), void*); -#endif - -#if defined(__arm__) -int mygetmcontext(ulong*); -void mysetmcontext(const ulong*); -#define setcontext(u) mysetmcontext(&(u)->uc_mcontext.arm_r0) -#define getcontext(u) mygetmcontext(&(u)->uc_mcontext.arm_r0) -#endif - - -typedef struct Context Context; typedef struct Execjob Execjob; typedef struct Proc Proc; typedef struct _Procrendez _Procrendez; @@ -54,11 +29,6 @@ enum STACK = 8192 }; -struct Context -{ - ucontext_t uc; -}; - struct Execjob { int *fd; @@ -72,11 +42,7 @@ struct _Procrendez { Lock *l; int asleep; -#ifdef PLAN9PORT_USING_PTHREADS pthread_cond_t cond; -#else - int pid; -#endif }; struct _Thread @@ -85,15 +51,10 @@ struct _Thread _Thread *prev; _Thread *allnext; _Thread *allprev; - Context context; void (*startfn)(void*); void *startarg; uint id; -#ifdef PLAN9PORT_USING_PTHREADS pthread_t osprocid; -#else - int osprocid; -#endif uchar *stk; uint stksize; int exiting; @@ -115,11 +76,7 @@ struct Proc Proc *next; Proc *prev; char msg[128]; -#ifdef PLAN9PORT_USING_PTHREADS pthread_t osprocid; -#else - int osprocid; -#endif Lock lock; int nswitch; _Thread *thread0; @@ -133,7 +90,6 @@ struct Proc _Procrendez runrend; Lock schedlock; _Thread *schedthread; - Context schedcontext; void *udata; Jmp sigjmp; int mainproc; diff --git a/src/libthread/x86_64-ucontext.c b/src/libthread/x86_64-ucontext.c deleted file mode 100644 index 5d1aaefc..00000000 --- a/src/libthread/x86_64-ucontext.c +++ /dev/null @@ -1,28 +0,0 @@ -#include "threadimpl.h" - -void -makecontext(ucontext_t *uc, void (*fn)(void), int argc, ...) -{ - uintptr *sp; - va_list arg; - - if(argc != 2) - sysfatal("libthread: makecontext misused"); - va_start(arg, argc); - uc->mc.di = va_arg(arg, uint); - uc->mc.si = va_arg(arg, uint); - va_end(arg); - - sp = USPALIGN(uc, 16); - *--sp = 0; // fn's return address - *--sp = (uintptr)fn; // return address of setcontext - uc->mc.sp = (uintptr)sp; -} - -int -swapcontext(ucontext_t *oucp, ucontext_t *ucp) -{ - if(getcontext(oucp) == 0) - setcontext(ucp); - return 0; -} diff --git a/src/libthread/x86_64-ucontext.h b/src/libthread/x86_64-ucontext.h deleted file mode 100644 index e0640761..00000000 --- a/src/libthread/x86_64-ucontext.h +++ /dev/null @@ -1,42 +0,0 @@ -#define setcontext(u) libthread_setmcontext(&(u)->mc) -#define getcontext(u) libthread_getmcontext(&(u)->mc) -typedef struct mcontext mcontext_t; -typedef struct ucontext ucontext_t; - -struct mcontext -{ - uintptr ax; - uintptr bx; - uintptr cx; - uintptr dx; - uintptr si; - uintptr di; - uintptr bp; - uintptr sp; - uintptr r8; - uintptr r9; - uintptr r10; - uintptr r11; - uintptr r12; - uintptr r13; - uintptr r14; - uintptr r15; -/* -// XXX: currently do not save vector registers or floating-point state -*/ -}; - -struct ucontext -{ - struct { - void *ss_sp; - uint ss_size; - } uc_stack; - sigset_t uc_sigmask; - mcontext_t mc; -}; - -void makecontext(ucontext_t*, void(*)(void), int, ...); -int swapcontext(ucontext_t*, ucontext_t*); -int libthread_getmcontext(mcontext_t*); -void libthread_setmcontext(mcontext_t*); -- cgit v1.2.3 From 91ececc99741b3111c69d455bc928e871b15d766 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Wed, 30 Dec 2020 08:41:01 -0500 Subject: libthread: drop schedlock Having two locks in the proc was causing deadlocks. --- src/libthread/thread.c | 2 +- src/libthread/threadimpl.h | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) (limited to 'src/libthread') diff --git a/src/libthread/thread.c b/src/libthread/thread.c index d72bf896..79e0ec71 100644 --- a/src/libthread/thread.c +++ b/src/libthread/thread.c @@ -281,7 +281,7 @@ static void pthreadwakeupschedlocked(Proc *p, _Thread *self, _Thread *t) { _threaddebug(self, "pthreadwakeupschedlocked %p %d", p, t->id);; - t->schedrend.l = &p->schedlock; + t->schedrend.l = &p->lock; p->schedthread = t; _procwakeup(&t->schedrend); } diff --git a/src/libthread/threadimpl.h b/src/libthread/threadimpl.h index 9eddba21..fd40f252 100644 --- a/src/libthread/threadimpl.h +++ b/src/libthread/threadimpl.h @@ -88,7 +88,6 @@ struct Proc uint nthread; uint sysproc; _Procrendez runrend; - Lock schedlock; _Thread *schedthread; void *udata; Jmp sigjmp; -- cgit v1.2.3 From 52b599a63c488d3a80bb9f5dd97bad0b10103c54 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Thu, 14 Jan 2021 10:30:24 -0500 Subject: libthread: call setpgrp in programs that will background This fixes the 'run stats from rc; exit rc; stats dies' problem. It's unclear whether this is the right fix or whether rc should be starting all its interactive commands in their own process groups. But at least it does fix stats dying. --- src/libthread/daemonize.c | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'src/libthread') diff --git a/src/libthread/daemonize.c b/src/libthread/daemonize.c index f994ffe1..29929068 100644 --- a/src/libthread/daemonize.c +++ b/src/libthread/daemonize.c @@ -101,6 +101,13 @@ _threadsetupdaemonize(void) sigpid = 1; + /* + * We've been told this program is likely to background itself. + * Put it in its own process group so that we don't get a SIGHUP + * when the parent exits. + */ + setpgrp(); + if(pipe(p) < 0) sysfatal("passer pipe: %r"); -- cgit v1.2.3