mirror of
https://github.com/rayaman/multi.git
synced 2026-09-05 07:27:35 -04:00
Changes to threads
This commit is contained in:
@@ -50,6 +50,7 @@ function multi:newSystemThreadedQueue(name)
|
||||
GLOBAL[name or "_"] = c
|
||||
return c
|
||||
end
|
||||
|
||||
function multi:newSystemThreadedTable(name)
|
||||
local c = {}
|
||||
function c:init()
|
||||
@@ -58,6 +59,7 @@ function multi:newSystemThreadedTable(name)
|
||||
GLOBAL[name or "_"] = c
|
||||
return c
|
||||
end
|
||||
|
||||
local setfenv = setfenv
|
||||
if not setfenv then
|
||||
if not debug then
|
||||
@@ -68,6 +70,7 @@ if not setfenv then
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function multi:newSystemThreadedJobQueue(n)
|
||||
local c = {}
|
||||
c.cores = n or THREAD.getCores()*2
|
||||
@@ -76,26 +79,32 @@ function multi:newSystemThreadedJobQueue(n)
|
||||
local ID=1
|
||||
local jid = 1
|
||||
local env = {}
|
||||
|
||||
setmetatable(env,{
|
||||
__index = _G
|
||||
})
|
||||
|
||||
local funcs = {}
|
||||
function c:doToAll(func)
|
||||
setfenv(func,env)()
|
||||
return self
|
||||
end
|
||||
|
||||
function c:registerFunction(name,func)
|
||||
funcs[name] = setfenv(func,env)
|
||||
return self
|
||||
end
|
||||
|
||||
function c:pushJob(name,...)
|
||||
table.insert(jobs,{name,jid,{...}})
|
||||
jid = jid + 1
|
||||
return jid-1
|
||||
end
|
||||
|
||||
function c:isEmpty()
|
||||
return #jobs == 0
|
||||
end
|
||||
|
||||
local nFunc = 0
|
||||
function c:newFunction(name,func,holup) -- This registers with the queue
|
||||
local func = stripUpValues(func)
|
||||
@@ -120,7 +129,7 @@ function multi:newSystemThreadedJobQueue(n)
|
||||
return unpack(rets) or multi.NIL
|
||||
end
|
||||
end)
|
||||
end,holup),name
|
||||
end, holup), name
|
||||
end
|
||||
for i=1,c.cores do
|
||||
thread:newThread("PesudoThreadedJobQueue_"..i,function()
|
||||
@@ -133,7 +142,7 @@ function multi:newSystemThreadedJobQueue(n)
|
||||
thread.sleep(.05)
|
||||
end
|
||||
end
|
||||
end).OnError(print)
|
||||
end).OnError(multi.error)
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
@@ -31,8 +31,8 @@ if multi.integration then
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
local GLOBAL, THREAD = require("multi.integration.pseudoManager.threads").init(thread)
|
||||
local activator = require("multi.integration.pseudoManager.threads")
|
||||
local GLOBAL, THREAD = activator.init(thread)
|
||||
|
||||
function multi:canSystemThread() -- We are emulating system threading
|
||||
return true
|
||||
@@ -55,22 +55,28 @@ tab = split(tab)
|
||||
|
||||
local id = 0
|
||||
|
||||
function multi:newSystemThread(name,func,...)
|
||||
GLOBAL["$THREAD_NAME"] = name
|
||||
GLOBAL["$__THREADNAME__"] = name
|
||||
GLOBAL["$THREAD_ID"] = id
|
||||
GLOBAL["$thread"] = thread
|
||||
print("Outerglobal",_G)
|
||||
|
||||
local env = {
|
||||
function multi:newSystemThread(name, func, ...)
|
||||
local env
|
||||
env = {
|
||||
GLOBAL = GLOBAL,
|
||||
THREAD = THREAD,
|
||||
THREAD_NAME = name,
|
||||
__THREADNAME__ = name,
|
||||
THREAD_NAME = tostring(name),
|
||||
__THREADNAME__ = tostring(name),
|
||||
test = "testing",
|
||||
THREAD_ID = id,
|
||||
thread = thread,
|
||||
multi = multi,
|
||||
}
|
||||
|
||||
env.__env = env
|
||||
for i, v in pairs(_G) do
|
||||
if not(env[i]) and not(i == "_G") and not(i == "local_global") then
|
||||
env[i] = v
|
||||
else
|
||||
multi.warn("skipping:",i)
|
||||
end
|
||||
end
|
||||
|
||||
if GLOBAL["__env"] then
|
||||
for i,v in pairs(GLOBAL["__env"]) do
|
||||
@@ -78,11 +84,11 @@ function multi:newSystemThread(name,func,...)
|
||||
end
|
||||
end
|
||||
|
||||
for i = 1,#tab do
|
||||
env[tab[i]] = _G[tab[i]]
|
||||
end
|
||||
env._G = env
|
||||
|
||||
local GLOBAL, THREAD = activator.init(thread, env)
|
||||
|
||||
local th = thread:newISOThread(name,func,env,...)
|
||||
local th = thread:newISOThread(name, func, env, ...)
|
||||
|
||||
id = id + 1
|
||||
|
||||
|
||||
@@ -33,6 +33,7 @@ end
|
||||
local function INIT(thread)
|
||||
local THREAD = {}
|
||||
local GLOBAL = {}
|
||||
|
||||
THREAD.Priority_Core = 3
|
||||
THREAD.Priority_High = 2
|
||||
THREAD.Priority_Above_Normal = 1
|
||||
@@ -84,14 +85,6 @@ local function INIT(thread)
|
||||
error("Thread was killed!")
|
||||
end
|
||||
|
||||
function THREAD.getName()
|
||||
return GLOBAL["$THREAD_NAME"]
|
||||
end
|
||||
|
||||
function THREAD.getID()
|
||||
return GLOBAL["$THREAD_ID"]
|
||||
end
|
||||
|
||||
THREAD.sleep = thread.sleep
|
||||
|
||||
THREAD.hold = thread.hold
|
||||
@@ -107,18 +100,17 @@ local function INIT(thread)
|
||||
end
|
||||
|
||||
function THREAD.exposeENV(name)
|
||||
print("env",__env)
|
||||
name = name or "__env"
|
||||
local env = THREAD.getENV(name)
|
||||
for i,v in pairs(env) do
|
||||
-- This may need to be reworked!
|
||||
_G[i] = v
|
||||
local_global[i] = v
|
||||
end
|
||||
end
|
||||
|
||||
return GLOBAL, THREAD
|
||||
end
|
||||
|
||||
return {init = function(thread)
|
||||
return INIT(thread)
|
||||
return {init = function(thread, global)
|
||||
return INIT(thread, global)
|
||||
end}
|
||||
Reference in New Issue
Block a user