xref: /csrg-svn/sys/kern/kern_synch.c (revision 181)
1 /*	kern_synch.c	3.8	10/14/12	*/
2 
3 #include "../h/param.h"
4 #include "../h/systm.h"
5 #include "../h/dir.h"
6 #include "../h/user.h"
7 #include "../h/proc.h"
8 #include "../h/file.h"
9 #include "../h/inode.h"
10 #include "../h/vm.h"
11 #include "../h/pte.h"
12 #include "../h/inline.h"
13 
14 
15 #define SQSIZE 0100	/* Must be power of 2 */
16 #define HASH(x)	(( (int) x >> 5) & (SQSIZE-1))
17 struct proc *slpque[SQSIZE];
18 
19 /*
20  * Give up the processor till a wakeup occurs
21  * on chan, at which time the process
22  * enters the scheduling queue at priority pri.
23  * The most important effect of pri is that when
24  * pri<=PZERO a signal cannot disturb the sleep;
25  * if pri>PZERO signals will be processed.
26  * Callers of this routine must be prepared for
27  * premature return, and check that the reason for
28  * sleeping has gone away.
29  */
30 sleep(chan, pri)
31 caddr_t chan;
32 {
33 	register struct proc *rp;
34 	register s, h;
35 
36 	rp = u.u_procp;
37 	s = spl6();
38 	if (chan==0 || rp->p_stat != SRUN || rp->p_rlink)
39 		panic("sleep");
40 	rp->p_stat = SSLEEP;
41 	rp->p_wchan = chan;
42 	rp->p_slptime = 0;
43 	rp->p_pri = pri;
44 	h = HASH(chan);
45 	rp->p_link = slpque[h];
46 	slpque[h] = rp;
47 	if(pri > PZERO) {
48 		if(ISSIG(rp)) {
49 			rp->p_wchan = 0;
50 			rp->p_stat = SRUN;
51 			slpque[h] = rp->p_link;
52 			(void) spl0();
53 			goto psig;
54 		}
55 		(void) spl0();
56 		if(runin != 0) {
57 			runin = 0;
58 			wakeup((caddr_t)&runin);
59 		}
60 		swtch();
61 		if(ISSIG(rp))
62 			goto psig;
63 	} else {
64 		(void) spl0();
65 		swtch();
66 	}
67 	splx(s);
68 	return;
69 
70 	/*
71 	 * If priority was low (>PZERO) and
72 	 * there has been a signal,
73 	 * execute non-local goto to
74 	 * the qsav location.
75 	 * (see trap1/trap.c)
76 	 */
77 psig:
78 	longjmp(u.u_qsav);
79 	/*NOTREACHED*/
80 }
81 
82 /*
83  * Sleep on chan at pri.
84  * Return in no more than the indicated number of seconds.
85  * (If seconds==0, no timeout implied)
86  * Return	TS_OK if chan was awakened normally
87  *		TS_TIME if timeout occurred
88  *		TS_SIG if asynchronous signal occurred
89  */
90 tsleep(chan, pri, seconds)
91 caddr_t chan;
92 {
93 	label_t lqsav;
94 	register struct proc *pp;
95 	register sec, n, rval;
96 
97 	pp = u.u_procp;
98 	n = spl7();
99 	sec = 0;
100 	rval = 0;
101 	if (pp->p_clktim && pp->p_clktim<seconds)
102 		seconds = 0;
103 	if (seconds) {
104 		pp->p_flag |= STIMO;
105 		sec = pp->p_clktim-seconds;
106 		pp->p_clktim = seconds;
107 	}
108 	bcopy((caddr_t)u.u_qsav, (caddr_t)lqsav, sizeof (label_t));
109 	if (setjmp(u.u_qsav))
110 		rval = TS_SIG;
111 	else {
112 		sleep(chan, pri);
113 		if ((pp->p_flag&STIMO)==0 && seconds)
114 			rval = TS_TIME;
115 		else
116 			rval = TS_OK;
117 	}
118 	pp->p_flag &= ~STIMO;
119 	bcopy((caddr_t)lqsav, (caddr_t)u.u_qsav, sizeof (label_t));
120 	if (sec > 0)
121 		pp->p_clktim += sec;
122 	else
123 		pp->p_clktim = 0;
124 	splx(n);
125 	return(rval);
126 }
127 
128 /*
129  * Remove a process from its wait queue
130  */
131 unsleep(p)
132 register struct proc *p;
133 {
134 	register struct proc **hp;
135 	register s;
136 
137 	s = spl6();
138 	if (p->p_wchan) {
139 		hp = &slpque[HASH(p->p_wchan)];
140 		while (*hp != p)
141 			hp = &(*hp)->p_link;
142 		*hp = p->p_link;
143 		p->p_wchan = 0;
144 	}
145 	splx(s);
146 }
147 
148 /*
149  * Wake up all processes sleeping on chan.
150  */
151 wakeup(chan)
152 register caddr_t chan;
153 {
154 	register struct proc *p, *q;
155 	register i;
156 	int s;
157 
158 	s = spl6();
159 	i = HASH(chan);
160 restart:
161 	p = slpque[i];
162 	q = NULL;
163 	while(p != NULL) {
164 		if (p->p_rlink || p->p_stat != SSLEEP && p->p_stat != SSTOP)
165 			panic("wakeup");
166 		if (p->p_wchan==chan && p->p_stat!=SZOMB) {
167 			if (q == NULL)
168 				slpque[i] = p->p_link;
169 			else
170 				q->p_link = p->p_link;
171 			p->p_wchan = 0;
172 			p->p_slptime = 0;
173 			if (p->p_stat == SSLEEP) {
174 				/* OPTIMIZED INLINE EXPANSION OF setrun(p) */
175 				p->p_stat = SRUN;
176 				if (p->p_flag & SLOAD) {
177 #ifndef FASTVAX
178 					p->p_link = runq;
179 					runq = p->p_link;
180 #else
181 					setrq(p);
182 #endif
183 				}
184 				if(p->p_pri < curpri)
185 					runrun++;
186 				if(runout != 0 && (p->p_flag&SLOAD) == 0) {
187 					runout = 0;
188 					wakeup((caddr_t)&runout);
189 				}
190 				/* END INLINE EXPANSION */
191 			}
192 			goto restart;
193 		}
194 		q = p;
195 		p = p->p_link;
196 	}
197 	splx(s);
198 }
199 
200 #ifdef FASTVAX
201 /*
202  * Initialize the (doubly-linked) run queues
203  * to be empty.
204  */
205 rqinit()
206 {
207 	register int i;
208 
209 	for (i = 0; i < NQS; i++)
210 		qs[i].ph_link = qs[i].ph_rlink = (struct proc *)&qs[i];
211 }
212 #endif
213 
214 /*
215  * Set the process running;
216  * arrange for it to be swapped in if necessary.
217  */
218 setrun(p)
219 register struct proc *p;
220 {
221 	register caddr_t w;
222 	register s;
223 
224 	s = spl6();
225 	switch (p->p_stat) {
226 
227 	case 0:
228 	case SWAIT:
229 	case SRUN:
230 	case SZOMB:
231 	default:
232 		panic("setrun");
233 
234 	case SSLEEP:
235 		unsleep(p);		/* e.g. when sending signals */
236 		break;
237 
238 	case SIDL:
239 	case SSTOP:
240 		break;
241 	}
242 	p->p_stat = SRUN;
243 	if (p->p_flag & SLOAD)
244 		setrq(p);
245 	splx(s);
246 	if(p->p_pri < curpri)
247 		runrun++;
248 	if(runout != 0 && (p->p_flag&SLOAD) == 0) {
249 		runout = 0;
250 		wakeup((caddr_t)&runout);
251 	}
252 }
253 
254 /*
255  * Set user priority.
256  * The rescheduling flag (runrun)
257  * is set if the priority is better
258  * than the currently running process.
259  */
260 setpri(pp)
261 register struct proc *pp;
262 {
263 	register p;
264 
265 	p = (pp->p_cpu & 0377)/16;
266 	p += PUSER + pp->p_nice - NZERO;
267 	if(p > 127)
268 		p = 127;
269 	if(p < curpri)
270 		runrun++;
271 	pp->p_usrpri = p;
272 	return(p);
273 }
274 
275 /*
276  * Create a new process-- the internal version of
277  * sys fork.
278  * It returns 1 in the new process, 0 in the old.
279  */
280 newproc(isvfork)
281 {
282 	register struct proc *p;
283 	register struct proc *rpp, *rip;
284 	register int n;
285 
286 	p = NULL;
287 	/*
288 	 * First, just locate a slot for a process
289 	 * and copy the useful info from this process into it.
290 	 * The panic "cannot happen" because fork has already
291 	 * checked for the existence of a slot.
292 	 */
293 retry:
294 	mpid++;
295 	if(mpid >= 30000) {
296 		mpid = 0;
297 		goto retry;
298 	}
299 	for(rpp = &proc[0]; rpp < &proc[NPROC]; rpp++) {
300 		if(rpp->p_stat == NULL && p==NULL)
301 			p = rpp;
302 		if (rpp->p_pid==mpid || rpp->p_pgrp==mpid)
303 			goto retry;
304 	}
305 	if ((rpp = p)==NULL)
306 		panic("no procs");
307 
308 	/*
309 	 * make proc entry for new proc
310 	 */
311 
312 	rip = u.u_procp;
313 	rpp->p_stat = SIDL;
314 	rpp->p_clktim = 0;
315 	rpp->p_flag = SLOAD | (rip->p_flag & SPAGI);
316 	if (isvfork) {
317 		rpp->p_flag |= SVFORK;
318 		rpp->p_ndx = rip->p_ndx;
319 	} else
320 		rpp->p_ndx = rpp - proc;
321 	rpp->p_uid = rip->p_uid;
322 	rpp->p_pgrp = rip->p_pgrp;
323 	rpp->p_nice = rip->p_nice;
324 	rpp->p_textp = isvfork ? 0 : rip->p_textp;
325 	rpp->p_pid = mpid;
326 	rpp->p_ppid = rip->p_pid;
327 	rpp->p_pptr = rip;
328 	rpp->p_time = 0;
329 	rpp->p_cpu = 0;
330 	rpp->p_siga0 = rip->p_siga0;
331 	rpp->p_siga1 = rip->p_siga1;
332 	/* take along any pending signals, like stops? */
333 	if (isvfork) {
334 		rpp->p_tsize = rpp->p_dsize = rpp->p_ssize = 0;
335 		rpp->p_szpt = clrnd(ctopt(UPAGES));
336 		forkstat.cntvfork++;
337 		forkstat.sizvfork += rip->p_dsize + rip->p_ssize;
338 	} else {
339 		rpp->p_tsize = rip->p_tsize;
340 		rpp->p_dsize = rip->p_dsize;
341 		rpp->p_ssize = rip->p_ssize;
342 		rpp->p_szpt = rip->p_szpt;
343 		forkstat.cntfork++;
344 		forkstat.sizfork += rip->p_dsize + rip->p_ssize;
345 	}
346 	rpp->p_rssize = 0;
347 	rpp->p_wchan = 0;
348 	rpp->p_slptime = 0;
349 	rpp->p_aveflt = rip->p_aveflt;
350 	rate.v_pgin += rip->p_aveflt;
351 	rpp->p_faults = 0;
352 	n = PIDHASH(rpp->p_pid);
353 	p->p_idhash = pidhash[n];
354 	pidhash[n] = rpp - proc;
355 
356 	/*
357 	 * make duplicate entries
358 	 * where needed
359 	 */
360 
361 	multprog++;
362 
363 	for(n=0; n<NOFILE; n++)
364 		if(u.u_ofile[n] != NULL) {
365 			u.u_ofile[n]->f_count++;
366 			if(!isvfork && u.u_vrpages[n])
367 				u.u_ofile[n]->f_inode->i_vfdcnt++;
368 		}
369 
370 	u.u_cdir->i_count++;
371 	if (u.u_rdir)
372 		u.u_rdir->i_count++;
373 	/*
374 	 * Partially simulate the environment
375 	 * of the new process so that when it is actually
376 	 * created (by copying) it will look right.
377 	 */
378 
379 	rip->p_flag |= SKEEP;	/* prevent parent from being swapped */
380 
381 	if (procdup(rpp, isvfork))
382 		return (1);
383 
384 	spl6();
385 	rpp->p_stat = SRUN;
386 	setrq(rpp);
387 	spl0();
388 	/* SSWAP NOT NEEDED IN THIS CASE AS u.u_pcb.pcb_sswap SUFFICES */
389 	/* rpp->p_flag |= SSWAP; */
390 	rip->p_flag &= ~SKEEP;
391 	if (isvfork) {
392 		u.u_procp->p_xlink = rpp;
393 		u.u_procp->p_flag |= SNOVM;
394 		while (rpp->p_flag & SVFORK)
395 			sleep((caddr_t)rpp, PZERO - 1);
396 		if ((rpp->p_flag & SLOAD) == 0)
397 			panic("newproc vfork");
398 		uaccess(rpp, Vfmap, &vfutl);
399 		u.u_procp->p_xlink = 0;
400 		vpassvm(rpp, u.u_procp, &vfutl, &u, Vfmap);
401 		for (n = 0; n < NOFILE; n++)
402 			if (vfutl.u_vrpages[n]) {
403 				if ((u.u_vrpages[n] = vfutl.u_vrpages[n] - 1) == 0)
404 					if (--u.u_ofile[n]->f_inode->i_vfdcnt < 0)
405 						panic("newproc i_vfdcnt");
406 				vfutl.u_vrpages[n] = 0;
407 			}
408 		u.u_procp->p_flag &= ~SNOVM;
409 		rpp->p_ndx = rpp - proc;
410 		rpp->p_flag |= SVFDONE;
411 		wakeup((caddr_t)rpp);
412 	}
413 	return (0);
414 }
415