130 | | * If it is running in DETACHED mode, the calling thread T sets the FLAG_SUICIDE bit in the "flags" bit_vector, registers the BLOCKED_GLOBAL bit in the "blocked" bit_vector, and de-schedule. The scheduler, detecting the FLAG_SUICIDE bit, remove the thread from the scheduler list, remove the thread from its process, and destroys the thread descriptor. |
131 | | * If it is running in ATTACHED mode, the calling thread T sets the BLOCKED_EXIT bit in the "blocked" bit vector and de-schedule. The FLAG_KILL bit in the "flags" bit-vector, and the BLOCKED_EXIT bit are set by the parent thread TP (using remote accesses) when it executes the pthread_join(), and detects the BLOCKED_EXIT bit in thread T. The scenario is then the kill scenario described below. |
| 130 | The thread_kill() function must be executed by a thread running in kernel mode in the same cluster as the target thread. It is called by the pthread_cancel() system call, to destroy one single thread, or by the kill() system call, to destroy all thread of a given process. The killer thread requires the target thread scheduler to do the job by writing in the target thread descriptor, and the target scheduler signals completion to the killer by writing in the killer thread descriptor. |
133 | | === 5.2) thread_kill === |
| 132 | * To request the kill, the killer thread sets the BLOCKED_GLOBAL bit in the target thread "blocked" field, sets the SIG_KILL bit in the target thread "signals" field, and register in the target thread "kill_rsp" field the address of a response counter allocated in the killer thread stack. |
| 133 | * If the target thread is running on another core than the killer thread, the killer thread send an IPI to the core running the target thread, to ask the target scheduler to handle the request. If the target is running on the same thread as the killer, the killer thread calls directly the sched_handle_signal() function. |
| 134 | * In both cases, the sched_handle_signals() function - detecting the SIG_KILL signal - detach the target thread from the scheduler, detach the target thread from the local process descriptor, release the memory allocated to the target thread descriptor, and atomically decrement the response counter in the killer thread to signal completion. |
135 | | The killer thread uses a remote access to set the BLOCKED_GLOBAL bit in the target thread "blocked" bit_vector, set the FLAG_KILL bit in the target thread "signals" bit_vector, and send an IPI to the target thread core to force scheduling. |
136 | | The target thread scheduler, detecting the FLAG_KILL bit set, removes the thread from the scheduler list, removes the thread from the local process descriptor, and finally destroys the thread descriptor. |
| 136 | === 5.2) thread_exit() when DETACHED === |
| 137 | |
| 138 | The thread_exit() is called by an user thread T executing the pthread_exit system call to suicide. The scenario is rather simple when the thread T is not running in ATTACHED mode. |
| 139 | |
| 140 | * The thread_exit() function sets the SIG_SUICIDE bit in the thread "signals" bit_vector, sets the BLOCKED_GLOBAL bit in the thread "blocked" bit_vector, and de-schedule. |
| 141 | * The scheduler, detecting the SIG_SUICIDE bit, detach the thread from the scheduler, detach the thread from the local process descriptor, and releases the memory allocated to the thread descriptor. |
| 142 | |
| 143 | == 5.3) thread_exit when ATTACHED === |
| 144 | |
| 145 | The thread_exit() scenario is more complex if the finishing thread T is running in ATTACHED mode, because another thread PT executing the pthread_join() system call must be informed of the exit of thread T. As the pthread_exit() can happen before of after the pthread_join(), this requires a synchronisation. This synchronisation uses three fields in thread descriptor: |
| 146 | |
| 147 | remote_spin_lock implemented in the T thread descriptor, |
| 148 | |
| 149 | * The thread T try to take the locksets the BLOCKED_EXIT bit in the "blocked" bit vector and de-schedule. The FLAG_KILL bit in the "flags" bit-vector, and the BLOCKED_EXIT bit are set by the parent thread TP (using remote accesses) when it executes the pthread_join(), and detects the BLOCKED_EXIT bit in thread T. The scenario is then the kill scenario described below. |
| 150 | * The thread PT try to take the lock |