mirror of
https://github.com/rayaman/multi.git
synced 2026-09-05 07:27:35 -04:00
Updated to 1.8.0
Added: SystemThreadedBenchmark SystemThreadedQueue Fixed a bunch of bugs in the intergrations and regular multi objects Fixed Error management in threads All errors trigger the multi.OnError connection Module creation support improved added more examples added Type to threaded objects
This commit is contained in:
+5
-2
@@ -45,7 +45,7 @@ function print(...)
|
||||
end
|
||||
end
|
||||
multi = {}
|
||||
multi.Version={1,7,6}
|
||||
multi.Version={1,8,0}
|
||||
multi.stage='stable'
|
||||
multi.__index = multi
|
||||
multi.Mainloop={}
|
||||
@@ -248,6 +248,9 @@ function multi:getPlatform()
|
||||
return "lanes"
|
||||
end
|
||||
end
|
||||
function multi:canSystemThread()
|
||||
return false
|
||||
end
|
||||
--Processor
|
||||
function multi:getError()
|
||||
if self.error then
|
||||
@@ -417,7 +420,7 @@ function multi:protect()
|
||||
local status, err=pcall(Loop[_D].Act,Loop[_D])
|
||||
if err and not(Loop[_D].error) then
|
||||
Loop[_D].error=err
|
||||
self.OnError:Fire(err,Loop[_D])
|
||||
self.OnError:Fire(Loop[_D],err)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -32,6 +32,9 @@ end
|
||||
lanes=require("lanes").configure()
|
||||
package.path="lua/?/init.lua;lua/?.lua;"..package.path
|
||||
require("multi.all") -- get it all and have it on all lanes
|
||||
function multi:canSystemThread()
|
||||
return true
|
||||
end
|
||||
local multi=multi
|
||||
-- Step 2 set up the linda objects
|
||||
local __GlobalLinda = lanes.linda() -- handles global stuff
|
||||
@@ -103,9 +106,10 @@ function multi:newSystemThread(name,func)
|
||||
local c={}
|
||||
local __self=c
|
||||
c.name=name
|
||||
c.Type="sthread"
|
||||
c.thread=lanes.gen("*", func)()
|
||||
function c:kill()
|
||||
self.status:Destroy()
|
||||
--self.status:Destroy()
|
||||
self.thread:cancel()
|
||||
print("Thread: '"..self.name.."' has been stopped!")
|
||||
end
|
||||
@@ -114,7 +118,8 @@ function multi:newSystemThread(name,func)
|
||||
c.status:OnUpdate(function(self)
|
||||
local v,err,t=self.link.thread:join(.001)
|
||||
if err then
|
||||
print("Error in thread: '"..self.link.name.."' <"..err..">")
|
||||
multi.OnError:Fire(self.link,err)
|
||||
print("Error in systemThread: '"..self.link.name.."' <"..err..">")
|
||||
self:Destroy()
|
||||
end
|
||||
end)
|
||||
@@ -124,7 +129,8 @@ print("Intergrated Lanes!")
|
||||
multi.intergration={} -- for module creators
|
||||
multi.intergration.GLOBAL=GLOBAL
|
||||
multi.intergration.THREAD=THREAD
|
||||
multi.intergration.lanes={} -- for module creators
|
||||
multi.intergration.lanes={}
|
||||
multi.intergration.lanes.GLOBAL=GLOBAL -- for module creators
|
||||
multi.intergration.lanes.THREAD=THREAD -- for module creators
|
||||
require("multi.intergration.shared.shared")
|
||||
return {init=function() return GLOBAL,THREAD end}
|
||||
|
||||
@@ -1,7 +1,11 @@
|
||||
require("multi.compat.love2d")
|
||||
function multi:canSystemThread()
|
||||
return true
|
||||
end
|
||||
multi.intergration={}
|
||||
multi.intergration.love2d={}
|
||||
multi.intergration.love2d.ThreadBase=[[
|
||||
__THREADNAME__=({...})[1]
|
||||
require("love.filesystem")
|
||||
require("love.system")
|
||||
require("love.timer")
|
||||
@@ -43,6 +47,8 @@ function ToStr(val, name, skipnewlines, depth)
|
||||
tmp = tmp .. string.format("%q", val)
|
||||
elseif type(val) == "boolean" then
|
||||
tmp = tmp .. (val and "true" or "false")
|
||||
elseif type(val) == "function" then
|
||||
tmp = tmp .. "loadDump([===["..dump(val).."]===])"
|
||||
else
|
||||
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
|
||||
end
|
||||
@@ -57,6 +63,8 @@ function resolveType(tp,d)
|
||||
return loadDump(d)
|
||||
elseif tp=="table" then
|
||||
return loadstring("return "..d)()
|
||||
elseif tp=="nil" then
|
||||
return nil
|
||||
else
|
||||
return d
|
||||
end
|
||||
@@ -67,7 +75,7 @@ function resolveData(v)
|
||||
data=ToStr(v)
|
||||
elseif type(v)=="function" then
|
||||
data=dump(v)
|
||||
elseif type(v)=="string" or type(v)=="number" or type(v)=="bool" then
|
||||
elseif type(v)=="string" or type(v)=="number" or type(v)=="bool" or type(v)=="nil" then
|
||||
data=tostring(v)
|
||||
end
|
||||
return data
|
||||
@@ -77,7 +85,7 @@ local function randomString(n)
|
||||
local c=os.clock()
|
||||
local a=0
|
||||
while os.clock()<c+.1 do
|
||||
a=a+1
|
||||
a=a+1 -- each cpu has a different load... Doing this allows up to make unique seeds for the random string
|
||||
end
|
||||
math.randomseed(a)
|
||||
local str = ''
|
||||
@@ -119,6 +127,9 @@ function sThread.waitFor(name)
|
||||
while data do
|
||||
if type(data)=="string" then
|
||||
local cmd,tp,name,d=data:match("(%S-) (%S-) (%S-) (.+)")
|
||||
if name=="__DIEPLZ"..__THREADNAME__.."__" then
|
||||
error("Thread: "..__THREADNAME__.." has been stopped!")
|
||||
end
|
||||
if cmd=="SYNC" then
|
||||
__proxy__[name]=resolveType(tp,d)
|
||||
end
|
||||
@@ -143,6 +154,9 @@ function sThread.hold(n)
|
||||
while data do
|
||||
if type(data)=="string" then
|
||||
local cmd,tp,name,d=data:match("(%S-) (%S-) (%S-) (.+)")
|
||||
if name=="__DIEPLZ"..__THREADNAME__.."__" then
|
||||
error("Thread: "..__THREADNAME__.." has been stopped!")
|
||||
end
|
||||
if cmd=="SYNC" then
|
||||
__proxy__[name]=resolveType(tp,d)
|
||||
end
|
||||
@@ -160,6 +174,9 @@ updater:OnUpdate(function(self)
|
||||
while data do
|
||||
if type(data)=="string" then
|
||||
local cmd,tp,name,d=data:match("(%S-) (%S-) (%S-) (.+)")
|
||||
if name=="__DIEPLZ"..__THREADNAME__.."__" then
|
||||
error("Thread: "..__THREADNAME__.." has been stopped!")
|
||||
end
|
||||
if cmd=="SYNC" then
|
||||
__proxy__[name]=resolveType(tp,d)
|
||||
end
|
||||
@@ -214,6 +231,8 @@ function ToStr(val, name, skipnewlines, depth)
|
||||
tmp = tmp .. string.format("%q", val)
|
||||
elseif type(val) == "boolean" then
|
||||
tmp = tmp .. (val and "true" or "false")
|
||||
elseif type(val) == "function" then
|
||||
tmp = tmp .. "loadDump([===["..dump(val).."]===])"
|
||||
else
|
||||
tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\""
|
||||
end
|
||||
@@ -228,6 +247,8 @@ function resolveType(tp,d)
|
||||
return loadDump(d)
|
||||
elseif tp=="table" then
|
||||
return loadstring("return "..d)()
|
||||
elseif tp=="nil" then
|
||||
return nil
|
||||
else
|
||||
return d
|
||||
end
|
||||
@@ -238,7 +259,7 @@ function resolveData(v)
|
||||
data=ToStr(v)
|
||||
elseif type(v)=="function" then
|
||||
data=dump(v)
|
||||
elseif type(v)=="string" or type(v)=="number" or type(v)=="bool" then
|
||||
elseif type(v)=="string" or type(v)=="number" or type(v)=="bool" or type(v)=="nil" then
|
||||
data=tostring(v)
|
||||
end
|
||||
return data
|
||||
@@ -257,15 +278,28 @@ function dump(func)
|
||||
end
|
||||
return table.concat(code)
|
||||
end
|
||||
local function randomString(n)
|
||||
local str = ''
|
||||
local strings = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','7','8','9','0','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'}
|
||||
for i=1,n do
|
||||
str = str..''..strings[math.random(1,#strings)]
|
||||
end
|
||||
return str
|
||||
end
|
||||
function multi:newSystemThread(name,func) -- the main method
|
||||
local c={}
|
||||
c.name=name
|
||||
c.ID=c.name.."<ID|"..randomString(8)..">"
|
||||
c.thread=love.thread.newThread(multi.intergration.love2d.ThreadBase:gsub("INSERT_USER_CODE",dump(func)))
|
||||
c.thread:start()
|
||||
c.thread:start(c.ID)
|
||||
function c:kill()
|
||||
multi.intergration.GLOBAL["__DIEPLZ"..self.ID.."__"]="__DIEPLZ"..self.ID.."__"
|
||||
end
|
||||
return c
|
||||
end
|
||||
function love.threaderror( thread, errorstr )
|
||||
print("Error in "..tostring(thread)..": "..errorstr)
|
||||
multi.OnError:Fire(thread,errorstr)
|
||||
print("Error in systemThread: "..tostring(thread)..": "..errorstr)
|
||||
end
|
||||
local THREAD={}
|
||||
function THREAD.set(name,val)
|
||||
@@ -327,6 +361,7 @@ updater:OnUpdate(function(self)
|
||||
data=multi.intergration.love2d.mainChannel:pop()
|
||||
end
|
||||
end)
|
||||
require("multi.intergration.shared.shared")
|
||||
print("Intergrated Love2d!")
|
||||
return {
|
||||
init=function(t)
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
--[[
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2017 Ryan Ward
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
]]
|
||||
function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a channel on both ends
|
||||
local c={} -- where we will store our object
|
||||
c.name=name -- set the name this is important for the love2d side
|
||||
if love then -- check love
|
||||
if love.thread then -- make sure we can use the threading module
|
||||
function c:init() -- create an init function so we can mimic on bith love2d and lanes
|
||||
self.chan=love.thread.getChannel(self.name) -- create channel by the name self.name
|
||||
function self:push(v) -- push to the channel
|
||||
self.chan:push(v)
|
||||
end
|
||||
function self:pop() -- pop from the channel
|
||||
return self.chan:pop()
|
||||
end
|
||||
GLOBAL[self.name]=self -- send the object to the thread through the global interface
|
||||
return self -- return the object
|
||||
end
|
||||
return c
|
||||
else
|
||||
error("Make sure you required the love.thread module!") -- tell the user if he/she didn't require said module
|
||||
end
|
||||
else
|
||||
c.linda=lanes.linda() -- lanes is a bit eaiser, create the linda on the main thread
|
||||
function c:push(v) -- push to the queue
|
||||
self.linda:send("Q",v)
|
||||
end
|
||||
function c:pop() -- pop the queue
|
||||
return ({self.linda:receive(0,"Q")})[2]
|
||||
end
|
||||
function c:init() -- mimic the feature that love2d requires, so code can be consistent
|
||||
return self
|
||||
end
|
||||
multi.intergration.GLOBAL[name]=c -- send the object to the thread through the global interface
|
||||
end
|
||||
return c
|
||||
end
|
||||
function multi:systemThreadedBenchmark(n,p)
|
||||
n=n or 1
|
||||
local cores=multi.intergration.THREAD.getCores()
|
||||
local queue=multi:newSystemThreadedQueue("QUEUE")
|
||||
multi.intergration.GLOBAL["__SYSTEMBENCHMARK__"]=n
|
||||
local sThread=multi.intergration.THREAD
|
||||
local GLOBAL=multi.intergration.GLOBAL
|
||||
for i=1,cores do
|
||||
multi:newSystemThread("STHREAD_BENCH",function()
|
||||
require("multi")
|
||||
if multi:getPlatform()=="love2d" then
|
||||
GLOBAL=_G.GLOBAL
|
||||
sThread=_G.sThread
|
||||
end -- we cannot have upvalues... in love2d globals not locals must be used
|
||||
queue=sThread.waitFor("QUEUE"):init() -- always wait for when looking for a variable at the start of the thread!
|
||||
multi:benchMark(sThread.waitFor("__SYSTEMBENCHMARK__")):OnBench(function(self,count)
|
||||
queue:push(count)
|
||||
multi:Stop()
|
||||
end)
|
||||
multi:mainloop()
|
||||
end)
|
||||
end
|
||||
local c={}
|
||||
c.tt=function() end
|
||||
c.p=p
|
||||
function c:OnBench(func)
|
||||
self.tt=func
|
||||
end
|
||||
multi:newThread("THREAD_BENCH",function()
|
||||
thread.sleep(n+.1)
|
||||
GLOBAL["QUEUE"]=nil -- time to clean up
|
||||
local num=0
|
||||
data=queue:pop()
|
||||
while data do
|
||||
num=num+data
|
||||
data=queue:pop()
|
||||
end
|
||||
if p then
|
||||
print(tostring(p)..num)
|
||||
end
|
||||
c.tt(c,num)
|
||||
end)
|
||||
return c
|
||||
end
|
||||
+8
-3
@@ -30,13 +30,13 @@ else
|
||||
thread.__CORES=tonumber(io.popen("nproc --all"):read("*n"))
|
||||
end
|
||||
function thread.sleep(n)
|
||||
coroutine.yield({"_sleep_",n})
|
||||
coroutine.yield({"_sleep_",n or 0})
|
||||
end
|
||||
function thread.hold(n)
|
||||
coroutine.yield({"_hold_",n})
|
||||
coroutine.yield({"_hold_",n or function() return true end})
|
||||
end
|
||||
function thread.skip(n)
|
||||
coroutine.yield({"_skip_",n})
|
||||
coroutine.yield({"_skip_",n or 0})
|
||||
end
|
||||
function thread.kill()
|
||||
coroutine.yield({"_kill_",":)"})
|
||||
@@ -79,6 +79,7 @@ function multi:newThread(name,func)
|
||||
c.Name=name
|
||||
c.thread=coroutine.create(func)
|
||||
c.sleep=1
|
||||
c.Type="thread"
|
||||
c.firstRunDone=false
|
||||
c.timer=multi.scheduler:newTimer()
|
||||
c.ref.Globals=self:linkDomain("Globals")
|
||||
@@ -141,6 +142,10 @@ multi.scheduler:OnUpdate(function(self)
|
||||
else
|
||||
_,ret=coroutine.resume(self.Threads[i].thread,self.Globals)
|
||||
end
|
||||
if _==false then
|
||||
self.Parent.OnError:Fire(self.Threads[i],ret)
|
||||
print("Error in thread: <"..self.Threads[i].Name.."> "..ret)
|
||||
end
|
||||
if ret==true or ret==false then
|
||||
print("Thread Ended!!!")
|
||||
ret={}
|
||||
|
||||
Reference in New Issue
Block a user