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