summaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/pthread_barrier.c
diff options
context:
space:
mode:
authorIan Moffett <ian@osmora.org>2024-03-07 17:28:00 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 17:28:32 -0500
commitbd5969fc876a10b18613302db7087ef3c40f18e1 (patch)
tree7c2b8619afe902abf99570df2873fbdf40a4d1a1 /lib/mlibc/tests/posix/pthread_barrier.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/pthread_barrier.c')
-rw-r--r--lib/mlibc/tests/posix/pthread_barrier.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/pthread_barrier.c b/lib/mlibc/tests/posix/pthread_barrier.c
new file mode 100644
index 0000000..213ba8f
--- /dev/null
+++ b/lib/mlibc/tests/posix/pthread_barrier.c
@@ -0,0 +1,56 @@
+#include <pthread.h>
+#include <unistd.h>
+#include <assert.h>
+
+pthread_barrier_t barrier;
+_Atomic int hitBarrierCount, pastBarrierCount;
+
+static void *worker(void *arg) {
+ (void)arg;
+ hitBarrierCount++;
+ pthread_barrier_wait(&barrier);
+ pastBarrierCount++;
+ return NULL;
+}
+
+int main() {
+ // pthread_barrierattr_t
+ pthread_barrierattr_t attr;
+ pthread_barrierattr_init(&attr);
+
+ int pshared;
+ pthread_barrierattr_getpshared(&attr, &pshared);
+ assert(pshared == PTHREAD_PROCESS_PRIVATE);
+
+ pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+ pthread_barrierattr_getpshared(&attr, &pshared);
+ assert(pshared == PTHREAD_PROCESS_SHARED);
+
+ pthread_barrierattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
+ pthread_barrierattr_getpshared(&attr, &pshared);
+ assert(pshared == PTHREAD_PROCESS_PRIVATE);
+
+ // pthread_barrier_t
+ pthread_barrier_init(&barrier, &attr, 3);
+ pthread_barrierattr_destroy(&attr);
+
+ pthread_t thread1;
+ int ret = pthread_create(&thread1, NULL, &worker, NULL);
+ assert(!ret);
+
+ pthread_t thread2;
+ ret = pthread_create(&thread2, NULL, &worker, NULL);
+ assert(!ret);
+
+ sleep(1);
+
+ // Make sure the barrier actually stops threads from proceeding.
+ assert(pastBarrierCount == 0);
+ assert(hitBarrierCount <= 2);
+
+ hitBarrierCount++;
+ pthread_barrier_wait(&barrier);
+ assert(hitBarrierCount == 3);
+
+ pthread_barrier_destroy(&barrier);
+}