summaryrefslogtreecommitdiff
path: root/tests/modules/scheduler/semaphore.lua
blob: 097b0e360b55bef288e384f373d8497f0f80a919 (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
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