Compare commits

...

4 Commits

Author SHA1 Message Date
fe5333512f Updated test file 2021-06-19 20:26:27 -04:00
b2ae7181d6 Fixed issues 2021-06-19 20:24:23 -04:00
2a8b3d095f Patched issue with threaded functions not returning multiple values 2021-06-19 20:14:03 -04:00
68908f093b cleaned up thread files 2020-04-01 10:10:11 -04:00
5 changed files with 18 additions and 34 deletions

View File

@ -1041,13 +1041,13 @@ function multi.holdFor(n,func)
end) end)
end end
local function cleanReturns(...) local function cleanReturns(...)
local n = select("#", ...)
local returns = {...} local returns = {...}
local rets = {} local rets = {}
local ind = 0 local ind = 0
for i=n,1,-1 do for i=#returns,1,-1 do
if returns[i] then if returns[i] then
ind=i ind = i
break
end end
end end
return unpack(returns,1,ind) return unpack(returns,1,ind)

View File

@ -53,11 +53,6 @@ local function INIT(__GlobalLinda,__SleepingLinda)
until __GlobalLinda:get(name) until __GlobalLinda:get(name)
return __GlobalLinda:get(name) return __GlobalLinda:get(name)
end end
if getOS() == "windows" then
THREAD.__CORES = tonumber(os.getenv("NUMBER_OF_PROCESSORS"))
else
THREAD.__CORES = tonumber(io.popen("nproc --all"):read("*n"))
end
function THREAD.getCores() function THREAD.getCores()
return THREAD.__CORES return THREAD.__CORES
end end
@ -92,17 +87,16 @@ local function INIT(__GlobalLinda,__SleepingLinda)
end end
_G.THREAD_ID = 0 _G.THREAD_ID = 0
function THREAD.sleep(n) function THREAD.sleep(n)
math.randomseed(os.time())
__SleepingLinda:receive(n, "__non_existing_variable") __SleepingLinda:receive(n, "__non_existing_variable")
end end
function THREAD.hold(n) function THREAD.hold(n)
local function wait() while true do
math.randomseed(os.time())
__SleepingLinda:receive(.001, "__non_existing_variable") __SleepingLinda:receive(.001, "__non_existing_variable")
local tab = {n()}
if tab[1] then
return unpack(tab)
end
end end
repeat
wait()
until n()
end end
local GLOBAL = {} local GLOBAL = {}
setmetatable(GLOBAL, { setmetatable(GLOBAL, {

View File

@ -213,9 +213,12 @@ function threads.createStaticTable(n)
) )
end end
function threads.hold(n) function threads.hold(n)
local dat while true do
while not(dat) do love.timer.sleep(.001)
dat = n() local tab = {n()}
if tab[1] then
return unpack(tab)
end
end end
end end
return threads return threads

View File

@ -48,35 +48,21 @@ local function INIT(env)
print("Waiting",thread) print("Waiting",thread)
return thread.hold(function() return GLOBAL[name] end) return thread.hold(function() return GLOBAL[name] end)
end end
if getOS() == "windows" then
THREAD.__CORES = tonumber(os.getenv("NUMBER_OF_PROCESSORS"))
else
THREAD.__CORES = tonumber(io.popen("nproc --all"):read("*n"))
end
function THREAD.getCores() function THREAD.getCores()
return THREAD.__CORES return THREAD.__CORES
end end
function THREAD.getConsole() function THREAD.getConsole()
local c = {} local c = {}
function c.print(...) c.print = print
print(...)
end
function c.error(err) function c.error(err)
error("ERROR in <"..GLOBAL["$__THREADNAME__"]..">: "..err) error("ERROR in <"..GLOBAL["$__THREADNAME__"]..">: "..err)
end end
return c return c
end end
function THREAD.getThreads() function THREAD.getThreads()
return {}--GLOBAL.__THREADS__ return {} --GLOBAL.__THREADS__
end
if os.getOS() == "windows" then
THREAD.__CORES = tonumber(os.getenv("NUMBER_OF_PROCESSORS"))
else
THREAD.__CORES = tonumber(io.popen("nproc --all"):read("*n"))
end
function THREAD.kill()
error("Thread was killed!")
end end
THREAD.__CORES = thread.__CORES
function THREAD.getName() function THREAD.getName()
return GLOBAL["$THREAD_NAME"] return GLOBAL["$THREAD_NAME"]
end end

View File

@ -1,3 +1,4 @@
package.path = "./?/init.lua;"..package.path
multi,thread = require("multi"):init() multi,thread = require("multi"):init()
GLOBAL,THREAD = require("multi.integration.threading"):init() -- Auto detects your enviroment and uses what's available GLOBAL,THREAD = require("multi.integration.threading"):init() -- Auto detects your enviroment and uses what's available