diff options
-rw-r--r-- | .gitignore | 3 | ||||
-rw-r--r-- | .vs/launch.vs.json | 16 | ||||
m--------- | 3rd/bee.lua | 0 | ||||
-rw-r--r-- | script/brave/brave.lua | 29 | ||||
-rw-r--r-- | script/pub/pub.lua | 36 |
5 files changed, 66 insertions, 18 deletions
@@ -6,3 +6,6 @@ !/meta/3rd !/meta/whimsical /bin* +/.vs/* +!/.vs/launch.vs.json +!/.vs/tasks.vs.json diff --git a/.vs/launch.vs.json b/.vs/launch.vs.json new file mode 100644 index 00000000..6a6569d5 --- /dev/null +++ b/.vs/launch.vs.json @@ -0,0 +1,16 @@ +{ + "version": "0.2.1", + "defaults": {}, + "configurations": [ + { + "type": "native", + "name": "debug", + "project": "bin\\lua-language-server.exe", + "args": [ + "test.lua" + ], + "currentDir": ".", + "projectTarget": "" + } + ] +}
\ No newline at end of file diff --git a/3rd/bee.lua b/3rd/bee.lua -Subproject f0ab1dd5a87852e49f42de6db9b372ad78eb580 +Subproject 196db37c961f739e3179512bb55fadedc3fa63d diff --git a/script/brave/brave.lua b/script/brave/brave.lua index 9ad7ebd1..5a15a6b2 100644 --- a/script/brave/brave.lua +++ b/script/brave/brave.lua @@ -1,7 +1,24 @@ -local thread = require 'bee.thread' +local channel = require 'bee.channel' +local select = require 'bee.select' -local taskPad = thread.channel('taskpad') -local waiter = thread.channel('waiter') +local function channel_init(chan) + local selector = select.create() + selector:event_add(chan:fd(), select.SELECT_READ) + return { selector, chan } +end + +local function channel_bpop(ctx) + local selector, chan = ctx[1], ctx[2] + for _ in selector:wait() do + local r = table.pack(chan:pop()) + if r[1] == true then + return table.unpack(r, 2) + end + end +end + +local taskPad = channel_init(channel.query('taskpad')) +local waiter = channel.query('waiter') ---@class pub_brave local m = {} @@ -42,11 +59,11 @@ end --- 开始找工作 function m.start(privatePad) - local reqPad = privatePad and thread.channel('req:' .. privatePad) or taskPad - local resPad = privatePad and thread.channel('res:' .. privatePad) or waiter + local reqPad = privatePad and channel_init(channel.query('req:' .. privatePad)) or taskPad + local resPad = privatePad and channel.query('res:' .. privatePad) or waiter m.push('mem', collectgarbage 'count') while true do - local name, id, params = reqPad:bpop() + local name, id, params = channel_bpop(reqPad) local ability = m.ability[name] -- TODO if not ability then diff --git a/script/pub/pub.lua b/script/pub/pub.lua index 5bbb381c..e8be65cd 100644 --- a/script/pub/pub.lua +++ b/script/pub/pub.lua @@ -1,13 +1,27 @@ local thread = require 'bee.thread' +local channel = require 'bee.channel' +local select = require 'bee.select' local utility = require 'utility' local await = require 'await' -thread.newchannel 'taskpad' -thread.newchannel 'waiter' +local function channel_init(chan) + local selector = select.create() + selector:event_add(chan:fd(), select.SELECT_READ) + return { selector, chan } +end + +local function channel_bpop(ctx) + local selector, chan = ctx[1], ctx[2] + for _ in selector:wait() do + local r = table.pack(chan:pop()) + if r[1] == true then + return table.unpack(r, 2) + end + end +end -local errLog = thread.channel 'errlog' -local taskPad = thread.channel 'taskpad' -local waiter = thread.channel 'waiter' +local taskPad = channel.create 'taskpad' +local waiter = channel_init(channel.create 'waiter') local type = type local counter = utility.counter() @@ -66,11 +80,9 @@ function m.recruitBraves(num, privatePad) } end if privatePad and not m.prvtPad[privatePad] then - thread.newchannel('req:' .. privatePad) - thread.newchannel('res:' .. privatePad) m.prvtPad[privatePad] = { - req = thread.channel('req:' .. privatePad), - res = thread.channel('res:' .. privatePad), + req = channel.create('req:' .. privatePad), + res = channel.create('res:' .. privatePad), } end end @@ -166,7 +178,7 @@ end --- 接收反馈 function m.recieve(block) if block then - local id, name, result = waiter:bpop() + local id, name, result = channel_bpop(waiter) if type(name) == 'string' then m.popReport(m.braves[id], name, result) else @@ -175,7 +187,7 @@ function m.recieve(block) else while true do local ok - if m.reciveFromPad(waiter) then + if m.reciveFromPad(waiter[2]) then ok = true end for _, pad in pairs(m.prvtPad) do @@ -194,7 +206,7 @@ end --- 检查伤亡情况 function m.checkDead() while true do - local suc, err = errLog:pop() + local suc, err = thread.errlog() if not suc then break end |