From e1adff76f2efecf0c82bd7a9db9ab60497be6926 Mon Sep 17 00:00:00 2001 From: ruki Date: Sat, 4 Oct 2025 23:07:02 +0800 Subject: add semaphore test --- tests/modules/scheduler/semaphore.lua | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 tests/modules/scheduler/semaphore.lua (limited to 'tests/modules/scheduler/semaphore.lua') diff --git a/tests/modules/scheduler/semaphore.lua b/tests/modules/scheduler/semaphore.lua new file mode 100644 index 000000000..097b0e360 --- /dev/null +++ b/tests/modules/scheduler/semaphore.lua @@ -0,0 +1,34 @@ +import("core.base.scheduler") + +function _loop(semaphore, id) + print("[%d]: start", id) + while true do + print("[%d]: wait ..", id) + --semaphore:wait(-1) + os.sleep(1000) + end + print("[%d]: end", id) +end + +function _input(semaphore) + while true do + if io.readable() then + local ch = io.read() + print(" -> post semaphore") + if ch then + semaphore:post(2) + end + else + os.sleep(1000) + end + end +end + +function main() + local semaphore = scheduler.co_semaphore("", 1) + for i = 1, 10 do + scheduler.co_start(_loop, semaphore, i) + end + scheduler.co_start(_input, semaphore) +end + -- cgit v1.3.1