1*0f4c859eSApple OSS Distributions<h2>thread_switch</h2> 2*0f4c859eSApple OSS Distributions<hr> 3*0f4c859eSApple OSS Distributions<p> 4*0f4c859eSApple OSS Distributions<strong>Function</strong> - Cause context switch with options. 5*0f4c859eSApple OSS Distributions<h3>SYNOPSIS</h3> 6*0f4c859eSApple OSS Distributions<pre> 7*0f4c859eSApple OSS Distributions<strong>kern_return_t thread_switch</strong> 8*0f4c859eSApple OSS Distributions <strong>(mach_port_t</strong> <var>new_thread</var>, 9*0f4c859eSApple OSS Distributions <strong>int</strong> <var>option</var>, 10*0f4c859eSApple OSS Distributions <strong>mach_msg_timeout_t</strong> <var>time</var><strong>);</strong> 11*0f4c859eSApple OSS Distributions</pre> 12*0f4c859eSApple OSS Distributions<h3>PARAMETERS</h3> 13*0f4c859eSApple OSS Distributions<dl> 14*0f4c859eSApple OSS Distributions<p> 15*0f4c859eSApple OSS Distributions<dt> <var>new_thread</var> 16*0f4c859eSApple OSS Distributions<dd> 17*0f4c859eSApple OSS Distributions[in thread send right] 18*0f4c859eSApple OSS DistributionsThread to which the processor should switch 19*0f4c859eSApple OSS Distributionscontext. 20*0f4c859eSApple OSS Distributions<p> 21*0f4c859eSApple OSS Distributions<dt> <var>option</var> 22*0f4c859eSApple OSS Distributions<dd> 23*0f4c859eSApple OSS Distributions[in scalar] 24*0f4c859eSApple OSS DistributionsOptions applicable to the context switch. 25*0f4c859eSApple OSS Distributions<p> 26*0f4c859eSApple OSS Distributions<dt> <var>time</var> 27*0f4c859eSApple OSS Distributions<dd> 28*0f4c859eSApple OSS Distributions[in scalar] 29*0f4c859eSApple OSS DistributionsTime duration during which the thread should be affected by 30*0f4c859eSApple OSS Distributions<var>option</var>. 31*0f4c859eSApple OSS Distributions</dl> 32*0f4c859eSApple OSS Distributions<h3>DESCRIPTION</h3> 33*0f4c859eSApple OSS Distributions<p> 34*0f4c859eSApple OSS DistributionsThe <strong>thread_switch</strong> function provides low-level access 35*0f4c859eSApple OSS Distributionsto the scheduler's 36*0f4c859eSApple OSS Distributionscontext switching code. <var>new_thread</var> is a hint that implements 37*0f4c859eSApple OSS Distributionshand-off scheduling. 38*0f4c859eSApple OSS DistributionsThe operating system will attempt to switch directly to the new thread 39*0f4c859eSApple OSS Distributions(bypassing the normal logic that selects the next thread to run) 40*0f4c859eSApple OSS Distributionsif possible. Since this is a hint, it may be incorrect; it is ignored if it 41*0f4c859eSApple OSS Distributionsdoesn't specify a thread on the same host as the current thread or if 42*0f4c859eSApple OSS Distributionsthe scheduler cannot switch to that thread (i.e., 43*0f4c859eSApple OSS Distributionsnot runable or already running on another processor). In this 44*0f4c859eSApple OSS Distributionscase, the normal 45*0f4c859eSApple OSS Distributionslogic to select the next thread to run is used; the current thread 46*0f4c859eSApple OSS Distributionsmay continue 47*0f4c859eSApple OSS Distributionsrunning if there is no other appropriate thread to run. 48*0f4c859eSApple OSS Distributions<p> 49*0f4c859eSApple OSS DistributionsThe <var>option</var> argument specifies the interpretation and use of <var>time</var>. 50*0f4c859eSApple OSS DistributionsThe possible values (from \*L<mach/thread_switch.h>\*O) are: 51*0f4c859eSApple OSS Distributions<dl> 52*0f4c859eSApple OSS Distributions<dt> <strong>SWITCH_OPTION_NONE</strong> 53*0f4c859eSApple OSS Distributions<dd> 54*0f4c859eSApple OSS DistributionsThe <var>time</var> argument is ignored. 55*0f4c859eSApple OSS Distributions<dt> <strong>SWITCH_OPTION_WAIT</strong> 56*0f4c859eSApple OSS Distributions<dd> 57*0f4c859eSApple OSS DistributionsThe thread is blocked for the specified <var>time</var>. This wait cannot be 58*0f4c859eSApple OSS Distributionscanceled by <strong>thread_resume</strong>; 59*0f4c859eSApple OSS Distributionsonly <strong>thread_abort</strong> can terminate this wait. 60*0f4c859eSApple OSS Distributions<dt> <strong>SWITCH_OPTION_DEPRESS</strong> 61*0f4c859eSApple OSS Distributions<dd> 62*0f4c859eSApple OSS DistributionsThe thread's scheduling attributes are temporarily set so as to provide 63*0f4c859eSApple OSS Distributionsit with the lowest possible service for duration <var>time</var>. The scheduling 64*0f4c859eSApple OSS Distributionsdepression is aborted when <var>time</var> has passed, when the current thread is 65*0f4c859eSApple OSS Distributionsnext run (either via hand-off scheduling or because the processor set 66*0f4c859eSApple OSS Distributionshas nothing better to do), or when <strong>thread_abort</strong> or 67*0f4c859eSApple OSS Distributions<strong>thread_depress_abort</strong> is applied to the current thread. 68*0f4c859eSApple OSS DistributionsChanging the thread's scheduling attributes (via <strong>thread_policy</strong>) 69*0f4c859eSApple OSS Distributionswill not affect this depression. 70*0f4c859eSApple OSS Distributions<dt> <strong>SWITCH_OPTION_IDLE</strong> 71*0f4c859eSApple OSS Distributions<dd> 72*0f4c859eSApple OSS DistributionsThis option is similar to <strong>SWITCH_OPTION_DEPRESS</strong> however, the 73*0f4c859eSApple OSS Distributionsthread's scheduling attributes are temporarily set so as to place it 74*0f4c859eSApple OSS Distributionsat a service level that is below all runnable threads for 75*0f4c859eSApple OSS Distributionsduration <var>time</var>. The scheduling depression is aborted 76*0f4c859eSApple OSS Distributionswhen <var>time</var> has passed, when the current thread is 77*0f4c859eSApple OSS Distributionsnext run (either via hand-off scheduling or because the processor set 78*0f4c859eSApple OSS Distributionshas nothing better to do), or when <strong>thread_abort</strong> or 79*0f4c859eSApple OSS Distributions<strong>thread_depress_abort</strong> is applied to the current thread. 80*0f4c859eSApple OSS DistributionsChanging the thread's scheduling attributes (via <strong>thread_policy</strong>) 81*0f4c859eSApple OSS Distributionswill not affect this depression. 82*0f4c859eSApple OSS Distributions</dl> 83*0f4c859eSApple OSS Distributions<p> 84*0f4c859eSApple OSS DistributionsThe minimum time and units of time can be obtained as the <var>min_timeout</var> 85*0f4c859eSApple OSS Distributionsvalue from the <strong>HOST_SCHED_INFO</strong> flavor of <strong>host_info</strong>. 86*0f4c859eSApple OSS Distributions<h3>NOTES</h3> 87*0f4c859eSApple OSS Distributions<p> 88*0f4c859eSApple OSS Distributions<strong>thread_switch</strong> is often called when the current thread 89*0f4c859eSApple OSS Distributionscan proceed no further 90*0f4c859eSApple OSS Distributionsfor some reason; the various options and arguments allow information about 91*0f4c859eSApple OSS Distributionsthis reason to be transmitted to the kernel. The <var>new_thread</var> 92*0f4c859eSApple OSS Distributionsargument (hand-off 93*0f4c859eSApple OSS Distributionsscheduling) is useful when the identity of the thread that must make progress 94*0f4c859eSApple OSS Distributionsbefore the current thread runs again is known. The <strong>SWITCH_OPTION_WAIT</strong> 95*0f4c859eSApple OSS Distributionsoption is used when the amount of time that the current thread 96*0f4c859eSApple OSS Distributionsmust wait before it 97*0f4c859eSApple OSS Distributionscan do anything useful can be estimated and is fairly short, 98*0f4c859eSApple OSS Distributionsespecially when the 99*0f4c859eSApple OSS Distributionsidentity of the thread for which this thread must wait is not known. 100*0f4c859eSApple OSS Distributions<h3>CAUTIONS</h3> 101*0f4c859eSApple OSS Distributions<p> 102*0f4c859eSApple OSS DistributionsUsers should beware of calling <strong>thread_switch</strong> with an 103*0f4c859eSApple OSS Distributionsinvalid hint (e.g., <strong>THREAD_NULL</strong>) and no option. 104*0f4c859eSApple OSS DistributionsBecause the time-sharing scheduler varies the 105*0f4c859eSApple OSS Distributionspriority of threads based on usage, this may result in a waste 106*0f4c859eSApple OSS Distributionsof CPU time if the 107*0f4c859eSApple OSS Distributionsthread that must be run is of lower priority. The use of the 108*0f4c859eSApple OSS Distributions<strong>SWITCH_OPTION_DEPRESS</strong> option in this situation is highly recommended. 109*0f4c859eSApple OSS Distributions<p> 110*0f4c859eSApple OSS Distributions<strong>thread_switch</strong> ignores policies. Users relying on the 111*0f4c859eSApple OSS Distributionspreemption semantics of a 112*0f4c859eSApple OSS Distributionsfixed time policy should be aware that <strong>thread_switch</strong> 113*0f4c859eSApple OSS Distributionsignores these semantics; 114*0f4c859eSApple OSS Distributionsit will run the specified <var>new_thread</var> independent of its scheduling 115*0f4c859eSApple OSS Distributionsattributes and 116*0f4c859eSApple OSS Distributionsthose of any threads that could run instead. 117*0f4c859eSApple OSS Distributions<h3>RETURN VALUES</h3> 118*0f4c859eSApple OSS Distributions<p> 119*0f4c859eSApple OSS DistributionsOnly generic errors apply. 120*0f4c859eSApple OSS Distributions<h3>RELATED INFORMATION</h3> 121*0f4c859eSApple OSS Distributions<p> 122*0f4c859eSApple OSS DistributionsFunctions: 123*0f4c859eSApple OSS Distributions<a href="thread_abort.html"><strong>thread_abort</strong></a>, 124*0f4c859eSApple OSS Distributions<a href="thread_depress_abort.html"><strong>thread_depress_abort</strong></a>. 125