1*30cd6fd7SMitchell Horne.\" 2*30cd6fd7SMitchell Horne.\" Copyright (c) 2023 The FreeBSD Foundation 3*30cd6fd7SMitchell Horne.\" 4*30cd6fd7SMitchell Horne.\" This documentation was written by Mitchell Horne <mhorne@FreeBSD.org> under 5*30cd6fd7SMitchell Horne.\" sponsorship from the FreeBSD Foundation. 6*30cd6fd7SMitchell Horne.\" 7*30cd6fd7SMitchell Horne.\" Redistribution and use in source and binary forms, with or without 8*30cd6fd7SMitchell Horne.\" modification, are permitted provided that the following conditions 9*30cd6fd7SMitchell Horne.\" are met: 10*30cd6fd7SMitchell Horne.\" 1. Redistributions of source code must retain the above copyright 11*30cd6fd7SMitchell Horne.\" notice, this list of conditions and the following disclaimer. 12*30cd6fd7SMitchell Horne.\" 2. Redistributions in binary form must reproduce the above copyright 13*30cd6fd7SMitchell Horne.\" notice, this list of conditions and the following disclaimer in the 14*30cd6fd7SMitchell Horne.\" documentation and/or other materials provided with the distribution. 15*30cd6fd7SMitchell Horne.\" 16*30cd6fd7SMitchell Horne.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17*30cd6fd7SMitchell Horne.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18*30cd6fd7SMitchell Horne.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19*30cd6fd7SMitchell Horne.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20*30cd6fd7SMitchell Horne.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21*30cd6fd7SMitchell Horne.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22*30cd6fd7SMitchell Horne.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23*30cd6fd7SMitchell Horne.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24*30cd6fd7SMitchell Horne.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25*30cd6fd7SMitchell Horne.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26*30cd6fd7SMitchell Horne.\" SUCH DAMAGE. 27*30cd6fd7SMitchell Horne.\" 28*30cd6fd7SMitchell Horne.Dd January 30, 2023 29*30cd6fd7SMitchell Horne.Dt KERN_YIELD 9 30*30cd6fd7SMitchell Horne.Os 31*30cd6fd7SMitchell Horne.Sh NAME 32*30cd6fd7SMitchell Horne.Nm kern_yield , 33*30cd6fd7SMitchell Horne.Nm maybe_yield , 34*30cd6fd7SMitchell Horne.Nm should_yield 35*30cd6fd7SMitchell Horne.Nd "functions for yielding execution of the current thread" 36*30cd6fd7SMitchell Horne.Sh SYNOPSIS 37*30cd6fd7SMitchell Horne.Ft void 38*30cd6fd7SMitchell Horne.Fn kern_yield "int prio" 39*30cd6fd7SMitchell Horne.Ft void 40*30cd6fd7SMitchell Horne.Fn maybe_yield 41*30cd6fd7SMitchell Horne.Ft bool 42*30cd6fd7SMitchell Horne.Fn should_yield 43*30cd6fd7SMitchell Horne.Sh DESCRIPTION 44*30cd6fd7SMitchell HorneThe 45*30cd6fd7SMitchell Horne.Fn kern_yield 46*30cd6fd7SMitchell Hornefunction causes the currently running thread to voluntarily, but 47*30cd6fd7SMitchell Horneunconditionally, surrender its execution to the scheduler. 48*30cd6fd7SMitchell HorneThe 49*30cd6fd7SMitchell Horne.Va prio 50*30cd6fd7SMitchell Horneargument specifies the scheduling priority to be assigned before the context 51*30cd6fd7SMitchell Horneswitch, which has an influence on when execution will resume. 52*30cd6fd7SMitchell HorneNote that the requested priority will take effect until the thread returns to 53*30cd6fd7SMitchell Horneusermode, after which its base user priority will be restored. 54*30cd6fd7SMitchell HorneValid values for 55*30cd6fd7SMitchell Horne.Va prio 56*30cd6fd7SMitchell Horneare any of the 57*30cd6fd7SMitchell Horne.Dv PRI_* 58*30cd6fd7SMitchell Hornevalues defined in 59*30cd6fd7SMitchell Horne.In sys/priority.h , 60*30cd6fd7SMitchell Horneas well as the following special values: 61*30cd6fd7SMitchell Horne.Bl -tag -width "PRI_UNCHANGED" 62*30cd6fd7SMitchell Horne.It Dv PRI_USER 63*30cd6fd7SMitchell HorneSchedule the thread with its base user priority; the value corresponding to 64*30cd6fd7SMitchell Horne.Xr setpriority 2 / 65*30cd6fd7SMitchell Horne.Xr nice 3 . 66*30cd6fd7SMitchell Horne.It Dv PRI_UNCHANGED 67*30cd6fd7SMitchell HorneYield the thread without changing its priority. 68*30cd6fd7SMitchell Horne.El 69*30cd6fd7SMitchell Horne.Pp 70*30cd6fd7SMitchell HorneThe 71*30cd6fd7SMitchell Horne.Fn should_yield 72*30cd6fd7SMitchell Hornefunction checks if sufficient time has passed since the thread's last voluntary 73*30cd6fd7SMitchell Hornecontext switch that yielding would be a useful service to other threads. 74*30cd6fd7SMitchell HorneIt returns 75*30cd6fd7SMitchell Horne.Va true 76*30cd6fd7SMitchell Hornewhen this is the case. 77*30cd6fd7SMitchell HorneSee 78*30cd6fd7SMitchell Horne.Sx USAGE NOTES 79*30cd6fd7SMitchell Hornefor an elaboration of what this means. 80*30cd6fd7SMitchell Horne.Pp 81*30cd6fd7SMitchell HorneThe 82*30cd6fd7SMitchell Horne.Fn maybe_yield 83*30cd6fd7SMitchell Hornefunction is a helper function for the common task of optionally yielding the 84*30cd6fd7SMitchell Horneprocessor. 85*30cd6fd7SMitchell HorneInternally, 86*30cd6fd7SMitchell Horne.Fn kern_yield "PRI_USER" 87*30cd6fd7SMitchell Hornewill be called if 88*30cd6fd7SMitchell Horne.Fn should_yield 89*30cd6fd7SMitchell Hornereturns 90*30cd6fd7SMitchell Horne.Va true . 91*30cd6fd7SMitchell Horne.Sh USAGE NOTES 92*30cd6fd7SMitchell HorneAlthough the kernel supports preemption, this is usually reserved for 93*30cd6fd7SMitchell Hornehigh-priority realtime or interrupt threads. 94*30cd6fd7SMitchell HorneKernel worker threads and timesharing threads are not guaranteed to preempt 95*30cd6fd7SMitchell Horneeach another. 96*30cd6fd7SMitchell HorneThus, threads executing in the kernel are expected to behave cooperatively 97*30cd6fd7SMitchell Hornewith respect to other threads in the system. 98*30cd6fd7SMitchell HorneThe yield functions are mostly intended to be used by threads which perform a 99*30cd6fd7SMitchell Hornelot of work inside the kernel. 100*30cd6fd7SMitchell HorneFor example: 101*30cd6fd7SMitchell Horne.Fn maybe_yield 102*30cd6fd7SMitchell Horneis called by the 103*30cd6fd7SMitchell Horne.Dv vlnru 104*30cd6fd7SMitchell Horneprocess each time it reclaims a vnode. 105*30cd6fd7SMitchell Horne.Pp 106*30cd6fd7SMitchell HorneThe scheduler aims to identify threads which monopolize the CPU, and will 107*30cd6fd7SMitchell Horneschedule them with decreased priority. 108*30cd6fd7SMitchell HorneThreads which regularly yield the processor will be given the chance to run 109*30cd6fd7SMitchell Hornemore often. 110*30cd6fd7SMitchell HorneThe possibly surprising effect of this is that, depending on the disposition of 111*30cd6fd7SMitchell Horneother threads on the CPU's runqueue, a call to 112*30cd6fd7SMitchell Horne.Fn kern_yield 113*30cd6fd7SMitchell Hornedoes not guarantee that the yielding thread will be taken off the CPU. 114*30cd6fd7SMitchell Horne.Pp 115*30cd6fd7SMitchell HorneWith the above considerations in mind, it is advised that code written using 116*30cd6fd7SMitchell Horne.Fn kern_yield 117*30cd6fd7SMitchell Hornebe measured to confirm that its use has a positive effect on relevant 118*30cd6fd7SMitchell Horneperformance or responsiveness metrics. 119*30cd6fd7SMitchell HorneSwitching thread contexts has a non-zero cost, and thus yielding the processor 120*30cd6fd7SMitchell Hornetoo eagerly could have a negative impact on performance. 121*30cd6fd7SMitchell Horne.Pp 122*30cd6fd7SMitchell HorneAdditionally, since yielding is a cooperative action, it is advised that the 123*30cd6fd7SMitchell Horneyielding thread release any locks that it may be holding, when possible. 124*30cd6fd7SMitchell HorneOtherwise, threads which have been given the chance to run could end up waiting 125*30cd6fd7SMitchell Horneon the yielding thread to release the lock, largely defeating the purpose of 126*30cd6fd7SMitchell Hornethe yield. 127*30cd6fd7SMitchell Horne.Sh SEE ALSO 128*30cd6fd7SMitchell Horne.Xr setpriority 2 , 129*30cd6fd7SMitchell Horne.Xr nice 3 , 130*30cd6fd7SMitchell Horne.Xr mi_switch 9 131*30cd6fd7SMitchell Horne.Sh AUTHORS 132*30cd6fd7SMitchell Horne.An -nosplit 133*30cd6fd7SMitchell HorneThis manual page was written by 134*30cd6fd7SMitchell Horne.An Mitchell Horne Aq Mt mhorne@FreeBSD.org . 135