diff --git a/sys/src/9/port/portdat.h b/sys/src/9/port/portdat.h index 5aca7fb14..ce3470e74 100644 --- a/sys/src/9/port/portdat.h +++ b/sys/src/9/port/portdat.h @@ -768,7 +768,6 @@ extern char* eve; extern char hostdomain[]; extern uchar initcode[]; extern Queue* kprintoq; -extern Ref noteidalloc; extern int nsyscall; extern Palloc palloc; extern Queue* serialoq; diff --git a/sys/src/9/port/portfns.h b/sys/src/9/port/portfns.h index 8c71593b6..3c9d43ee6 100644 --- a/sys/src/9/port/portfns.h +++ b/sys/src/9/port/portfns.h @@ -214,6 +214,7 @@ ulong perfticks(void); void pexit(char*, int); void pgrpcpy(Pgrp*, Pgrp*); void pgrpnote(ulong, char*, long, int); +int pidalloc(Proc*); void pio(Segment *, ulong, ulong, Page **); #define poperror() up->nerrlab-- void portcountpagerefs(ulong*, int); diff --git a/sys/src/9/port/proc.c b/sys/src/9/port/proc.c index 3b74575b7..dd8862baa 100644 --- a/sys/src/9/port/proc.c +++ b/sys/src/9/port/proc.c @@ -9,7 +9,6 @@ int schedgain = 30; /* units in seconds */ int nrdy; -Ref noteidalloc; void updatecpu(Proc*); int reprioritize(Proc*); @@ -19,8 +18,6 @@ long skipscheds; long preempts; ulong load; -static Ref pidalloc; - static struct Procalloc { Lock; @@ -56,8 +53,7 @@ char *statename[] = "Waitrelease", }; -static void pidhash(Proc*); -static void pidunhash(Proc*); +static void pidfree(Proc*); static void rebalance(void); /* @@ -637,11 +633,7 @@ newproc(void) p->nargs = 0; p->setargs = 0; memset(p->seg, 0, sizeof p->seg); - p->pid = incref(&pidalloc); - pidhash(p); - p->noteid = incref(¬eidalloc); - if(p->pid==0 || p->noteid==0) - panic("pidalloc"); + p->noteid = pidalloc(p); if(p->kstack == 0) p->kstack = smalloc(KSTACK); @@ -1182,7 +1174,7 @@ pexit(char *exitstr, int freemem) qunlock(&up->seglock); lock(&up->exl); /* Prevent my children from leaving waits */ - pidunhash(up); + pidfree(up); up->pid = 0; wakeup(&up->waitr); unlock(&up->exl); @@ -1597,20 +1589,33 @@ accounttime(void) m->load = (m->load*(HZ-1)+n)/HZ; } -static void -pidhash(Proc *p) +int +pidalloc(Proc *p) { - int h; + static Ref ref; + int pid, h; + Proc *x; - h = p->pid % nelem(procalloc.ht); lock(&procalloc); - p->pidhash = procalloc.ht[h]; - procalloc.ht[h] = p; +Retry: + pid = incref(&ref) & 0x7FFFFFFF; + if(pid == 0) + goto Retry; + h = pid % nelem(procalloc.ht); + for(x = procalloc.ht[h]; x != nil; x = x->pidhash) + if(x->pid == pid) + goto Retry; + if(p){ + p->pid = pid; + p->pidhash = procalloc.ht[h]; + procalloc.ht[h] = p; + } unlock(&procalloc); + return pid; } static void -pidunhash(Proc *p) +pidfree(Proc *p) { int h; Proc **l; diff --git a/sys/src/9/port/sysproc.c b/sys/src/9/port/sysproc.c index be5a83b94..e4bf72335 100644 --- a/sys/src/9/port/sysproc.c +++ b/sys/src/9/port/sysproc.c @@ -78,7 +78,7 @@ sysrfork(ulong *arg) closeegrp(oeg); } if(flag & RFNOTEG) - up->noteid = incref(¬eidalloc); + up->noteid = pidalloc(0); return 0; }