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