summaryrefslogtreecommitdiff
path: root/server-beta/src
diff options
context:
space:
mode:
author最萌小汐 <sumneko@hotmail.com>2019-11-08 14:59:03 +0800
committer最萌小汐 <sumneko@hotmail.com>2019-11-08 14:59:03 +0800
commit6b222a0a14135e6f39eb9e80f3c2bf879811ca2c (patch)
treeb9db4a53303fec89dbceab74b0f257f959fe7f5c /server-beta/src
parentd8c8c6ded148a8e130956b08010db3a0cda8585a (diff)
downloadlua-language-server-6b222a0a14135e6f39eb9e80f3c2bf879811ca2c.zip
协作模式改为队列
Diffstat (limited to 'server-beta/src')
-rw-r--r--server-beta/src/await.lua38
-rw-r--r--server-beta/src/provider/diagnostic.lua2
-rw-r--r--server-beta/src/pub/pub.lua8
-rw-r--r--server-beta/src/service/service.lua8
4 files changed, 44 insertions, 12 deletions
diff --git a/server-beta/src/await.lua b/server-beta/src/await.lua
index ee64e896..37cade79 100644
--- a/server-beta/src/await.lua
+++ b/server-beta/src/await.lua
@@ -5,6 +5,8 @@ local m = {}
m.type = 'await'
m.coTracker = setmetatable({}, { __mode = 'k' })
+m.delayQueue = {}
+m.delayQueueIndex = 1
--- 设置错误处理器
---@param errHandle function {comment = '当有错误发生时,会以错误堆栈为参数调用该函数'}
@@ -44,8 +46,8 @@ function m.sleep(time, ...)
end
--- 等待直到唤醒
----@param waker function
-function m.wait(waker, ...)
+---@param callback function
+function m.wait(callback, ...)
local co, main = coroutine.running()
if main then
if m.errorHandle then
@@ -53,10 +55,40 @@ function m.wait(waker, ...)
end
return
end
- waker(function (...)
+ callback(function (...)
return m.checkResult(co, coroutine.resume(co, ...))
end)
return coroutine.yield(...)
end
+--- 延迟
+function m.delay(...)
+ local co, main = coroutine.running()
+ if main then
+ if m.errorHandle then
+ m.errorHandle(debug.traceback('Cant wait in main thread'))
+ end
+ return
+ end
+ m.delayQueue[#m.delayQueue+1] = function (...)
+ return m.checkResult(co, coroutine.resume(co, ...))
+ end
+ return coroutine.yield(...)
+end
+
+--- 步进
+function m.step()
+ local waker = m.delayQueue[m.delayQueueIndex]
+ if waker then
+ m.delayQueueIndex = m.delayQueueIndex + 1
+ waker()
+ return true
+ else
+ for i = 1, #m.delayQueue do
+ m.delayQueue[i] = nil
+ end
+ return false
+ end
+end
+
return m
diff --git a/server-beta/src/provider/diagnostic.lua b/server-beta/src/provider/diagnostic.lua
index 2b4ae9b3..d7d4be1e 100644
--- a/server-beta/src/provider/diagnostic.lua
+++ b/server-beta/src/provider/diagnostic.lua
@@ -173,7 +173,7 @@ function m.refresh(uri)
for destUri in files.eachFile() do
if destUri ~= uri then
m.doDiagnostic(files.getOriginUri(destUri))
- await.sleep(0.001)
+ await.delay()
if myVersion ~= m.version then
return
end
diff --git a/server-beta/src/pub/pub.lua b/server-beta/src/pub/pub.lua
index 3e97fe9f..19800e95 100644
--- a/server-beta/src/pub/pub.lua
+++ b/server-beta/src/pub/pub.lua
@@ -226,11 +226,9 @@ function m.checkDead()
end
end
-function m.listen()
- timer.loop(0.001, function ()
- m.checkDead()
- m.recieve()
- end)
+function m.step()
+ m.checkDead()
+ m.recieve()
end
return m
diff --git a/server-beta/src/service/service.lua b/server-beta/src/service/service.lua
index 6ff82897..1d65a1ea 100644
--- a/server-beta/src/service/service.lua
+++ b/server-beta/src/service/service.lua
@@ -73,8 +73,11 @@ end
function m.startTimer()
while true do
- thread.sleep(0.001)
- timer.update()
+ pub.step()
+ if not await.step() then
+ thread.sleep(0.001)
+ timer.update()
+ end
end
end
@@ -82,7 +85,6 @@ function m.start()
await.setErrorHandle(log.error)
pub.recruitBraves(4)
proto.listen()
- pub.listen()
m.report()
require 'provider'