From 583c426996e76c64533c604d2cb25b9da3d50021 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Tue, 13 Dec 2022 09:19:32 -0800 Subject: [PATCH] threads: enable access to `pthread_barrier_*` functions (#358) In building some `libc-test` tests, I found these functions were not compiled in. This change adds `pthread_barrier_init`, `pthread_barrier_wait`, and `pthread_barrier_destroy` to the `THREAD_MODEL=posix` build. As has been done with previous pthreads PRs, this PR skips any inter-process locking by removing any calls to `__vm_lock` and friends. If in the future WASI gains the "process" concept, then these locations (and the pre-existing ones) will need to be modified. --- Makefile | 3 +++ expected/wasm32-wasi/posix/defined-symbols.txt | 3 +++ libc-top-half/musl/src/thread/pthread_barrier_destroy.c | 2 ++ libc-top-half/musl/src/thread/pthread_barrier_wait.c | 8 ++++++++ 4 files changed, 16 insertions(+) diff --git a/Makefile b/Makefile index f2aa917..3ed8a18 100644 --- a/Makefile +++ b/Makefile @@ -204,6 +204,9 @@ LIBC_TOP_HALF_MUSL_SOURCES += \ thread/pthread_attr_init.c \ thread/pthread_attr_setstack.c \ thread/pthread_attr_setstacksize.c \ + thread/pthread_barrier_destroy.c \ + thread/pthread_barrier_init.c \ + thread/pthread_barrier_wait.c \ thread/pthread_cleanup_push.c \ thread/pthread_cond_broadcast.c \ thread/pthread_cond_destroy.c \ diff --git a/expected/wasm32-wasi/posix/defined-symbols.txt b/expected/wasm32-wasi/posix/defined-symbols.txt index 01e10c5..f5e0013 100644 --- a/expected/wasm32-wasi/posix/defined-symbols.txt +++ b/expected/wasm32-wasi/posix/defined-symbols.txt @@ -981,6 +981,9 @@ pthread_attr_getstacksize pthread_attr_init pthread_attr_setstack pthread_attr_setstacksize +pthread_barrier_destroy +pthread_barrier_init +pthread_barrier_wait pthread_barrierattr_getpshared pthread_cond_broadcast pthread_cond_destroy diff --git a/libc-top-half/musl/src/thread/pthread_barrier_destroy.c b/libc-top-half/musl/src/thread/pthread_barrier_destroy.c index 4ce0b2e..a347a2c 100644 --- a/libc-top-half/musl/src/thread/pthread_barrier_destroy.c +++ b/libc-top-half/musl/src/thread/pthread_barrier_destroy.c @@ -9,7 +9,9 @@ int pthread_barrier_destroy(pthread_barrier_t *b) while ((v = b->_b_lock) & INT_MAX) __wait(&b->_b_lock, 0, v, 0); } +#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */ __vm_wait(); +#endif } return 0; } diff --git a/libc-top-half/musl/src/thread/pthread_barrier_wait.c b/libc-top-half/musl/src/thread/pthread_barrier_wait.c index cc2a8bb..0891b71 100644 --- a/libc-top-half/musl/src/thread/pthread_barrier_wait.c +++ b/libc-top-half/musl/src/thread/pthread_barrier_wait.c @@ -23,7 +23,9 @@ static int pshared_barrier_wait(pthread_barrier_t *b) __wait(&b->_b_count, &b->_b_waiters2, v, 0); } +#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */ __vm_lock(); +#endif /* Ensure all threads have a vm lock before proceeding */ if (a_fetch_add(&b->_b_count, -1)==1-limit) { @@ -44,7 +46,9 @@ static int pshared_barrier_wait(pthread_barrier_t *b) if (v==INT_MIN+1 || (v==1 && w)) __wake(&b->_b_lock, 1, 0); +#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */ __vm_unlock(); +#endif return ret; } @@ -84,8 +88,12 @@ int pthread_barrier_wait(pthread_barrier_t *b) a_spin(); a_inc(&inst->finished); while (inst->finished == 1) +#ifdef __wasilibc_unmodified_upstream __syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0) != -ENOSYS || __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0); +#else + __futexwait(&inst->finished, 1, 0); +#endif return PTHREAD_BARRIER_SERIAL_THREAD; } -- 2.39.2