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:52 -0500
committerIan Moffett <ian@osmora.org>2024-03-07 18:24:51 -0500
commitf5e48e94a2f4d4bbd6e5628c7f2afafc6dbcc459 (patch)
tree93b156621dc0303816b37f60ba88051b702d92f6 /lib/mlibc/tests/posix/pthread_mutex.c
parentbd5969fc876a10b18613302db7087ef3c40f18e1 (diff)
build: Build mlibc + add distclean target
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, 0 insertions, 101 deletions
diff --git a/lib/mlibc/tests/posix/pthread_mutex.c b/lib/mlibc/tests/posix/pthread_mutex.c
deleted file mode 100644
index 0a9b82f..0000000
--- a/lib/mlibc/tests/posix/pthread_mutex.c
+++ /dev/null
@@ -1,101 +0,0 @@
-#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();
-}