xref: /xnu-11215/osfmk/man/thread_abort.html (revision 186b8fce)
1*e13b1fa5SApple OSS Distributions<h2>thread_abort</h2>
2*e13b1fa5SApple OSS Distributions<hr>
3*e13b1fa5SApple OSS Distributions<p>
4*e13b1fa5SApple OSS Distributions<strong>Function</strong> - Abort a thread.
5*e13b1fa5SApple OSS Distributions<h3>SYNOPSIS</h3>
6*e13b1fa5SApple OSS Distributions<pre>
7*e13b1fa5SApple OSS Distributions<strong>kern_return_t   thread_abort</strong>
8*e13b1fa5SApple OSS Distributions                <strong>(thread_act_t</strong>                     <var>target_thread</var><strong>);</strong>
9*e13b1fa5SApple OSS Distributions</pre>
10*e13b1fa5SApple OSS Distributions<h3>PARAMETERS</h3>
11*e13b1fa5SApple OSS Distributions<dl>
12*e13b1fa5SApple OSS Distributions<p>
13*e13b1fa5SApple OSS Distributions<dt> <var>target_thread</var>
14*e13b1fa5SApple OSS Distributions<dd>
15*e13b1fa5SApple OSS Distributions[in thread send right]
16*e13b1fa5SApple OSS DistributionsThe thread to be aborted.
17*e13b1fa5SApple OSS Distributions</dl>
18*e13b1fa5SApple OSS Distributions<h3>DESCRIPTION</h3>
19*e13b1fa5SApple OSS Distributions<p>
20*e13b1fa5SApple OSS DistributionsThe <strong>thread_abort</strong> function aborts page faults and any
21*e13b1fa5SApple OSS Distributionsmessage primitive calls
22*e13b1fa5SApple OSS Distributionsin use by <var>target_thread</var>.  Scheduling depressions and clock sleeps are also
23*e13b1fa5SApple OSS Distributionsaborted.  The call returns a code indicating that it was interrupted.
24*e13b1fa5SApple OSS DistributionsThe call is
25*e13b1fa5SApple OSS Distributionsinterrupted even if the thread (or the task containing it) is
26*e13b1fa5SApple OSS Distributionssuspended.  If it is
27*e13b1fa5SApple OSS Distributionssuspended, the thread receives the interrupt when it resumes.
28*e13b1fa5SApple OSS Distributions<p>
29*e13b1fa5SApple OSS DistributionsIf its state is not modified before it resumes, the thread will
30*e13b1fa5SApple OSS Distributionsretry an aborted
31*e13b1fa5SApple OSS Distributionspage fault.  The Mach message trap returns either
32*e13b1fa5SApple OSS Distributions<strong>MACH_SEND_INTERRUPTED</strong> or <strong>MACH_RCV_INTERRUPTED</strong>, depending
33*e13b1fa5SApple OSS Distributionson whether the send or the
34*e13b1fa5SApple OSS Distributionsreceive side was interrupted.  Note, though, that the Mach message trap is
35*e13b1fa5SApple OSS Distributionscontained within the <strong>mach_msg</strong> library routine, which,
36*e13b1fa5SApple OSS Distributionsby default, retries
37*e13b1fa5SApple OSS Distributionsinterrupted message calls.
38*e13b1fa5SApple OSS Distributions<p>
39*e13b1fa5SApple OSS DistributionsThe basic purpose of <strong>thread_abort</strong> is to let one thread
40*e13b1fa5SApple OSS Distributionscleanly stop another thread (<var>target_thread</var>).
41*e13b1fa5SApple OSS DistributionsThe target thread is stopped in such a manner that its
42*e13b1fa5SApple OSS Distributionsfuture execution can be controlled in a predictable way.  When
43*e13b1fa5SApple OSS Distributions<strong>thread_abort</strong>
44*e13b1fa5SApple OSS Distributionsreturns, the target thread will appear to have just returned
45*e13b1fa5SApple OSS Distributionsfrom the kernel (if it
46*e13b1fa5SApple OSS Distributionshad been in kernel mode).
47*e13b1fa5SApple OSS Distributions<h3>NOTES</h3>
48*e13b1fa5SApple OSS Distributions<p>
49*e13b1fa5SApple OSS DistributionsBy way of comparison, the <strong>thread_suspend</strong> function keeps
50*e13b1fa5SApple OSS Distributionsthe target thread
51*e13b1fa5SApple OSS Distributionsfrom executing any further instructions at the user level, including
52*e13b1fa5SApple OSS Distributionsthe return
53*e13b1fa5SApple OSS Distributionsfrom a system call.  The <strong>thread_get_state</strong> function
54*e13b1fa5SApple OSS Distributionsreturns the thread's user
55*e13b1fa5SApple OSS Distributionsstate, while <strong>thread_set_state</strong> allows modification of the user state.
56*e13b1fa5SApple OSS Distributions<p>
57*e13b1fa5SApple OSS DistributionsA problem occurs if a suspended thread had been executing within a system
58*e13b1fa5SApple OSS Distributionscall.  In this case, the thread has, not only a user state, but
59*e13b1fa5SApple OSS Distributionsan associated kernel
60*e13b1fa5SApple OSS Distributionsstate. (The kernel state cannot be changed with <strong>thread_set_state</strong>.)
61*e13b1fa5SApple OSS DistributionsAs a result,
62*e13b1fa5SApple OSS Distributionswhen the thread resumes, the system call can return, producing a change in the
63*e13b1fa5SApple OSS Distributionsuser state and, possibly, user memory.
64*e13b1fa5SApple OSS Distributions<p>
65*e13b1fa5SApple OSS DistributionsFor a thread executing within a system call, <strong>thread_abort</strong>
66*e13b1fa5SApple OSS Distributionsaborts the kernel call
67*e13b1fa5SApple OSS Distributionsfrom the thread's point of view.  Specifically, it resets the
68*e13b1fa5SApple OSS Distributionskernel state so that the
69*e13b1fa5SApple OSS Distributionsthread will resume execution at the system call return, with the return code
70*e13b1fa5SApple OSS Distributionsvalue set to one of the interrupted codes.  The system call itself
71*e13b1fa5SApple OSS Distributionsmay be completed
72*e13b1fa5SApple OSS Distributionsentirely, aborted entirely or be partially completed, depending on when the
73*e13b1fa5SApple OSS Distributionsabort is received.  As a result, if the thread's user state has
74*e13b1fa5SApple OSS Distributionsbeen modified by
75*e13b1fa5SApple OSS Distributions<strong>thread_set_state</strong>, it will not be altered un-predictably
76*e13b1fa5SApple OSS Distributionsby any unexpected
77*e13b1fa5SApple OSS Distributionssystem call side effects.
78*e13b1fa5SApple OSS Distributions<p>
79*e13b1fa5SApple OSS DistributionsFor example, to simulate a POSIX signal, use the following sequence of calls:
80*e13b1fa5SApple OSS Distributions<dl>
81*e13b1fa5SApple OSS Distributions<dd>
82*e13b1fa5SApple OSS Distributions<strong>thread_suspend</strong>\(emTo stop the thread.
83*e13b1fa5SApple OSS Distributions<dd>
84*e13b1fa5SApple OSS Distributions<strong>thread_abort</strong>\(emTo interrupt any system call in progress
85*e13b1fa5SApple OSS Distributionsand set the return
86*e13b1fa5SApple OSS Distributionsvalue to "interrupted".  Because the thread is already stopped, it will not
87*e13b1fa5SApple OSS Distributionsreturn to user code.
88*e13b1fa5SApple OSS Distributions<dd>
89*e13b1fa5SApple OSS Distributions<strong>thread_set_state</strong>\(emTo modify the thread's user state to simulate a
90*e13b1fa5SApple OSS Distributionsprocedure call to the signal handler.
91*e13b1fa5SApple OSS Distributions<dd>
92*e13b1fa5SApple OSS Distributions<strong>thread_resume</strong>\(emTo resume execution at the signal handler.
93*e13b1fa5SApple OSS DistributionsIf the thread's
94*e13b1fa5SApple OSS Distributionsstack is set up correctly, the thread can return to the interrupted system call.
95*e13b1fa5SApple OSS DistributionsNote that the code to push an extra stack frame and change the registers is
96*e13b1fa5SApple OSS Distributionshighly machine dependent.
97*e13b1fa5SApple OSS Distributions</dl>
98*e13b1fa5SApple OSS Distributions<h3>CAUTIONS</h3>
99*e13b1fa5SApple OSS Distributions<p>
100*e13b1fa5SApple OSS DistributionsAs a rule, do not use <strong>thread_abort</strong> on a non-suspended
101*e13b1fa5SApple OSS Distributionsthread.  This operation
102*e13b1fa5SApple OSS Distributionsis very risky because it is difficult to know which system trap, if any, is
103*e13b1fa5SApple OSS Distributionsexecuting and whether an interrupt return will result in some
104*e13b1fa5SApple OSS Distributionsuseful action by the
105*e13b1fa5SApple OSS Distributionsthread.
106*e13b1fa5SApple OSS Distributions<p>
107*e13b1fa5SApple OSS Distributions<strong>thread_abort</strong> will abort any non-atomic operation (such as a multi-page
108*e13b1fa5SApple OSS Distributions<strong>memory_object_data_supply</strong>) at an arbitrary point in a non-restartable
109*e13b1fa5SApple OSS Distributionsway.  Such problems can be avoided by using <strong>thread_abort_safely</strong>.
110*e13b1fa5SApple OSS Distributions<h3>RETURN VALUES</h3>
111*e13b1fa5SApple OSS Distributions<dl>
112*e13b1fa5SApple OSS Distributions<p>
113*e13b1fa5SApple OSS Distributions<dt> <strong>KERN_EXCEPTION_PROTECTED</strong>
114*e13b1fa5SApple OSS Distributions<dd>
115*e13b1fa5SApple OSS DistributionsThe thread is processing a protected exception.
116*e13b1fa5SApple OSS Distributions</dl>
117*e13b1fa5SApple OSS Distributions<h3>RELATED INFORMATION</h3>
118*e13b1fa5SApple OSS Distributions<p>
119*e13b1fa5SApple OSS DistributionsFunctions:
120*e13b1fa5SApple OSS Distributions<a href="mach_msg.html"><strong>mach_msg</strong></a>,
121*e13b1fa5SApple OSS Distributions<a href="thread_get_state.html"><strong>thread_get_state</strong></a>,
122*e13b1fa5SApple OSS Distributions<a href="thread_info.html"><strong>thread_info</strong></a>,
123*e13b1fa5SApple OSS Distributions<a href="thread_set_state.html"><strong>thread_set_state</strong></a>,
124*e13b1fa5SApple OSS Distributions<a href="thread_suspend.html"><strong>thread_suspend</strong></a>,
125*e13b1fa5SApple OSS Distributions<a href="thread_terminate.html"><strong>thread_terminate</strong></a>,
126*e13b1fa5SApple OSS Distributions<a href="thread_abort_safely.html"><strong>thread_abort_safely</strong></a>,
127*e13b1fa5SApple OSS Distributions<a href="thread_set_exception_ports.html"><strong>thread_set_exception_ports</strong></a>.
128