summaryrefslogtreecommitdiff
path: root/tests/modules
diff options
context:
space:
mode:
authorruki <[email protected]>2025-10-05 09:45:26 +0800
committerGitHub <[email protected]>2025-10-05 09:45:26 +0800
commit6ffb08da4ee35ccc5914fbec6f96158e6e033692 (patch)
tree2bfa5006760400b2469698a4408e677297112ae4 /tests/modules
parent82f581a047820d7136b7bf6b6a969b4d3a96e497 (diff)
parent137a9d0875ac96e3636410479d1551f89acf3632 (diff)
Merge pull request #6891 from xmake-io/sem
Add coroutine semaphore
Diffstat (limited to 'tests/modules')
-rw-r--r--tests/modules/scheduler/semaphore.lua33
1 files changed, 33 insertions, 0 deletions
diff --git a/tests/modules/scheduler/semaphore.lua b/tests/modules/scheduler/semaphore.lua
new file mode 100644
index 000000000..45cc43e1e
--- /dev/null
+++ b/tests/modules/scheduler/semaphore.lua
@@ -0,0 +1,33 @@
+import("core.base.scheduler")
+
+function _loop(semaphore, id)
+ print("[%d]: start", id)
+ while true do
+ print("[%d]: wait ..", id)
+ local value = semaphore:wait(-1)
+ print("[%d]: -> triggered, value: %d ..", id, value)
+ end
+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
+