aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--tests/protect_allocator.lua50
1 files changed, 50 insertions, 0 deletions
diff --git a/tests/protect_allocator.lua b/tests/protect_allocator.lua
new file mode 100644
index 0000000..8877eaf
--- /dev/null
+++ b/tests/protect_allocator.lua
@@ -0,0 +1,50 @@
1local print = print
2
3local lanes = require "lanes".configure{ with_timers = false, protect_allocator=true}
4
5local linda = lanes.linda()
6
7local body = function( id_)
8 set_finalizer( function( err, stk)
9 if err == lanes.cancel_error then
10 -- lane cancellation is performed by throwing a special userdata as error
11 print( "after cancel")
12 elseif err then
13 -- no special error: true error
14 print( " error: "..tostring(err))
15 else
16 -- no error: we just got finalized
17 print( "[" .. id_ .. "] done")
18 end
19 end)
20
21 print( "[" .. id_ .. "] waiting for signal")
22 -- wait for the semaphore to be available
23 local _, cost = linda:receive( "lock")
24 -- starting to work
25 print( "[" .. id_ .. "] doing some allocations")
26 for i = 1, cost * (1 + id_ * 0.1) do
27 local t = { "some", "table", "with", "a", "few", "fields"}
28 end
29 linda:send( "key", "done")
30end
31
32-- start threads
33local COUNT = 4
34local gen = lanes.gen( "*", body)
35for i = 1, COUNT do
36 gen( i)
37end
38
39-- wait a bit
40print "waiting a bit ..."
41linda:receive( 2, "foo")
42-- tell lanes to start running
43print "GO!"
44for i = 1, COUNT do
45 linda:send( "lock", 300000)
46end
47
48-- wait for completion
49linda:receive( linda.batched, "key", COUNT)
50print "SUCCESS"