summaryrefslogtreecommitdiff
path: root/src/thread/pthread_barrier_wait.c
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
committerRich Felker <dalias@aerifal.cx>2011-02-12 00:22:29 -0500
commit0b44a0315b47dd8eced9f3b7f31580cf14bbfc01 (patch)
tree6eaef0d8a720fa3da580de87b647fff796fe80b3 /src/thread/pthread_barrier_wait.c
downloadmusl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.gz
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.bz2
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.tar.xz
musl-0b44a0315b47dd8eced9f3b7f31580cf14bbfc01.zip
initial check-in, version 0.5.0v0.5.0
Diffstat (limited to 'src/thread/pthread_barrier_wait.c')
-rw-r--r--src/thread/pthread_barrier_wait.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/thread/pthread_barrier_wait.c b/src/thread/pthread_barrier_wait.c
new file mode 100644
index 00000000..02c252ad
--- /dev/null
+++ b/src/thread/pthread_barrier_wait.c
@@ -0,0 +1,31 @@
+#include "pthread_impl.h"
+
+int pthread_barrier_wait(pthread_barrier_t *b)
+{
+ int cur;
+
+ /* Trivial case: count was set at 1 */
+ if (!b->__limit) return PTHREAD_BARRIER_SERIAL_THREAD;
+
+ /* Wait for anyone still suspended at previous use of barrier */
+ while ((cur=b->__left))
+ __wait(&b->__left, &b->__waiters, cur, 0);
+
+ /* If we are the last to reach barrier, reset it and wake others */
+ if (a_fetch_add(&b->__count, 1) == b->__limit) {
+ b->__left = b->__limit;
+ b->__count = 0;
+ __wake(&b->__count, -1, 0);
+ return PTHREAD_BARRIER_SERIAL_THREAD;
+ }
+
+ /* Wait for our peers to reach the barrier */
+ while ((cur=b->__count))
+ __wait(&b->__count, 0, cur, 0);
+
+ /* If we're the last to wake up and barrier is awaiting reuse */
+ if (a_fetch_add(&b->__left, -1) == 1 && b->__waiters)
+ __wake(&b->__left, -1, 0);
+
+ return 0;
+}