diff --git a/docs/changes.md b/docs/changes.md index b78ff3f..0667130 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -62,12 +62,13 @@ Table of contents ## Added New Integration: **priorityManager** -Allows the user to have multi auto set priorities. Also adds the functionality to create your own runners (multi:mainloop(), multi:umanager()) that you can set using the priority manager. Even if you do not have `chronos` installed these features will still work! +Allows the user to have multi auto set priorities (Requires chronos). Also adds the functionality to create your own runners (multi:mainloop(), multi:umanager()) that you can set using the priority manager. Even if you do not have `chronos` installed all other features will still work! - Allows the creation of custom priorityManagers for example: - Added --- +- multi.Processors:getHandler() -- returns the thread handler for a process +- multi.OnPriorityChanged(self, priority) -- Connection is triggered whenever the priority of an object is changed! - multi.setClock(clock_func) -- If you have access to a clock function that works like os.clock() you can set it using this function. The priorityManager if chronos is installed sets the clock to it's current version. - multi:setCurrentTask() -- Used to set the current processor. Used in custom processors. - multi:setCurrentProcess() -- Used to set the current processor. It should only be called on a processor object @@ -160,6 +161,9 @@ Added conn4:Fire(3,2,3) + -- This second one won't trigger the Hi's + conn4:Fire(1,2,3) + conn5(function() print("Test 1") end) diff --git a/init.lua b/init.lua index b19e453..8057a15 100644 --- a/init.lua +++ b/init.lua @@ -633,6 +633,7 @@ function multi:newBase(ins) c.funcTM={} c.funcTMR={} c.OnBreak = multi:newConnection() + c.OnPriorityChanged = multi:newConnection() c.TID = _tid c.Act=function() end c.Parent=self @@ -1061,6 +1062,10 @@ function multi:newProcessor(name, nothread) c.OnError(multi.error) + function c:getHandler() + return handler + end + function c:getThreads() return c.threads end @@ -1412,6 +1417,38 @@ function thread:newFunction(func, holdme) end, holdme)() end +function thread:newProcessor(name) + -- Inactive proxy proc + local proc = multi:getCurrentProcess():newProcessor(name, true) + local thread_proc = multi:getCurrentProcess():newProcessor(name, true) + + local handler = thread_proc:getHandler() + + function proc:getThreads() + return thread_proc.threads + end + + function proc:newThread(name, func,...) + return thread.newThread(thread_proc, name, func, ...) + end + + function c:newFunction(func, holdme) + return thread:newFunctionBase(function(...) + return thread_proc:newThread("Threaded Function Handler", func, ...) + end, holdme)() + end + + proc.OnObjectCreated(function(obj) + thread_proc:newThread(function() + while true do + thread.yield() + -- + end + end) + end) + return proc +end + -- A cross version way to set enviroments, not the same as fenv though function multi.setEnv(func,env) local f = string.dump(func) @@ -2102,6 +2139,7 @@ function multi:setPriority(s) elseif s:lower()=='idle' or s:lower()=='i' then self.Priority=self.Priority_Idle end + self.OnPriorityChanged:Fire(self, self.Priority) end if not self.PrioritySet then self.defPriority = self.Priority @@ -2225,7 +2263,6 @@ function multi:reallocate(processor, index) index=index or #processor.Mainloop+1 local int=self.Parent self.Parent=processor - print("Moving task to new processor!") if index then table.insert(processor.Mainloop, index, self) else diff --git a/tests/test.lua b/tests/test.lua index 299d78f..2b0e4e1 100644 --- a/tests/test.lua +++ b/tests/test.lua @@ -2,19 +2,58 @@ package.path = "../?/init.lua;../?.lua;"..package.path multi, thread = require("multi"):init{print=true,warn=true,error=true} require("multi.integration.priorityManager") -test = multi:newProcessor("Test") -test:setPriorityScheme(multi.priorityScheme.TimeBased) -multi.OnObjectCreated(function(proc, obj) - print("MULTI",proc.Type,obj.Type) -end) -local a = 0 -test:newUpdater(100000):OnUpdate(function() - print("Print is slowish") +-- test = multi:newProcessor("Test") +-- test:setPriorityScheme(multi.priorityScheme.TimeBased) + +-- test:newUpdater(10000000):OnUpdate(function() +-- print("Print is slowish") +-- end) + +-- print("Running...") + +local conn1, conn2 = multi:newConnection(), multi:newConnection() +conn3 = conn1 + conn2 + +conn1(function() + print("Hi 1") end) -print("Running...") +conn2(function() + print("Hi 2") +end) -multi:mainloop() +conn3(function() + print("Hi 3") +end) + +function test(a,b,c) + print("I run before all and control if execution should continue!") + return a>b +end + +conn4 = test .. conn1 + +conn5 = conn2 .. function() print("I run after it all!") end + +conn4:Fire(3,2,3) +-- This second one won't trigger the Hi's +conn4:Fire(1,2,3) + +conn5(function() + print("Test 1") +end) + +conn5(function() + print("Test 2") +end) + +conn5(function() + print("Test 3") +end) + +conn5:Fire() + +--multi:mainloop() -- local conn1, conn2, conn3 = multi:newConnection(nil,nil,true), multi:newConnection(), multi:newConnection()