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