how did pseudothreading go away
This commit is contained in:
parent
df429b617e
commit
3ae2acbd78
141
integration/pesudoManager/extensions.lua
Normal file
141
integration/pesudoManager/extensions.lua
Normal file
@ -0,0 +1,141 @@
|
||||
--[[
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 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, sub-license, 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.
|
||||
]]
|
||||
local multi, thread = require("multi"):init()
|
||||
local GLOBAL, THREAD = multi.integration.GLOBAL,multi.integration.THREAD
|
||||
|
||||
local function stripUpValues(func)
|
||||
local dmp = string.dump(func)
|
||||
if setfenv then
|
||||
return loadstring(dmp,"IsolatedThread_PesudoThreading")
|
||||
else
|
||||
return load(dmp,"IsolatedThread_PesudoThreading","bt")
|
||||
end
|
||||
end
|
||||
|
||||
function multi:newSystemThreadedQueue(name)
|
||||
local c = {}
|
||||
function c:push(v)
|
||||
table.insert(self,v)
|
||||
end
|
||||
function c:pop()
|
||||
return table.remove(self,1)
|
||||
end
|
||||
function c:peek()
|
||||
return self[1]
|
||||
end
|
||||
function c:init()
|
||||
return self
|
||||
end
|
||||
GLOBAL[name or "_"] = c
|
||||
return c
|
||||
end
|
||||
function multi:newSystemThreadedTable(name)
|
||||
local c = {}
|
||||
function c:init()
|
||||
return self
|
||||
end
|
||||
GLOBAL[name or "_"] = c
|
||||
return c
|
||||
end
|
||||
local setfenv = setfenv
|
||||
if not setfenv then
|
||||
if not debug then
|
||||
multi.print("Unable to implement setfenv in lua 5.2+ the debug module is not available!")
|
||||
else
|
||||
setfenv = function(f, env)
|
||||
return load(string.dump(f), nil, nil, env)
|
||||
end
|
||||
end
|
||||
end
|
||||
function multi:newSystemThreadedJobQueue(n)
|
||||
local c = {}
|
||||
c.cores = n or THREAD.getCores()*2
|
||||
c.OnJobCompleted = multi:newConnection()
|
||||
local jobs = {}
|
||||
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()
|
||||
print(#jobs)
|
||||
return #jobs == 0
|
||||
end
|
||||
local nFunc = 0
|
||||
function c:newFunction(name,func,holup) -- This registers with the queue
|
||||
local func = stripUpValues(func)
|
||||
if type(name)=="function" then
|
||||
holup = func
|
||||
func = name
|
||||
name = "JQ_Function_"..nFunc
|
||||
end
|
||||
nFunc = nFunc + 1
|
||||
c:registerFunction(name,func)
|
||||
return thread:newFunction(function(...)
|
||||
local id = c:pushJob(name,...)
|
||||
local link
|
||||
local rets
|
||||
link = c.OnJobCompleted(function(jid,...)
|
||||
if id==jid then
|
||||
rets = {...}
|
||||
link:Destroy()
|
||||
end
|
||||
end)
|
||||
return thread.hold(function()
|
||||
if rets then
|
||||
return unpack(rets) or multi.NIL
|
||||
end
|
||||
end)
|
||||
end,holup),name
|
||||
end
|
||||
for i=1,c.cores do
|
||||
thread:newthread("PesudoThreadedJobQueue_"..i,function()
|
||||
while true do
|
||||
thread.yield()
|
||||
if #jobs>0 then
|
||||
local j = table.remove(jobs,1)
|
||||
c.OnJobCompleted:Fire(j[2],funcs[j[1]](unpack(j[3])))
|
||||
else
|
||||
thread.sleep(.05)
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
return c
|
||||
end
|
||||
101
integration/pesudoManager/init.lua
Normal file
101
integration/pesudoManager/init.lua
Normal file
@ -0,0 +1,101 @@
|
||||
--[[
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2020 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, sub-license, 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.
|
||||
]]
|
||||
package.path = "?/init.lua;?.lua;" .. package.path
|
||||
local multi, thread = require("multi"):init()
|
||||
|
||||
if multi.integration then
|
||||
return {
|
||||
init = function()
|
||||
return multi.integration.GLOBAL, multi.integration.THREAD
|
||||
end
|
||||
}
|
||||
end
|
||||
|
||||
local GLOBAL, THREAD = require("multi.integration.pesudoManager.threads").init(thread)
|
||||
|
||||
function multi:canSystemThread() -- We are emulating system threading
|
||||
return true
|
||||
end
|
||||
|
||||
function multi:getPlatform()
|
||||
return "pesudo"
|
||||
end
|
||||
|
||||
local function split(str)
|
||||
local tab = {}
|
||||
for word in string.gmatch(str, '([^,]+)') do
|
||||
table.insert(tab,word)
|
||||
end
|
||||
return tab
|
||||
end
|
||||
|
||||
local tab = [[_VERSION,io,os,require,load,debug,assert,collectgarbage,error,getfenv,getmetatable,ipairs,loadstring,module,next,pairs,pcall,print,rawequal,rawget,rawset,select,setfenv,setmetatable,tonumber,tostring,type,unpack,xpcall,math,coroutine,string,table]]
|
||||
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
|
||||
local env = {
|
||||
GLOBAL = GLOBAL,
|
||||
THREAD = THREAD,
|
||||
THREAD_NAME = name,
|
||||
__THREADNAME__ = name,
|
||||
THREAD_ID = id,
|
||||
thread = thread
|
||||
}
|
||||
|
||||
for i = 1,#tab do
|
||||
env[tab[i]] = _G[tab[i]]
|
||||
end
|
||||
|
||||
local th = thread:newISOThread(name,func,env,...)
|
||||
|
||||
id = id + 1
|
||||
|
||||
return th
|
||||
end
|
||||
|
||||
THREAD.newSystemThread = multi.newSystemThread
|
||||
-- System threads as implemented here cannot share memory, but use a message passing system.
|
||||
-- An isolated thread allows us to mimic that behavior so if access data from the "main" thread happens things will not work. This behavior is in line with how the system threading works
|
||||
|
||||
function THREAD:newFunction(func,holdme)
|
||||
return thread:newFunctionBase(function(...)
|
||||
return multi:newSystemThread("TempSystemThread",func,...)
|
||||
end,holdme)()
|
||||
end
|
||||
|
||||
multi.print("Integrated Pesudo Threading!")
|
||||
multi.integration = {} -- for module creators
|
||||
multi.integration.GLOBAL = GLOBAL
|
||||
multi.integration.THREAD = THREAD
|
||||
require("multi.integration.pesudoManager.extensions")
|
||||
return {
|
||||
init = function()
|
||||
return GLOBAL, THREAD
|
||||
end
|
||||
}
|
||||
110
integration/pesudoManager/threads.lua
Normal file
110
integration/pesudoManager/threads.lua
Normal file
@ -0,0 +1,110 @@
|
||||
--[[
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2022 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, sub-license, 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.
|
||||
]]
|
||||
|
||||
local function getOS()
|
||||
if package.config:sub(1, 1) == "\\" then
|
||||
return "windows"
|
||||
else
|
||||
return "unix"
|
||||
end
|
||||
end
|
||||
|
||||
local function INIT(thread)
|
||||
local THREAD = {}
|
||||
local GLOBAL = {}
|
||||
THREAD.Priority_Core = 3
|
||||
THREAD.Priority_High = 2
|
||||
THREAD.Priority_Above_Normal = 1
|
||||
THREAD.Priority_Normal = 0
|
||||
THREAD.Priority_Below_Normal = -1
|
||||
THREAD.Priority_Low = -2
|
||||
THREAD.Priority_Idle = -3
|
||||
|
||||
function THREAD.set(name, val)
|
||||
GLOBAL[name] = val
|
||||
end
|
||||
|
||||
function THREAD.get(name)
|
||||
return GLOBAL[name]
|
||||
end
|
||||
|
||||
function THREAD.waitFor(name)
|
||||
return thread.hold(function() return GLOBAL[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()
|
||||
return THREAD.__CORES
|
||||
end
|
||||
|
||||
function THREAD.getConsole()
|
||||
local c = {}
|
||||
function c.print(...)
|
||||
print(...)
|
||||
end
|
||||
function c.error(err)
|
||||
error("ERROR in <"..GLOBAL["$__THREADNAME__"]..">: "..err)
|
||||
end
|
||||
return c
|
||||
end
|
||||
|
||||
function THREAD.getThreads()
|
||||
return {}--GLOBAL.__THREADS__
|
||||
end
|
||||
|
||||
THREAD.pushStatus = thread.pushStatus
|
||||
|
||||
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
|
||||
|
||||
function THREAD.getName()
|
||||
return GLOBAL["$THREAD_NAME"]
|
||||
end
|
||||
|
||||
function THREAD.getID()
|
||||
return GLOBAL["$THREAD_ID"]
|
||||
end
|
||||
|
||||
THREAD.sleep = thread.sleep
|
||||
|
||||
THREAD.hold = thread.hold
|
||||
|
||||
return GLOBAL, THREAD
|
||||
end
|
||||
|
||||
return {init = function(thread)
|
||||
return INIT(thread)
|
||||
end}
|
||||
Loading…
x
Reference in New Issue
Block a user