From 77052122e4e0741026bef6623b13a95e63e524e3 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sun, 2 Mar 2025 20:34:10 -0700 Subject: libcr_ipc: Add a test for mutex --- libcr_ipc/tests/test_mutex.c | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 libcr_ipc/tests/test_mutex.c (limited to 'libcr_ipc/tests/test_mutex.c') diff --git a/libcr_ipc/tests/test_mutex.c b/libcr_ipc/tests/test_mutex.c new file mode 100644 index 0000000..ee088f4 --- /dev/null +++ b/libcr_ipc/tests/test_mutex.c @@ -0,0 +1,37 @@ +/* libcr_ipc/tests/test_mutex.c - Tests for + * + * Copyright (C) 2025 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include +#include + +#include "test.h" + +int counter = 0; + +COROUTINE cr_worker(void *_mu) { + cr_mutex_t *mu = _mu; + cr_begin(); + + for (int i = 0; i < 100; i++) { + cr_mutex_lock(mu); + int a = counter; + cr_yield(); + counter = a + 1; + cr_mutex_unlock(mu); + cr_yield(); + } + + cr_end(); +} + +int main() { + cr_mutex_t mu = {0}; + coroutine_add("a", cr_worker, &mu); + coroutine_add("b", cr_worker, &mu); + coroutine_main(); + test_assert(counter == 200); + return 0; +} -- cgit v1.2.3-2-g168b From 4d5a8b2f99be5e04954c5067080d1725af8c0ae7 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Mon, 7 Apr 2025 00:09:00 -0600 Subject: libcr_ipc: Pull as much as possible from public .h to .c files --- libcr_ipc/tests/test_mutex.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'libcr_ipc/tests/test_mutex.c') diff --git a/libcr_ipc/tests/test_mutex.c b/libcr_ipc/tests/test_mutex.c index ee088f4..43714c9 100644 --- a/libcr_ipc/tests/test_mutex.c +++ b/libcr_ipc/tests/test_mutex.c @@ -28,7 +28,7 @@ COROUTINE cr_worker(void *_mu) { } int main() { - cr_mutex_t mu = {0}; + cr_mutex_t mu = {}; coroutine_add("a", cr_worker, &mu); coroutine_add("b", cr_worker, &mu); coroutine_main(); -- cgit v1.2.3-2-g168b