Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
421005c
Restructure thread impls
TartanLlama Feb 11, 2026
fd62b0b
Cmake fixes
TartanLlama Feb 11, 2026
974fc8a
Common files
TartanLlama Feb 11, 2026
2789d8f
Rename folder
TartanLlama Feb 11, 2026
8bf9a00
First pass
TartanLlama Feb 20, 2026
9f4cb70
Merge branch 'main' into sy/coop-threading
TartanLlama Feb 20, 2026
ed7f5d2
Merge in WASIP3
TartanLlama Feb 20, 2026
bd108e2
Linking fixes
TartanLlama Mar 2, 2026
5f6f5ae
Various clocks and build fixes
TartanLlama Mar 2, 2026
6bc21e1
Lock fixes
TartanLlama Mar 2, 2026
2f613cc
Sysconf changes
TartanLlama Mar 2, 2026
8f2a99c
sem_destroy fixes
TartanLlama Mar 2, 2026
16d406a
Link and test updates
TartanLlama Mar 4, 2026
5b95b09
Merge branch 'main' into sy/coop-threading
TartanLlama Mar 4, 2026
72ac6f8
WASIp3 bump
TartanLlama Mar 4, 2026
36d2c17
crt1 bump
TartanLlama Mar 4, 2026
aba8568
Fix bad lock initializations
TartanLlama Mar 12, 2026
3647168
Ifdef lock
TartanLlama Mar 12, 2026
e055edf
typedef locks with no threading
TartanLlama Mar 12, 2026
c10da06
Unused variable warnings
TartanLlama Mar 12, 2026
7b431f1
More lock nonsense
TartanLlama Mar 12, 2026
787c289
Redo lock changes
TartanLlama Mar 12, 2026
1000351
Move lock_ptc
TartanLlama Mar 12, 2026
1fc2a40
OFL lock fix
TartanLlama Mar 12, 2026
c37f771
Add wasi-threads sem_destroy
TartanLlama Mar 12, 2026
0ef525a
wasi-threads fixes
TartanLlama Mar 12, 2026
cfec5f1
wasi-threads fixes
TartanLlama Mar 12, 2026
c78d35c
wasi-threads fixes
TartanLlama Mar 12, 2026
73dff1a
wasi-threads fixes
TartanLlama Mar 12, 2026
df6a4da
Pointer shenanigans
TartanLlama Mar 12, 2026
35b1ed9
Pointer shenanigans
TartanLlama Mar 12, 2026
d8fb9a6
Pointer shenanigans
TartanLlama Mar 12, 2026
cb5f6a4
More lock changes
TartanLlama Mar 12, 2026
cce6bc0
Defined symbol changes
TartanLlama Mar 12, 2026
53be6e5
Bump test suite
TartanLlama Mar 12, 2026
6e3518a
Call compiler-rt
TartanLlama Mar 23, 2026
ffdf6b0
Merge branch 'main' into sy/coop-threading
TartanLlama Mar 23, 2026
fd5840e
Correct mtx cmake
TartanLlama Mar 23, 2026
ddd75e1
Update
TartanLlama Jun 1, 2026
e02f07d
Merge branch 'main' into sy/coop-threading
TartanLlama Jun 12, 2026
4d2e463
Rename coop threads macro
TartanLlama Jun 12, 2026
d7961f8
Lock name fixes
TartanLlama Jun 12, 2026
8fe99d7
Fix macro name
TartanLlama Jun 12, 2026
84383bb
Fix lock issue
TartanLlama Jun 22, 2026
5d1f328
Remove stale files
TartanLlama Jun 22, 2026
d249f32
Merge branch 'main' into sy/coop-threading
TartanLlama Jun 22, 2026
3c017ff
Move thread index retrieval
TartanLlama Jun 22, 2026
112e662
Move TLS allocation to C
TartanLlama Jun 22, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ endif()
if(TARGET_TRIPLE MATCHES "-threads$")
set(THREADS ON)
add_compile_options(-mthread-model posix -pthread -ftls-model=local-exec -matomics)
elseif(TARGET_TRIPLE MATCHES "-wasip3$")
set(THREADS OFF)
add_compile_options(-mthread-model posix -pthread -ftls-model=local-exec)
else()
set(THREADS OFF)
add_compile_options(-mthread-model single)
Expand Down
9 changes: 3 additions & 6 deletions expected/wasm32-wasip1-threads/defined-symbols.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ __acquire_ptc
__aio_close
__asctime_r
__assert_fail
__at_quick_exit_lockptr
__atexit_lockptr
__c_dot_utf8
__c_dot_utf8_locale
__c_locale
Expand Down Expand Up @@ -81,6 +79,7 @@ __ftello
__ftello_unlocked
__funcs_on_exit
__funcs_on_quick_exit
__futexwait
__futimesat
__fwritable
__fwritex
Expand All @@ -92,6 +91,7 @@ __getopt_msg
__gmtime_r
__hwcap
__inet_aton
__inhibit_ptc
__init_ssp
__init_tp
__intscan
Expand Down Expand Up @@ -148,7 +148,6 @@ __libc_free
__libc_malloc
__loc_is_allocated
__locale_lock
__locale_lockptr
__localtime_r
__lock
__lockfile
Expand Down Expand Up @@ -219,7 +218,6 @@ __pthread_tsd_size
__putenv
__qsort_r
__rand48_step
__random_lockptr
__reallocarray
__register_locked_file
__release_ptc
Expand Down Expand Up @@ -248,7 +246,6 @@ __stdin_used
__stdio_close
__stdio_exit
__stdio_exit_needed
__stdio_ofl_lockptr
__stdio_read
__stdio_seek
__stdio_write
Expand Down Expand Up @@ -308,6 +305,7 @@ __unlockfile
__uselocale
__utc
__wait
__wake
__wasi_args_get
__wasi_args_sizes_get
__wasi_clock_res_get
Expand Down Expand Up @@ -359,7 +357,6 @@ __wasi_thread_start_C
__wasilibc_access
__wasilibc_cwd
__wasilibc_cwd_lock
__wasilibc_cwd_unlock
__wasilibc_deinitialize_environ
__wasilibc_dttoif
__wasilibc_enable_futex_busywait_on_current_thread
Expand Down
1 change: 1 addition & 0 deletions libc-bottom-half/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ endif()

if (WASI STREQUAL "p3")
list(APPEND bottom_half_sources
sources/__wasm_init_task.s
sources/wasip3.c
sources/wasip3_block_on.c
sources/wasip3_stdio.c
Expand Down
10 changes: 9 additions & 1 deletion libc-bottom-half/cloudlibc/src/libc/sched/sched_yield.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
#include <sched.h>

int sched_yield(void) {
#ifdef __wasip1__
#ifdef __wasm_libcall_thread_context__
#ifdef __wasip3__
wasip3_thread_yield();
return 0;
#else
#error "Unknown WASI version"
#endif

#elif defined(__wasip1__)
__wasi_errno_t error = __wasi_sched_yield();
if (error != 0) {
errno = error;
Expand Down
2 changes: 1 addition & 1 deletion libc-bottom-half/cloudlibc/src/libc/unistd/pwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ ssize_t pwrite(int fildes, const void *buf, size_t nbyte, off_t offset) {
&closed);
filesystem_stream_u8_drop_writable(writer);

// Wait for the subtask to resolve now that the writer half is closed and if
// Wait for the future to resolve now that the writer half is closed and if
// we failed to write bytes (0 bytes written) and the result is an error we
// can return -1.
filesystem_result_void_error_code_t result;
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
Expand Up @@ -9,6 +9,14 @@ extern void __wasm_call_ctors(void);
extern int __main_void(void);
extern void __wasm_call_dtors(void);

#ifdef __wasm_libcall_thread_context__
// Force __wasm_init_task and __wasm_init_async_task to be linked in for wasip3
extern void __wasm_init_task(void);
extern void __wasm_init_async_task(void);
__attribute__((used)) void *__wasm_init_task_ref = __wasm_init_task;
__attribute__((used)) void *__wasm_init_async_task_ref = __wasm_init_async_task;
#endif

#if defined(__wasip1__)
__attribute__((export_name("_start"))) void _start(void)
#elif defined(__wasip2__)
Expand Down
8 changes: 8 additions & 0 deletions libc-bottom-half/crt/crt1-reactor.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@
extern void __wasi_init_tp(void);
extern void __wasm_call_ctors(void);

#ifdef __wasm_libcall_thread_context__
// Force __wasm_init_task and __wasm_init_async_task to be linked in for wasip3
extern void __wasm_init_task(void);
extern void __wasm_init_async_task(void);
__attribute__((used)) static void *__wasm_init_task_ref = __wasm_init_task;
__attribute__((used)) static void *__wasm_init_async_task_ref = __wasm_init_async_task;
#endif

__attribute__((export_name("_initialize"))) void _initialize(void) {
#if defined(_REENTRANT)
static volatile atomic_int initialized = 0;
Expand Down
69 changes: 69 additions & 0 deletions libc-bottom-half/sources/__wasm_init_task.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
.text

.export_name __wasm_init_task, __wasm_init_task
.export_name __wasm_init_async_task, __wasm_init_async_task

.globaltype __init_stack_pointer, i32, immutable
.globaltype __init_tls_base, i32, immutable
.globaltype __tls_size, i32, immutable
.globaltype __tls_align, i32, immutable

.functype __wasm_set_stack_pointer (i32) -> ()
.functype __wasm_set_tls_base (i32) -> ()

.functype malloc (i32) -> (i32)
.functype __copy_tls (i32) -> (i32)
.functype __allocate_tls () -> (i32)

.globl __wasm_init_task
.type __wasm_init_task,@function
.globl __wasm_init_async_task
.type __wasm_init_async_task,@function

.functype __wasi_init_tp() -> ()

__wasm_init_task:
.functype __wasm_init_task () -> ()

global.get __init_stack_pointer
call __wasm_set_stack_pointer

global.get __init_tls_base
call __wasm_set_tls_base

# Allocate a new TLS area
call __allocate_tls
call __copy_tls
call __wasm_set_tls_base

call __wasi_init_tp

end_function

__wasm_init_async_task:
.functype __wasm_init_async_task () -> ()

# malloc and __init_tls may use the stack pointer and TLS base, so set those first
# to the statically-allocated values used for synchronous tasks.
global.get __init_stack_pointer
call __wasm_set_stack_pointer

global.get __init_tls_base
call __wasm_set_tls_base

# Allocate a new stack
# Constant copied from __default_stacksize, TODO(wasip3) find a way
# to reference this constant without linking failing for shared libraries,
# i.e. a position-independent data reference.
i32.const 131072
call malloc
call __wasm_set_stack_pointer

# Allocate a new TLS area
call __allocate_tls
call __copy_tls
call __wasm_set_tls_base
Comment thread
TartanLlama marked this conversation as resolved.

call __wasi_init_tp

end_function
32 changes: 13 additions & 19 deletions libc-bottom-half/sources/chdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,9 @@
#include <unistd.h>
#include <wasi/libc-find-relpath.h>
#include <wasi/libc.h>
#include "lock.h"

#ifdef _REENTRANT
void __wasilibc_cwd_lock(void);
void __wasilibc_cwd_unlock(void);
static volatile int lock[1];
#else
#define __wasilibc_cwd_lock() (void)0
#define __wasilibc_cwd_unlock() (void)0
#endif
DECLARE_WEAK_LOCK(__wasilibc_cwd_lock, extern);
extern char *__wasilibc_cwd;
static int __wasilibc_cwd_mallocd = 0;

Expand Down Expand Up @@ -66,10 +60,10 @@ int chdir(const char *path) {

// And set our new malloc'd buffer into the global cwd, freeing the
// previous one if necessary.
__wasilibc_cwd_lock();
WEAK_LOCK(__wasilibc_cwd_lock);
char *prev_cwd = __wasilibc_cwd;
__wasilibc_cwd = new_cwd;
__wasilibc_cwd_unlock();
WEAK_UNLOCK(__wasilibc_cwd_lock);
if (__wasilibc_cwd_mallocd)
free(prev_cwd);
__wasilibc_cwd_mallocd = 1;
Expand Down Expand Up @@ -100,22 +94,22 @@ static const char *make_absolute(const char *path) {
// Otherwise we'll take the current directory, add a `/`, and then add the
// input `path`. Note that this doesn't do any normalization (like removing
// `/./`).
__wasilibc_cwd_lock();
WEAK_LOCK(__wasilibc_cwd_lock);
size_t cwd_len = strlen(__wasilibc_cwd);
size_t path_len = path ? strlen(path) : 0;
int need_slash = __wasilibc_cwd[cwd_len - 1] == '/' ? 0 : 1;
size_t alloc_len = cwd_len + path_len + 1 + need_slash;
if (alloc_len > make_absolute_len) {
char *tmp = realloc(make_absolute_buf, alloc_len);
if (tmp == NULL) {
__wasilibc_cwd_unlock();
WEAK_UNLOCK(__wasilibc_cwd_lock);
return NULL;
}
make_absolute_buf = tmp;
make_absolute_len = alloc_len;
}
strcpy(make_absolute_buf, __wasilibc_cwd);
__wasilibc_cwd_unlock();
WEAK_UNLOCK(__wasilibc_cwd_lock);

#ifdef _REENTRANT
if (path[0] == 0 || !strcmp(path, ".") || !strcmp(path, "./")) {
Expand All @@ -135,12 +129,12 @@ static const char *make_absolute(const char *path) {
int __wasilibc_find_relpath_alloc(const char *path, const char **abs_prefix,
char **relative_buf, size_t *relative_buf_len,
int can_realloc) {
LOCK(lock);
WEAK_LOCK(__wasilibc_cwd_lock);

// First, make our path absolute taking the cwd into account.
const char *abspath = make_absolute(path);
if (abspath == NULL) {
UNLOCK(lock);
WEAK_UNLOCK(__wasilibc_cwd_lock);
errno = ENOMEM;
return -1;
}
Expand All @@ -151,27 +145,27 @@ int __wasilibc_find_relpath_alloc(const char *path, const char **abs_prefix,
const char *rel;
int fd = __wasilibc_find_abspath(abspath, abs_prefix, &rel);
if (fd == -1) {
UNLOCK(lock);
WEAK_UNLOCK(__wasilibc_cwd_lock);
return -1;
}

size_t rel_len = strlen(rel);
if (*relative_buf_len < rel_len + 1) {
if (!can_realloc) {
UNLOCK(lock);
WEAK_UNLOCK(__wasilibc_cwd_lock);
errno = ERANGE;
return -1;
}
char *tmp = realloc(*relative_buf, rel_len + 1);
if (tmp == NULL) {
UNLOCK(lock);
WEAK_UNLOCK(__wasilibc_cwd_lock);
errno = ENOMEM;
return -1;
}
*relative_buf = tmp;
*relative_buf_len = rel_len + 1;
}
strcpy(*relative_buf, rel);
UNLOCK(lock);
WEAK_UNLOCK(__wasilibc_cwd_lock);
return fd;
}
18 changes: 6 additions & 12 deletions libc-bottom-half/sources/getcwd.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,28 @@
#include "lock.h"

char *__wasilibc_cwd = "/";
DECLARE_WEAK_LOCK(__wasilibc_cwd_lock);

#ifdef _REENTRANT
static volatile int lock[1];
void __wasilibc_cwd_lock(void) { LOCK(lock); }
void __wasilibc_cwd_unlock(void) { UNLOCK(lock); }
#else
#define __wasilibc_cwd_lock() (void)0
#define __wasilibc_cwd_unlock() (void)0
#endif

char *getcwd(char *buf, size_t size) {
__wasilibc_cwd_lock();
// Critical section contains no yield points, so we can use weak locks.
WEAK_LOCK(__wasilibc_cwd_lock);
if (!buf) {
buf = strdup(__wasilibc_cwd);
if (!buf) {
errno = ENOMEM;
__wasilibc_cwd_unlock();
WEAK_UNLOCK(__wasilibc_cwd_lock);
return NULL;
}
} else {
size_t len = strlen(__wasilibc_cwd);
if (size < len + 1) {
errno = ERANGE;
__wasilibc_cwd_unlock();
WEAK_UNLOCK(__wasilibc_cwd_lock);
return NULL;
}
strcpy(buf, __wasilibc_cwd);
}
__wasilibc_cwd_unlock();
WEAK_UNLOCK(__wasilibc_cwd_lock);
return buf;
}
Loading
Loading