aboutsummaryrefslogtreecommitdiff
path: root/tests/keeper.lua
blob: 73ed3cf835a4993896e99ce8ece193de96e08a3e (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
--
-- KEEPER.LUA
--
-- Test program for Lua Lanes
--

local lanes = require "lanes".configure{ with_timers = false}

local function keeper(linda)
    local mt= {
        __index= function( _, key )
            return linda:get( key )
        end,
        __newindex= function( _, key, val ) 
            linda:set( key, val )
        end
    }
    return setmetatable( {}, mt )
end

--
local lindaA= lanes.linda()
local A= keeper( lindaA )

local lindaB= lanes.linda()
local B= keeper( lindaB )

A.some= 1
print( A.some )
assert( A.some==1 )

B.some= "hoo"
assert( B.some=="hoo" )
assert( A.some==1 )

function lane()
    local a= keeper(lindaA)
    print( a.some )
    assert( a.some==1 )
    a.some= 2
end

local h= lanes.gen( "io", lane )()
h:join()

print( A.some )     -- 2
assert( A.some==2 )