diff options
| author | ruki <[email protected]> | 2019-11-28 22:43:03 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2019-11-28 10:18:18 +0800 |
| commit | 6e42cbc962f56c1e841f66cfda0e4c8f8cde787a (patch) | |
| tree | aa290f75a273a0dc603ef2de35778e790bba8ca5 | |
| parent | 5419b4eb3112fbe9b43249c62f9e319518caa71e (diff) | |
switch socket/wait to scheduler/wait
| -rw-r--r-- | xmake/core/base/scheduler.lua | 27 | ||||
| -rw-r--r-- | xmake/core/base/socket.lua | 19 |
2 files changed, 40 insertions, 6 deletions
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 426523a68..b0f50f963 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -58,10 +58,34 @@ function scheduler:run(func, ...) return true end +-- get the current running coroutine in scheduler +function scheduler:running() + return self._RUNNING and coroutine.running() or nil +end + +-- wait socket/pipe and process events +function scheduler:wait(object, events, timeout) + + -- ensure to run on coroutine with scheduler + if not self:running() then + return -1, "please wait events on coroutine with scheduler!" + end + + -- TODO +end + -- TODO -- run loop, schedule coroutine with socket/io and sub-processes function scheduler:runloop(opt) + -- ensure only one scheduler + if self._RUNNING then + return false, "there is already a running scheduler!" + end + + -- start scheduler + self._RUNNING = true + -- run loop local coroutines_ready = self:_coroutines_ready() while #coroutines_ready > 0 do @@ -80,6 +104,9 @@ function scheduler:runloop(opt) table.remove(coroutines_ready, 1) end end + + -- stop scheduler + self._RUNNING = false return true end diff --git a/xmake/core/base/socket.lua b/xmake/core/base/socket.lua index 4a2dfdcdc..1dbc9f7da 100644 --- a/xmake/core/base/socket.lua +++ b/xmake/core/base/socket.lua @@ -23,10 +23,11 @@ local socket = socket or {} local _instance = _instance or {} -- load modules -local io = require("base/io") -local bytes = require("base/bytes") -local table = require("base/table") -local string = require("base/string") +local io = require("base/io") +local bytes = require("base/bytes") +local table = require("base/table") +local string = require("base/string") +local scheduler = require("base/scheduler") -- the socket types socket.TCP = 1 @@ -472,8 +473,14 @@ function _instance:wait(events, timeout) return -1, errors end - -- wait it - local events, errors = io.socket_wait(self._SOCK, events, timeout or -1) + -- wait events + local events = -1 + local errors = nil + if scheduler.runing() then + events, errors = scheduler.wait(self._SOCK, events, timeout or -1) + else + events, errors = io.socket_wait(self._SOCK, events, timeout or -1) + end if events < 0 and errors then errors = string.format("%s: %s", self, errors) end |
