summaryrefslogtreecommitdiff
path: root/sys/src/9/port/proc.c
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@centraldogma>2011-12-20 22:22:08 +0100
committercinap_lenrek <cinap_lenrek@centraldogma>2011-12-20 22:22:08 +0100
commit2450b55c7b80ffc81b94d1428d96288788187fcf (patch)
tree07ccdc5721a0b3607fd8acf54a301375a29b9ce4 /sys/src/9/port/proc.c
parent0df8b0c63cdd0328fa4d0865cd760232a694e0af (diff)
kernel: add pidalloc() and reuse pid once the counter wraps arround
Diffstat (limited to 'sys/src/9/port/proc.c')
-rw-r--r--sys/src/9/port/proc.c41
1 files changed, 23 insertions, 18 deletions
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(&noteidalloc);
- 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;