aboutsummaryrefslogtreecommitdiff
path: root/tests/appendud.lua
blob: f6f99c1161db662cae2c1afaefd0bd8c13de9688 (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
59
60
--
-- 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
--
local lanes = 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( "base,io", { name = 'auto'}, appendud )( _tab, _ud )   -- create & launch a thread
assert(t)
assert(not err)

-- test
-- print("t:join()")
a,b,c = t[1],t[2],t[3]    -- Need to explicitly wait for the thread, since 'ipairs()' does not
--a,b,c = 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..?)

print(a,b,c)
-- print("io.stderr:write(t[1])")
-- io.stderr:write(t[1])
_ = t[0]
print(_)