blob: 756e33cb8dc8fc8a7fb7e3d5c7c396f13d7dc806 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
local lanes = require "lanes"
-- launch lanes that cooperate properly with cancellation request
local lane1 = function()
lane_threadname("lane1")
-- loop breaks on cancellation request
repeat
lanes.sleep(0)
until cancel_test()
print "lane1 cancelled"
end
local lane2 = function(linda_)
lane_threadname("lane2")
-- loop breaks on lane/linda cancellation
repeat
local k, v = linda_:receive('k')
until v == lanes.cancel_error
print "lane2 cancelled"
end
local lane3 = function()
lane_threadname("lane3")
-- this one cooperates too, because of the hook cancellation modes that Lanes will be using
-- but not with LuaJIT, because the function is compiled, and we don't call anyone, so no hook triggers
local fixture = require "fixture"
repeat until fixture.give_me_back(false)
end
-- the generators
local g1 = lanes.gen("*", { name = 'auto' }, lane1)
local g2 = lanes.gen("*", { name = 'auto' }, lane2)
local g3 = lanes.gen("*", { name = 'auto' }, lane3)
-- launch lanes
local h1 = g1()
local linda = lanes.linda()
local h2 = g2(linda)
local h3 = g3()
-- wait until they are both started
repeat until h1.status == "running" and h2.status == "waiting" and h3.status == "running"
-- let the script terminate, Lanes should not crash at shutdown
|