Skip to content

Commit 3c5d631

Browse files
author
Siva Chandra Reddy
committed
[libc][NFC] Move thread platform data pointer to thread attributes.
Along the way, added constexpr constructors to the Thread data structures.
1 parent 9049c46 commit 3c5d631

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

libc/include/llvm-libc-types/__thread_type.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
typedef struct {
1313
void *__attrib;
14-
void *__platform_attrib;
1514
} __thread_type;
1615

1716
#endif // __LLVM_LIBC_TYPES_THREAD_TYPE_H__

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ __attribute__((always_inline)) inline uintptr_t get_start_args_addr() {
9999
#endif
100100
}
101101

102-
static void start_thread() __attribute__((noinline)) {
102+
__attribute__((noinline))
103+
static void start_thread() {
103104
auto *start_args = reinterpret_cast<StartArgs *>(get_start_args_addr());
104105
auto *attrib = start_args->thread_attrib;
105106
long retval;
@@ -174,7 +175,7 @@ int Thread::run(ThreadStyle style, ThreadRunner runner, void *arg, void *stack,
174175
auto clear_tid = reinterpret_cast<cpp::Atomic<FutexWordType> *>(
175176
adjusted_stack + sizeof(StartArgs) + sizeof(ThreadAttributes));
176177
clear_tid->val = CLEAR_TID_VALUE;
177-
platform_data = clear_tid;
178+
attrib->platform_data = clear_tid;
178179

179180
// The clone syscall takes arguments in an architecture specific order.
180181
// Also, we want the result of the syscall to be in a register as the child
@@ -252,7 +253,7 @@ void Thread::wait() {
252253
// If not, it is a spurious wake and we should continue to wait on
253254
// the futex.
254255
auto *clear_tid =
255-
reinterpret_cast<cpp::Atomic<FutexWordType> *>(platform_data);
256+
reinterpret_cast<cpp::Atomic<FutexWordType> *>(attrib->platform_data);
256257
while (clear_tid->load() != 0) {
257258
// We cannot do a FUTEX_WAIT_PRIVATE here as the kernel does a
258259
// FUTEX_WAKE and not a FUTEX_WAKE_PRIVATE.

libc/src/__support/threads/thread.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ union ThreadRunner {
2828
union ThreadReturnValue {
2929
void *posix_retval;
3030
int stdc_retval;
31+
constexpr ThreadReturnValue() : posix_retval(nullptr) {}
3132
};
3233

3334
#if (defined(LLVM_LIBC_ARCH_AARCH64) || defined(LLVM_LIBC_ARCH_X86_64))
@@ -87,13 +88,21 @@ struct alignas(STACK_ALIGNMENT) ThreadAttributes {
8788
int tid;
8889
ThreadStyle style;
8990
ThreadReturnValue retval;
91+
void *platform_data;
92+
93+
constexpr ThreadAttributes()
94+
: detach_state(uint32_t(DetachState::DETACHED)), stack(nullptr),
95+
tls(nullptr), stack_size(0), owned_stack(false), tid(-1),
96+
style(ThreadStyle::POSIX), retval(),
97+
platform_data(nullptr) {
98+
}
9099
};
91100

92101
struct Thread {
93102
ThreadAttributes *attrib;
94-
void *platform_data;
95103

96-
Thread() = default;
104+
constexpr Thread() : attrib(nullptr) {}
105+
constexpr Thread(ThreadAttributes *attr) : attrib(attr) {}
97106

98107
int run(ThreadRunnerPosix *func, void *arg, void *stack, size_t size,
99108
bool detached = false) {

0 commit comments

Comments
 (0)