From b88e761a6a386bd786ee3642b21ec032243e91da Mon Sep 17 00:00:00 2001 From: Ale32bit Date: Wed, 12 Oct 2016 20:17:25 +0200 Subject: [PATCH] Create task.lua --- src/apis/task.lua | 60 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/apis/task.lua diff --git a/src/apis/task.lua b/src/apis/task.lua new file mode 100644 index 0000000..f1d22f8 --- /dev/null +++ b/src/apis/task.lua @@ -0,0 +1,60 @@ +local processList = {} +local exit = false +local autoExit = true +local killProcesses = {} +local pid = 0 + +function setAutoExit(val) + autoExit = val +end + +function run() + local firstRun = true + while (function() + local rtn = false + for i, co in pairs(processList) do + if coroutine.status(co) ~= "dead" then + rtn = true + end + end + return rtn + end)() or not autoExit do + if exit then + break + end + local event = {} + if not firstRun then + event = {os.pullEventRaw()} + end + firstRun = false + for k, co in pairs(processList) do + if coroutine.status(co) ~= "dead" then + coroutine.resume(co,unpack(event)) + end + end + for k, code in pairs(killProcesses) do + processList[k] = nil + end + end +end + +function add(func) + local co = coroutine.create(func) + pid = pid + 1 + processList[pid] = co + return tonumber(pid) +end + +function kill(pid) + killProcesses[pid] = true +end + +function getList() + p = 0 + for k,v in pairs(processList) do + if v then + p = p + 1 + end + end + return processList, p +end