aboutsummaryrefslogtreecommitdiff
path: root/tests/appendud.lua
blob: afea0e9a936fd4456b81eabedef87197ea15907a (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
50
51
52
53
54
55
56
57
58
--
-- APPENDUD.LUA
--
-- Lanes version for John Belmonte's challenge on Lua list (about finalizers):
-- <http://lua-users.org/lists/lua-l/2008-02/msg00243.html>
--
-- Needs Lanes >= 2.0.3
--
require "lanes"

local _tab = {
    beginupdate = function (this) print('tab.beginupdate') end;
    endupdate = function (this) print('tab.endupdate') end;
}
local _ud = {
    lock = function (this) print('ud.lock') end;
    unlock = function (this) print('ud.unlock') end;
    1,2,3,4,5;
}

-- 
-- This sample is with the 'finalize/guard' patch applied (new keywords):
--
--function appendud(tab, ud)
--    tab:beginupdate() finalize tab:endupdate() end
--    ud:lock() finalize ud:unlock() end
--    for i = 1,#ud do
--        tab[#tab+1] = ud[i]
--    end
--end


function appendud(tab, ud)
io.stderr:write "Starting"
    tab:beginupdate() set_finalizer( function() tab:endupdate() end )
    ud:lock() set_finalizer( function() ud:unlock() end )
    for i = 1,#ud do
        tab[#tab+1] = ud[i]
    end
io.stderr:write "Ending"
    return tab  -- need to return 'tab' since we're running in a separate thread
                -- ('tab' is passed over lanes by value, not by reference)
end

local t,err= lanes.gen( appendud )( _tab, _ud )   -- create & launch a thread
assert(t)
assert(not err)

-- test

t:join()    -- Need to explicitly wait for the thread, since 'ipairs()' does not
            -- value the '__index' metamethod (wouldn't it be cool if it did..?)

io.stderr:write(t[1])

for k,v in ipairs(t) do
    print(k,v)
end