shithub: riscv

Download patch

ref: 2450b55c7b80ffc81b94d1428d96288788187fcf
parent: 0df8b0c63cdd0328fa4d0865cd760232a694e0af
author: cinap_lenrek <cinap_lenrek@centraldogma>
date: Tue Dec 20 17:22:08 EST 2011

kernel: add pidalloc() and reuse pid once the counter wraps arround

--- a/sys/src/9/port/portdat.h
+++ b/sys/src/9/port/portdat.h
@@ -768,7 +768,6 @@
 extern	char	hostdomain[];
 extern	uchar	initcode[];
 extern	Queue*	kprintoq;
-extern 	Ref	noteidalloc;
 extern	int	nsyscall;
 extern	Palloc	palloc;
 extern	Queue*	serialoq;
--- a/sys/src/9/port/portfns.h
+++ b/sys/src/9/port/portfns.h
@@ -214,6 +214,7 @@
 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);
--- 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 preempts;
 ulong load;
 
-static Ref	pidalloc;
-
 static struct Procalloc
 {
 	Lock;
@@ -56,8 +53,7 @@
 	"Waitrelease",
 };
 
-static void pidhash(Proc*);
-static void pidunhash(Proc*);
+static void pidfree(Proc*);
 static void rebalance(void);
 
 /*
@@ -637,11 +633,7 @@
 	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 @@
 	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 @@
 	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;
--- a/sys/src/9/port/sysproc.c
+++ b/sys/src/9/port/sysproc.c
@@ -78,7 +78,7 @@
 			closeegrp(oeg);
 		}
 		if(flag & RFNOTEG)
-			up->noteid = incref(&noteidalloc);
+			up->noteid = pidalloc(0);
 		return 0;
 	}