Skip to content

Commit 98c4293

Browse files
committed
shelve
1 parent 07d37e7 commit 98c4293

File tree

2 files changed

+37
-15
lines changed

2 files changed

+37
-15
lines changed

script/brave/brave.lua

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,22 +42,23 @@ end
4242

4343
--- 开始找工作
4444
function m.start(privatePad)
45-
local myPad = privatePad and thread.channel('private:' .. privatePad) or taskPad
45+
local reqPad = privatePad and thread.channel('req:' .. privatePad) or taskPad
46+
local resPad = privatePad and thread.channel('res:' .. privatePad) or waiter
4647
m.push('mem', collectgarbage 'count')
4748
while true do
48-
local name, id, params = myPad:bpop()
49+
local name, id, params = reqPad:bpop()
4950
local ability = m.ability[name]
5051
-- TODO
5152
if not ability then
52-
waiter:push(m.id, id)
53+
resPad:push(m.id, id)
5354
log.error('Brave can not handle this work: ' .. name)
5455
goto CONTINUE
5556
end
5657
local ok, res = xpcall(ability, log.error, params)
5758
if ok then
58-
waiter:push(m.id, id, res)
59+
resPad:push(m.id, id, res)
5960
else
60-
waiter:push(m.id, id)
61+
resPad:push(m.id, id)
6162
end
6263
m.push('mem', collectgarbage 'count')
6364
::CONTINUE::

script/pub/pub.lua

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,12 @@ function m.recruitBraves(num, privatePad)
6666
}
6767
end
6868
if privatePad and not m.prvtPad[privatePad] then
69-
thread.newchannel('private:' .. privatePad)
70-
m.prvtPad[privatePad] = thread.channel('private:' .. privatePad)
69+
thread.newchannel('req:' .. privatePad)
70+
thread.newchannel('res:' .. privatePad)
71+
m.prvtPad[privatePad] = {
72+
req = thread.channel('req:' .. privatePad),
73+
res = thread.channel('res:' .. privatePad),
74+
}
7175
end
7276
end
7377

@@ -77,7 +81,7 @@ function m.pushTask(info)
7781
return false
7882
end
7983
if m.prvtPad[info.name] then
80-
m.prvtPad[info.name]:push(info.name, info.id, info.params)
84+
m.prvtPad[info.name].req:push(info.name, info.id, info.params)
8185
else
8286
taskPad:push(info.name, info.id, info.params)
8387
end
@@ -146,6 +150,19 @@ function m.task(name, params, callback)
146150
return m.pushTask(info)
147151
end
148152

153+
function m.reciveFromPad(pad)
154+
local suc, id, name, result = pad:pop()
155+
if not suc then
156+
return false
157+
end
158+
if type(name) == 'string' then
159+
m.popReport(m.braves[id], name, result)
160+
else
161+
m.popTask(m.braves[id], name, result)
162+
end
163+
return true
164+
end
165+
149166
--- 接收反馈
150167
function m.recieve(block)
151168
if block then
@@ -157,14 +174,18 @@ function m.recieve(block)
157174
end
158175
else
159176
while true do
160-
local suc, id, name, result = waiter:pop()
161-
if not suc then
162-
break
177+
local ok
178+
if m.reciveFromPad(waiter) then
179+
ok = true
163180
end
164-
if type(name) == 'string' then
165-
m.popReport(m.braves[id], name, result)
166-
else
167-
m.popTask(m.braves[id], name, result)
181+
for _, pad in pairs(m.prvtPad) do
182+
if m.reciveFromPad(pad.res) then
183+
ok = true
184+
end
185+
end
186+
187+
if not ok then
188+
break
168189
end
169190
end
170191
end

0 commit comments

Comments
 (0)