diff options
| author | ruki <[email protected]> | 2017-05-31 14:54:09 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2017-05-31 14:54:09 +0800 |
| commit | d830d323ae5b3bd46a8c66a6276f1696576cb9f9 (patch) | |
| tree | b5ebe1544fa075d156f8740bd341c2d7fcf090c1 | |
| parent | eac4b74e3c8dfce984f6f7851e3c83cb98cc486c (diff) | |
improve process.runjobs and support timer
| -rw-r--r-- | xmake/core/base/process.lua | 37 | ||||
| -rw-r--r-- | xmake/core/sandbox/modules/process.lua | 4 |
2 files changed, 28 insertions, 13 deletions
diff --git a/xmake/core/base/process.lua b/xmake/core/base/process.lua index 5f2db9818..8cf96b2ea 100644 --- a/xmake/core/base/process.lua +++ b/xmake/core/base/process.lua @@ -82,15 +82,20 @@ function process.asyncrun(task, waitchars) end -- run jobs with processes -function process.runjobs(jobfunc, total, comax, timeout) +function process.runjobs(jobfunc, total, comax, timeout, timer) -- init max coroutine count comax = comax or total + -- init timeout + timeout = timeout or -1 + -- make objects - local index = 1 - local tasks = {} - local procs = {} + local index = 1 + local tasks = {} + local procs = {} + local indices = {} + local time = os.mclock() repeat -- wait processes @@ -99,11 +104,17 @@ function process.runjobs(jobfunc, total, comax, timeout) if procs_count > 0 then -- wait them - local count, procinfos = process.waitlist(procs, utils.ifelse(procs_count < comax and index <= total, 0, -1)) + local count, procinfos = process.waitlist(procs, utils.ifelse(procs_count < comax and index <= total, 0, timeout)) if count < 0 then return false, string.format("wait processes(%d) failed(%d)", #procs, count) end + -- timer is triggered? call timer + if timer and os.mclock() - time > timeout then + timer(indices) + time = os.mclock() + end + -- wait ok for _, procinfo in ipairs(procinfos) do @@ -141,16 +152,19 @@ function process.runjobs(jobfunc, total, comax, timeout) end -- update the pending tasks and procs - local tasks_pending = {} - local procs_pending = {} + local tasks_pending = {} + local procs_pending = {} + local indices_pending = {} for taskid, job_task in ipairs(tasks) do if not tasks_finished[taskid] then - table.insert(tasks_pending, job_task) - table.insert(procs_pending, procs[taskid]) + table.insert(tasks_pending, job_task) + table.insert(procs_pending, procs[taskid]) + table.insert(indices_pending, indices[taskid]) end end - tasks = tasks_pending - procs = procs_pending + tasks = tasks_pending + procs = procs_pending + indices = indices_pending -- produce tasks while #tasks < comax and index <= total do @@ -173,6 +187,7 @@ function process.runjobs(jobfunc, total, comax, timeout) -- put task and proc to the pendings tasks table.insert(tasks, job_task) table.insert(procs, job_proc_or_errors) + table.insert(indices, index) end -- next index diff --git a/xmake/core/sandbox/modules/process.lua b/xmake/core/sandbox/modules/process.lua index e69a05e21..f8f2a2a7e 100644 --- a/xmake/core/sandbox/modules/process.lua +++ b/xmake/core/sandbox/modules/process.lua @@ -139,10 +139,10 @@ function sandbox_process.asyncrun(task, waitchars) end -- run jobs with processes -function sandbox_process.runjobs(jobfunc, total, comax) +function sandbox_process.runjobs(jobfunc, total, comax, timeout, timer) -- run them - local ok, errors = process.runjobs(jobfunc, total, comax) + local ok, errors = process.runjobs(jobfunc, total, comax, timeout, timer) if not ok then raise(errors) end |
