Skip to content

Commit

Permalink
fix: initialize struct pthread for the main thread
Browse files Browse the repository at this point in the history
Signed-off-by: Harald Hoyer <harald@profian.com>
  • Loading branch information
haraldh committed Oct 26, 2022
1 parent 2f577a8 commit 7bd1746
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions expected/wasm32-wasi/posix/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ __getopt_msg
__gmtime_r
__hwcap
__inet_aton
__init_tp
__intscan
__invtrigl_R
__isalnum_l
Expand Down Expand Up @@ -324,6 +325,7 @@ __wasi_fd_seek
__wasi_fd_sync
__wasi_fd_tell
__wasi_fd_write
__wasi_init_tp
__wasi_path_create_directory
__wasi_path_filestat_get
__wasi_path_filestat_set_times
Expand Down
1 change: 1 addition & 0 deletions expected/wasm32-wasi/single/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ __wasi_fd_seek
__wasi_fd_sync
__wasi_fd_tell
__wasi_fd_write
__wasi_init_tp
__wasi_path_create_directory
__wasi_path_filestat_get
__wasi_path_filestat_set_times
Expand Down
8 changes: 8 additions & 0 deletions libc-bottom-half/crt/crt1-command.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include "libc.h"
#include <wasi/api.h>
extern void __wasm_call_ctors(void);
extern int __main_void(void);
Expand All @@ -7,6 +8,11 @@ extern void __wasm_call_dtors(void);
// that the `_start` function isn't started more than once.
static volatile int started = 0;

static void dummy_0()
{
}
weak_alias(dummy_0, __wasi_init_tp);

__attribute__((export_name("_start")))
void _start(void) {
// Don't allow the program to be called multiple times.
Expand All @@ -15,6 +21,8 @@ void _start(void) {
}
started = 1;

__wasi_init_tp();

// The linker synthesizes this to call constructors.
__wasm_call_ctors();

Expand Down
11 changes: 10 additions & 1 deletion libc-top-half/musl/src/env/__init_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,32 @@

volatile int __thread_list_lock;

#ifdef __wasilibc_unmodified_upstream
#ifndef __wasilibc_unmodified_upstream
void __wasi_init_tp() {
__init_tp((void *)__get_tp());
}
#endif

int __init_tp(void *p)
{
pthread_t td = p;
td->self = td;
#ifdef __wasilibc_unmodified_upstream
int r = __set_thread_area(TP_ADJ(p));
if (r < 0) return -1;
if (!r) libc.can_do_threads = 1;
td->detach_state = DT_JOINABLE;
td->tid = __syscall(SYS_set_tid_address, &__thread_list_lock);
#endif
td->locale = &libc.global_locale;
td->robust_list.head = &td->robust_list.head;
td->sysinfo = __sysinfo;
td->next = td->prev = td;
return 0;
}

#ifdef __wasilibc_unmodified_upstream

static struct builtin_tls {
char c;
struct pthread pt;
Expand Down

0 comments on commit 7bd1746

Please sign in to comment.