summaryrefslogtreecommitdiff
path: root/source/tests/mutex.test.c
diff options
context:
space:
mode:
Diffstat (limited to 'source/tests/mutex.test.c')
-rw-r--r--source/tests/mutex.test.c79
1 files changed, 79 insertions, 0 deletions
diff --git a/source/tests/mutex.test.c b/source/tests/mutex.test.c
new file mode 100644
index 0000000..ce26991
--- /dev/null
+++ b/source/tests/mutex.test.c
@@ -0,0 +1,79 @@
+#include "../kit/mutex.h"
+#include "../kit/thread.h"
+
+#define KIT_TEST_FILE mutex
+#include "../kit_test/test.h"
+
+enum { SLEEP = 400000000, TICK_COUNT = 200, THREAD_COUNT = 100 };
+
+typedef struct {
+ mtx_t lock;
+ int value;
+} mtx_test_data_t;
+
+static int mtx_test_run(void *data) {
+ int i;
+ mtx_test_data_t *x = (mtx_test_data_t *) data;
+ for (i = 0; i < TICK_COUNT; i++) {
+ mtx_lock(&x->lock);
+
+ x->value += i;
+ thrd_yield();
+ x->value -= i + 42;
+ thrd_yield();
+ x->value += i + 20;
+ thrd_yield();
+ x->value += 22 - i;
+
+ mtx_unlock(&x->lock);
+ }
+ return 0;
+}
+
+TEST("mutex lock") {
+ ptrdiff_t i;
+
+ mtx_test_data_t data;
+ thrd_t pool[THREAD_COUNT];
+ data.value = 42;
+ REQUIRE(mtx_init(&data.lock, mtx_plain) == thrd_success);
+
+ for (i = 0; i < THREAD_COUNT; i++)
+ thrd_create(pool + i, mtx_test_run, &data);
+ for (i = 0; i < THREAD_COUNT; i++) thrd_join(pool[i], NULL);
+
+ mtx_destroy(&data.lock);
+ REQUIRE(data.value == 42);
+}
+
+static int test_lock(void *data) {
+ mtx_t *m = (mtx_t *) data;
+ mtx_lock(m);
+
+ struct timespec sec = { .tv_sec = 0, .tv_nsec = SLEEP };
+ thrd_sleep(&sec, NULL);
+
+ mtx_unlock(m);
+
+ return 0;
+}
+
+TEST("mutex try lock") {
+ mtx_t m;
+ REQUIRE(mtx_init(&m, mtx_plain) == thrd_success);
+
+ thrd_t t;
+ REQUIRE(thrd_create(&t, test_lock, &m) == thrd_success);
+
+ struct timespec sec = { .tv_sec = 0, .tv_nsec = SLEEP / 2 };
+ REQUIRE(thrd_sleep(&sec, NULL) == thrd_success);
+
+ REQUIRE(mtx_trylock(&m) == thrd_busy);
+
+ REQUIRE(thrd_join(t, NULL) == thrd_success);
+
+ REQUIRE(mtx_trylock(&m) == thrd_success);
+ REQUIRE(mtx_unlock(&m) == thrd_success);
+
+ mtx_destroy(&m);
+}