diff options
| author | ruki <[email protected]> | 2020-02-05 00:34:53 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2020-02-07 22:45:56 +0800 |
| commit | 94d3eb649e489a597a2f5d966d2249a9e64d87e3 (patch) | |
| tree | c5bb5c3286c64a5ca491b2aacc0c09a4c9cc84ba | |
| parent | a615d353a701e0d23e322060e5dd8e364d38dfee (diff) | |
impl private.async.runjobs
| -rw-r--r-- | tests/modules/scheduler/runjobs.lua | 8 | ||||
| -rw-r--r-- | tests/modules/scheduler/test.lua | 15 | ||||
| -rw-r--r-- | xmake/core/base/os.lua | 24 | ||||
| -rw-r--r-- | xmake/core/base/scheduler.lua | 10 | ||||
| -rw-r--r-- | xmake/modules/private/async/runjobs.lua | 15 |
5 files changed, 41 insertions, 31 deletions
diff --git a/tests/modules/scheduler/runjobs.lua b/tests/modules/scheduler/runjobs.lua index 505573190..36f72b47f 100644 --- a/tests/modules/scheduler/runjobs.lua +++ b/tests/modules/scheduler/runjobs.lua @@ -1,9 +1,15 @@ +import("core.base.scheduler") import("private.async.runjobs") function _jobfunc(index) + print("%s: run job (%d)", scheduler.co_running(), index) + local dt = os.mclock() + os.sleep(1000) + dt = os.mclock() - dt + print("%s: run job (%d) end, dt: %d ms", scheduler.co_running(), index, dt) end function main() - runjobs(_jobfunc, 100, 4) + runjobs("test", _jobfunc, 100, 6) end diff --git a/tests/modules/scheduler/test.lua b/tests/modules/scheduler/test.lua index de62e47ed..853957567 100644 --- a/tests/modules/scheduler/test.lua +++ b/tests/modules/scheduler/test.lua @@ -1,6 +1,6 @@ import("core.base.scheduler") -function test_runjobs(t) +function test_group(t) local count = 0 local task = function (a) @@ -46,3 +46,16 @@ function test_yield(t) scheduler.co_group_wait("test") t:are_equal(count, 10) end + +function test_runjobs(t) + import("private.async.runjobs") + + local total = 100 + local comax = 6 + local count = 0 + runjobs("test", function (index) + t:require(index >= 1 and index <= total) + count = count + 1 + end, total, comax) + t:are_equal(count, total) +end diff --git a/xmake/core/base/os.lua b/xmake/core/base/os.lua index 46a0ad1a1..35099cc9e 100644 --- a/xmake/core/base/os.lua +++ b/xmake/core/base/os.lua @@ -659,29 +659,7 @@ function os.execv(program, argv, opt) if proc ~= nil then -- wait process - local waitok = -1 - local status = -1 - if coroutine.running() then - - -- save the current directory - local curdir = os.curdir() - - -- wait it - repeat - -- poll it - waitok, status = proc:wait(0) - if waitok == 0 then - waitok, status = coroutine.yield(proc) - end - until waitok ~= 0 - - -- resume the current directory - os.cd(curdir) - else - waitok, status = proc:wait(-1) - end - - -- get status + local waitok, status = proc:wait(-1) if waitok > 0 then ok = status end diff --git a/xmake/core/base/scheduler.lua b/xmake/core/base/scheduler.lua index 6c3b511b1..b1091caf6 100644 --- a/xmake/core/base/scheduler.lua +++ b/xmake/core/base/scheduler.lua @@ -89,7 +89,7 @@ end -- tostring(coroutine) function _coroutine:__tostring() - return string.format("<co: %s/%s>", self:thread(), self:name()) + return string.format("<co: %s/%s>", self:name(), self:status()) end -- gc(coroutine) @@ -265,13 +265,13 @@ function scheduler:_co_groups_resume() -- resume the waiting coroutine of this group local co_waiting = self._CO_GROUPS_WAITING[name] if co_waiting and co_waiting:is_suspended() then + resumed_count = resumed_count + 1 + self._CO_GROUPS_WAITING[name] = nil + self:_co_tasks_suspended():remove(co_waiting) local ok, errors = self:co_resume(co_waiting) if not ok then return -1, errors end - resumed_count = resumed_count + 1 - self._CO_GROUPS_WAITING[name] = nil - self:_co_tasks_suspended():remove(co_waiting) end end end @@ -703,7 +703,7 @@ function scheduler:runloop() if resumed_count < 0 then ok = false errors = resumed_errors - break; + break elseif resumed_count == 0 then -- get the next timeout diff --git a/xmake/modules/private/async/runjobs.lua b/xmake/modules/private/async/runjobs.lua index 07680003c..5205ef3ed 100644 --- a/xmake/modules/private/async/runjobs.lua +++ b/xmake/modules/private/async/runjobs.lua @@ -22,6 +22,19 @@ import("core.base.scheduler") -- main entry -function main(jobfunc, total, comax) +function main(name, jobfunc, total, comax) + local index = 0 + local group_name = name + comax = comax or total + while index < total do + scheduler.co_group_begin(group_name, function () + local max = math.min(index + comax, total) + while index < max do + index = index + 1 + scheduler.co_start_named(name .. '/' .. tostring(index), jobfunc, index) + end + end) + scheduler.co_group_wait(group_name) + end end |
