diff options
Diffstat (limited to 'unit_tests/scripts/lane/tasking_cancelling.lua')
-rw-r--r-- | unit_tests/scripts/lane/tasking_cancelling.lua | 89 |
1 files changed, 22 insertions, 67 deletions
diff --git a/unit_tests/scripts/lane/tasking_cancelling.lua b/unit_tests/scripts/lane/tasking_cancelling.lua index 85600ab..8bee3a6 100644 --- a/unit_tests/scripts/lane/tasking_cancelling.lua +++ b/unit_tests/scripts/lane/tasking_cancelling.lua | |||
@@ -15,69 +15,12 @@ local lanes_linda = assert(lanes.linda) | |||
15 | -- ################################################################################################## | 15 | -- ################################################################################################## |
16 | -- ################################################################################################## | 16 | -- ################################################################################################## |
17 | 17 | ||
18 | local function task(a, b, c) | ||
19 | lane_threadname("task("..a..","..b..","..c..")") | ||
20 | --error "111" -- testing error messages | ||
21 | assert(hey) | ||
22 | local v=0 | ||
23 | for i=a,b,c do | ||
24 | v= v+i | ||
25 | end | ||
26 | return v, hey | ||
27 | end | ||
28 | |||
29 | local gc_cb = function(name_, status_) | ||
30 | PRINT(" ---> lane '" .. name_ .. "' collected with status '" .. status_ .. "'") | ||
31 | end | ||
32 | |||
33 | -- ################################################################################################## | ||
34 | -- ################################################################################################## | ||
35 | -- ################################################################################################## | ||
36 | |||
37 | PRINT("\n\n", "---=== Tasking (cancelling) ===---", "\n\n") | ||
38 | |||
39 | local task_launch2 = lanes_gen("", { name = 'auto', globals={hey=true}, gc_cb = gc_cb }, task) | ||
40 | |||
41 | local N=999999999 | ||
42 | local lane9= task_launch2(1,N,1) -- huuuuuuge... | ||
43 | |||
44 | -- Wait until state changes "pending"->"running" | ||
45 | -- | ||
46 | local st | ||
47 | local t0= os.time() | ||
48 | while os.time()-t0 < 5 do | ||
49 | st= lane9.status | ||
50 | io.stderr:write((i==1) and st.." " or '.') | ||
51 | if st~="pending" then break end | ||
52 | end | ||
53 | PRINT(" "..st) | ||
54 | |||
55 | if st=="error" then | ||
56 | local _= lane9[0] -- propagate the error here | ||
57 | end | ||
58 | if st=="done" then | ||
59 | error("Looping to "..N.." was not long enough (cannot test cancellation)") | ||
60 | end | ||
61 | assert(st=="running", "st == " .. st) | ||
62 | |||
63 | -- when running under luajit, the function is JIT-ed, and the instruction count isn't hit, so we need a different hook | ||
64 | lane9:cancel(jit and "line" or "count", 100) -- 0 timeout, hook triggers cancelslation when reaching the specified count | ||
65 | |||
66 | local t0= os.time() | ||
67 | while os.time()-t0 < 5 do | ||
68 | st= lane9.status | ||
69 | io.stderr:write((i==1) and st.." " or '.') | ||
70 | if st~="running" then break end | ||
71 | end | ||
72 | PRINT(" "..st) | ||
73 | assert(st == "cancelled", "st is '" .. st .. "' instead of 'cancelled'") | ||
74 | |||
75 | -- cancellation of lanes waiting on a linda | 18 | -- cancellation of lanes waiting on a linda |
76 | local limited = lanes_linda("limited") | 19 | local limited = lanes_linda("limited") |
77 | assert.fails(function() limited:limit("key", -1) end) | 20 | assert.fails(function() limited:limit("key", -1) end) |
78 | assert.failsnot(function() limited:limit("key", 1) end) | 21 | assert.failsnot(function() limited:limit("key", 1) end) |
79 | -- [[################################################ | 22 | -- [[################################################ |
80 | limited:send("key", "hello") -- saturate linda | 23 | limited:send("key", "hello") -- saturate linda, so that subsequent sends will block |
81 | for k, v in pairs(limited:dump()) do | 24 | for k, v in pairs(limited:dump()) do |
82 | PRINT("limited[" .. tostring(k) .. "] = " .. tostring(v)) | 25 | PRINT("limited[" .. tostring(k) .. "] = " .. tostring(v)) |
83 | end | 26 | end |
@@ -88,11 +31,15 @@ local wait_send = function() | |||
88 | end | 31 | end |
89 | 32 | ||
90 | local wait_send_lane = lanes_gen("*", { name = 'auto' }, wait_send)() | 33 | local wait_send_lane = lanes_gen("*", { name = 'auto' }, wait_send)() |
91 | repeat until wait_send_lane.status == "waiting" | 34 | repeat |
92 | print "wait_send_lane is waiting" | 35 | io.stderr:write('!') |
36 | -- currently mingw64 builds can deadlock if we cancel the lane too early (before the linda blocks, at it causes the linda condvar not to be signalled) | ||
37 | lanes.sleep(0.1) | ||
38 | until wait_send_lane.status == "waiting" | ||
39 | PRINT "wait_send_lane is waiting" | ||
93 | wait_send_lane:cancel() -- hard cancel, 0 timeout | 40 | wait_send_lane:cancel() -- hard cancel, 0 timeout |
94 | repeat until wait_send_lane.status == "cancelled" | 41 | repeat until wait_send_lane.status == "cancelled" |
95 | print "wait_send_lane is cancelled" | 42 | PRINT "wait_send_lane is cancelled" |
96 | --################################################]] | 43 | --################################################]] |
97 | local wait_receive = function() | 44 | local wait_receive = function() |
98 | local k, v | 45 | local k, v |
@@ -101,11 +48,15 @@ local wait_receive = function() | |||
101 | end | 48 | end |
102 | 49 | ||
103 | local wait_receive_lane = lanes_gen("*", { name = 'auto' }, wait_receive)() | 50 | local wait_receive_lane = lanes_gen("*", { name = 'auto' }, wait_receive)() |
104 | repeat until wait_receive_lane.status == "waiting" | 51 | repeat |
105 | print "wait_receive_lane is waiting" | 52 | io.stderr:write('!') |
53 | -- currently mingw64 builds can deadlock if we cancel the lane too early (before the linda blocks, at it causes the linda condvar not to be signalled) | ||
54 | lanes.sleep(0.1) | ||
55 | until wait_receive_lane.status == "waiting" | ||
56 | PRINT "wait_receive_lane is waiting" | ||
106 | wait_receive_lane:cancel() -- hard cancel, 0 timeout | 57 | wait_receive_lane:cancel() -- hard cancel, 0 timeout |
107 | repeat until wait_receive_lane.status == "cancelled" | 58 | repeat until wait_receive_lane.status == "cancelled" |
108 | print "wait_receive_lane is cancelled" | 59 | PRINT "wait_receive_lane is cancelled" |
109 | --################################################]] | 60 | --################################################]] |
110 | local wait_receive_batched = function() | 61 | local wait_receive_batched = function() |
111 | local k, v1, v2 | 62 | local k, v1, v2 |
@@ -114,9 +65,13 @@ local wait_receive_batched = function() | |||
114 | end | 65 | end |
115 | 66 | ||
116 | local wait_receive_batched_lane = lanes_gen("*", { name = 'auto' }, wait_receive_batched)() | 67 | local wait_receive_batched_lane = lanes_gen("*", { name = 'auto' }, wait_receive_batched)() |
117 | repeat until wait_receive_batched_lane.status == "waiting" | 68 | repeat |
118 | print "wait_receive_batched_lane is waiting" | 69 | io.stderr:write('!') |
70 | -- currently mingw64 builds can deadlock if we cancel the lane too early (before the linda blocks, at it causes the linda condvar not to be signalled) | ||
71 | lanes.sleep(0.1) | ||
72 | until wait_receive_batched_lane.status == "waiting" | ||
73 | PRINT "wait_receive_batched_lane is waiting" | ||
119 | wait_receive_batched_lane:cancel() -- hard cancel, 0 timeout | 74 | wait_receive_batched_lane:cancel() -- hard cancel, 0 timeout |
120 | repeat until wait_receive_batched_lane.status == "cancelled" | 75 | repeat until wait_receive_batched_lane.status == "cancelled" |
121 | print "wait_receive_batched_lane is cancelled" | 76 | PRINT "wait_receive_batched_lane is cancelled" |
122 | --################################################]] | 77 | --################################################]] |