diff options
Diffstat (limited to '')
-rw-r--r-- | tests/appendud.lua | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/tests/appendud.lua b/tests/appendud.lua new file mode 100644 index 0000000..afea0e9 --- /dev/null +++ b/tests/appendud.lua | |||
@@ -0,0 +1,58 @@ | |||
1 | -- | ||
2 | -- APPENDUD.LUA | ||
3 | -- | ||
4 | -- Lanes version for John Belmonte's challenge on Lua list (about finalizers): | ||
5 | -- <http://lua-users.org/lists/lua-l/2008-02/msg00243.html> | ||
6 | -- | ||
7 | -- Needs Lanes >= 2.0.3 | ||
8 | -- | ||
9 | require "lanes" | ||
10 | |||
11 | local _tab = { | ||
12 | beginupdate = function (this) print('tab.beginupdate') end; | ||
13 | endupdate = function (this) print('tab.endupdate') end; | ||
14 | } | ||
15 | local _ud = { | ||
16 | lock = function (this) print('ud.lock') end; | ||
17 | unlock = function (this) print('ud.unlock') end; | ||
18 | 1,2,3,4,5; | ||
19 | } | ||
20 | |||
21 | -- | ||
22 | -- This sample is with the 'finalize/guard' patch applied (new keywords): | ||
23 | -- | ||
24 | --function appendud(tab, ud) | ||
25 | -- tab:beginupdate() finalize tab:endupdate() end | ||
26 | -- ud:lock() finalize ud:unlock() end | ||
27 | -- for i = 1,#ud do | ||
28 | -- tab[#tab+1] = ud[i] | ||
29 | -- end | ||
30 | --end | ||
31 | |||
32 | |||
33 | function appendud(tab, ud) | ||
34 | io.stderr:write "Starting" | ||
35 | tab:beginupdate() set_finalizer( function() tab:endupdate() end ) | ||
36 | ud:lock() set_finalizer( function() ud:unlock() end ) | ||
37 | for i = 1,#ud do | ||
38 | tab[#tab+1] = ud[i] | ||
39 | end | ||
40 | io.stderr:write "Ending" | ||
41 | return tab -- need to return 'tab' since we're running in a separate thread | ||
42 | -- ('tab' is passed over lanes by value, not by reference) | ||
43 | end | ||
44 | |||
45 | local t,err= lanes.gen( appendud )( _tab, _ud ) -- create & launch a thread | ||
46 | assert(t) | ||
47 | assert(not err) | ||
48 | |||
49 | -- test | ||
50 | |||
51 | t:join() -- Need to explicitly wait for the thread, since 'ipairs()' does not | ||
52 | -- value the '__index' metamethod (wouldn't it be cool if it did..?) | ||
53 | |||
54 | io.stderr:write(t[1]) | ||
55 | |||
56 | for k,v in ipairs(t) do | ||
57 | print(k,v) | ||
58 | end | ||