-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample06.lua
65 lines (51 loc) · 2.66 KB
/
example06.lua
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
local llthreads = require("llthreads2.ex")
local mtmsg = require("mtmsg")
local threadIn = mtmsg.newbuffer()
local lst = mtmsg.newlistener()
local threadOut1 = lst:newbuffer()
local threadOut2 = lst:newbuffer()
local notifier = mtmsg.newbuffer() -- buffer used as common notifier
threadOut1:notifier(notifier)
threadOut2:notifier(notifier)
local thread = llthreads.new(function(inId, outId1, outId2)
local mtmsg = require("mtmsg")
local threadIn = mtmsg.buffer(inId)
local threadOut1 = mtmsg.buffer(outId1)
local threadOut2 = mtmsg.buffer(outId2)
threadOut1:addmsg("started")
assert(threadIn:nextmsg() == "next1") -- blocks for next message
threadOut2:addmsg(nil)
assert(threadIn:nextmsg() == "next2") -- blocks for next message
threadOut1:addmsg() -- empty message
assert(threadIn:nextmsg() == "exit") -- blocks for next message
threadOut1:addmsg("finished")
end,
threadIn:id(),
threadOut1:id(), threadOut2:id())
thread:start()
local function assertNone(...)
assert(select("#", ...) == 0)
end
local function assertIs(expected, ...)
assert(select("#", ...) == 1)
local value = ...
assert(value == expected)
end
do
assertNone( notifier:nextmsg()) -- blocks for next message
assertIs("started", threadOut1:nextmsg(0)) -- nonblocking read from buffer1
assertNone( threadOut2:nextmsg(0)) -- nonblocking read from buffer2
threadIn:addmsg("next1")
assertNone( notifier:nextmsg()) -- blocks for next message
assertNone( threadOut1:nextmsg(0)) -- nonblocking read from buffer1
assertIs(nil, threadOut2:nextmsg(0)) -- nonblocking read from buffer2
threadIn:addmsg("next2")
assertNone( notifier:nextmsg()) -- blocks for next message
assertNone( threadOut1:nextmsg(0)) -- nonblocking read from buffer1
assertNone( threadOut2:nextmsg(0)) -- nonblocking read from buffer2
threadIn:addmsg("exit")
assertNone( notifier:nextmsg()) -- blocks for next message
assertIs("finished", threadOut1:nextmsg(0)) -- nonblocking read from buffer1
assertNone( threadOut2:nextmsg(0)) -- nonblocking read from buffer2
end
print("Example06 OK.")