aboutsummaryrefslogtreecommitdiff
path: root/libiot/pthread/test/pthread-join.c
diff options
context:
space:
mode:
authorbhgv <bhgv.empire@gmail.com>2020-05-12 12:50:49 +0300
committerbhgv <bhgv.empire@gmail.com>2020-05-12 12:50:49 +0300
commit9b5d5f8a4640dbecdc87e5b6e7e95f71018632cf (patch)
treed3135c3861ef93ed2523642d3c5f64c7819b7def /libiot/pthread/test/pthread-join.c
parent73c13e732072c17f3e584e11a51d1f7dc8d88e32 (diff)
parent31b4edc67b75658ce5e2d41f2fc87331f4b26d49 (diff)
Merge branch 'master' of https://github.com/bhgv/Inferno-OS-bhgv
Diffstat (limited to 'libiot/pthread/test/pthread-join.c')
-rw-r--r--libiot/pthread/test/pthread-join.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/libiot/pthread/test/pthread-join.c b/libiot/pthread/test/pthread-join.c
new file mode 100644
index 0000000..ba5f976
--- /dev/null
+++ b/libiot/pthread/test/pthread-join.c
@@ -0,0 +1,85 @@
+#include "unity.h"
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <pthread.h>
+
+#include <sys/delay.h>
+
+#define NUM_THREADS 2
+
+static pthread_t threads[NUM_THREADS];
+
+static void *thread1(void *args) {
+ int count = 0;
+
+ for (count = 0; count < 100; count++) {
+ // Simulate some work
+ usleep(1000);
+ }
+
+ int *ret = malloc(sizeof(int));
+ *ret = count;
+
+ pthread_exit(ret);
+ }
+
+static void *thread2(void *args) {
+ int count;
+
+ for (count = 0; count < 50; count++) {
+ // Simulate some work
+ usleep(1000);
+ }
+
+ int *ret = malloc(sizeof(int));
+ *ret = count;
+
+ pthread_exit(ret);
+ }
+
+
+TEST_CASE("pthread join", "[pthread]") {
+ pthread_attr_t attr;
+ int i, ret;
+ int *res;
+
+ ret = pthread_attr_init(&attr);
+ TEST_ASSERT(ret == 0);
+
+ ret = pthread_create(&threads[0], &attr, thread1, NULL);
+ TEST_ASSERT(ret == 0);
+
+ ret = pthread_create(&threads[1], &attr, thread2, NULL);
+ TEST_ASSERT(ret == 0);
+
+ // Wait for all threads completion
+ for (i=0; i< NUM_THREADS; i++) {
+ ret = pthread_join(threads[i], (void **)&res);
+ TEST_ASSERT(ret == 0);
+
+ if (i == 0) {
+ TEST_ASSERT(*res == 100);
+ } else if (i == 1) {
+ TEST_ASSERT(*res == 50);
+ }
+
+ free(res);
+ }
+
+ // Check that a detached thread is not joinable
+ ret = pthread_attr_setdetachstate(&attr,PTHREAD_CREATE_DETACHED);
+ TEST_ASSERT(ret == 0);
+
+ ret = pthread_create(&threads[0], &attr, thread1, NULL);
+ TEST_ASSERT(ret == 0);
+
+ ret = pthread_join(threads[0], (void **)&res);
+ TEST_ASSERT(ret == EINVAL);
+
+ // Clean up
+ ret = pthread_attr_destroy(&attr);
+ TEST_ASSERT(ret == 0);
+}