mirror of
https://github.com/rayaman/multi.git
synced 2026-09-05 07:27:35 -04:00
newProxy and STP work
This commit is contained in:
@@ -1,34 +1,77 @@
|
||||
local multi, thread = require("multi"):init()
|
||||
--[[
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2023 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()
|
||||
|
||||
-- Returns a handler that allows a user to interact with an object on another thread!
|
||||
-- Create on the thread that you want to interact with, send over the handle
|
||||
function multi:newProxy(obj)
|
||||
|
||||
local c = {
|
||||
__index = function()
|
||||
--
|
||||
end,
|
||||
__newindex = function()
|
||||
--
|
||||
end,
|
||||
__call = function()
|
||||
--
|
||||
end
|
||||
}
|
||||
|
||||
c.name = multi.randomString(12)
|
||||
|
||||
function c:init()
|
||||
if not multi.isMainThread then
|
||||
c.send = multi:newSystemThreadedQueue(self.name.."_S"):init()
|
||||
c.recv = multi:newSystemThreadedQueue(self.name.."_R"):init()
|
||||
c.ref = obj
|
||||
local multi, thread = require("multi"):init()
|
||||
local function check()
|
||||
return self.send:pop()
|
||||
end
|
||||
self.send = multi:newSystemThreadedQueue(self.name.."_S"):init()
|
||||
self.recv = multi:newSystemThreadedQueue(self.name.."_R"):init()
|
||||
self.ref = obj
|
||||
self.funcs = {}
|
||||
for i, v in pairs(obj) do
|
||||
if type(v) == "function" then
|
||||
self.funcs[#self.funcs] = i
|
||||
end
|
||||
end
|
||||
thread:newThread(function()
|
||||
while true do
|
||||
local data = thread.hold(check)
|
||||
local func = table.remove(data, 1)
|
||||
local ret = {self.ref[func](multi.unpack(data))}
|
||||
table.insert(ret, 1, func)
|
||||
self.recv:push(ret)
|
||||
end
|
||||
end)
|
||||
else
|
||||
GLOBAL = multi.integration.GLOBAL
|
||||
THREAD = multi.integration.THREAD
|
||||
c.send = THREAD.waitFor(self.name.."_S")
|
||||
c.recv = THREAD.waitFor(self.name.."_R")
|
||||
self.send = THREAD.waitFor(self.name.."_S")
|
||||
self.recv = THREAD.waitFor(self.name.."_R")
|
||||
for _,v in pairs(self.funcs) do
|
||||
self[v] = thread:newFunction(function(...)
|
||||
self.send:push({v, ...})
|
||||
return thread.hold(function()
|
||||
local data = self.recv:peek()
|
||||
if data[1] == v then
|
||||
self.recv:pop()
|
||||
thread.remove(data, 1)
|
||||
return multi.unpack(data)
|
||||
end
|
||||
end)
|
||||
end, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -54,26 +97,35 @@ function multi:newSystemThreadedProcessor(name, cores)
|
||||
c.parent = self
|
||||
c.jobqueue = multi:newSystemThreadedJobQueue(c.cores)
|
||||
|
||||
c.jobqueue:registerFunction("__spawnThread__", function(name, func, ...)
|
||||
local spawnThread = c.jobqueue:newFunction(function(name, func, ...)
|
||||
local multi, thread = require("multi"):init()
|
||||
thread:newThread(name, func, ...)
|
||||
return true
|
||||
end)
|
||||
print("hmm")
|
||||
local proxy = multi:newProxy(thread:newThread(name, func, ...))
|
||||
multi:newTask(function()
|
||||
proxy:init()
|
||||
end)
|
||||
return proxy
|
||||
end, true)
|
||||
|
||||
c.jobqueue:registerFunction("__spawnTask__", function(obj, ...)
|
||||
local spawnTask = c.jobqueue:newFunction(function(name, func, ...)
|
||||
local multi, thread = require("multi"):init()
|
||||
multi[obj](multi, func)
|
||||
return true
|
||||
end)
|
||||
local proxy = multi:newProxy(multi[obj](multi, func))
|
||||
multi:newTask(function()
|
||||
proxy:init()
|
||||
end)
|
||||
return proxy
|
||||
end, true)
|
||||
|
||||
c.newLoop = thread:newFunction(function(self, func, notime)
|
||||
return spawnTask("newLoop", func, notime):init()
|
||||
end, true)
|
||||
|
||||
c.newUpdater = thread:newFunction(function(self, skip, func)
|
||||
return spawnTask("newUpdater", func, notime):init()
|
||||
end, true)
|
||||
|
||||
c.OnObjectCreated(function(proc, obj)
|
||||
if obj.Type == multi.UPDATER then
|
||||
local func = obj.OnUpdate:Remove()[1]
|
||||
c.jobqueue:pushJob("__spawnTask__", "newUpdater", func)
|
||||
elseif obj.Type == multi.LOOP then
|
||||
local func = obj.OnLoop:Remove()[1]
|
||||
c.jobqueue:pushJob("__spawnTask__", "newLoop", func)
|
||||
else
|
||||
if not(obj.Type == multi.UPDATER or obj.Type == multi.LOOP) then
|
||||
return multi.error("Invalid type!")
|
||||
end
|
||||
end)
|
||||
@@ -94,9 +146,9 @@ function multi:newSystemThreadedProcessor(name, cores)
|
||||
return self.Name
|
||||
end
|
||||
|
||||
function c:newThread(name, func, ...)
|
||||
c.jobqueue:pushJob("__spawnThread__", name, func, ...)
|
||||
end
|
||||
c.newThread = thread:newFunction(function(self, name, func, ...)
|
||||
return spawnThread(name, func, ...):init()
|
||||
end, true)
|
||||
|
||||
function c:newFunction(func, holdme)
|
||||
return c.jobqueue:newFunction(func, holdme)
|
||||
|
||||
Reference in New Issue
Block a user