[libc-commits] [libc] 3c5d631 - [libc][NFC] Move thread platform data pointer to thread attributes.

Siva Chandra Reddy via libc-commits libc-commits at lists.llvm.org
Wed Jul 13 00:11:06 PDT 2022


Author: Siva Chandra Reddy
Date: 2022-07-13T07:09:40Z
New Revision: 3c5d6312c4c8a184b5b3d08d51428f705e705ab9

URL: https://github.com/llvm/llvm-project/commit/3c5d6312c4c8a184b5b3d08d51428f705e705ab9
DIFF: https://github.com/llvm/llvm-project/commit/3c5d6312c4c8a184b5b3d08d51428f705e705ab9.diff

LOG: [libc][NFC] Move thread platform data pointer to thread attributes.

Along the way, added constexpr constructors to the Thread data
structures.

Added: 
    

Modified: 
    libc/include/llvm-libc-types/__thread_type.h
    libc/src/__support/threads/linux/thread.cpp
    libc/src/__support/threads/thread.h

Removed: 
    


################################################################################
diff  --git a/libc/include/llvm-libc-types/__thread_type.h b/libc/include/llvm-libc-types/__thread_type.h
index 1d64f909bd80..da5b898e5750 100644
--- a/libc/include/llvm-libc-types/__thread_type.h
+++ b/libc/include/llvm-libc-types/__thread_type.h
@@ -11,7 +11,6 @@
 
 typedef struct {
   void *__attrib;
-  void *__platform_attrib;
 } __thread_type;
 
 #endif // __LLVM_LIBC_TYPES_THREAD_TYPE_H__

diff  --git a/libc/src/__support/threads/linux/thread.cpp b/libc/src/__support/threads/linux/thread.cpp
index ff10a1ecbc65..89b3b4ff518a 100644
--- a/libc/src/__support/threads/linux/thread.cpp
+++ b/libc/src/__support/threads/linux/thread.cpp
@@ -99,7 +99,8 @@ __attribute__((always_inline)) inline uintptr_t get_start_args_addr() {
 #endif
 }
 
-static void start_thread() __attribute__((noinline)) {
+__attribute__((noinline))
+static void start_thread() {
   auto *start_args = reinterpret_cast<StartArgs *>(get_start_args_addr());
   auto *attrib = start_args->thread_attrib;
   long retval;
@@ -174,7 +175,7 @@ int Thread::run(ThreadStyle style, ThreadRunner runner, void *arg, void *stack,
   auto clear_tid = reinterpret_cast<cpp::Atomic<FutexWordType> *>(
       adjusted_stack + sizeof(StartArgs) + sizeof(ThreadAttributes));
   clear_tid->val = CLEAR_TID_VALUE;
-  platform_data = clear_tid;
+  attrib->platform_data = clear_tid;
 
   // The clone syscall takes arguments in an architecture specific order.
   // Also, we want the result of the syscall to be in a register as the child
@@ -252,7 +253,7 @@ void Thread::wait() {
   // If not, it is a spurious wake and we should continue to wait on
   // the futex.
   auto *clear_tid =
-      reinterpret_cast<cpp::Atomic<FutexWordType> *>(platform_data);
+      reinterpret_cast<cpp::Atomic<FutexWordType> *>(attrib->platform_data);
   while (clear_tid->load() != 0) {
     // We cannot do a FUTEX_WAIT_PRIVATE here as the kernel does a
     // FUTEX_WAKE and not a FUTEX_WAKE_PRIVATE.

diff  --git a/libc/src/__support/threads/thread.h b/libc/src/__support/threads/thread.h
index 36a191cbc046..dfafbed3b5f6 100644
--- a/libc/src/__support/threads/thread.h
+++ b/libc/src/__support/threads/thread.h
@@ -28,6 +28,7 @@ union ThreadRunner {
 union ThreadReturnValue {
   void *posix_retval;
   int stdc_retval;
+  constexpr ThreadReturnValue() : posix_retval(nullptr) {}
 };
 
 #if (defined(LLVM_LIBC_ARCH_AARCH64) || defined(LLVM_LIBC_ARCH_X86_64))
@@ -87,13 +88,21 @@ struct alignas(STACK_ALIGNMENT) ThreadAttributes {
   int tid;
   ThreadStyle style;
   ThreadReturnValue retval;
+  void *platform_data;
+
+  constexpr ThreadAttributes()
+      : detach_state(uint32_t(DetachState::DETACHED)), stack(nullptr),
+        tls(nullptr), stack_size(0), owned_stack(false), tid(-1),
+        style(ThreadStyle::POSIX), retval(),
+        platform_data(nullptr) {
+  }
 };
 
 struct Thread {
   ThreadAttributes *attrib;
-  void *platform_data;
 
-  Thread() = default;
+  constexpr Thread() : attrib(nullptr) {}
+  constexpr Thread(ThreadAttributes *attr) : attrib(attr) {}
 
   int run(ThreadRunnerPosix *func, void *arg, void *stack, size_t size,
           bool detached = false) {


        


More information about the libc-commits mailing list