kern_synch.c revision 283735
1104862Sru/*-
2151497Sru * Copyright (c) 1982, 1986, 1990, 1991, 1993
3104862Sru *	The Regents of the University of California.  All rights reserved.
4114402Sru * (c) UNIX System Laboratories, Inc.
5104862Sru * All or some portions of this file are derived from material licensed
6104862Sru * to the University of California by American Telephone and Telegraph
7104862Sru * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8104862Sru * the permission of UNIX System Laboratories, Inc.
9104862Sru *
10104862Sru * Redistribution and use in source and binary forms, with or without
11104862Sru * modification, are permitted provided that the following conditions
12104862Sru * are met:
13104862Sru * 1. Redistributions of source code must retain the above copyright
14104862Sru *    notice, this list of conditions and the following disclaimer.
15104862Sru * 2. Redistributions in binary form must reproduce the above copyright
16104862Sru *    notice, this list of conditions and the following disclaimer in the
17104862Sru *    documentation and/or other materials provided with the distribution.
18104862Sru * 4. Neither the name of the University nor the names of its contributors
19104862Sru *    may be used to endorse or promote products derived from this software
20104862Sru *    without specific prior written permission.
21104862Sru *
22104862Sru * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23104862Sru * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24104862Sru * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25104862Sru * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26104862Sru * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27151497Sru * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28104862Sru * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29104862Sru * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30104862Sru * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31104862Sru * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32104862Sru * SUCH DAMAGE.
33104862Sru *
34104862Sru *	@(#)kern_synch.c	8.9 (Berkeley) 5/19/95
35104862Sru */
36104862Sru
37104862Sru#include <sys/cdefs.h>
38104862Sru__FBSDID("$FreeBSD: head/sys/kern/kern_synch.c 283735 2015-05-29 13:24:17Z kib $");
39104862Sru
40104862Sru#include "opt_ktrace.h"
41104862Sru#include "opt_sched.h"
42104862Sru
43104862Sru#include <sys/param.h>
44104862Sru#include <sys/systm.h>
45104862Sru#include <sys/condvar.h>
46104862Sru#include <sys/kdb.h>
47104862Sru#include <sys/kernel.h>
48151497Sru#include <sys/ktr.h>
49104862Sru#include <sys/lock.h>
50104862Sru#include <sys/mutex.h>
51104862Sru#include <sys/proc.h>
52104862Sru#include <sys/resourcevar.h>
53104862Sru#include <sys/sched.h>
54104862Sru#include <sys/sdt.h>
55104862Sru#include <sys/signalvar.h>
56104862Sru#include <sys/sleepqueue.h>
57104862Sru#include <sys/smp.h>
58104862Sru#include <sys/sx.h>
59104862Sru#include <sys/sysctl.h>
60104862Sru#include <sys/sysproto.h>
61104862Sru#include <sys/vmmeter.h>
62104862Sru#ifdef KTRACE
63104862Sru#include <sys/uio.h>
64104862Sru#include <sys/ktrace.h>
65104862Sru#endif
66104862Sru
67104862Sru#include <machine/cpu.h>
68104862Sru
69104862Sru#define	KTDSTATE(td)							\
70104862Sru	(((td)->td_inhibitors & TDI_SLEEPING) != 0 ? "sleep"  :		\
71104862Sru	((td)->td_inhibitors & TDI_SUSPENDED) != 0 ? "suspended" :	\
72104862Sru	((td)->td_inhibitors & TDI_SWAPPED) != 0 ? "swapped" :		\
73104862Sru	((td)->td_inhibitors & TDI_LOCK) != 0 ? "blocked" :		\
74104862Sru	((td)->td_inhibitors & TDI_IWAIT) != 0 ? "iwait" : "yielding")
75104862Sru
76104862Srustatic void synch_setup(void *dummy);
77104862SruSYSINIT(synch_setup, SI_SUB_KICK_SCHEDULER, SI_ORDER_FIRST, synch_setup,
78104862Sru    NULL);
79104862Sru
80104862Sruint	hogticks;
81104862Srustatic uint8_t pause_wchan[MAXCPU];
82104862Sru
83104862Srustatic struct callout loadav_callout;
84104862Sru
85104862Srustruct loadavg averunnable =
86104862Sru	{ {0, 0, 0}, FSCALE };	/* load average, of runnable procs */
87104862Sru/*
88104862Sru * Constants for averages over 1, 5, and 15 minutes
89104862Sru * when sampling at 5 second intervals.
90104862Sru */
91104862Srustatic fixpt_t cexp[3] = {
92104862Sru	0.9200444146293232 * FSCALE,	/* exp(-1/12) */
93104862Sru	0.9834714538216174 * FSCALE,	/* exp(-1/60) */
94104862Sru	0.9944598480048967 * FSCALE,	/* exp(-1/180) */
95151497Sru};
96104862Sru
97104862Sru/* kernel uses `FSCALE', userland (SHOULD) use kern.fscale */
98151497SruSYSCTL_INT(_kern, OID_AUTO, fscale, CTLFLAG_RD, SYSCTL_NULL_INT_PTR, FSCALE, "");
99104862Sru
100104862Srustatic void	loadav(void *arg);
101104862Sru
102104862SruSDT_PROVIDER_DECLARE(sched);
103151497SruSDT_PROBE_DEFINE(sched, , , preempt);
104104862Sru
105104862Srustatic void
106104862Srusleepinit(void *unused)
107104862Sru{
108104862Sru
109104862Sru	hogticks = (hz / 10) * 2;	/* Default only. */
110104862Sru	init_sleepqueues();
111104862Sru}
112104862Sru
113104862Sru/*
114104862Sru * vmem tries to lock the sleepq mutexes when free'ing kva, so make sure
115104862Sru * it is available.
116104862Sru */
117104862SruSYSINIT(sleepinit, SI_SUB_KMEM, SI_ORDER_ANY, sleepinit, 0);
118104862Sru
119104862Sru/*
120104862Sru * General sleep call.  Suspends the current thread until a wakeup is
121104862Sru * performed on the specified identifier.  The thread will then be made
122104862Sru * runnable with the specified priority.  Sleeps at most sbt units of time
123104862Sru * (0 means no timeout).  If pri includes the PCATCH flag, let signals
124104862Sru * interrupt the sleep, otherwise ignore them while sleeping.  Returns 0 if
125104862Sru * awakened, EWOULDBLOCK if the timeout expires.  If PCATCH is set and a
126104862Sru * signal becomes pending, ERESTART is returned if the current system
127104862Sru * call should be restarted if possible, and EINTR is returned if the system
128104862Sru * call should be interrupted by the signal (return EINTR).
129104862Sru *
130104862Sru * The lock argument is unlocked before the caller is suspended, and
131104862Sru * re-locked before _sleep() returns.  If priority includes the PDROP
132104862Sru * flag the lock is not re-locked before returning.
133 */
134int
135_sleep(void *ident, struct lock_object *lock, int priority,
136    const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags)
137{
138	struct thread *td;
139	struct proc *p;
140	struct lock_class *class;
141	uintptr_t lock_state;
142	int catch, pri, rval, sleepq_flags;
143	WITNESS_SAVE_DECL(lock_witness);
144
145	td = curthread;
146	p = td->td_proc;
147#ifdef KTRACE
148	if (KTRPOINT(td, KTR_CSW))
149		ktrcsw(1, 0, wmesg);
150#endif
151	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, lock,
152	    "Sleeping on \"%s\"", wmesg);
153	KASSERT(sbt != 0 || mtx_owned(&Giant) || lock != NULL,
154	    ("sleeping without a lock"));
155	KASSERT(p != NULL, ("msleep1"));
156	KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep"));
157	if (priority & PDROP)
158		KASSERT(lock != NULL && lock != &Giant.lock_object,
159		    ("PDROP requires a non-Giant lock"));
160	if (lock != NULL)
161		class = LOCK_CLASS(lock);
162	else
163		class = NULL;
164
165	if (cold || SCHEDULER_STOPPED()) {
166		/*
167		 * During autoconfiguration, just return;
168		 * don't run any other threads or panic below,
169		 * in case this is the idle thread and already asleep.
170		 * XXX: this used to do "s = splhigh(); splx(safepri);
171		 * splx(s);" to give interrupts a chance, but there is
172		 * no way to give interrupts a chance now.
173		 */
174		if (lock != NULL && priority & PDROP)
175			class->lc_unlock(lock);
176		return (0);
177	}
178	catch = priority & PCATCH;
179	pri = priority & PRIMASK;
180
181	/*
182	 * If we are already on a sleep queue, then remove us from that
183	 * sleep queue first.  We have to do this to handle recursive
184	 * sleeps.
185	 */
186	if (TD_ON_SLEEPQ(td))
187		sleepq_remove(td, td->td_wchan);
188
189	if ((uint8_t *)ident >= &pause_wchan[0] &&
190	    (uint8_t *)ident <= &pause_wchan[MAXCPU - 1])
191		sleepq_flags = SLEEPQ_PAUSE;
192	else
193		sleepq_flags = SLEEPQ_SLEEP;
194	if (catch)
195		sleepq_flags |= SLEEPQ_INTERRUPTIBLE;
196
197	sleepq_lock(ident);
198	CTR5(KTR_PROC, "sleep: thread %ld (pid %ld, %s) on %s (%p)",
199	    td->td_tid, p->p_pid, td->td_name, wmesg, ident);
200
201	if (lock == &Giant.lock_object)
202		mtx_assert(&Giant, MA_OWNED);
203	DROP_GIANT();
204	if (lock != NULL && lock != &Giant.lock_object &&
205	    !(class->lc_flags & LC_SLEEPABLE)) {
206		WITNESS_SAVE(lock, lock_witness);
207		lock_state = class->lc_unlock(lock);
208	} else
209		/* GCC needs to follow the Yellow Brick Road */
210		lock_state = -1;
211
212	/*
213	 * We put ourselves on the sleep queue and start our timeout
214	 * before calling thread_suspend_check, as we could stop there,
215	 * and a wakeup or a SIGCONT (or both) could occur while we were
216	 * stopped without resuming us.  Thus, we must be ready for sleep
217	 * when cursig() is called.  If the wakeup happens while we're
218	 * stopped, then td will no longer be on a sleep queue upon
219	 * return from cursig().
220	 */
221	sleepq_add(ident, lock, wmesg, sleepq_flags, 0);
222	if (sbt != 0)
223		sleepq_set_timeout_sbt(ident, sbt, pr, flags);
224	if (lock != NULL && class->lc_flags & LC_SLEEPABLE) {
225		sleepq_release(ident);
226		WITNESS_SAVE(lock, lock_witness);
227		lock_state = class->lc_unlock(lock);
228		sleepq_lock(ident);
229	}
230	if (sbt != 0 && catch)
231		rval = sleepq_timedwait_sig(ident, pri);
232	else if (sbt != 0)
233		rval = sleepq_timedwait(ident, pri);
234	else if (catch)
235		rval = sleepq_wait_sig(ident, pri);
236	else {
237		sleepq_wait(ident, pri);
238		rval = 0;
239	}
240#ifdef KTRACE
241	if (KTRPOINT(td, KTR_CSW))
242		ktrcsw(0, 0, wmesg);
243#endif
244	PICKUP_GIANT();
245	if (lock != NULL && lock != &Giant.lock_object && !(priority & PDROP)) {
246		class->lc_lock(lock, lock_state);
247		WITNESS_RESTORE(lock, lock_witness);
248	}
249	return (rval);
250}
251
252int
253msleep_spin_sbt(void *ident, struct mtx *mtx, const char *wmesg,
254    sbintime_t sbt, sbintime_t pr, int flags)
255{
256	struct thread *td;
257	struct proc *p;
258	int rval;
259	WITNESS_SAVE_DECL(mtx);
260
261	td = curthread;
262	p = td->td_proc;
263	KASSERT(mtx != NULL, ("sleeping without a mutex"));
264	KASSERT(p != NULL, ("msleep1"));
265	KASSERT(ident != NULL && TD_IS_RUNNING(td), ("msleep"));
266
267	if (cold || SCHEDULER_STOPPED()) {
268		/*
269		 * During autoconfiguration, just return;
270		 * don't run any other threads or panic below,
271		 * in case this is the idle thread and already asleep.
272		 * XXX: this used to do "s = splhigh(); splx(safepri);
273		 * splx(s);" to give interrupts a chance, but there is
274		 * no way to give interrupts a chance now.
275		 */
276		return (0);
277	}
278
279	sleepq_lock(ident);
280	CTR5(KTR_PROC, "msleep_spin: thread %ld (pid %ld, %s) on %s (%p)",
281	    td->td_tid, p->p_pid, td->td_name, wmesg, ident);
282
283	DROP_GIANT();
284	mtx_assert(mtx, MA_OWNED | MA_NOTRECURSED);
285	WITNESS_SAVE(&mtx->lock_object, mtx);
286	mtx_unlock_spin(mtx);
287
288	/*
289	 * We put ourselves on the sleep queue and start our timeout.
290	 */
291	sleepq_add(ident, &mtx->lock_object, wmesg, SLEEPQ_SLEEP, 0);
292	if (sbt != 0)
293		sleepq_set_timeout_sbt(ident, sbt, pr, flags);
294
295	/*
296	 * Can't call ktrace with any spin locks held so it can lock the
297	 * ktrace_mtx lock, and WITNESS_WARN considers it an error to hold
298	 * any spin lock.  Thus, we have to drop the sleepq spin lock while
299	 * we handle those requests.  This is safe since we have placed our
300	 * thread on the sleep queue already.
301	 */
302#ifdef KTRACE
303	if (KTRPOINT(td, KTR_CSW)) {
304		sleepq_release(ident);
305		ktrcsw(1, 0, wmesg);
306		sleepq_lock(ident);
307	}
308#endif
309#ifdef WITNESS
310	sleepq_release(ident);
311	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, "Sleeping on \"%s\"",
312	    wmesg);
313	sleepq_lock(ident);
314#endif
315	if (sbt != 0)
316		rval = sleepq_timedwait(ident, 0);
317	else {
318		sleepq_wait(ident, 0);
319		rval = 0;
320	}
321#ifdef KTRACE
322	if (KTRPOINT(td, KTR_CSW))
323		ktrcsw(0, 0, wmesg);
324#endif
325	PICKUP_GIANT();
326	mtx_lock_spin(mtx);
327	WITNESS_RESTORE(&mtx->lock_object, mtx);
328	return (rval);
329}
330
331/*
332 * pause() delays the calling thread by the given number of system ticks.
333 * During cold bootup, pause() uses the DELAY() function instead of
334 * the tsleep() function to do the waiting. The "timo" argument must be
335 * greater than or equal to zero. A "timo" value of zero is equivalent
336 * to a "timo" value of one.
337 */
338int
339pause_sbt(const char *wmesg, sbintime_t sbt, sbintime_t pr, int flags)
340{
341	KASSERT(sbt >= 0, ("pause: timeout must be >= 0"));
342
343	/* silently convert invalid timeouts */
344	if (sbt == 0)
345		sbt = tick_sbt;
346
347	if (cold || kdb_active) {
348		/*
349		 * We delay one second at a time to avoid overflowing the
350		 * system specific DELAY() function(s):
351		 */
352		while (sbt >= SBT_1S) {
353			DELAY(1000000);
354			sbt -= SBT_1S;
355		}
356		/* Do the delay remainder, if any */
357		sbt = (sbt + SBT_1US - 1) / SBT_1US;
358		if (sbt > 0)
359			DELAY(sbt);
360		return (0);
361	}
362	return (_sleep(&pause_wchan[curcpu], NULL, 0, wmesg, sbt, pr, flags));
363}
364
365/*
366 * Make all threads sleeping on the specified identifier runnable.
367 */
368void
369wakeup(void *ident)
370{
371	int wakeup_swapper;
372
373	sleepq_lock(ident);
374	wakeup_swapper = sleepq_broadcast(ident, SLEEPQ_SLEEP, 0, 0);
375	sleepq_release(ident);
376	if (wakeup_swapper) {
377		KASSERT(ident != &proc0,
378		    ("wakeup and wakeup_swapper and proc0"));
379		kick_proc0();
380	}
381}
382
383/*
384 * Make a thread sleeping on the specified identifier runnable.
385 * May wake more than one thread if a target thread is currently
386 * swapped out.
387 */
388void
389wakeup_one(void *ident)
390{
391	int wakeup_swapper;
392
393	sleepq_lock(ident);
394	wakeup_swapper = sleepq_signal(ident, SLEEPQ_SLEEP, 0, 0);
395	sleepq_release(ident);
396	if (wakeup_swapper)
397		kick_proc0();
398}
399
400static void
401kdb_switch(void)
402{
403	thread_unlock(curthread);
404	kdb_backtrace();
405	kdb_reenter();
406	panic("%s: did not reenter debugger", __func__);
407}
408
409/*
410 * The machine independent parts of context switching.
411 */
412void
413mi_switch(int flags, struct thread *newtd)
414{
415	uint64_t runtime, new_switchtime;
416	struct thread *td;
417
418	td = curthread;			/* XXX */
419	THREAD_LOCK_ASSERT(td, MA_OWNED | MA_NOTRECURSED);
420	KASSERT(!TD_ON_RUNQ(td), ("mi_switch: called by old code"));
421#ifdef INVARIANTS
422	if (!TD_ON_LOCK(td) && !TD_IS_RUNNING(td))
423		mtx_assert(&Giant, MA_NOTOWNED);
424#endif
425	KASSERT(td->td_critnest == 1 || panicstr,
426	    ("mi_switch: switch in a critical section"));
427	KASSERT((flags & (SW_INVOL | SW_VOL)) != 0,
428	    ("mi_switch: switch must be voluntary or involuntary"));
429	KASSERT(newtd != curthread, ("mi_switch: preempting back to ourself"));
430
431	/*
432	 * Don't perform context switches from the debugger.
433	 */
434	if (kdb_active)
435		kdb_switch();
436	if (SCHEDULER_STOPPED())
437		return;
438	if (flags & SW_VOL) {
439		td->td_ru.ru_nvcsw++;
440		td->td_swvoltick = ticks;
441	} else
442		td->td_ru.ru_nivcsw++;
443#ifdef SCHED_STATS
444	SCHED_STAT_INC(sched_switch_stats[flags & SW_TYPE_MASK]);
445#endif
446	/*
447	 * Compute the amount of time during which the current
448	 * thread was running, and add that to its total so far.
449	 */
450	new_switchtime = cpu_ticks();
451	runtime = new_switchtime - PCPU_GET(switchtime);
452	td->td_runtime += runtime;
453	td->td_incruntime += runtime;
454	PCPU_SET(switchtime, new_switchtime);
455	td->td_generation++;	/* bump preempt-detect counter */
456	PCPU_INC(cnt.v_swtch);
457	PCPU_SET(switchticks, ticks);
458	CTR4(KTR_PROC, "mi_switch: old thread %ld (td_sched %p, pid %ld, %s)",
459	    td->td_tid, td->td_sched, td->td_proc->p_pid, td->td_name);
460#if (KTR_COMPILE & KTR_SCHED) != 0
461	if (TD_IS_IDLETHREAD(td))
462		KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "idle",
463		    "prio:%d", td->td_priority);
464	else
465		KTR_STATE3(KTR_SCHED, "thread", sched_tdname(td), KTDSTATE(td),
466		    "prio:%d", td->td_priority, "wmesg:\"%s\"", td->td_wmesg,
467		    "lockname:\"%s\"", td->td_lockname);
468#endif
469	SDT_PROBE0(sched, , , preempt);
470	sched_switch(td, newtd, flags);
471	KTR_STATE1(KTR_SCHED, "thread", sched_tdname(td), "running",
472	    "prio:%d", td->td_priority);
473
474	CTR4(KTR_PROC, "mi_switch: new thread %ld (td_sched %p, pid %ld, %s)",
475	    td->td_tid, td->td_sched, td->td_proc->p_pid, td->td_name);
476
477	/*
478	 * If the last thread was exiting, finish cleaning it up.
479	 */
480	if ((td = PCPU_GET(deadthread))) {
481		PCPU_SET(deadthread, NULL);
482		thread_stash(td);
483	}
484}
485
486/*
487 * Change thread state to be runnable, placing it on the run queue if
488 * it is in memory.  If it is swapped out, return true so our caller
489 * will know to awaken the swapper.
490 */
491int
492setrunnable(struct thread *td)
493{
494
495	THREAD_LOCK_ASSERT(td, MA_OWNED);
496	KASSERT(td->td_proc->p_state != PRS_ZOMBIE,
497	    ("setrunnable: pid %d is a zombie", td->td_proc->p_pid));
498	switch (td->td_state) {
499	case TDS_RUNNING:
500	case TDS_RUNQ:
501		return (0);
502	case TDS_INHIBITED:
503		/*
504		 * If we are only inhibited because we are swapped out
505		 * then arange to swap in this process. Otherwise just return.
506		 */
507		if (td->td_inhibitors != TDI_SWAPPED)
508			return (0);
509		/* FALLTHROUGH */
510	case TDS_CAN_RUN:
511		break;
512	default:
513		printf("state is 0x%x", td->td_state);
514		panic("setrunnable(2)");
515	}
516	if ((td->td_flags & TDF_INMEM) == 0) {
517		if ((td->td_flags & TDF_SWAPINREQ) == 0) {
518			td->td_flags |= TDF_SWAPINREQ;
519			return (1);
520		}
521	} else
522		sched_wakeup(td);
523	return (0);
524}
525
526/*
527 * Compute a tenex style load average of a quantity on
528 * 1, 5 and 15 minute intervals.
529 */
530static void
531loadav(void *arg)
532{
533	int i, nrun;
534	struct loadavg *avg;
535
536	nrun = sched_load();
537	avg = &averunnable;
538
539	for (i = 0; i < 3; i++)
540		avg->ldavg[i] = (cexp[i] * avg->ldavg[i] +
541		    nrun * FSCALE * (FSCALE - cexp[i])) >> FSHIFT;
542
543	/*
544	 * Schedule the next update to occur after 5 seconds, but add a
545	 * random variation to avoid synchronisation with processes that
546	 * run at regular intervals.
547	 */
548	callout_reset_sbt(&loadav_callout,
549	    SBT_1US * (4000000 + (int)(random() % 2000001)), SBT_1US,
550	    loadav, NULL, C_DIRECT_EXEC | C_PREL(32));
551}
552
553/* ARGSUSED */
554static void
555synch_setup(void *dummy)
556{
557	callout_init(&loadav_callout, 1);
558
559	/* Kick off timeout driven events by calling first time. */
560	loadav(NULL);
561}
562
563int
564should_yield(void)
565{
566
567	return ((u_int)ticks - (u_int)curthread->td_swvoltick >= hogticks);
568}
569
570void
571maybe_yield(void)
572{
573
574	if (should_yield())
575		kern_yield(PRI_USER);
576}
577
578void
579kern_yield(int prio)
580{
581	struct thread *td;
582
583	td = curthread;
584	DROP_GIANT();
585	thread_lock(td);
586	if (prio == PRI_USER)
587		prio = td->td_user_pri;
588	if (prio >= 0)
589		sched_prio(td, prio);
590	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
591	thread_unlock(td);
592	PICKUP_GIANT();
593}
594
595/*
596 * General purpose yield system call.
597 */
598int
599sys_yield(struct thread *td, struct yield_args *uap)
600{
601
602	thread_lock(td);
603	if (PRI_BASE(td->td_pri_class) == PRI_TIMESHARE)
604		sched_prio(td, PRI_MAX_TIMESHARE);
605	mi_switch(SW_VOL | SWT_RELINQUISH, NULL);
606	thread_unlock(td);
607	td->td_retval[0] = 0;
608	return (0);
609}
610