1- local thread = require ' bee-compat'
2- local channel = require ' bee.channel'
3- local utility = require ' utility'
4- local await = require ' await'
1+ local thread = require ' bee.thread'
2+ local channelMod = require ' bee.channel'
3+ local selectMod = require ' bee.select'
4+ local utility = require ' utility'
5+ local await = require ' await'
56
6- thread .newchannel ' taskpad'
7- thread .newchannel ' waiter'
7+ local taskPad = channelMod .create (' taskpad' )
8+ local waiter = channelMod .create (' waiter' )
9+ local selector = selectMod .create ()
10+ selector :event_add (waiter :fd (), selectMod .SELECT_READ )
811
9- local errLog = thread .channel ' errlog'
10- local taskPad = thread .channel ' taskpad'
11- local waiter = thread .channel ' waiter'
1212local type = type
1313local counter = utility .counter ()
1414
@@ -51,7 +51,7 @@ function m.recruitBraves(num, privatePad)
5151 log .debug (' Create brave:' , id )
5252 m .braves [id ] = {
5353 id = id ,
54- thread = thread .thread (braveTemplate :format (
54+ thread = thread .create (braveTemplate :format (
5555 package.path ,
5656 package.cpath ,
5757 DEVELOP ,
@@ -67,11 +67,12 @@ function m.recruitBraves(num, privatePad)
6767 }
6868 end
6969 if privatePad and not m .prvtPad [privatePad ] then
70- thread .newchannel (' req:' .. privatePad )
71- thread .newchannel (' res:' .. privatePad )
70+ local reqCh = channelMod .create (' req:' .. privatePad )
71+ local resCh = channelMod .create (' res:' .. privatePad )
72+ selector :event_add (resCh :fd (), selectMod .SELECT_READ )
7273 m .prvtPad [privatePad ] = {
73- req = thread . channel ( ' req: ' .. privatePad ) ,
74- res = thread . channel ( ' res: ' .. privatePad ) ,
74+ req = reqCh ,
75+ res = resCh ,
7576 }
7677 end
7778end
@@ -167,11 +168,18 @@ end
167168--- 接收反馈
168169function m .recieve (block )
169170 if block then
170- local id , name , result = waiter :bpop ()
171- if type (name ) == ' string' then
172- m .popReport (m .braves [id ], name , result )
173- else
174- m .popTask (m .braves [id ], name , result )
171+ -- 使用 select 等待数据
172+ while true do
173+ local ok , id , name , result = waiter :pop ()
174+ if ok then
175+ if type (name ) == ' string' then
176+ m .popReport (m .braves [id ], name , result )
177+ else
178+ m .popTask (m .braves [id ], name , result )
179+ end
180+ break
181+ end
182+ selector :wait (- 1 )
175183 end
176184 else
177185 while true do
195203--- 检查伤亡情况
196204function m .checkDead ()
197205 while true do
198- local suc , err = errLog : pop ()
199- if not suc then
206+ local err = thread . errlog ()
207+ if not err then
200208 break
201209 end
202210 log .error (' Brave is dead!: ' .. err )
0 commit comments