Skip to content

Commit 719d7c2

Browse files
committed
update bee
1 parent 94b06e3 commit 719d7c2

File tree

5 files changed

+67
-19
lines changed

5 files changed

+67
-19
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,6 @@
66
!/meta/3rd
77
!/meta/whimsical
88
/bin*
9+
/.vs/*
10+
!/.vs/launch.vs.json
11+
!/.vs/tasks.vs.json

.vs/launch.vs.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"version": "0.2.1",
3+
"defaults": {},
4+
"configurations": [
5+
{
6+
"type": "native",
7+
"name": "debug",
8+
"project": "bin\\lua-language-server.exe",
9+
"args": [
10+
"test.lua"
11+
],
12+
"currentDir": ".",
13+
"projectTarget": ""
14+
}
15+
]
16+
}

script/brave/brave.lua

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
1-
local thread = require 'bee.thread'
1+
local channel = require 'bee.channel'
2+
local select = require 'bee.select'
23

3-
local taskPad = thread.channel('taskpad')
4-
local waiter = thread.channel('waiter')
4+
local function channel_init(chan)
5+
local selector = select.create()
6+
selector:event_add(chan:fd(), select.SELECT_READ)
7+
return { selector, chan }
8+
end
9+
10+
local function channel_bpop(ctx)
11+
local selector, chan = ctx[1], ctx[2]
12+
for _ in selector:wait() do
13+
local r = table.pack(chan:pop())
14+
if r[1] == true then
15+
return table.unpack(r, 2)
16+
end
17+
end
18+
end
19+
20+
local taskPad = channel_init(channel.query('taskpad'))
21+
local waiter = channel.query('waiter')
522

623
---@class pub_brave
724
local m = {}
@@ -42,11 +59,11 @@ end
4259

4360
--- 开始找工作
4461
function m.start(privatePad)
45-
local reqPad = privatePad and thread.channel('req:' .. privatePad) or taskPad
46-
local resPad = privatePad and thread.channel('res:' .. privatePad) or waiter
62+
local reqPad = privatePad and channel_init(channel.query('req:' .. privatePad)) or taskPad
63+
local resPad = privatePad and channel.query('res:' .. privatePad) or waiter
4764
m.push('mem', collectgarbage 'count')
4865
while true do
49-
local name, id, params = reqPad:bpop()
66+
local name, id, params = channel_bpop(reqPad)
5067
local ability = m.ability[name]
5168
-- TODO
5269
if not ability then

script/pub/pub.lua

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
11
local thread = require 'bee.thread'
2+
local channel = require 'bee.channel'
3+
local select = require 'bee.select'
24
local utility = require 'utility'
35
local await = require 'await'
46

5-
thread.newchannel 'taskpad'
6-
thread.newchannel 'waiter'
7+
local function channel_init(chan)
8+
local selector = select.create()
9+
selector:event_add(chan:fd(), select.SELECT_READ)
10+
return { selector, chan }
11+
end
12+
13+
local function channel_bpop(ctx)
14+
local selector, chan = ctx[1], ctx[2]
15+
for _ in selector:wait() do
16+
local r = table.pack(chan:pop())
17+
if r[1] == true then
18+
return table.unpack(r, 2)
19+
end
20+
end
21+
end
722

8-
local errLog = thread.channel 'errlog'
9-
local taskPad = thread.channel 'taskpad'
10-
local waiter = thread.channel 'waiter'
23+
local taskPad = channel.create 'taskpad'
24+
local waiter = channel_init(channel.create 'waiter')
1125
local type = type
1226
local counter = utility.counter()
1327

@@ -66,11 +80,9 @@ function m.recruitBraves(num, privatePad)
6680
}
6781
end
6882
if privatePad and not m.prvtPad[privatePad] then
69-
thread.newchannel('req:' .. privatePad)
70-
thread.newchannel('res:' .. privatePad)
7183
m.prvtPad[privatePad] = {
72-
req = thread.channel('req:' .. privatePad),
73-
res = thread.channel('res:' .. privatePad),
84+
req = channel.create('req:' .. privatePad),
85+
res = channel.create('res:' .. privatePad),
7486
}
7587
end
7688
end
@@ -166,7 +178,7 @@ end
166178
--- 接收反馈
167179
function m.recieve(block)
168180
if block then
169-
local id, name, result = waiter:bpop()
181+
local id, name, result = channel_bpop(waiter)
170182
if type(name) == 'string' then
171183
m.popReport(m.braves[id], name, result)
172184
else
@@ -175,7 +187,7 @@ function m.recieve(block)
175187
else
176188
while true do
177189
local ok
178-
if m.reciveFromPad(waiter) then
190+
if m.reciveFromPad(waiter[2]) then
179191
ok = true
180192
end
181193
for _, pad in pairs(m.prvtPad) do
@@ -194,7 +206,7 @@ end
194206
--- 检查伤亡情况
195207
function m.checkDead()
196208
while true do
197-
local suc, err = errLog:pop()
209+
local suc, err = thread.errlog()
198210
if not suc then
199211
break
200212
end

0 commit comments

Comments
 (0)