aboutsummaryrefslogtreecommitdiff
path: root/tests/fifo.lua
blob: bef60d55774ac0a75a36205202048efff70355e8 (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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
--
-- FIFO.LUA
--
-- Sample program for Lua Lanes
--

local lanes = require "lanes".configure{shutdown_timeout=3,with_timers=true}

local linda = lanes.linda( "atom")
local atomic_inc= lanes.genatomic( linda, "FIFO_n")

assert( atomic_inc()==1)
assert( atomic_inc()==2)

local function FIFO()
    local my_channel= "FIFO"..atomic_inc()

    return {
        -- Giving explicit 'nil' timeout allows numbers to be used as 'my_channel'
        --
        send = function(self, ...)
            linda:send( nil, my_channel, ...)
        end,
        receive = function(self, timeout)
            return linda:receive( timeout, my_channel)
        end
    }
end

local A = FIFO()
local B = FIFO()

print "Sending to A.."
A:send( 1,2,3,4,5)

print "Sending to B.."
B:send( 'a','b','c')

print "Reading A.."
print( A:receive( 1.0))

print "Reading B.."
print( B:receive( 2.0))

-- Note: A and B can be passed between threads, or used as upvalues
--       by multiple threads (other parts will be copied but the 'linda'
--       handle is shared userdata and will thus point to the single place)
lanes.timer_lane:cancel() -- hard cancel, 0 timeout
lanes.timer_lane:join()