diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-02-21 00:53:45 -0700 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-02-21 10:33:20 -0700 |
commit | b5e84c26699d9b2beadf2e44adbd92c2c18b5bfa (patch) | |
tree | b78617e485d06ac5e6fb4cdd7663d81c82f1ba2b /libcr_ipc | |
parent | 6639031e15b23d342efb87cc00fa51233d66b392 (diff) |
libcr_ipc: Fixes and tests for sema.h
Diffstat (limited to 'libcr_ipc')
-rw-r--r-- | libcr_ipc/CMakeLists.txt | 2 | ||||
-rw-r--r-- | libcr_ipc/include/libcr_ipc/sema.h | 33 | ||||
-rw-r--r-- | libcr_ipc/tests/test_sema.c | 74 |
3 files changed, 96 insertions, 13 deletions
diff --git a/libcr_ipc/CMakeLists.txt b/libcr_ipc/CMakeLists.txt index 05e6fa3..a7d6021 100644 --- a/libcr_ipc/CMakeLists.txt +++ b/libcr_ipc/CMakeLists.txt @@ -15,7 +15,7 @@ set(ipc_tests #mutex #owned_mutex rpc - #sema + sema ) if (ENABLE_TESTS) foreach(test IN LISTS ipc_tests) diff --git a/libcr_ipc/include/libcr_ipc/sema.h b/libcr_ipc/include/libcr_ipc/sema.h index 1064182..6db4015 100644 --- a/libcr_ipc/include/libcr_ipc/sema.h +++ b/libcr_ipc/include/libcr_ipc/sema.h @@ -1,12 +1,14 @@ /* libcr_ipc/sema.h - Simple semaphores for libcr * - * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> * SPDX-License-Identifier: AGPL-3.0-or-later */ #ifndef _LIBCR_IPC_SEMA_H_ #define _LIBCR_IPC_SEMA_H_ +#include <stdbool.h> + #include <libcr/coroutine.h> /* for cid_t, cr_* */ #include <libcr_ipc/_linkedlist.h> @@ -23,6 +25,7 @@ struct _cr_sema_waiter { */ typedef struct { unsigned int cnt; + bool unpausing; _cr_ipc_sll_root waiters; } cr_sema_t; @@ -39,10 +42,10 @@ static inline void cr_sema_signal(cr_sema_t *sema) { bool saved = cr_save_and_disable_interrupts(); sema->cnt++; - if (sema->waiters.front) { + if (sema->waiters.front && !sema->unpausing) { cr_unpause( _cr_ipc_sll_node_cast(struct _cr_sema_waiter, sema->waiters.front)->cid); - _cr_ipc_sll_pop_from_front(&sema->waiters); + sema->unpausing = true; } cr_restore_interrupts(saved); } @@ -57,10 +60,10 @@ static inline void cr_sema_signal_from_intrhandler(cr_sema_t *sema) { cr_assert_in_intrhandler(); sema->cnt++; - if (sema->waiters.front) { + if (sema->waiters.front && !sema->unpausing) { cr_unpause_from_intrhandler( _cr_ipc_sll_node_cast(struct _cr_sema_waiter, sema->waiters.front)->cid); - _cr_ipc_sll_pop_from_front(&sema->waiters); + sema->unpausing = true; } } @@ -76,15 +79,21 @@ static inline void cr_sema_wait(cr_sema_t *sema) { cr_assert_in_coroutine(); bool saved = cr_save_and_disable_interrupts(); - if (!sema->cnt) { - struct _cr_sema_waiter self = { - .cid = cr_getcid(), - }; - _cr_ipc_sll_push_to_rear(&sema->waiters, &self); + + struct _cr_sema_waiter self = { + .cid = cr_getcid(), + }; + _cr_ipc_sll_push_to_rear(&sema->waiters, &self); + if (sema->waiters.front != &self || !sema->cnt) cr_pause_and_yield(); - } - assert(sema->cnt); + assert(sema->waiters.front == &self && sema->cnt); + _cr_ipc_sll_pop_from_front(&sema->waiters); sema->cnt--; + if (sema->cnt && sema->waiters.front) + cr_unpause( + _cr_ipc_sll_node_cast(struct _cr_sema_waiter, sema->waiters.front)->cid); + else + sema->unpausing = false; cr_restore_interrupts(saved); } diff --git a/libcr_ipc/tests/test_sema.c b/libcr_ipc/tests/test_sema.c new file mode 100644 index 0000000..3208237 --- /dev/null +++ b/libcr_ipc/tests/test_sema.c @@ -0,0 +1,74 @@ +/* libcr_ipc/tests/test_sema.c - Tests for <libcr_ipc/sema.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/sema.h> + +#include "test.h" + +int counter = 0; + +COROUTINE cr_first(void *_sema) { + cr_sema_t *sema = _sema; + cr_begin(); + + cr_sema_wait(sema); + counter++; + cr_sema_signal(sema); + + cr_exit(); +} + +COROUTINE cr_second(void *_sema) { + cr_sema_t *sema = _sema; + cr_begin(); + + cr_sema_signal(sema); /* should be claimed by cr_first, which has been waiting */ + cr_sema_wait(sema); /* should block, because cr_first claimed it */ + test_assert(counter == 1); + + cr_exit(); +} + +COROUTINE cr_producer(void *_sema) { + cr_sema_t *sema = _sema; + cr_begin(); + + for (int i = 0; i < 10; i++) + cr_sema_signal(sema); + + cr_end(); +} + +COROUTINE cr_consumer(void *_sema) { + cr_sema_t *sema = _sema; + cr_begin(); + + for (int i = 0; i < 5; i++) + cr_sema_wait(sema); + + cr_end(); +} + +int main() { + cr_sema_t sema = {0}; + + printf("== test 1 =========================================\n"); + coroutine_add("first", cr_first, &sema); + coroutine_add("second", cr_second, &sema); + coroutine_main(); + test_assert(sema.cnt == 0); + + printf("== test 2 =========================================\n"); + coroutine_add("consumer", cr_consumer, &sema); + coroutine_add("producer", cr_producer, &sema); + coroutine_main(); + coroutine_add("consumer", cr_consumer, &sema); + coroutine_main(); + test_assert(sema.cnt == 0); + + return 0; +} |