diff options
| -rw-r--r-- | xmake/core/base/scheduler.lua | 65 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/import/core/base/scheduler.lua | 12 |
2 files changed, 71 insertions, 6 deletions
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 9b2bacf41..1d46850bf 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -22,15 +22,72 @@ local scheduler = scheduler or {} -- load modules -local utils = require("base/utils") +local table = require("base/table") local option = require("base/option") local string = require("base/string") local coroutine = require("base/coroutine") +-- TODO +-- the socket loop coroutine +function scheduler:_co_loop_socket() + print("socket loop") +end + +-- TODO +-- the process loop coroutine +function scheduler:_co_loop_process() + print("process loop") +end + +-- get all ready coroutines +-- +-- ready: ready -> ready -> .. -> running -> .. -> ready -> ..-> +-- | | +-- ---------------------------<------------------------ +-- +function scheduler:_coroutines_ready() + local coroutines_ready = self._COROUTINES_READY + if not coroutines_ready then + coroutines_ready = {} + self._COROUTINES_READY = coroutines_ready + end + return coroutines_ready +end + +-- run new coroutine function, it will insert to the pending queue +function scheduler:run(func, ...) + local argv = table.pack(...) + local co = coroutine.create(function () return func(table.unpack(argv)) end) + if not co then + return false, "create coroutine failed!" + end + table.insert(self:_coroutines_ready(), co) + return true +end + +-- TODO -- run loop, schedule coroutine with socket/io and sub-processes -function scheduler:runloop(main) - -- TODO - return coroutine.resume(coroutine.create(main)) +function scheduler:runloop(opt) + + -- run loop + local coroutines_ready = self:_coroutines_ready() + while #coroutines_ready > 0 do + + -- get the first ready coroutine + local co_ready = coroutines_ready[1] + + -- switch to this coroutine + local ok, result_or_errors = coroutine.resume(co_ready) + if not ok then + return false, result_or_errors + end + + -- this coroutine has been finished? we remove it from the ready queue + if coroutine.status(co_ready) == "dead" then + table.remove(coroutines_ready, 1) + end + end + return true end -- return module: scheduler diff --git a/xmake/core/sandbox/modules/import/core/base/scheduler.lua b/xmake/core/sandbox/modules/import/core/base/scheduler.lua index 0b2c98c2d..eacedc074 100644 --- a/xmake/core/sandbox/modules/import/core/base/scheduler.lua +++ b/xmake/core/sandbox/modules/import/core/base/scheduler.lua @@ -25,9 +25,17 @@ local sandbox_core_base_scheduler = sandbox_core_base_scheduler or {} local scheduler = require("base/scheduler") local raise = require("sandbox/modules/raise") +-- run a new coroutine function +function sandbox_core_base_scheduler.run(func, ...) + local ok, errors = scheduler:run(func, ...) + if not ok then + raise(errors) + end +end + -- run loop -function sandbox_core_base_scheduler.runloop(main) - local ok, errors = scheduler:runloop(main) +function sandbox_core_base_scheduler.runloop(opt) + local ok, errors = scheduler:runloop(opt) if not ok then raise(errors) end |
