kern_synch.c revision 125162
1/*-
2 * Copyright (c) 1982, 1986, 1990, 1991, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 *    notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 *    notice, this list of conditions and the following disclaimer in the
17 *    documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *	This product includes software developed by the University of
21 *	California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 *    may be used to endorse or promote products derived from this software
24 *    without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
39 */
40
41#include <sys/cdefs.h>
42__FBSDID("$FreeBSD: head/sys/kern/kern_synch.c 125162 2004-01-28 20:44:41Z jhb $");
43
44#include "opt_ddb.h"
45#include "opt_ktrace.h"
46
47#include <sys/param.h>
48#include <sys/systm.h>
49#include <sys/condvar.h>
50#include <sys/kernel.h>
51#include <sys/ktr.h>
52#include <sys/kthread.h>
53#include <sys/lock.h>
54#include <sys/mutex.h>
55#include <sys/proc.h>
56#include <sys/resourcevar.h>
57#include <sys/sched.h>
58#include <sys/signalvar.h>
59#include <sys/smp.h>
60#include <sys/sx.h>
61#include <sys/sysctl.h>
62#include <sys/sysproto.h>
63#include <sys/vmmeter.h>
64#ifdef DDB
65#include <ddb/ddb.h>
66#endif
67#ifdef KTRACE
68#include <sys/uio.h>
69#include <sys/ktrace.h>
70#endif
71
72#include <machine/cpu.h>
73
74static void synch_setup(void *dummy);
75SYSINIT(synch_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, synch_setup, NULL)
76
77int	hogticks;
78int	lbolt;
79
80static struct callout lbolt_callout;
81
82struct loadavg averunnable =
83	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
84/*
85 * Constants for averages over 1, 5, and 15 minutes
86 * when sampling at 5 second intervals.
87 */
88static fixpt_t cexp[3] = {
89	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
90	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
91	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
92};
93
94/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
95static int      fscale __unused = FSCALE;
96SYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, 0, FSCALE, "");
97
98static void	endtsleep(void *);
99static void	loadav(void);
100static void	loadav_thread(void *dummy);
101static void	lboltcb(void *arg);
102
103/*
104 * We're only looking at 7 bits of the address; everything is
105 * aligned to 4, lots of things are aligned to greater powers
106 * of 2.  Shift right by 8, i.e. drop the bottom 256 worth.
107 */
108#define TABLESIZE	128
109static TAILQ_HEAD(slpquehead, thread) slpque[TABLESIZE];
110#define LOOKUP(x)	(((intptr_t)(x) >> 8) & (TABLESIZE - 1))
111
112void
113sleepinit(void)
114{
115	int i;
116
117	hogticks = (hz / 10) * 2;	/* Default only. */
118	for (i = 0; i < TABLESIZE; i++)
119		TAILQ_INIT(&slpque[i]);
120}
121
122/*
123 * General sleep call.  Suspends the current process until a wakeup is
124 * performed on the specified identifier.  The process will then be made
125 * runnable with the specified priority.  Sleeps at most timo/hz seconds
126 * (0 means no timeout).  If pri includes PCATCH flag, signals are checked
127 * before and after sleeping, else signals are not checked.  Returns 0 if
128 * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
129 * signal needs to be delivered, ERESTART is returned if the current system
130 * call should be restarted if possible, and EINTR is returned if the system
131 * call should be interrupted by the signal (return EINTR).
132 *
133 * The mutex argument is exited before the caller is suspended, and
134 * entered before msleep returns.  If priority includes the PDROP
135 * flag the mutex is not entered before returning.
136 */
137
138int
139msleep(ident, mtx, priority, wmesg, timo)
140	void *ident;
141	struct mtx *mtx;
142	int priority, timo;
143	const char *wmesg;
144{
145	struct thread *td = curthread;
146	struct proc *p = td->td_proc;
147	int sig, catch = priority & PCATCH;
148	int rval = 0;
149	WITNESS_SAVE_DECL(mtx);
150
151#ifdef KTRACE
152	if (KTRPOINT(td, KTR_CSW))
153		ktrcsw(1, 0);
154#endif
155	/* XXX: mtx == NULL ?? */
156	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, &mtx->mtx_object,
157	    "Sleeping on \"%s\"", wmesg);
158	KASSERT(timo != 0 || mtx_owned(&Giant) || mtx != NULL,
159	    ("sleeping without a mutex"));
160	/*
161	 * If we are capable of async syscalls and there isn't already
162	 * another one ready to return, start a new thread
163	 * and queue it as ready to run. Note that there is danger here
164	 * because we need to make sure that we don't sleep allocating
165	 * the thread (recursion here might be bad).
166	 */
167	mtx_lock_spin(&sched_lock);
168	if (p->p_flag & P_SA || p->p_numthreads > 1) {
169		/*
170		 * Just don't bother if we are exiting
171		 * and not the exiting thread or thread was marked as
172		 * interrupted.
173		 */
174		if (catch) {
175			if ((p->p_flag & P_WEXIT) && p->p_singlethread != td) {
176				mtx_unlock_spin(&sched_lock);
177				return (EINTR);
178			}
179			if (td->td_flags & TDF_INTERRUPT) {
180				mtx_unlock_spin(&sched_lock);
181				return (td->td_intrval);
182			}
183		}
184	}
185	if (cold ) {
186		/*
187		 * During autoconfiguration, just return;
188		 * don't run any other procs or panic below,
189		 * in case this is the idle process and already asleep.
190		 * XXX: this used to do "s = splhigh(); splx(safepri);
191		 * splx(s);" to give interrupts a chance, but there is
192		 * no way to give interrupts a chance now.
193		 */
194		if (mtx != NULL && priority & PDROP)
195			mtx_unlock(mtx);
196		mtx_unlock_spin(&sched_lock);
197		return (0);
198	}
199	DROP_GIANT();
200	if (mtx != NULL) {
201		mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
202		WITNESS_SAVE(&mtx->mtx_object, mtx);
203		mtx_unlock(mtx);
204		if (priority & PDROP)
205			mtx = NULL;
206	}
207	KASSERT(p != NULL, ("msleep1"));
208	KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep"));
209
210	CTR5(KTR_PROC, "msleep: thread %p (pid %d, %s) on %s (%p)",
211	    td, p->p_pid, p->p_comm, wmesg, ident);
212
213	td->td_wchan = ident;
214	td->td_wmesg = wmesg;
215	TAILQ_INSERT_TAIL(&slpque[LOOKUP(ident)], td, td_slpq);
216	TD_SET_ON_SLEEPQ(td);
217	if (timo)
218		callout_reset(&td->td_slpcallout, timo, endtsleep, td);
219	/*
220	 * We put ourselves on the sleep queue and start our timeout
221	 * before calling thread_suspend_check, as we could stop there, and
222	 * a wakeup or a SIGCONT (or both) could occur while we were stopped.
223	 * without resuming us, thus we must be ready for sleep
224	 * when cursig is called.  If the wakeup happens while we're
225	 * stopped, td->td_wchan will be 0 upon return from cursig.
226	 */
227	if (catch) {
228		CTR3(KTR_PROC, "msleep caught: thread %p (pid %d, %s)", td,
229		    p->p_pid, p->p_comm);
230		td->td_flags |= TDF_SINTR;
231		mtx_unlock_spin(&sched_lock);
232		PROC_LOCK(p);
233		mtx_lock(&p->p_sigacts->ps_mtx);
234		sig = cursig(td);
235		mtx_unlock(&p->p_sigacts->ps_mtx);
236		if (sig == 0 && thread_suspend_check(1))
237			sig = SIGSTOP;
238		mtx_lock_spin(&sched_lock);
239		PROC_UNLOCK(p);
240		if (sig != 0) {
241			if (TD_ON_SLEEPQ(td))
242				unsleep(td);
243		} else if (!TD_ON_SLEEPQ(td))
244			catch = 0;
245	} else
246		sig = 0;
247
248	/*
249	 * Let the scheduler know we're about to voluntarily go to sleep.
250	 */
251	sched_sleep(td, priority & PRIMASK);
252
253	if (TD_ON_SLEEPQ(td)) {
254		TD_SET_SLEEPING(td);
255		mi_switch(SW_VOL);
256	}
257	/*
258	 * We're awake from voluntary sleep.
259	 */
260	CTR3(KTR_PROC, "msleep resume: thread %p (pid %d, %s)", td, p->p_pid,
261	    p->p_comm);
262	KASSERT(TD_IS_RUNNING(td), ("running but not TDS_RUNNING"));
263	td->td_flags &= ~TDF_SINTR;
264	if (td->td_flags & TDF_TIMEOUT) {
265		td->td_flags &= ~TDF_TIMEOUT;
266		if (sig == 0)
267			rval = EWOULDBLOCK;
268	} else if (td->td_flags & TDF_TIMOFAIL) {
269		td->td_flags &= ~TDF_TIMOFAIL;
270	} else if (timo && callout_stop(&td->td_slpcallout) == 0) {
271		/*
272		 * This isn't supposed to be pretty.  If we are here, then
273		 * the endtsleep() callout is currently executing on another
274		 * CPU and is either spinning on the sched_lock or will be
275		 * soon.  If we don't synchronize here, there is a chance
276		 * that this process may msleep() again before the callout
277		 * has a chance to run and the callout may end up waking up
278		 * the wrong msleep().  Yuck.
279		 */
280		TD_SET_SLEEPING(td);
281		mi_switch(SW_INVOL);
282		td->td_flags &= ~TDF_TIMOFAIL;
283	}
284	if ((td->td_flags & TDF_INTERRUPT) && (priority & PCATCH) &&
285	    (rval == 0)) {
286		rval = td->td_intrval;
287	}
288	mtx_unlock_spin(&sched_lock);
289	if (rval == 0 && catch) {
290		PROC_LOCK(p);
291		/* XXX: shouldn't we always be calling cursig()? */
292		mtx_lock(&p->p_sigacts->ps_mtx);
293		if (sig != 0 || (sig = cursig(td))) {
294			if (SIGISMEMBER(p->p_sigacts->ps_sigintr, sig))
295				rval = EINTR;
296			else
297				rval = ERESTART;
298		}
299		mtx_unlock(&p->p_sigacts->ps_mtx);
300		PROC_UNLOCK(p);
301	}
302#ifdef KTRACE
303	if (KTRPOINT(td, KTR_CSW))
304		ktrcsw(0, 0);
305#endif
306	PICKUP_GIANT();
307	if (mtx != NULL) {
308		mtx_lock(mtx);
309		WITNESS_RESTORE(&mtx->mtx_object, mtx);
310	}
311	return (rval);
312}
313
314/*
315 * Implement timeout for msleep().
316 *
317 * If process hasn't been awakened (wchan non-zero),
318 * set timeout flag and undo the sleep.  If proc
319 * is stopped, just unsleep so it will remain stopped.
320 * MP-safe, called without the Giant mutex.
321 */
322static void
323endtsleep(arg)
324	void *arg;
325{
326	register struct thread *td;
327
328	td = (struct thread *)arg;
329	CTR3(KTR_PROC, "endtsleep: thread %p (pid %d, %s)",
330	    td, td->td_proc->p_pid, td->td_proc->p_comm);
331	mtx_lock_spin(&sched_lock);
332	/*
333	 * This is the other half of the synchronization with msleep()
334	 * described above.  If the TDS_TIMEOUT flag is set, we lost the
335	 * race and just need to put the process back on the runqueue.
336	 */
337	if (TD_ON_SLEEPQ(td)) {
338		TAILQ_REMOVE(&slpque[LOOKUP(td->td_wchan)], td, td_slpq);
339		TD_CLR_ON_SLEEPQ(td);
340		td->td_flags |= TDF_TIMEOUT;
341		td->td_wmesg = NULL;
342	} else
343		td->td_flags |= TDF_TIMOFAIL;
344	TD_CLR_SLEEPING(td);
345	setrunnable(td);
346	mtx_unlock_spin(&sched_lock);
347}
348
349/*
350 * Abort a thread, as if an interrupt had occured.  Only abort
351 * interruptable waits (unfortunatly it isn't only safe to abort others).
352 * This is about identical to cv_abort().
353 * Think about merging them?
354 * Also, whatever the signal code does...
355 */
356void
357abortsleep(struct thread *td)
358{
359
360	mtx_assert(&sched_lock, MA_OWNED);
361	/*
362	 * If the TDF_TIMEOUT flag is set, just leave. A
363	 * timeout is scheduled anyhow.
364	 */
365	if ((td->td_flags & (TDF_TIMEOUT | TDF_SINTR)) == TDF_SINTR) {
366		if (TD_ON_SLEEPQ(td)) {
367			unsleep(td);
368			TD_CLR_SLEEPING(td);
369			setrunnable(td);
370		}
371	}
372}
373
374/*
375 * Remove a process from its wait queue
376 */
377void
378unsleep(struct thread *td)
379{
380
381	mtx_lock_spin(&sched_lock);
382	if (TD_ON_SLEEPQ(td)) {
383		TAILQ_REMOVE(&slpque[LOOKUP(td->td_wchan)], td, td_slpq);
384		TD_CLR_ON_SLEEPQ(td);
385		td->td_wmesg = NULL;
386	}
387	mtx_unlock_spin(&sched_lock);
388}
389
390/*
391 * Make all processes sleeping on the specified identifier runnable.
392 */
393void
394wakeup(ident)
395	register void *ident;
396{
397	register struct slpquehead *qp;
398	register struct thread *td;
399	struct thread *ntd;
400	struct proc *p;
401
402	mtx_lock_spin(&sched_lock);
403	qp = &slpque[LOOKUP(ident)];
404restart:
405	for (td = TAILQ_FIRST(qp); td != NULL; td = ntd) {
406		ntd = TAILQ_NEXT(td, td_slpq);
407		if (td->td_wchan == ident) {
408			unsleep(td);
409			TD_CLR_SLEEPING(td);
410			setrunnable(td);
411			p = td->td_proc;
412			CTR3(KTR_PROC,"wakeup: thread %p (pid %d, %s)",
413			    td, p->p_pid, p->p_comm);
414			goto restart;
415		}
416	}
417	mtx_unlock_spin(&sched_lock);
418}
419
420/*
421 * Make a process sleeping on the specified identifier runnable.
422 * May wake more than one process if a target process is currently
423 * swapped out.
424 */
425void
426wakeup_one(ident)
427	register void *ident;
428{
429	register struct proc *p;
430	register struct slpquehead *qp;
431	register struct thread *td;
432	struct thread *ntd;
433
434	mtx_lock_spin(&sched_lock);
435	qp = &slpque[LOOKUP(ident)];
436	for (td = TAILQ_FIRST(qp); td != NULL; td = ntd) {
437		ntd = TAILQ_NEXT(td, td_slpq);
438		if (td->td_wchan == ident) {
439			unsleep(td);
440			TD_CLR_SLEEPING(td);
441			setrunnable(td);
442			p = td->td_proc;
443			CTR3(KTR_PROC,"wakeup1: thread %p (pid %d, %s)",
444			    td, p->p_pid, p->p_comm);
445			break;
446		}
447	}
448	mtx_unlock_spin(&sched_lock);
449}
450
451/*
452 * The machine independent parts of mi_switch().
453 */
454void
455mi_switch(int flags)
456{
457	struct bintime new_switchtime;
458	struct thread *td;
459	struct proc *p;
460
461	mtx_assert(&sched_lock, MA_OWNED | MA_NOTRECURSED);
462	td = curthread;			/* XXX */
463	p = td->td_proc;		/* XXX */
464	KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code"));
465#ifdef INVARIANTS
466	if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td))
467		mtx_assert(&Giant, MA_NOTOWNED);
468#endif
469	KASSERT(td->td_critnest == 1,
470	    ("mi_switch: switch in a critical section"));
471	KASSERT((flags & (SW_INVOL | SW_VOL)) != 0,
472	    ("mi_switch: switch must be voluntary or involuntary"));
473
474	if (flags & SW_VOL)
475		p->p_stats->p_ru.ru_nvcsw++;
476	else
477		p->p_stats->p_ru.ru_nivcsw++;
478	/*
479	 * Compute the amount of time during which the current
480	 * process was running, and add that to its total so far.
481	 */
482	binuptime(&new_switchtime);
483	bintime_add(&p->p_runtime, &new_switchtime);
484	bintime_sub(&p->p_runtime, PCPU_PTR(switchtime));
485
486	td->td_generation++;	/* bump preempt-detect counter */
487
488#ifdef DDB
489	/*
490	 * Don't perform context switches from the debugger.
491	 */
492	if (db_active) {
493		mtx_unlock_spin(&sched_lock);
494		db_print_backtrace();
495		db_error("Context switches not allowed in the debugger");
496	}
497#endif
498
499	/*
500	 * Check if the process exceeds its cpu resource allocation.  If
501	 * over max, arrange to kill the process in ast().
502	 */
503	if (p->p_cpulimit != RLIM_INFINITY &&
504	    p->p_runtime.sec > p->p_cpulimit) {
505		p->p_sflag |= PS_XCPU;
506		td->td_flags |= TDF_ASTPENDING;
507	}
508
509	/*
510	 * Finish up stats for outgoing thread.
511	 */
512	cnt.v_swtch++;
513	PCPU_SET(switchtime, new_switchtime);
514	PCPU_SET(switchticks, ticks);
515	CTR3(KTR_PROC, "mi_switch: old thread %p (pid %d, %s)", td, p->p_pid,
516	    p->p_comm);
517	if (td->td_proc->p_flag & P_SA)
518		thread_switchout(td);
519	sched_switch(td);
520
521	CTR3(KTR_PROC, "mi_switch: new thread %p (pid %d, %s)", td, p->p_pid,
522	    p->p_comm);
523
524	/*
525	 * If the last thread was exiting, finish cleaning it up.
526	 */
527	if ((td = PCPU_GET(deadthread))) {
528		PCPU_SET(deadthread, NULL);
529		thread_stash(td);
530	}
531}
532
533/*
534 * Change process state to be runnable,
535 * placing it on the run queue if it is in memory,
536 * and awakening the swapper if it isn't in memory.
537 */
538void
539setrunnable(struct thread *td)
540{
541	struct proc *p;
542
543	p = td->td_proc;
544	mtx_assert(&sched_lock, MA_OWNED);
545	switch (p->p_state) {
546	case PRS_ZOMBIE:
547		panic("setrunnable(1)");
548	default:
549		break;
550	}
551	switch (td->td_state) {
552	case TDS_RUNNING:
553	case TDS_RUNQ:
554		return;
555	case TDS_INHIBITED:
556		/*
557		 * If we are only inhibited because we are swapped out
558		 * then arange to swap in this process. Otherwise just return.
559		 */
560		if (td->td_inhibitors != TDI_SWAPPED)
561			return;
562		/* XXX: intentional fall-through ? */
563	case TDS_CAN_RUN:
564		break;
565	default:
566		printf("state is 0x%x", td->td_state);
567		panic("setrunnable(2)");
568	}
569	if ((p->p_sflag & PS_INMEM) == 0) {
570		if ((p->p_sflag & PS_SWAPPINGIN) == 0) {
571			p->p_sflag |= PS_SWAPINREQ;
572			wakeup(&proc0);
573		}
574	} else
575		sched_wakeup(td);
576}
577
578/*
579 * Compute a tenex style load average of a quantity on
580 * 1, 5 and 15 minute intervals.
581 * XXXKSE   Needs complete rewrite when correct info is available.
582 * Completely Bogus.. only works with 1:1 (but compiles ok now :-)
583 */
584static void
585loadav(void)
586{
587	int i, nrun;
588	struct loadavg *avg;
589	struct proc *p;
590	struct thread *td;
591
592	avg = &averunnable;
593	sx_slock(&allproc_lock);
594	nrun = 0;
595	FOREACH_PROC_IN_SYSTEM(p) {
596		FOREACH_THREAD_IN_PROC(p, td) {
597			switch (td->td_state) {
598			case TDS_RUNQ:
599			case TDS_RUNNING:
600				if ((p->p_flag & P_NOLOAD) != 0)
601					goto nextproc;
602				nrun++; /* XXXKSE */
603			default:
604				break;
605			}
606nextproc:
607			continue;
608		}
609	}
610	sx_sunlock(&allproc_lock);
611	for (i = 0; i < 3; i++)
612		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
613		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
614}
615
616/*
617 * Main loop for a kthread that executes loadav() periodically.
618 */
619static void
620loadav_thread(void *dummy)
621{
622	struct proc *p;
623	int nowake;
624
625	p = curthread->td_proc;
626	PROC_LOCK(p);
627	p->p_flag |= P_NOLOAD;
628	PROC_UNLOCK(p);
629	for (;;) {
630		loadav();
631		/*
632		 * Schedule the next update to occur after 5 seconds, but
633		 * add a random variation to avoid synchronisation with
634		 * processes that run at regular intervals.
635		 */
636		tsleep(&nowake, curthread->td_priority, "-", hz * 4 +
637		    (int)(random() % (hz * 2 + 1)));
638	}
639}
640
641static void
642lboltcb(void *arg)
643{
644	wakeup(&lbolt);
645	callout_reset(&lbolt_callout, hz, lboltcb, NULL);
646}
647
648/* ARGSUSED */
649static void
650synch_setup(dummy)
651	void *dummy;
652{
653	callout_init(&lbolt_callout, CALLOUT_MPSAFE);
654
655	/* Kick off timeout driven events by calling first time. */
656	lboltcb(NULL);
657
658	/* Kick off loadav kernel process. */
659	kthread_create(loadav_thread, NULL, NULL, 0, 0, "loadav");
660}
661
662/*
663 * General purpose yield system call
664 */
665int
666yield(struct thread *td, struct yield_args *uap)
667{
668	struct ksegrp *kg;
669
670	kg = td->td_ksegrp;
671	mtx_assert(&Giant, MA_NOTOWNED);
672	mtx_lock_spin(&sched_lock);
673	sched_prio(td, PRI_MAX_TIMESHARE);
674	mi_switch(SW_VOL);
675	mtx_unlock_spin(&sched_lock);
676	td->td_retval[0] = 0;
677	return (0);
678}
679