Continue switching library over to pthreads when possible.

Tprimes works on Linux 2.6.  You can only have 128 procs
though.
This commit is contained in:
rsc 2004-09-17 03:34:32 +00:00
parent 06bb4ed20d
commit bcf527a98e
15 changed files with 146 additions and 561 deletions

View file

@ -29,7 +29,7 @@ canexec(Alt *a)
/* are there senders or receivers blocked? */
otherop = (CHANSND+CHANRCV) - a->op;
for(i=0; i<c->nentry; i++)
if(c->qentry[i] && c->qentry[i]->op==otherop && c->qentry[i]->thread==nil){
if(c->qentry[i] && c->qentry[i]->op==otherop && c->qentry[i]->thread->altc==nil){
_threaddebug(DBGCHAN, "can rendez alt %p chan %p", a, c);
return 1;
}
@ -460,7 +460,7 @@ altexec(Alt *a, int spl)
b = nil;
me = a->v;
for(i=0; i<c->nentry; i++)
if(c->qentry[i] && c->qentry[i]->op==otherop && c->qentry[i]->thread==nil)
if(c->qentry[i] && c->qentry[i]->op==otherop && c->qentry[i]->thread->altc==nil)
if(nrand(++n) == 0)
b = c->qentry[i];
if(b != nil){
@ -488,7 +488,7 @@ altexec(Alt *a, int spl)
altcopy(waiter, me, c->e);
}
b->thread->altc = c;
_procwakeup(&b->thread->altrend);
_threadwakeup(&b->thread->altrend);
_threaddebug(DBGCHAN, "chanlock is %lud", *(ulong*)(void*)&chanlock);
_threaddebug(DBGCHAN, "unlocking the chanlock");
unlock(&chanlock);

View file

@ -49,9 +49,7 @@ main(int argc, char **argv)
a = _threadmalloc(sizeof *a, 1);
a->argc = argc;
a->argv = argv;
malloc(10);
p = _newproc(mainlauncher, a, mainstacksize, "threadmain", 0, 0);
malloc(10);
_schedinit(p);
abort(); /* not reached */
return 0;
@ -62,9 +60,7 @@ mainlauncher(void *arg)
{
Mainarg *a;
malloc(10);
a = arg;
malloc(10);
threadmain(a->argc, a->argv);
threadexits("threadmain");
}

View file

@ -41,6 +41,9 @@ HFILES=\
<$PLAN9/src/mksyslib
tfork: tfork.$O $PLAN9/lib/$LIB
$LD -o tfork tfork.$O $LDFLAGS -lthread -l9
tprimes: tprimes.$O $PLAN9/lib/$LIB
$LD -o tprimes tprimes.$O $LDFLAGS -lthread -l9

View file

@ -36,7 +36,6 @@ _schedinit(void *arg)
unlock(&p->lock);
while(_setlabel(&p->sched))
;
malloc(10);
_threaddebug(DBGSCHED, "top of schedinit, _threadexitsallstatus=%p", _threadexitsallstatus);
if(_threadexitsallstatus)
_exits(_threadexitsallstatus);
@ -148,12 +147,10 @@ relock:
_threaddebug(DBGSCHED, "sleeping for more work (%d threads)", p->nthreads);
q->asleep = 1;
unlock(&p->readylock);
while(rendezvous((ulong)q, 0) == ~0){
if(_threadexitsallstatus)
_exits(_threadexitsallstatus);
}
/* lock picked up from _threadready */
p->rend.l = &p->readylock;
_procsleep(&p->rend);
if(_threadexitsallstatus)
_exits(_threadexitsallstatus);
}
t = q->head;
q->head = t->next;
@ -185,18 +182,15 @@ _sched(void)
Resched:
p = _threadgetproc();
//fprint(2, "p %p\n", p);
malloc(10);
if((t = p->thread) != nil){
needstack(512);
// _threaddebug(DBGSCHED, "pausing, state=%s set %p goto %p",
// psstate(t->state), &t->sched, &p->sched);
print("swap\n");
if(_setlabel(&t->sched)==0)
_gotolabel(&p->sched);
_threadstacklimit(t->stk, t->stk+t->stksize);
return p->nsched++;
}else{
malloc(10);
t = runthread(p);
if(t == nil){
_threaddebug(DBGSCHED, "all threads gone; exiting");
@ -211,8 +205,6 @@ malloc(10);
}
t->state = Running;
t->nextstate = Ready;
malloc(10);
print("gotolabel\n");
_gotolabel(&t->sched);
for(;;);
}
@ -253,13 +245,11 @@ _threadready(Thread *t)
assert(q->asleep == 1);
q->asleep = 0;
/* lock passes to runthread */
_threaddebug(DBGSCHED, "waking process %d", t->proc->pid);
while(rendezvous((ulong)q, 0) == ~0){
if(_threadexitsallstatus)
_exits(_threadexitsallstatus);
}
}else
unlock(&t->proc->readylock);
_procwakeup(&t->proc->rend);
}
unlock(&t->proc->readylock);
if(_threadexitsallstatus)
_exits(_threadexitsallstatus);
}
void

View file

@ -95,6 +95,8 @@ struct Thread
Chanstate chan; /* which channel operation is current */
Alt *alt; /* pointer to current alt structure (debugging) */
ulong userpc;
Channel *c;
pthread_cond_t cond;
void* udata[NPRIV]; /* User per-thread data pointer */
int lastfd;
@ -136,6 +138,8 @@ struct Proc
uint nextID; /* ID of most recently created thread */
Proc *next; /* linked list of Procs */
_Procrend rend; /* sleep here for more ready threads */
void *arg; /* passed between shared and unshared stk */
char str[ERRMAX]; /* used by threadexits to avoid malloc */
char errbuf[ERRMAX]; /* errstr */

View file

@ -41,7 +41,6 @@ threadmain(int argc, char **argv)
int i;
Channel *c;
malloc(10);
ARGBEGIN{
case 'D':
_threaddebuglevel = atoi(ARGF());