diff options
| author | ruki <[email protected]> | 2025-10-05 09:45:26 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-10-05 09:45:26 +0800 |
| commit | 6ffb08da4ee35ccc5914fbec6f96158e6e033692 (patch) | |
| tree | 2bfa5006760400b2469698a4408e677297112ae4 /tests | |
| parent | 82f581a047820d7136b7bf6b6a969b4d3a96e497 (diff) | |
| parent | 137a9d0875ac96e3636410479d1551f89acf3632 (diff) | |
Merge pull request #6891 from xmake-io/sem
Add coroutine semaphore
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/benchmarks/async/runjobs.lua | 33 | ||||
| -rw-r--r-- | tests/modules/scheduler/semaphore.lua | 33 |
2 files changed, 66 insertions, 0 deletions
diff --git a/tests/benchmarks/async/runjobs.lua b/tests/benchmarks/async/runjobs.lua new file mode 100644 index 000000000..937697413 --- /dev/null +++ b/tests/benchmarks/async/runjobs.lua @@ -0,0 +1,33 @@ +import("async.runjobs") + +function test_run(total, comax) + local f = function () end + local t1 = os.mclock() + runjobs("test", f, {total = total, comax = comax}) + t1 = os.mclock() - t1 + + local n = total + local t2 = os.mclock() + while n ~= 0 do + f() + n = n - 1 + end + t2 = os.mclock() - t2 + print("runjobs(%d/%d): %d ms, plain: %d ms", total, comax, t1, t2) +end + +function test_run_proc(total, comax) + local f = function () os.runv(os.programfile(), {"--version"}) end + local t1 = os.mclock() + runjobs("test", f, {total = total, comax = comax}) + t1 = os.mclock() - t1 + print("runjobs_proc(%d/%d): %d ms", total, comax, t1) +end + +function main() + test_run(10000, 1) + test_run(10000, 10) + test_run(10000, 100) + test_run_proc(1000, 10) +end + 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 + |
