]> git.proxmox.com Git - wasi-libc.git/commitdiff
threads: enable access to `pthread_barrier_*` functions (#358)
authorAndrew Brown <andrew.brown@intel.com>
Tue, 13 Dec 2022 17:19:32 +0000 (09:19 -0800)
committerFabian Grünbichler <f.gruenbichler@proxmox.com>
Wed, 2 Aug 2023 10:24:08 +0000 (12:24 +0200)
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
expected/wasm32-wasi/posix/defined-symbols.txt
libc-top-half/musl/src/thread/pthread_barrier_destroy.c
libc-top-half/musl/src/thread/pthread_barrier_wait.c

index f2aa917c9b903b5be78421ce5d2c606b367ad542..3ed8a189a1840b3830bdeb157401d286d14f2185 100644 (file)
--- 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 \
index 01e10c5f368952eeb67227769b019c7911b2f7ba..f5e0013df6723c65986c81e35980ce0670747de7 100644 (file)
@@ -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
index 4ce0b2e1278499de5e417f870d4e4e6c04655638..a347a2c12a4804831b741aae7e86987c650e1c50 100644 (file)
@@ -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;
 }
index cc2a8bbf58a9cfee193d7f30bf2bcfd778debe7a..0891b713ece90255fd43682d37162e9bfab2644b 100644 (file)
@@ -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;
        }