xref: /plan9-contrib/sys/src/9k/port/ps.c (revision 9ef1f84b659abcb917c5c090acbce0772e494f21)
1 #include "u.h"
2 #include "../port/lib.h"
3 #include "mem.h"
4 #include "dat.h"
5 #include "fns.h"
6 
7 void
pshash(Proc * p)8 pshash(Proc *p)
9 {
10 	int h;
11 
12 	h = p->pid % nelem(procalloc.ht);
13 	lock(&procalloc);
14 	p->pidhash = procalloc.ht[h];
15 	procalloc.ht[h] = p;
16 	unlock(&procalloc);
17 }
18 
19 void
psunhash(Proc * p)20 psunhash(Proc *p)
21 {
22 	int h;
23 	Proc **l;
24 
25 	h = p->pid % nelem(procalloc.ht);
26 	lock(&procalloc);
27 	for(l = &procalloc.ht[h]; *l != nil; l = &(*l)->pidhash)
28 		if(*l == p){
29 			*l = p->pidhash;
30 			break;
31 		}
32 	unlock(&procalloc);
33 }
34 
35 int
psindex(int pid)36 psindex(int pid)
37 {
38 	Proc *p;
39 	int h;
40 	int s;
41 
42 	s = -1;
43 	h = pid % nelem(procalloc.ht);
44 	lock(&procalloc);
45 	for(p = procalloc.ht[h]; p != nil; p = p->pidhash)
46 		if(p->pid == pid){
47 			s = p->index;
48 			break;
49 		}
50 	unlock(&procalloc);
51 	return s;
52 }
53 
54 Proc*
psincref(int i)55 psincref(int i)
56 {
57 	/*
58 	 * Placeholder.
59 	 */
60 	if(i >= PROCMAX)
61 		return nil;
62 	return &procalloc.arena[i];
63 }
64 
65 void
psdecref(Proc * p)66 psdecref(Proc *p)
67 {
68 	/*
69 	 * Placeholder.
70 	 */
71 	USED(p);
72 }
73 
74 void
psrelease(Proc * p)75 psrelease(Proc* p)
76 {
77 	p->qnext = procalloc.free;
78 	procalloc.free = p;
79 }
80 
81 Proc*
psalloc(void)82 psalloc(void)
83 {
84 	Proc *p;
85 
86 	lock(&procalloc);
87 	for(;;) {
88 		if(p = procalloc.free)
89 			break;
90 
91 		unlock(&procalloc);
92 		resrcwait("no procs");
93 		lock(&procalloc);
94 	}
95 	procalloc.free = p->qnext;
96 	unlock(&procalloc);
97 
98 	return p;
99 }
100 
101 void
psinit(void)102 psinit(void)
103 {
104 	Proc *p;
105 	int i;
106 
107 	procalloc.free = malloc(PROCMAX*sizeof(Proc));
108 	if(procalloc.free == nil)
109 		panic("cannot allocate %ud procs (%udMB)\n", PROCMAX, PROCMAX*sizeof(Proc)/MiB);
110 	procalloc.arena = procalloc.free;
111 
112 	p = procalloc.free;
113 	for(i=0; i<PROCMAX-1; i++,p++){
114 		p->qnext = p+1;
115 		p->index = i;
116 	}
117 	p->qnext = 0;
118 	p->index = i;
119 }
120