- if (self->detached && self->map_base) {
- if (self->detached == 2)
- __syscall(SYS_set_tid_address, 0);
- __syscall(SYS_rt_sigprocmask, SIG_BLOCK,
- SIGALL_SET, 0, __SYSCALL_SSLEN);
+ __do_orphaned_stdio_locks();
+ __dl_thread_cleanup();
+
+ /* This atomic potentially competes with a concurrent pthread_detach
+ * call; the loser is responsible for freeing thread resources. */
+ int state = a_cas(&self->detach_state, DT_JOINABLE, DT_EXITING);
+
+ if (state>=DT_DETACHED && self->map_base) {
+ /* Detached threads must avoid the kernel clear_child_tid
+ * feature, since the virtual address will have been
+ * unmapped and possibly already reused by a new mapping
+ * at the time the kernel would perform the write. In
+ * the case of threads that started out detached, the
+ * initial clone flags are correct, but if the thread was
+ * detached later, we need to clear it here. */
+ if (state == DT_DYNAMIC) __syscall(SYS_set_tid_address, 0);
+
+ /* Robust list will no longer be valid, and was already
+ * processed above, so unregister it with the kernel. */
+ if (self->robust_list.off)
+ __syscall(SYS_set_robust_list, 0, 3*sizeof(long));
+
+ /* Since __unmapself bypasses the normal munmap code path,
+ * explicitly wait for vmlock holders first. */
+ __vm_wait();
+
+ /* The following call unmaps the thread's stack mapping
+ * and then exits without touching the stack. */