aboutsummaryrefslogtreecommitdiff
path: root/libiot/pthread/test/pthread-once.c
diff options
context:
space:
mode:
authorbhgv <bhgv.empire@gmail.com>2020-05-10 02:59:23 +0300
committerbhgv <bhgv.empire@gmail.com>2020-05-10 02:59:23 +0300
commit31b4edc67b75658ce5e2d41f2fc87331f4b26d49 (patch)
treea7b6ea659fe62e0a7239f29170024f524595fb4d /libiot/pthread/test/pthread-once.c
parentc76314f0f38f4ed028610a6db4452879a556b35f (diff)
a try to add support of FreeRTOS riscV-64 (k210 cpu). first step
Diffstat (limited to 'libiot/pthread/test/pthread-once.c')
-rw-r--r--libiot/pthread/test/pthread-once.c68
1 files changed, 68 insertions, 0 deletions
diff --git a/libiot/pthread/test/pthread-once.c b/libiot/pthread/test/pthread-once.c
new file mode 100644
index 0000000..d3a3db6
--- /dev/null
+++ b/libiot/pthread/test/pthread-once.c
@@ -0,0 +1,68 @@
+#include "unity.h"
+
+#include <pthread.h>
+#include <sys/delay.h>
+
+#define NUM_THREADS 2
+
+static pthread_t threads[NUM_THREADS];
+
+static pthread_once_t once = PTHREAD_ONCE_INIT;
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+static int execs;
+
+static void init() {
+ int ret;
+
+ ret = pthread_mutex_lock(&mutex);
+ TEST_ASSERT(ret == 0);
+
+ execs++;
+
+ ret = pthread_mutex_unlock(&mutex);
+ TEST_ASSERT(ret == 0);
+}
+
+static void *thread1(void *args) {
+ int ret;
+
+ ret = pthread_once(&once, init);
+ TEST_ASSERT(ret == 0);
+
+ // Simulate some work
+ usleep(1000);
+
+ pthread_exit(NULL);
+ }
+
+TEST_CASE("pthread once", "[pthread]") {
+ pthread_attr_t attr;
+ int i, ret;
+
+ ret = pthread_attr_init(&attr);
+ TEST_ASSERT(ret == 0);
+
+ ret = pthread_mutex_init(&mutex, NULL);
+ TEST_ASSERT(ret == 0);
+
+ ret = pthread_create(&threads[0], &attr, thread1, NULL);
+ TEST_ASSERT(ret == 0);
+
+ ret = pthread_create(&threads[1], &attr, thread1, NULL);
+ TEST_ASSERT(ret == 0);
+
+ // Wait for all threads completion
+ for (i=0; i< NUM_THREADS; i++) {
+ ret = pthread_join(threads[i], NULL);
+ TEST_ASSERT(ret == 0);
+ }
+
+ TEST_ASSERT(execs == 1);
+
+ // Clean up
+ ret = pthread_attr_destroy(&attr);
+ TEST_ASSERT(ret == 0);
+
+ ret = pthread_mutex_destroy(&mutex);
+ TEST_ASSERT(ret == 0);
+}