Skip to content

Commit c8ef60a

Browse files
authored
Add support for pthread_getattr_np (#470)
1 parent a1b4def commit c8ef60a

4 files changed

Lines changed: 34 additions & 10 deletions

File tree

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
258258
thread/pthread_create.c \
259259
thread/pthread_detach.c \
260260
thread/pthread_equal.c \
261+
thread/pthread_getattr_np.c \
261262
thread/pthread_getspecific.c \
262263
thread/pthread_join.c \
263264
thread/pthread_key_create.c \

expected/wasm32-wasip1-threads/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1010,6 +1010,7 @@ pthread_condattr_setpshared
10101010
pthread_create
10111011
pthread_detach
10121012
pthread_equal
1013+
pthread_getattr_np
10131014
pthread_getspecific
10141015
pthread_join
10151016
pthread_key_create

libc-top-half/musl/src/env/__init_tls.c

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,25 +31,35 @@ extern unsigned char __global_base;
3131
extern weak unsigned char __stack_high;
3232
extern weak unsigned char __stack_low;
3333

34-
static inline void setup_default_stack_size()
34+
struct stack_bounds {
35+
void *base;
36+
size_t size;
37+
};
38+
39+
static inline struct stack_bounds get_stack_bounds()
3540
{
36-
ptrdiff_t stack_size;
41+
struct stack_bounds bounds;
3742

38-
if (&__stack_high)
39-
stack_size = &__stack_high - &__stack_low;
40-
else {
43+
if (&__stack_high) {
44+
bounds.base = &__stack_high;
45+
bounds.size = &__stack_high - &__stack_low;
46+
} else {
4147
unsigned char *sp;
4248
__asm__(
4349
".globaltype __stack_pointer, i32\n"
4450
"global.get __stack_pointer\n"
4551
"local.set %0\n"
4652
: "=r"(sp));
47-
stack_size = sp > &__global_base ? &__heap_base - &__data_end : (ptrdiff_t)&__global_base;
53+
if (sp > &__global_base) {
54+
bounds.base = &__heap_base;
55+
bounds.size = &__heap_base - &__data_end;
56+
} else {
57+
bounds.base = &__global_base;
58+
bounds.size = (size_t)&__global_base;
59+
}
4860
}
4961

50-
__default_stacksize =
51-
stack_size < DEFAULT_STACK_MAX ?
52-
stack_size : DEFAULT_STACK_MAX;
62+
return bounds;
5363
}
5464

5565
void __wasi_init_tp() {
@@ -68,8 +78,14 @@ int __init_tp(void *p)
6878
td->detach_state = DT_JOINABLE;
6979
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
7080
#else
71-
setup_default_stack_size();
81+
struct stack_bounds bounds = get_stack_bounds();
82+
__default_stacksize =
83+
bounds.size < DEFAULT_STACK_MAX ?
84+
bounds.size : DEFAULT_STACK_MAX;
7285
td->detach_state = DT_JOINABLE;
86+
td->stack = bounds.base;
87+
td->stack_size = bounds.size;
88+
td->guard_size = 0;
7389
/*
7490
* Initialize the TID to a value which doesn't conflict with
7591
* host-allocated TIDs, so that TID-based locks can work.

libc-top-half/musl/src/thread/pthread_getattr_np.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#define _GNU_SOURCE
22
#include "pthread_impl.h"
33
#include "libc.h"
4+
#ifdef __wasilibc_unmodified_upstream
45
#include <sys/mman.h>
6+
#endif
57

68
int pthread_getattr_np(pthread_t t, pthread_attr_t *a)
79
{
@@ -12,13 +14,17 @@ int pthread_getattr_np(pthread_t t, pthread_attr_t *a)
1214
a->_a_stackaddr = (uintptr_t)t->stack;
1315
a->_a_stacksize = t->stack_size;
1416
} else {
17+
#ifdef __wasilibc_unmodified_upstream
1518
char *p = (void *)libc.auxv;
1619
size_t l = PAGE_SIZE;
1720
p += -(uintptr_t)p & PAGE_SIZE-1;
1821
a->_a_stackaddr = (uintptr_t)p;
1922
while (mremap(p-l-PAGE_SIZE, PAGE_SIZE, 2*PAGE_SIZE, 0)==MAP_FAILED && errno==ENOMEM)
2023
l += PAGE_SIZE;
2124
a->_a_stacksize = l;
25+
#else
26+
return ENOSYS;
27+
#endif
2228
}
2329
return 0;
2430
}

0 commit comments

Comments
 (0)