diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2011-01-04 21:31:17 +0100 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2011-01-04 21:31:17 +0100 |
commit | 79e46938c5d8daf164ab2d934f668fa27b32e4cf (patch) | |
tree | 407761f25bbdc3d5b2066a705dcbcf8711690242 /tests/appendud.lua | |
parent | ed07b457b6b45ece85d367dc8b89bf3c040abd9a (diff) | |
download | lanes-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.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 | ||