Skip to content

Commit 7bd1746

Browse files
committed
fix: initialize struct pthread for the main thread
Signed-off-by: Harald Hoyer <[email protected]>
1 parent 2f577a8 commit 7bd1746

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

expected/wasm32-wasi/posix/defined-symbols.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ __getopt_msg
9090
__gmtime_r
9191
__hwcap
9292
__inet_aton
93+
__init_tp
9394
__intscan
9495
__invtrigl_R
9596
__isalnum_l
@@ -324,6 +325,7 @@ __wasi_fd_seek
324325
__wasi_fd_sync
325326
__wasi_fd_tell
326327
__wasi_fd_write
328+
__wasi_init_tp
327329
__wasi_path_create_directory
328330
__wasi_path_filestat_get
329331
__wasi_path_filestat_set_times

expected/wasm32-wasi/single/defined-symbols.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ __wasi_fd_seek
275275
__wasi_fd_sync
276276
__wasi_fd_tell
277277
__wasi_fd_write
278+
__wasi_init_tp
278279
__wasi_path_create_directory
279280
__wasi_path_filestat_get
280281
__wasi_path_filestat_set_times

libc-bottom-half/crt/crt1-command.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "libc.h"
12
#include <wasi/api.h>
23
extern void __wasm_call_ctors(void);
34
extern int __main_void(void);
@@ -7,6 +8,11 @@ extern void __wasm_call_dtors(void);
78
// that the `_start` function isn't started more than once.
89
static volatile int started = 0;
910

11+
static void dummy_0()
12+
{
13+
}
14+
weak_alias(dummy_0, __wasi_init_tp);
15+
1016
__attribute__((export_name("_start")))
1117
void _start(void) {
1218
// Don't allow the program to be called multiple times.
@@ -15,6 +21,8 @@ void _start(void) {
1521
}
1622
started = 1;
1723

24+
__wasi_init_tp();
25+
1826
// The linker synthesizes this to call constructors.
1927
__wasm_call_ctors();
2028

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,32 @@
1515

1616
volatile int __thread_list_lock;
1717

18-
#ifdef __wasilibc_unmodified_upstream
18+
#ifndef __wasilibc_unmodified_upstream
19+
void __wasi_init_tp() {
20+
__init_tp((void *)__get_tp());
21+
}
22+
#endif
23+
1924
int __init_tp(void *p)
2025
{
2126
pthread_t td = p;
2227
td->self = td;
28+
#ifdef __wasilibc_unmodified_upstream
2329
int r = __set_thread_area(TP_ADJ(p));
2430
if (r < 0) return -1;
2531
if (!r) libc.can_do_threads = 1;
2632
td->detach_state = DT_JOINABLE;
2733
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
34+
#endif
2835
td->locale = &libc.global_locale;
2936
td->robust_list.head = &td->robust_list.head;
3037
td->sysinfo = __sysinfo;
3138
td->next = td->prev = td;
3239
return 0;
3340
}
3441

42+
#ifdef __wasilibc_unmodified_upstream
43+
3544
static struct builtin_tls {
3645
char c;
3746
struct pthread pt;

0 commit comments

Comments
 (0)