Skip to content

Commit dbcf819

Browse files
authored
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.
1 parent a6e91a7 commit dbcf819

4 files changed

Lines changed: 16 additions & 0 deletions

File tree

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,9 @@ LIBC_TOP_HALF_MUSL_SOURCES += \
204204
thread/pthread_attr_init.c \
205205
thread/pthread_attr_setstack.c \
206206
thread/pthread_attr_setstacksize.c \
207+
thread/pthread_barrier_destroy.c \
208+
thread/pthread_barrier_init.c \
209+
thread/pthread_barrier_wait.c \
207210
thread/pthread_cleanup_push.c \
208211
thread/pthread_cond_broadcast.c \
209212
thread/pthread_cond_destroy.c \

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,9 @@ pthread_attr_getstacksize
981981
pthread_attr_init
982982
pthread_attr_setstack
983983
pthread_attr_setstacksize
984+
pthread_barrier_destroy
985+
pthread_barrier_init
986+
pthread_barrier_wait
984987
pthread_barrierattr_getpshared
985988
pthread_cond_broadcast
986989
pthread_cond_destroy

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 & 0 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
}
@@ -84,8 +88,12 @@ int pthread_barrier_wait(pthread_barrier_t *b)
8488
a_spin();
8589
a_inc(&inst->finished);
8690
while (inst->finished == 1)
91+
#ifdef __wasilibc_unmodified_upstream
8792
__syscall(SYS_futex,&inst->finished,FUTEX_WAIT|FUTEX_PRIVATE,1,0) != -ENOSYS
8893
|| __syscall(SYS_futex,&inst->finished,FUTEX_WAIT,1,0);
94+
#else
95+
__futexwait(&inst->finished, 1, 0);
96+
#endif
8997
return PTHREAD_BARRIER_SERIAL_THREAD;
9098
}
9199

0 commit comments

Comments
 (0)