diff options
-rw-r--r-- | tests/protect_allocator.lua | 50 |
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 @@ | |||
1 | local print = print | ||
2 | |||
3 | local lanes = require "lanes".configure{ with_timers = false, protect_allocator=true} | ||
4 | |||
5 | local linda = lanes.linda() | ||
6 | |||
7 | local 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") | ||
30 | end | ||
31 | |||
32 | -- start threads | ||
33 | local COUNT = 4 | ||
34 | local gen = lanes.gen( "*", body) | ||
35 | for i = 1, COUNT do | ||
36 | gen( i) | ||
37 | end | ||
38 | |||
39 | -- wait a bit | ||
40 | print "waiting a bit ..." | ||
41 | linda:receive( 2, "foo") | ||
42 | -- tell lanes to start running | ||
43 | print "GO!" | ||
44 | for i = 1, COUNT do | ||
45 | linda:send( "lock", 300000) | ||
46 | end | ||
47 | |||
48 | -- wait for completion | ||
49 | linda:receive( linda.batched, "key", COUNT) | ||
50 | print "SUCCESS" | ||