summaryrefslogtreecommitdiff
path: root/tests/modules
diff options
context:
space:
mode:
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
+