summaryrefslogtreecommitdiff
path: root/tests/modules/scheduler/semaphore.lua
blob: 45cc43e1ee7cd69be1f522933a0a796f4e1804a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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