mirror of
https://github.com/rayaman/multi.git
synced 2026-09-05 07:27:35 -04:00
Working on 16.0.0 (#53)
* Fixed spelling, started ideaing for 16.0.0 * Updated files * Updated readme * Updated version * Concat conns now properly transfer events * Testing types * Connections can be % with functions * Updated connections * Fixed issue with double thread activations (Looking for another solution) * Working on issue with love threaded functions not waiting when in a thread * Working on issue where threads created in threads don't work * Fixed broken threads for love * Fixed some issues with threads * removed test * Updated changes.md * Plan on testing parity between the threading modules * Writing tests for system threading * Added test cases for threading, fixed issues. Todo test love2d * Fixed love2d to succeed with tests * All tests working * Updated files for testing * Modified tests to make it more seamless * removed extra __cores in lanes/pseudo * Working on new priority scheme * Working on priority management * Working on custom prioritySchemes * Fixed issues with missing code * Threaded processors * THREAD.exposeENV(), thread:newProcessor() * Typo in changes.md * Fixed typo in pseudoManager * fixing * Trying to fix exposeENV with pseudoThreading * Changes to threads * updated changes.md * Working on systemthreadedprocess, and experimental newProxy for threading * newProxy and STP work * newProxy implemented * Proxies work with connections now :D * Added tstep to STP, updated changes.md * thread.hold(proxy.conn) * Clean up connection events when holding, working on scheduling tasks/threads to system threaded processors * Getting loads of processors implemented * Finished getLoad(type) * Fixed some bugs * Added an easy way to share a table, found some limitations with lanes threading. * THREAD_NAME set for main thread, connections break the rules for proxies * Testing * Really close to portable proxies, currently extreamly unstable! * Debugging what is going on... * Fixed critical issue with coroutine based threads * Removed extra bloat, proxies are portable now! * Started work on the debugManager * Testing actions, fixing bugs with lanes * Testing... * fixing actions * typo fixed * Throw an error when things break * fixing stuff * Fixed issue with errors not going through * Removed system threaded connections, soon to be replaced by proxies * Testing love2d tests * Test love2d * Use later love-build * Use ubuntu for build * Fixed path * Use appimage * use sudo * No window for love2d * Fixed love2d tests * Testing love2d * Use workspace * Moved other tests while testing * actually pull the repo * packagepath set * Fixed pull * Update multi * Removed link * Edited symlink * Added timeout to build * Rewriting loveManager, too much outdated code * Still implementing new love2d threading code * Rewriting love2d threading binding * Working on adding a Hold method to all objects. Will document how they all work when done. * jobqueues having isues with stp * new pack/unpack for tables, current issue is things being turned into strings * Fixed packing of values into threads, need to fix system proxies and system threaded processors * testing... * Not hard crashing when error is encountered * Should now push non 0 exit codes * Push error when an error happens * Closer to getting things working... * Working on new type system, planning out debugmanager * Fixed error code issue * Test for 5.1 * Planning out debugManager * Some work on the debug manager, proxies working on lanes, todo get pseudo manager and love2d working * Working on getting pseudoThreading tests to work * Added function / connection * Added boost method * Document new features to conns, todo fix newTask * Fixed newTask() * Updated changes.md and fixed some bugs * Added thread.defer(func) * Fixed tests on lanes and pseudo threading, todo fix love2d threading * Fixed paths * Working on paths * Testing paths * Add test for rockspec * Fixed issues * Fixed typo * Added test for defer * Threading working in love2d * Fixed, conf * lanes uses a threaded function like waitfor function * Cleaned up changes.md * added priorityManager to rockspec
This commit is contained in:
+165
-187
@@ -25,208 +25,96 @@ require("love.timer")
|
||||
require("love.system")
|
||||
require("love.data")
|
||||
require("love.thread")
|
||||
local socket = require("socket")
|
||||
local multi, thread = require("multi").init()
|
||||
local threads = {}
|
||||
local multi, thread = require("multi"):init()
|
||||
|
||||
function threads.loadDump(d)
|
||||
return loadstring(d:getString())
|
||||
end
|
||||
|
||||
function threads.dump(func)
|
||||
return love.data.newByteData(string.dump(func))
|
||||
end
|
||||
|
||||
local fRef = {"func",nil}
|
||||
local function manage(channel, value)
|
||||
channel:clear()
|
||||
if type(value) == "function" then
|
||||
fRef[2] = THREAD.dump(value)
|
||||
channel:push(fRef)
|
||||
return
|
||||
else
|
||||
channel:push(value)
|
||||
-- Checks if the given value is a LOVE2D object (i.e. has metatable with __index field) and if that __index field contains functions typical of LOVE2D objects
|
||||
function isLoveObject(value)
|
||||
-- Check if the value has metatable
|
||||
if type(value) == "userdata" and getmetatable(value) then
|
||||
-- Check if the metatable has the __index field
|
||||
local index = getmetatable(value).__index
|
||||
if type(index) == "table" then
|
||||
-- Check if the metatable's __index table contains functions typical of LOVE2D objects
|
||||
if index.draw or index.update or index.getWidth or index.getHeight or index.getString or index.getPointer then
|
||||
return true
|
||||
end
|
||||
end
|
||||
end
|
||||
return false
|
||||
end
|
||||
|
||||
local function RandomVariable(length)
|
||||
local res = {}
|
||||
math.randomseed(socket.gettime()*10000)
|
||||
for i = 1, length do
|
||||
res[#res+1] = string.char(math.random(97, 122))
|
||||
end
|
||||
return table.concat(res)
|
||||
end
|
||||
|
||||
local GNAME = "__GLOBAL_"
|
||||
local proxy = {}
|
||||
function threads.set(name,val)
|
||||
if not proxy[name] then proxy[name] = love.thread.getChannel(GNAME..name) end
|
||||
proxy[name]:performAtomic(manage, val)
|
||||
end
|
||||
|
||||
function threads.get(name)
|
||||
if not proxy[name] then proxy[name] = love.thread.getChannel(GNAME..name) end
|
||||
local dat = proxy[name]:peek()
|
||||
if type(dat)=="table" and dat[1]=="func" then
|
||||
return THREAD.loadDump(dat[2])
|
||||
else
|
||||
return dat
|
||||
-- Converts any function values in a table to a string with the value "\1\2:func:<function_string>" where <function_string> is the Lua stringified version of the function
|
||||
function tableToFunctionString(t)
|
||||
if type(t) == "nil" then return "\1\2:nil:" end
|
||||
if type(t) == "function" then return "\1\2:func:"..string.dump(t) end
|
||||
if type(t) ~= "table" then return t end
|
||||
local newtable = {}
|
||||
for k, v in pairs(t) do
|
||||
if type(v) == "function" then
|
||||
newtable[k] = "\1\2:func:"..string.dump(v)
|
||||
elseif type(v) == "table" then
|
||||
newtable[k] = tableToFunctionString(v)
|
||||
elseif isLoveObject(v) then
|
||||
newtable[k] = v
|
||||
elseif type(v) == "userdata" then
|
||||
newtable[k] = tostring(v)
|
||||
else
|
||||
newtable[k] = v
|
||||
end
|
||||
end
|
||||
return newtable
|
||||
end
|
||||
|
||||
function threads.waitFor(name)
|
||||
if thread.isThread() then
|
||||
return thread.hold(function()
|
||||
return threads.get(name)
|
||||
end)
|
||||
-- Converts strings with the value "\1\2:func:<function_string>" back to functions
|
||||
function functionStringToTable(t)
|
||||
if type(t) == "string" and t:sub(1, 8) == "\1\2:func:" then return loadstring(t:sub(9, -1)) end
|
||||
if type(t) == "string" and t:sub(1, 7) == "\1\2:nil:" then return nil end
|
||||
if type(t) ~= "table" then return t end
|
||||
for k, v in pairs(t) do
|
||||
if type(v) == "string" then
|
||||
if v:sub(1, 8) == "\1\2:func:" then
|
||||
t[k] = loadstring(v:sub(9, -1))
|
||||
else
|
||||
t[k] = v
|
||||
end
|
||||
elseif type(v) == "table" then
|
||||
t[k] = functionStringToTable(v)
|
||||
else
|
||||
t[k] = v
|
||||
end
|
||||
end
|
||||
while threads.get(name)==nil do
|
||||
love.timer.sleep(.001)
|
||||
end
|
||||
local dat = threads.get(name)
|
||||
if type(dat) == "table" and dat.init then
|
||||
dat.init = threads.loadDump(dat.init)
|
||||
end
|
||||
return dat
|
||||
end
|
||||
|
||||
function threads.package(name,val)
|
||||
local init = val.init
|
||||
val.init=threads.dump(val.init)
|
||||
GLOBAL[name]=val
|
||||
val.init=init
|
||||
end
|
||||
|
||||
function threads.getCores()
|
||||
return love.system.getProcessorCount()
|
||||
end
|
||||
|
||||
function threads.kill()
|
||||
error("Thread Killed!\1")
|
||||
end
|
||||
|
||||
function threads.pushStatus(...)
|
||||
local status_channel = love.thread.getChannel("__"..__THREADID__.."__MULTI__STATUS_CHANNEL__")
|
||||
local args = {...}
|
||||
status_channel:push(__THREADID__, args)
|
||||
end
|
||||
|
||||
function threads.getThreads()
|
||||
local t = {}
|
||||
for i=1,GLOBAL["__THREAD_COUNT"] do
|
||||
t[#t+1]=GLOBAL["__THREAD_"..i]
|
||||
if t.init then
|
||||
t:init()
|
||||
end
|
||||
return t
|
||||
end
|
||||
|
||||
function threads.getThread(n)
|
||||
return GLOBAL["__THREAD_"..n]
|
||||
local function packValue(t)
|
||||
return tableToFunctionString(t)
|
||||
end
|
||||
|
||||
function threads.getName()
|
||||
return __THREADNAME__
|
||||
local function unpackValue(t)
|
||||
return functionStringToTable(t)
|
||||
end
|
||||
|
||||
function threads.getID()
|
||||
return __THREADID__
|
||||
end
|
||||
|
||||
function threads.sleep(n)
|
||||
love.timer.sleep(n)
|
||||
end
|
||||
|
||||
function threads.getGlobal()
|
||||
return setmetatable({},
|
||||
{
|
||||
__index = function(t, k)
|
||||
return THREAD.get(k)
|
||||
end,
|
||||
__newindex = function(t, k, v)
|
||||
THREAD.set(k,v)
|
||||
end
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
function threads.createTable(n)
|
||||
local _proxy = {}
|
||||
local function set(name,val)
|
||||
if not _proxy[name] then _proxy[name] = love.thread.getChannel(n..name) end
|
||||
_proxy[name]:performAtomic(manage, val)
|
||||
local function createTable(n)
|
||||
if not n then
|
||||
n = "STAB"..multi.randomString(8)
|
||||
end
|
||||
local function get(name)
|
||||
if not _proxy[name] then _proxy[name] = love.thread.getChannel(n..name) end
|
||||
local dat = _proxy[name]:peek()
|
||||
if type(dat)=="table" and dat[1]=="func" then
|
||||
return THREAD.loadDump(dat[2])
|
||||
else
|
||||
return dat
|
||||
end
|
||||
end
|
||||
return setmetatable({},
|
||||
{
|
||||
__index = function(t, k)
|
||||
return get(k)
|
||||
end,
|
||||
__newindex = function(t, k, v)
|
||||
set(k,v)
|
||||
end
|
||||
}
|
||||
)
|
||||
end
|
||||
|
||||
function threads.getConsole()
|
||||
local c = {}
|
||||
c.queue = love.thread.getChannel("__CONSOLE__")
|
||||
function c.print(...)
|
||||
c.queue:push{...}
|
||||
end
|
||||
function c.error(err)
|
||||
c.queue:push{"ERROR in <"..__THREADNAME__..">: "..err,__THREADID__}
|
||||
error(err)
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
if not ISTHREAD then
|
||||
local clock = os.clock
|
||||
local lastproc = clock()
|
||||
local queue = love.thread.getChannel("__CONSOLE__")
|
||||
thread:newThread("consoleManager",function()
|
||||
while true do
|
||||
thread.yield()
|
||||
dat = queue:pop()
|
||||
if dat then
|
||||
lastproc = clock()
|
||||
print(unpack(dat))
|
||||
end
|
||||
if clock()-lastproc>2 then
|
||||
thread.sleep(.1)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
function threads.createStaticTable(n)
|
||||
local __proxy = {}
|
||||
local function set(name,val)
|
||||
if __proxy[name] then return end
|
||||
local chan = love.thread.getChannel(n..name)
|
||||
if chan:getCount()>0 then return end
|
||||
chan:performAtomic(manage, val)
|
||||
__proxy[name] = val
|
||||
local function set(name, val)
|
||||
local chan = love.thread.getChannel(n .. name)
|
||||
if chan:getCount() == 1 then chan:pop() end
|
||||
__proxy[name] = true
|
||||
chan:push(packValue(val))
|
||||
end
|
||||
local function get(name)
|
||||
if __proxy[name] then return __proxy[name] end
|
||||
local dat = love.thread.getChannel(n..name):peek()
|
||||
if type(dat)=="table" and dat[1]=="func" then
|
||||
__proxy[name] = THREAD.loadDump(dat[2])
|
||||
return __proxy[name]
|
||||
else
|
||||
__proxy[name] = dat
|
||||
return __proxy[name]
|
||||
end
|
||||
return unpackValue(love.thread.getChannel(n .. name):peek())
|
||||
-- if type(data) == "table" and data.init then
|
||||
-- return data:init()
|
||||
-- else
|
||||
-- return data
|
||||
-- end
|
||||
end
|
||||
return setmetatable({},
|
||||
{
|
||||
@@ -240,11 +128,101 @@ function threads.createStaticTable(n)
|
||||
)
|
||||
end
|
||||
|
||||
function threads.hold(n)
|
||||
local dat
|
||||
while not(dat) do
|
||||
dat = n()
|
||||
function INIT()
|
||||
local GLOBAL, THREAD, DEFER = createTable("__GLOBAL__"), {}, {}
|
||||
local status_channel, console_channel = love.thread.getChannel("__status_channel__" .. THREAD_ID),
|
||||
love.thread.getChannel("__console_channel__")
|
||||
|
||||
-- Non portable methods, shouldn't be used unless you know what you are doing
|
||||
THREAD.packValue = packValue
|
||||
THREAD.unpackValue = unpackValue
|
||||
THREAD.createTable = createTable
|
||||
|
||||
function THREAD.set(name, val)
|
||||
GLOBAL[name] = val
|
||||
end
|
||||
|
||||
function THREAD.get(name, val)
|
||||
return GLOBAL[name]
|
||||
end
|
||||
|
||||
THREAD.waitFor = thread:newFunction(function(name)
|
||||
local function wait()
|
||||
math.randomseed(os.time())
|
||||
thread.yield()
|
||||
end
|
||||
repeat
|
||||
wait()
|
||||
until GLOBAL[name] ~= nil
|
||||
return GLOBAL[name]
|
||||
end, true)
|
||||
|
||||
function THREAD.getCores()
|
||||
return love.system.getProcessorCount()
|
||||
end
|
||||
|
||||
function THREAD.getConsole()
|
||||
local c = {}
|
||||
c.queue = console_channel
|
||||
function c.print(...)
|
||||
c.queue:push(table.concat(multi.pack(...), "\t"))
|
||||
end
|
||||
function c.error(err)
|
||||
c.queue:push("Error in <"..THREAD_NAME..":" .. THREAD_ID .. ">: ".. err)
|
||||
multi.error(err)
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
function THREAD.getThreads()
|
||||
--
|
||||
end
|
||||
|
||||
function THREAD.kill() -- trigger the lane destruction
|
||||
error("Thread was killed!\1")
|
||||
end
|
||||
|
||||
function THREAD.pushStatus(...)
|
||||
status_channel:push(multi.pack(...))
|
||||
end
|
||||
|
||||
function THREAD.sleep(n)
|
||||
love.timer.sleep(n)
|
||||
end
|
||||
|
||||
THREAD.hold = thread:newFunction(function(n)
|
||||
thread.hold(n)
|
||||
end, true)
|
||||
|
||||
function THREAD.setENV(env, name)
|
||||
GLOBAL[name or "__env"] = env
|
||||
end
|
||||
|
||||
function THREAD.getENV(name)
|
||||
return GLOBAL[name or "__env"]
|
||||
end
|
||||
|
||||
function THREAD.exposeENV(name)
|
||||
name = name or "__env"
|
||||
local env = THREAD.getENV(name)
|
||||
for i,v in pairs(env) do
|
||||
_G[i] = v
|
||||
end
|
||||
end
|
||||
|
||||
function THREAD.defer(func)
|
||||
table.insert(DEFER, func)
|
||||
end
|
||||
|
||||
function THREAD.sync()
|
||||
-- Maybe do something...
|
||||
end
|
||||
|
||||
return GLOBAL, THREAD, DEFER
|
||||
end
|
||||
|
||||
return threads
|
||||
return {
|
||||
init = function()
|
||||
return INIT()
|
||||
end
|
||||
}
|
||||
Reference in New Issue
Block a user