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