1 <h2>thread_abort_safely
</h2>
4 <strong>Function
</strong> - Abort a thread, restartably.
7 <strong>kern_return_t thread_abort_safely
</strong>
8 <strong>(thread_act_t
</strong> <var>target_thread
</var><strong>);
</strong>
13 <dt> <var>target_thread
</var>
15 [in thread send right]
16 The thread to be aborted.
20 The
<strong>thread_abort_safely
</strong> function aborts page faults and any message
21 primitive calls in use by
<var>target_thread
</var>. Scheduling depressions
23 also aborted. The call returns a code indicating that it was
25 is interrupted even if the thread (or the task containing it)
26 is suspended. If it is
27 suspended, the thread receives the interrupt when it resumes.
29 If its state is not modified before it resumes, the thread will
31 page fault. The Mach message trap returns either
32 <strong>MACH_SEND_INTERRUPTED
</strong> or
<strong>MACH_RCV_INTERRUPTED
</strong>, depending
33 on whether the send or the
34 receive side was interrupted. Note, though, that the Mach message trap is
35 contained within the
<strong>mach_msg
</strong> library routine, which,
37 interrupted message calls.
39 The basic purpose of
<strong>thread_abort_safely
</strong> is to let
40 one thread cleanly stop
41 another thread (
<var>target_thread
</var>). The target thread is stopped
43 its future execution can be controlled in a predictable way. When
44 <strong>thread_abort_safely
</strong> returns (if successful), the target
45 thread will appear to have just
46 returned from the kernel (if it had been in kernel mode).
49 By way of comparison, the
<strong>thread_suspend
</strong> function keeps
51 from executing any further instructions at the user level, including
53 from a system call. The
<strong>thread_get_state
</strong> function
54 returns the thread's user
55 state, while
<strong>thread_set_state
</strong> allows modification of the user state.
57 A problem occurs if a suspended thread had been executing within a system
58 call. In this case, the thread has, not only a user state, but
60 state. (The kernel state cannot be changed with
<strong>thread_set_state
</strong>.)
62 when the thread resumes, the system call can return, producing a change in the
63 user state and, possibly, user memory.
65 For a thread executing within a system call,
<strong>thread_abort_safely
</strong>
67 kernel call from the thread's point of view. Specifically, it
68 resets the kernel state so
69 that the thread will resume execution at the system call return,
71 code value set to one of the interrupted codes. The system call itself may
72 completed entirely, aborted entirely or be partially completed,
74 the abort is received. As a result, if the thread's user state
76 <strong>thread_set_state
</strong>, it will not be altered un-predictably
78 system call side effects.
80 For example, to simulate a POSIX signal, use the following sequence of calls:
83 <strong>thread_suspend
</strong>\(emTo stop the thread.
85 <strong>thread_abort_safely
</strong>\(emTo interrupt any system call in
87 return value to "interrupted". Because the thread is already stopped, it will
88 not return to user code.
90 <strong>thread_set_state
</strong>\(emTo modify the thread's user state to simulate a
91 procedure call to the signal handler.
93 <strong>thread_resume
</strong>\(emTo resume execution at the signal handler.
95 stack is set up correctly, the thread can return to the interrupted
97 Note that the code to push an extra stack frame and change the registers is
98 highly machine dependent.
102 As a rule, do not use
<strong>thread_abort_safely
</strong> on a non-suspended
104 operation is very risky because it is difficult to know which
105 system trap, if any, is
106 executing and whether an interrupt return will result in some useful action by
109 <strong>thread_abort_safely
</strong> will not abort any non-atomic operation
111 multi-page
<strong>memory_object_data_supply
</strong> or exception processing)
113 error instead. The caller of this function must then allow the
115 and attempt to abort it later. If the thread must be aborted,
117 would abort any non-atomic operations,
<strong>thread_abort
</strong> would be used.
118 <h3>RETURN VALUES
</h3>
121 <dt> <strong>KERN_FAILURE
</strong>
123 The thread is in the middle of a non-restartable operation.
125 <h3>RELATED INFORMATION
</h3>
128 <a href=
"mach_msg.html"><strong>mach_msg
</strong></a>,
129 <a href=
"thread_get_state.html"><strong>thread_get_state
</strong></a>,
130 <a href=
"thread_info.html"><strong>thread_info
</strong></a>,
131 <a href=
"thread_set_state.html"><strong>thread_set_state
</strong></a>,
132 <a href=
"thread_suspend.html"><strong>thread_suspend
</strong></a>,
133 <a href=
"thread_terminate.html"><strong>thread_terminate
</strong></a>,
134 <a href=
"thread_abort.html"><strong>thread_abort
</strong></a>.