summaryrefslogtreecommitdiff
path: root/tests/appendud.lua
diff options
context:
space:
mode:
authorBenoit Germain <bnt.germain@gmail.com>2011-01-04 21:31:17 +0100
committerBenoit Germain <bnt.germain@gmail.com>2011-01-04 21:31:17 +0100
commit79e46938c5d8daf164ab2d934f668fa27b32e4cf (patch)
tree407761f25bbdc3d5b2066a705dcbcf8711690242 /tests/appendud.lua
parented07b457b6b45ece85d367dc8b89bf3c040abd9a (diff)
downloadlanes-79e46938c5d8daf164ab2d934f668fa27b32e4cf.tar.gz
lanes-79e46938c5d8daf164ab2d934f668fa27b32e4cf.tar.bz2
lanes-79e46938c5d8daf164ab2d934f668fa27b32e4cf.zip
Take all code from Asko Kauppi's SVN server, and push it here so that the github repository becomes the official Lanes source codebase.
Note that Asko's SVN server holds version 2.0.9, whereas this is version 2.0.10, but I don't see any real need to update SVN if it is to become deprecated. Next steps: - upgrade the rockspec to the latest version - make the html help available online somewhere Signed-off-by: Benoit Germain <bnt.germain@gmail.com>
Diffstat (limited to 'tests/appendud.lua')
-rw-r--r--tests/appendud.lua58
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--
9require "lanes"
10
11local _tab = {
12 beginupdate = function (this) print('tab.beginupdate') end;
13 endupdate = function (this) print('tab.endupdate') end;
14}
15local _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
33function appendud(tab, ud)
34io.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
40io.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)
43end
44
45local t,err= lanes.gen( appendud )( _tab, _ud ) -- create & launch a thread
46assert(t)
47assert(not err)
48
49-- test
50
51t: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
54io.stderr:write(t[1])
55
56for k,v in ipairs(t) do
57 print(k,v)
58end