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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
local lanes = require "lanes" .configure{ with_timers = false, track_lanes = true}
local wait = lanes.sleep
print "hello"
local track = function( title_, expected_count_)
print( title_)
local count = 0
local threads = lanes.threads()
for k, v in pairs(threads)
do
print( k, v.name, v.status)
count = count + 1
end
assert(count == expected_count_, "unexpected active lane count")
print( "\n")
return threads
end
local sleeper = function( name_, seconds_)
-- print( "entering '" .. name_ .. "'")
local lanes = require "lanes"
-- no set_debug_threadname in main thread
if set_debug_threadname
then
-- print( "set_debug_threadname('" .. name_ .. "')")
set_debug_threadname( name_)
end
-- suspend the lane for the specified duration with a failed linda read
lanes.sleep(seconds_)
-- print( "exiting '" .. name_ .. "'")
end
-- sleeper( "main", 1)
-- the generator
local g = lanes.gen( "*", sleeper)
-- start a forever-waiting lane (nil timeout)
g( "forever", 'indefinitely')
-- start a lane that will last 2 seconds
g( "two_seconds", 2)
-- give a bit of time to reach the linda waiting call
wait( 0.1)
-- list the known lanes (should be living lanes)
local threads = track( "============= START", 2)
-- two_seconds forever
assert(threads[1].status == 'waiting' and threads[2].status == 'waiting')
-- wait until "two_seconds has completed"
wait(2.1)
local threads = track( "============= two_seconds dead", 2)
-- two_seconds forever
assert(threads[1].status == 'done' and threads[2].status == 'waiting')
-- start another lane that will last 2 seconds, with the same name
g( "two_seconds", 2)
-- give a bit of time to reach the linda waiting call
wait( 0.1)
-- list the known lanes
-- unless garbage collector cleaned it, we should have 3 lanes
local threads = track( "============= ANOTHER", 3)
-- two_seconds #2 two_seconds #1 forever
assert(threads[1].status == 'waiting' and threads[2].status == 'done' and threads[3].status == 'waiting')
-- this will collect the completed lane (and remove it from the tracking queue)
collectgarbage()
-- list the known lanes
local threads = track( "============= AFTER COLLECTGARBAGE", 2)
-- two_seconds #2 forever
assert(threads[1].status == 'waiting' and threads[2].status == 'waiting')
print "done"
|