diff options
| author | ruki <[email protected]> | 2020-02-04 00:35:32 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-02-07 22:45:56 +0800 |
| commit | 486cc3bb146e19542991fd2ac3ac385f101bde1a (patch) | |
| tree | 1c455d39efd6489ceccdb6c1c9b902a7a60da8a4 /xmake/core/base | |
| parent | 81f94cfcc81beb8a0218506108813ceeb7eb9048 (diff) | |
add scheduler.co_yield
Diffstat (limited to 'xmake/core/base')
| -rw-r--r-- | xmake/core/base/scheduler.lua | 103 |
1 files changed, 72 insertions, 31 deletions
diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 1464ae2dc..1c6db570d 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -251,6 +251,13 @@ end -- start a new named coroutine task function scheduler:co_start_named(coname, cotask, ...) + + -- check coroutine task + if not cotask then + return nil, string.format("cannot start coroutine, invalid cotask(%s/%s)", coname and coname or "anonymous", cotask) + end + + -- start coroutine local co co = _coroutine.new(coname, coroutine.create(function(...) cotask(...) @@ -283,6 +290,71 @@ function scheduler:co_suspend(...) return coroutine.yield(...) end +-- yield the current coroutine +function scheduler:co_yield() + + -- get the running coroutine + local running = self:co_running() + if not running then + return false, "we must call co_yield() in coroutine with scheduler!" + end + + -- is stopped? + if not self._STARTED then + return false, "the scheduler is stopped!" + end + + -- register timeout task to timer without 0 ms (no delay) + self:_timer():post(function (cancel) + if running:is_suspended() then + self:_co_tasks_suspended():remove(running) + self:co_resume(running) + end + end, 0) + + -- save the suspended coroutine + self:_co_tasks_suspended():insert(running) + + -- wait + self:co_suspend() + return true +end + +-- sleep some times (ms) +function scheduler:co_sleep(ms) + + -- we need not do sleep + if ms == 0 then + return true + end + + -- get the running coroutine + local running = self:co_running() + if not running then + return false, "we must call sleep() in coroutine with scheduler!" + end + + -- is stopped? + if not self._STARTED then + return false, "the scheduler is stopped!" + end + + -- register timeout task to timer + self:_timer():post(function (cancel) + if running:is_suspended() then + self:_co_tasks_suspended():remove(running) + self:co_resume(running) + end + end, ms) + + -- save the suspended coroutine + self:_co_tasks_suspended():insert(running) + + -- wait + self:co_suspend() + return true +end + -- get the current running coroutine function scheduler:co_running() local running = coroutine.running() @@ -499,37 +571,6 @@ function scheduler:poller_cancel(obj) return true end --- sleep some times (ms) -function scheduler:sleep(ms) - - -- we need not do sleep - if ms == 0 then - return true - end - - -- get the running coroutine - local running = self:co_running() - if not running then - return false, "we must call sleep() in coroutine with scheduler!" - end - - -- is stopped? - if not self._STARTED then - return false, "the scheduler is stopped!" - end - - -- register timeout task to timer - self:_timer():post(function (cancel) - if running:is_suspended() then - self:co_resume(running) - end - end, ms) - - -- wait - self:co_suspend() - return true -end - -- stop the scheduler loop function scheduler:stop() -- mark scheduler status as stopped and spank the poller:wait() |
