[libc] Linux threads - Set CLEAR_TID addr to 0 when exiting a detached thread.
authorSiva Chandra Reddy <sivachandra@google.com>
Sun, 10 Jul 2022 09:48:23 +0000 (09:48 +0000)
committerSiva Chandra Reddy <sivachandra@google.com>
Mon, 11 Jul 2022 04:42:59 +0000 (04:42 +0000)
A detached thread cleans itself up at completion. So, the CLEAR_TID memory is
also gone by the time the kernel tries to signal potential waiters. By nulling
the CLEAR_TID address, we prevent the kernel from signalling at a non-existent
futex location.

libc/src/__support/threads/linux/thread.cpp

index 30c1820..ff10a1e 100644 (file)
@@ -119,6 +119,10 @@ static void start_thread() __attribute__((noinline)) {
     // Thread is detached so cleanup the resources.
     if (attrib->owned_stack)
       free_stack(attrib->stack, attrib->stack_size);
+
+    // Set the CLEAR_TID address to nullptr to prevent the kernel
+    // from signalling at a non-existent futex location.
+    __llvm_libc::syscall(SYS_set_tid_address, 0);
   }
 
   __llvm_libc::syscall(SYS_exit, retval);