add multitask package
This commit is contained in:
parent
f09cf70391
commit
ee2b2fe10f
51
multitask/lib/multitask.lua
Normal file
51
multitask/lib/multitask.lua
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
local mt = {}
|
||||||
|
|
||||||
|
mt.newPool = function()
|
||||||
|
local pool = {}
|
||||||
|
pool.threads = {}
|
||||||
|
pool.threadcount = 0
|
||||||
|
|
||||||
|
pool.clear = function()
|
||||||
|
pool.threads = {}
|
||||||
|
end
|
||||||
|
|
||||||
|
pool.add = function(fn, options)
|
||||||
|
options = options or {}
|
||||||
|
options.co = coroutine.create(fn)
|
||||||
|
pool.threads[options.co] = options
|
||||||
|
pool.threadcount = pool.threadcount + 1
|
||||||
|
os.queueEvent("pool_fn_added",options,pool)
|
||||||
|
end
|
||||||
|
|
||||||
|
pool.run = function(terminable)
|
||||||
|
terminable = terminable or true -- if false, terminate events will be echoed to coroutines instead of
|
||||||
|
-- terminating this pool
|
||||||
|
while true do
|
||||||
|
local event = {coroutine.yield()}
|
||||||
|
if event[1] == "terminate" and terminable then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
for thread, options in pairs(pool.threads) do
|
||||||
|
if coroutine.status(thread) ~= "dead" then
|
||||||
|
if event[1] == options.filter or options.filter == nil then
|
||||||
|
local x,e,y = pcall(coroutine.resume,thread,unpack(event))
|
||||||
|
options.filter = y
|
||||||
|
if not x then
|
||||||
|
os.queueEvent("pool_fn_crash",e,options,pool)
|
||||||
|
pool.threadcount = pool.threadcount - 1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
else
|
||||||
|
os.queueEvent("pool_fn_end",options,pool)
|
||||||
|
pool.threadcount = pool.threadcount - 1
|
||||||
|
pool.threads[thread] = nil
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if pool.threadcount == 0 then return end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
return pool
|
||||||
|
end
|
||||||
|
|
||||||
|
return mt
|
10
multitask/pkgmeta.ltn
Normal file
10
multitask/pkgmeta.ltn
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
["version"] = "0.1.0",
|
||||||
|
["dependencies"] = {},
|
||||||
|
["description"] = "Adds convinientish multitasking functionality.",
|
||||||
|
["files"] = {
|
||||||
|
["lib"] = {
|
||||||
|
"multitask.lua"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user