diff options
| author | ruki <[email protected]> | 2025-10-04 23:07:02 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2025-10-04 23:07:02 +0800 |
| commit | e1adff76f2efecf0c82bd7a9db9ab60497be6926 (patch) | |
| tree | 5dd0aeadf6840482cff821f3baa4fbec8ce151aa /tests/modules | |
| parent | 1705c0f34f8910c399b680f66dd60f453f91904c (diff) | |
add semaphore test
Diffstat (limited to 'tests/modules')
| -rw-r--r-- | tests/modules/scheduler/semaphore.lua | 34 |
1 files changed, 34 insertions, 0 deletions
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 + |
