Skip to content

Commit 0478f9d

Browse files
abrownjohn-sharratt
authored andcommitted
threads: enable access to pthread_barrier_* functions (WebAssembly#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.
1 parent 3043721 commit 0478f9d

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,9 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
232232
thread/pthread_attr_init.c \
233233
thread/pthread_attr_setstack.c \
234234
thread/pthread_attr_setstacksize.c \
235+
thread/pthread_barrier_destroy.c \
236+
thread/pthread_barrier_init.c \
237+
thread/pthread_barrier_wait.c \
235238
thread/pthread_cleanup_push.c \
236239
thread/pthread_cond_broadcast.c \
237240
thread/pthread_cond_destroy.c \

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ int pthread_barrier_destroy(pthread_barrier_t *b)
99
while ((v = b->_b_lock) & INT_MAX)
1010
__wait(&b->_b_lock, 0, v, 0);
1111
}
12+
#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */
1213
__vm_wait();
14+
#endif
1315
}
1416
return 0;
1517
}

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ static int pshared_barrier_wait(pthread_barrier_t *b)
2323
__wait(&b->_b_count, &b->_b_waiters2, v, 0);
2424
}
2525

26+
#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */
2627
__vm_lock();
28+
#endif
2729

2830
/* Ensure all threads have a vm lock before proceeding */
2931
if (a_fetch_add(&b->_b_count, -1)==1-limit) {
@@ -44,7 +46,9 @@ static int pshared_barrier_wait(pthread_barrier_t *b)
4446
if (v==INT_MIN+1 || (v==1 && w))
4547
__wake(&b->_b_lock, 1, 0);
4648

49+
#ifdef __wasilibc_unmodified_upstream /* WASI does not understand processes or locking between them. */
4750
__vm_unlock();
51+
#endif
4852

4953
return ret;
5054
}
@@ -83,15 +87,13 @@ int pthread_barrier_wait(pthread_barrier_t *b)
8387
while (spins-- && !inst->finished)
8488
a_spin();
8589
a_inc(&inst->finished);
86-
while (inst->finished == 1) {
90+
while (inst->finished == 1)
8791
#ifdef __wasilibc_unmodified_upstream
88-
__syscall(SYS_futex, &inst->finished, FUTEX_WAIT|FUTEX_PRIVATE, 1, 0) != -ENOSYS
89-
|| __syscall(SYS_futex, &inst->finished, FUTEX_WAIT, 1, 0);
92+
__syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0) != -ENOSYS
93+
|| __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0);
9094
#else
91-
__wasilibc_futex_wait_wasix((int*)&inst->finished, FUTEX_WAIT, 1, 0);
92-
//__builtin_wasm_memory_atomic_wait32((int*)addr, val);
95+
__futexwait(&inst->finished, 1, 0);
9396
#endif
94-
}
9597
return PTHREAD_BARRIER_SERIAL_THREAD;
9698
}
9799

0 commit comments

Comments
 (0)