diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-03-02 20:34:10 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-04-07 01:36:33 -0600 |
commit | 77052122e4e0741026bef6623b13a95e63e524e3 (patch) | |
tree | a6bdf9520f8f7cd974b2e23d1d1660b5e86d1cb0 | |
parent | 33b7cf675238367db2fc0528dea103d7792eaa0e (diff) |
libcr_ipc: Add a test for mutex
-rw-r--r-- | libcr_ipc/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libcr_ipc/tests/test_mutex.c | 37 |
2 files changed, 38 insertions, 1 deletions
diff --git a/libcr_ipc/CMakeLists.txt b/libcr_ipc/CMakeLists.txt index 8545798..ae515bc 100644 --- a/libcr_ipc/CMakeLists.txt +++ b/libcr_ipc/CMakeLists.txt @@ -16,7 +16,7 @@ target_link_libraries(libcr_ipc INTERFACE set(ipc_tests chan #select - #mutex + mutex #owned_mutex rpc sema 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 <libcr_ipc/mutex.h> + * + * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <libcr/coroutine.h> +#include <libcr_ipc/mutex.h> + +#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; +} |