Skip to content

Commit 2732996

Browse files
committed
删除 bee.lua 兼容性层和相关测试,更新相关模块以直接使用新 API
1 parent bbf96bd commit 2732996

File tree

6 files changed

+58
-226
lines changed

6 files changed

+58
-226
lines changed

COMPAT_FIXES.md

Lines changed: 0 additions & 64 deletions
This file was deleted.

script/bee-compat.lua

Lines changed: 0 additions & 85 deletions
This file was deleted.

script/brave/brave.lua

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

3-
local taskPad = thread.channel('taskpad')
4-
local waiter = thread.channel('waiter')
5+
local taskPad = channelMod.query('taskpad')
6+
local waiter = channelMod.query('waiter')
7+
8+
assert(taskPad, 'taskpad channel not found')
9+
assert(waiter, 'waiter channel not found')
510

611
---@class pub_brave
712
local m = {}
@@ -42,11 +47,24 @@ end
4247

4348
--- 开始找工作
4449
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
50+
local reqPad = privatePad and channelMod.query('req:' .. privatePad) or taskPad
51+
local resPad = privatePad and channelMod.query('res:' .. privatePad) or waiter
52+
local selector = selectMod.create()
53+
selector:event_add(reqPad:fd(), selectMod.SELECT_READ)
54+
4755
m.push('mem', collectgarbage 'count')
4856
while true do
49-
local name, id, params = reqPad:bpop()
57+
-- 使用 select 实现阻塞等待
58+
local name, id, params
59+
while true do
60+
local ok, n, i, p = reqPad:pop()
61+
if ok then
62+
name, id, params = n, i, p
63+
break
64+
end
65+
selector:wait(-1)
66+
end
67+
5068
local ability = m.ability[name]
5169
-- TODO
5270
if not ability then

script/meta/bee/channel.lua

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---@meta
22

33
---@class bee.channel
4-
local channel_mod = {}
4+
local channelMod = {}
55

66
---@class bee.channel.object
77
local channel = {}
@@ -21,16 +21,16 @@ end
2121

2222
---@param name string
2323
---@return bee.channel.object
24-
function channel_mod.create(name)
24+
function channelMod.create(name)
2525
end
2626

2727
---@param name string
28-
function channel_mod.destroy(name)
28+
function channelMod.destroy(name)
2929
end
3030

3131
---@param name string
3232
---@return bee.channel.object?
33-
function channel_mod.query(name)
33+
function channelMod.query(name)
3434
end
3535

36-
return channel_mod
36+
return channelMod

script/pub/pub.lua

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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'
1212
local type = type
1313
local 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
7778
end
@@ -167,11 +168,18 @@ end
167168
--- 接收反馈
168169
function 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
@@ -195,8 +203,8 @@ end
195203
--- 检查伤亡情况
196204
function 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)

test_compat.lua

Lines changed: 0 additions & 45 deletions
This file was deleted.

0 commit comments

Comments
 (0)