aboutsummaryrefslogtreecommitdiff
path: root/lib/mlibc/tests/posix/pthread_mutex.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_mutex.c
parenta95b38b1b92b172e6cc4e8e56a88a30cc65907b0 (diff)
lib: Add mlibc
Signed-off-by: Ian Moffett <ian@osmora.org>
Diffstat (limited to 'lib/mlibc/tests/posix/pthread_mutex.c')
-rw-r--r--lib/mlibc/tests/posix/pthread_mutex.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/mlibc/tests/posix/pthread_mutex.c b/lib/mlibc/tests/posix/pthread_mutex.c
new file mode 100644
index 0000000..0a9b82f
--- /dev/null
+++ b/lib/mlibc/tests/posix/pthread_mutex.c
@@ -0,0 +1,101 @@
+#include <pthread.h>
+#include <assert.h>
+#include <errno.h>
+
+#define TEST_ATTR(attr, field, value) ({ \
+ int x; \
+ assert(!pthread_mutexattr_set ## field (&(attr), (value))); \
+ assert(!pthread_mutexattr_get ## field (&(attr), &x)); \
+ assert(x == (value)); \
+ })
+
+int variable;
+pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void *worker(void *arg) {
+ (void)arg;
+ pthread_mutex_lock(&mutex);
+ variable = 1;
+ pthread_mutex_unlock(&mutex);
+ return NULL;
+}
+
+static void testAttr() {
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+
+ TEST_ATTR(attr, type, PTHREAD_MUTEX_DEFAULT);
+ TEST_ATTR(attr, type, PTHREAD_MUTEX_NORMAL);
+ TEST_ATTR(attr, type, PTHREAD_MUTEX_ERRORCHECK);
+ TEST_ATTR(attr, type, PTHREAD_MUTEX_RECURSIVE);
+
+ TEST_ATTR(attr, robust, PTHREAD_MUTEX_STALLED);
+ TEST_ATTR(attr, robust, PTHREAD_MUTEX_ROBUST);
+
+ TEST_ATTR(attr, protocol, PTHREAD_PRIO_NONE);
+ TEST_ATTR(attr, protocol, PTHREAD_PRIO_INHERIT);
+ TEST_ATTR(attr, protocol, PTHREAD_PRIO_PROTECT);
+
+ TEST_ATTR(attr, pshared, PTHREAD_PROCESS_PRIVATE);
+ TEST_ATTR(attr, pshared, PTHREAD_PROCESS_SHARED);
+
+ // TODO: sched_get_priority* is unimplemented.
+ // int prio = sched_get_priority_max(SCHED_FIFO);
+ // TEST_ATTR(attr, prioceiling, prio);
+
+ pthread_mutexattr_destroy(&attr);
+}
+
+static void testNormal() {
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ pthread_mutex_init(&mutex, &attr);
+ pthread_mutexattr_destroy(&attr);
+
+ pthread_mutex_lock(&mutex);
+ variable = 0;
+
+ pthread_t thread;
+ int ret = pthread_create(&thread, NULL, &worker, NULL);
+ assert(!ret);
+
+ assert(pthread_mutex_trylock(&mutex) == EBUSY);
+ pthread_mutex_unlock(&mutex);
+
+ ret = pthread_join(thread, NULL);
+ assert(!ret);
+ assert(variable == 1);
+
+ pthread_mutex_destroy(&mutex);
+}
+
+static void testRecursive() {
+ pthread_mutexattr_t attr;
+ pthread_mutexattr_init(&attr);
+ pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
+ pthread_mutex_init(&mutex, &attr);
+ pthread_mutexattr_destroy(&attr);
+
+ pthread_mutex_lock(&mutex);
+ variable = 0;
+
+ pthread_t thread;
+ int ret = pthread_create(&thread, NULL, &worker, NULL);
+ assert(!ret);
+
+ assert(pthread_mutex_trylock(&mutex) == 0);
+ pthread_mutex_unlock(&mutex);
+ pthread_mutex_unlock(&mutex);
+
+ ret = pthread_join(thread, NULL);
+ assert(!ret);
+ assert(variable == 1);
+
+ pthread_mutex_destroy(&mutex);
+}
+
+int main() {
+ testAttr();
+ testNormal();
+ testRecursive();
+}