1*0f4c859eSApple OSS Distributions<h2>thread_abort_safely</h2> 2*0f4c859eSApple OSS Distributions<hr> 3*0f4c859eSApple OSS Distributions<p> 4*0f4c859eSApple OSS Distributions<strong>Function</strong> - Abort a thread, restartably. 5*0f4c859eSApple OSS Distributions<h3>SYNOPSIS</h3> 6*0f4c859eSApple OSS Distributions<pre> 7*0f4c859eSApple OSS Distributions<strong>kern_return_t thread_abort_safely</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_safely</strong> function aborts page faults and any message 21*0f4c859eSApple OSS Distributionsprimitive calls in use by <var>target_thread</var>. Scheduling depressions 22*0f4c859eSApple OSS Distributionsand clock sleeps are 23*0f4c859eSApple OSS Distributionsalso aborted. The call returns a code indicating that it was 24*0f4c859eSApple OSS Distributionsinterrupted. The call 25*0f4c859eSApple OSS Distributionsis interrupted even if the thread (or the task containing it) 26*0f4c859eSApple OSS Distributionsis suspended. 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_safely</strong> is to let 40*0f4c859eSApple OSS Distributionsone thread cleanly stop 41*0f4c859eSApple OSS Distributionsanother thread (<var>target_thread</var>). The target thread is stopped 42*0f4c859eSApple OSS Distributionsin such a manner that 43*0f4c859eSApple OSS Distributionsits future execution can be controlled in a predictable way. When 44*0f4c859eSApple OSS Distributions<strong>thread_abort_safely</strong> returns (if successful), the target 45*0f4c859eSApple OSS Distributionsthread will appear to have just 46*0f4c859eSApple OSS Distributionsreturned from the kernel (if it had 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_safely</strong> 66*0f4c859eSApple OSS Distributionsaborts the 67*0f4c859eSApple OSS Distributionskernel call from the thread's point of view. Specifically, it 68*0f4c859eSApple OSS Distributionsresets the kernel state so 69*0f4c859eSApple OSS Distributionsthat the thread will resume execution at the system call return, 70*0f4c859eSApple OSS Distributionswith the return 71*0f4c859eSApple OSS Distributionscode value set to one of the interrupted codes. The system call itself may 72*0f4c859eSApple OSS Distributionscompleted entirely, aborted entirely or be partially completed, 73*0f4c859eSApple OSS Distributionsdepending on when 74*0f4c859eSApple OSS Distributionsthe abort is received. As a result, if the thread's user state 75*0f4c859eSApple OSS Distributionshas been modified by 76*0f4c859eSApple OSS Distributions<strong>thread_set_state</strong>, it will not be altered un-predictably 77*0f4c859eSApple OSS Distributionsby any unexpected 78*0f4c859eSApple OSS Distributionssystem call side effects. 79*0f4c859eSApple OSS Distributions<p> 80*0f4c859eSApple OSS DistributionsFor example, to simulate a POSIX signal, use the following sequence of calls: 81*0f4c859eSApple OSS Distributions<dl> 82*0f4c859eSApple OSS Distributions<dd> 83*0f4c859eSApple OSS Distributions<strong>thread_suspend</strong>\(emTo stop the thread. 84*0f4c859eSApple OSS Distributions<dd> 85*0f4c859eSApple OSS Distributions<strong>thread_abort_safely</strong>\(emTo interrupt any system call in 86*0f4c859eSApple OSS Distributionsprogress and set the 87*0f4c859eSApple OSS Distributionsreturn value to "interrupted". Because the thread is already stopped, it will 88*0f4c859eSApple OSS Distributionsnot return to user code. 89*0f4c859eSApple OSS Distributions<dd> 90*0f4c859eSApple OSS Distributions<strong>thread_set_state</strong>\(emTo modify the thread's user state to simulate a 91*0f4c859eSApple OSS Distributionsprocedure call to the signal handler. 92*0f4c859eSApple OSS Distributions<dd> 93*0f4c859eSApple OSS Distributions<strong>thread_resume</strong>\(emTo resume execution at the signal handler. 94*0f4c859eSApple OSS DistributionsIf the thread's 95*0f4c859eSApple OSS Distributionsstack is set up correctly, the thread can return to the interrupted 96*0f4c859eSApple OSS Distributionssystem call. 97*0f4c859eSApple OSS DistributionsNote that the code to push an extra stack frame and change the registers is 98*0f4c859eSApple OSS Distributionshighly machine dependent. 99*0f4c859eSApple OSS Distributions</dl> 100*0f4c859eSApple OSS Distributions<h3>CAUTIONS</h3> 101*0f4c859eSApple OSS Distributions<p> 102*0f4c859eSApple OSS DistributionsAs a rule, do not use <strong>thread_abort_safely</strong> on a non-suspended 103*0f4c859eSApple OSS Distributionsthread. This 104*0f4c859eSApple OSS Distributionsoperation is very risky because it is difficult to know which 105*0f4c859eSApple OSS Distributionssystem trap, if any, is 106*0f4c859eSApple OSS Distributionsexecuting and whether an interrupt return will result in some useful action by 107*0f4c859eSApple OSS Distributionsthe thread. 108*0f4c859eSApple OSS Distributions<p> 109*0f4c859eSApple OSS Distributions<strong>thread_abort_safely</strong> will not abort any non-atomic operation 110*0f4c859eSApple OSS Distributions(such as a 111*0f4c859eSApple OSS Distributionsmulti-page <strong>memory_object_data_supply</strong> or exception processing) 112*0f4c859eSApple OSS Distributionsbut will return an 113*0f4c859eSApple OSS Distributionserror instead. The caller of this function must then allow the 114*0f4c859eSApple OSS Distributionsthread to resume 115*0f4c859eSApple OSS Distributionsand attempt to abort it later. If the thread must be aborted, 116*0f4c859eSApple OSS Distributionseven if doing so 117*0f4c859eSApple OSS Distributionswould abort any non-atomic operations, <strong>thread_abort</strong> would be used. 118*0f4c859eSApple OSS Distributions<h3>RETURN VALUES</h3> 119*0f4c859eSApple OSS Distributions<dl> 120*0f4c859eSApple OSS Distributions<p> 121*0f4c859eSApple OSS Distributions<dt> <strong>KERN_FAILURE</strong> 122*0f4c859eSApple OSS Distributions<dd> 123*0f4c859eSApple OSS DistributionsThe thread is in the middle of a non-restartable operation. 124*0f4c859eSApple OSS Distributions</dl> 125*0f4c859eSApple OSS Distributions<h3>RELATED INFORMATION</h3> 126*0f4c859eSApple OSS Distributions<p> 127*0f4c859eSApple OSS DistributionsFunctions: 128*0f4c859eSApple OSS Distributions<a href="mach_msg.html"><strong>mach_msg</strong></a>, 129*0f4c859eSApple OSS Distributions<a href="thread_get_state.html"><strong>thread_get_state</strong></a>, 130*0f4c859eSApple OSS Distributions<a href="thread_info.html"><strong>thread_info</strong></a>, 131*0f4c859eSApple OSS Distributions<a href="thread_set_state.html"><strong>thread_set_state</strong></a>, 132*0f4c859eSApple OSS Distributions<a href="thread_suspend.html"><strong>thread_suspend</strong></a>, 133*0f4c859eSApple OSS Distributions<a href="thread_terminate.html"><strong>thread_terminate</strong></a>, 134*0f4c859eSApple OSS Distributions<a href="thread_abort.html"><strong>thread_abort</strong></a>. 135