diff --git a/Documentation.html b/Documentation.html new file mode 100644 index 0000000..f876dbe --- /dev/null +++ b/Documentation.html @@ -0,0 +1,415 @@ + + + + + Documentation.html + + + + + + +

Table of contents

+

Multi static variables

multi.Priority_Core — Highest level of pirority that can be given to a process
multi.Priority_High
multi.Priority_Above_Normal
multi.Priority_Normal — The default level of pirority that is given to a process
multi.Priority_Below_Normal
multi.Priority_Low
multi.Priority_Idle — Lowest level of pirority that can be given to a process

Multi Runners

multi:mainloop(TABLE: settings) — This runs the mainloop by having its own internal while loop running
multi:threadloop(TABLE: settings) — This runs the mainloop by having its own internal while loop running, but prioritizes threads over multi-objects
multi:uManager(TABLE: settings) — This runs the mainloop, but does not have its own while loop and thus needs to be within a loop of some kind.

Multi Settings

Note: Most settings have been fined tuned to be at the peak of performance already, however preLoop, protect (Which drastically lowers preformance), and stopOnError should be used freely to fit your needs.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SettingType: defaultPurpose
preLoopfunction: nilThis is a function that is called after all the important components of the library are loaded. This is called once only. The first and only argument passed is a reference to itself.
protectboolean: falseThis runs code within a protected call. To catch when errors happen see built in connections
stopOnErrorboolean: falseThis setting is used with protect. If an object crashes due to some error should it be paused?
prioritynumber: 0This sets the priority scheme. Look at the P-Charts below for examples.
auto_priorityboolean: falseNote: This overrides any value set for priority! If auto priority is enabled then priority scheme 3 is used and processes are considered for “recheck” after a certain amount of time. If a process isn’t taking too long to complete anymore then it will be reset to core, if it starts to take a lot of time all of a sudden it will be set to idle.
auto_stretchnumber: 1For use with auto_priority. Modifies the internal reperesentation of idle time by multiplying multi.Priority_Idle by the value given
auto_delaynumber: 3For use with auto_priority. This changes the time in seconds that process are “rechecked”
auto_lowerboundnumber: multi.Priority_IdleFor use with auto_priority. The lowerbound is what is considered to be idle time. A higher value combined with auto_stretch allows one to fine tune how pirority is managed.

P-Chart: Priority 1

P1 follows a forumla that resembles this: ~n=I*PRank where n is the amount of steps given to an object with PRank and where I is the idle time see chart below. The aim of this priority scheme was to make core objects run fastest while letting idle processes get decent time as well.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Priority: nPRankFormula
Core: 33222697n = ~I*7
High: 28476606n = ~I*6
Above_Normal: 23730505n = ~I*5
Normal: 18984404n = ~I*4
Below_Normal: 14238303n = ~I*3
Low: 9492202n = ~I*2
Idle: 4746101n = ~I*1

General Rule: ~n=I*PRank

P-Chart: Priority 2

P2 follows a formula that resembles this: ~n=n*4 where n starts as the initial idle time, see chart below. The goal of this one was to make core process’ higher while keeping idle process’ low.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
Priority: n
Core: 6700821
High: 1675205
Above_Normal: 418801
Normal: 104700
Below_Normal: 26175
Low: 6543
Idle: 1635

General Rule: ~n=n*4 Where the inital n = I

P-Chart: Priority 3

P3 Ignores using a basic formula and instead bases its processing time on the amount of cpu time is there. If cpu-time is low and a process is set at a lower priority it will get its time reduced. There is no formula, at idle almost all process work at the same speed!

There are 2 settings for this: Core and Idle. If a process takes too long then it is set to idle. Otherwise it will stay core.

Example of settings:

settings = {
+    preLoop = function(m)
+        print("All settings have been loaded!")
+    end,
+    protect = false,
+    stopOnError = false,
+    priority = 0,
+    auto_priority = false,
+    auto_stretch = 1,
+    auto_delay = 3,
+    auto_lowerbound = multi.Priority_Idle
+}
+
+-- Below are how the runners work
+
+multi:mainloop(settings)
+
+-- or
+
+multi:threadloop(settings)
+
+-- or
+
+while true do
+    multi:uManager(settings)
+end
+

Multi constructors - Multi-Objs

Processors
proc = multi:newProcessor(STRING: file [nil])

Non-Actors
timer = multi:newTimer()
conn = multi:newConnection(BOOLEAN: protect [true])
nil = multi:newJob(FUNCTION: func, STRING: name)
range = multi:newRange()
cond = multi:newCondition(FUNCTION: func)

Actors
event = multi:newEvent(FUNCTION: task)
updater = multi:newUpdater(NUMBER: skip [1])
alarm = multi:newAlarm(NUMBER: [0])
loop = multi:newLoop(FUNCTION: func)
tloop = multi:newTLoop(FUNCTION: func ,NUMBER: set [1])
func = multi:newFunction(FUNCTION: func)
step = multi:newStep(NUMBER: start, NUMBER: reset, NUMBER: count [1], NUMBER: skip [0])
tstep = multi:newStep(NUMBER: start, NUMBER: reset, NUMBER: count [1], NUMBER: set [1])
trigger = multi:newTrigger(FUNCTION: func)
stamper = multi:newTimeStamper()
watcher = multi:newWatcher(STRING name)
watcher = multi:newWatcher(TABLE namespace, STRING name)
cobj = multi:newCustomObject(TABLE objRef, BOOLEAN isActor)

Note: A lot of methods will return self as a return. This is due to the ability to chain that was added in 12.x.x

Processor

proc = multi:newProcessor(STRING: file [nil])
Creates a processor runner that acts like the multi runner. Actors and Non-Actors can be created on these objects. Pausing a process pauses all objects that are running on that process.

An optional argument file is used if you want to load a file containing the processor data.
Note: This isn’t portable on all areas where lua is used. Some interperters disable loadstring so it is not encouraged to use the file method for creating processors

loop = Processor:getController() — returns the loop that runs the “runner” that drives this processor
self = Processor:Start() — Starts the processor
self = Processor:Pause() — Pauses the processor
self = Processor:Resume() — Resumes a paused processor
nil = Processor:Destroy() — Destroys the processor and all of the Actors running on it

Example

multi = require("multi")
+proc = multi:newProcessor()
+proc:newTLoop(function() -- create a t loop that runs every second
+    print("Hi!")
+end,1) -- where we set the 1 second
+proc:Start() -- let's start the processor
+multi:mainloop() -- the main runner that drives everything
+

Timers

timer = multi:newTimer()
Creates a timer object that can keep track of time

self = timer:Start() — Starts the timer
time_elapsed = timer:Get() — Returns the time elapsed since timer:Start() was called
boolean = timer:isPaused() — Returns if the timer is paused or not
self = timer:Pause() — Pauses the timer, it skips time that would be counted during the time that it is paused
self = timer:Resume() — Resumes a paused timer. See note below
self = timer:tofile(STRING path) — Saves the object to a file at location path

Note: If a timer was paused after 1 second then resumed a second later and Get() was called a second later, timer would have 2 seconds counted though 3 really have passed.

Connections

Arguable my favorite object in this library, next to threads

conn = multi:newConnection(BOOLEAN: protect [true])
Creates a connection object and defaults to a protective state. All calls will run within pcall()

self = conn:HoldUT(NUMBER n [0]) — Will hold futhur execution of the thread until the connection was triggered. If n is supplied the connection must be triggered n times before it will allow ececution to continue.
self = conn:FConnect(FUNCTION func) — Creates a connection that is forced to execute when Fire() is called. Deprecated
returns or nil = conn:Fire(…) — Triggers the connection with arguments …, “returns” if non-nil is a table containing return values from the triggered connections.
self = conn:Bind(TABLE t) — sets the table to hold the connections. Leaving it alone is best unless you know what you are doing
self = conn:Remove() — removes the bind that was put in place. This will also destroy all connections that existed before.
Link = conn:connect(FUNCTION func, STRING name [nil], NUMBER [#conns+1]) — Connects to the object using function func which will recieve the arguments passed by Fire(…). You can name a connection, which allows you to use conn:getConnection(name). Names must be unique! num is simple the position in the order in which connections are triggered. The return Link is the link to the connected event that was made. You can remove this event or even trigger it specifically if need be.
Link:Fire(…) — Fires the created event
bool = Link:Destroy() — returns true if success.
subConn = conn:getConnection(STRING name, BOOLEAN ingore) — returns the sub connection which matches name.
returns or nil subConn:Fire() — “returns” if non-nil is a table containing return values from the triggered connections.
self = conn:tofile(STRING path) — Saves the object to a file at location path

The connect feature has some syntax sugar to it as seen below
Link = conn(FUNCTION func, STRING name [nil], NUMBER [#conns+1])

Example:

local multi = require("multi")
+-- Let’s create the events
+yawn={}
+OnCustomSafeEvent=multi:newConnection(true) -- lets pcall the calls in case something goes wrong default
+OnCustomEvent=multi:newConnection(false) -- let’s not pcall the calls and let errors happen.
+OnCustomEvent:Bind(yawn) -- create the connection lookup data in yawn
+
+-- Let’s connect to them, a recent update adds a nice syntax to connect to these
+cd1=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
+  print("CSE1",arg1,arg2,...)
+end,"bob") -- let’s give this connection a name
+cd2=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
+  print("CSE2",arg1,arg2,...)
+end,"joe") -- let’s give this connection a name
+cd3=OnCustomSafeEvent:Connect(function(arg1,arg2,...)
+  print("CSE3",arg1,arg2,...)
+end) -- let’s not give this connection a name
+
+-- Using syntax sugar
+OnCustomEvent(function(arg1,arg2,...)
+  print(arg1,arg2,...)
+end)
+
+-- Now within some loop/other object you trigger the connection like
+OnCustomEvent:Fire(1,2,"Hello!!!") -- fire all connections
+
+-- You may have noticed that some events have names! See the following example!
+OnCustomSafeEvent:getConnection("bob"):Fire(1,100,"Bye!") -- fire only bob!
+OnCustomSafeEvent:getConnection("joe"):Fire(1,100,"Hello!") -- fire only joe!!
+OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all!!!
+
+-- Connections have more to them than that though!
+-- As seen above cd1-cd3 these are hooks to the connection object. This allows you to remove a connection
+-- For Example:
+cd1:Remove() -- remove this connection from the master connection object
+print("------")
+OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!!
+-- To remove all connections use:
+OnCustomSafeEvent:Remove()
+print("------")
+OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!!
+

Jobs

nil = multi:newJob(FUNCTION: func, STRING: name) — Adds a job to a queue of jobs that get executed after some time. func is the job that is being ran, name is the name of the job.
nil = multi:setJobSpeed(NUMBER n) — seconds between when each job should be done.
bool, number = multi:hasJobs() — returns true if there are jobs to be processed. And the number of jobs to be processed
num = multi:getJobs() — returns the number of jobs left to be processed.
number = multi:removeJob(name) — removes all jobs of name, name. Returns the number of jobs removed

Note: Jobs may be turned into actual objects in the future.

Example:

local multi = require("multi")
+print(multi:hasJobs())
+multi:setJobSpeed(1) -- set job speed to 1 second
+multi:newJob(function()
+    print("A job!")
+end,"test")
+
+multi:newJob(function()
+    print("Another job!")
+    multi:removeJob("test") -- removes all jobs with name "test"
+end,"test")
+
+multi:newJob(function()
+    print("Almost done!")
+end,"test")
+
+multi:newJob(function()
+    print("Final job!")
+end,"test")
+print(multi:hasJobs())
+print("There are "..multi:getJobs().." jobs in the queue!")
+multi:mainloop()
+

Ranges

Conditions

+ + + + diff --git a/Documentation.md b/Documentation.md index 9b61958..2fa8daa 100644 --- a/Documentation.md +++ b/Documentation.md @@ -1 +1,280 @@ -Im going to write it I promise I will \ No newline at end of file +Table of contents +----------------- +[TOC] +Multi static variables +------------------------ +`multi.Version = "12.3.0"` +`multi.Priority_Core` -- Highest level of pirority that can be given to a process +`multi.Priority_High` +`multi.Priority_Above_Normal` +`multi.Priority_Normal` -- The default level of pirority that is given to a process +`multi.Priority_Below_Normal` +`multi.Priority_Low` +`multi.Priority_Idle` -- Lowest level of pirority that can be given to a process + +Multi Runners +------------- +multi:mainloop(**TABLE:** settings) -- This runs the mainloop by having its own internal while loop running +multi:threadloop(**TABLE:** settings) -- This runs the mainloop by having its own internal while loop running, but prioritizes threads over multi-objects +multi:uManager(**TABLE:** settings) -- This runs the mainloop, but does not have its own while loop and thus needs to be within a loop of some kind. + +Multi Settings +-------------- + +**Note:** Most settings have been fined tuned to be at the peak of performance already, however preLoop, protect (Which drastically lowers preformance), and stopOnError should be used freely to fit your needs. + +| Setting | Type: default | Purpose | +|-|-|-| +| preLoop | function: nil | This is a function that is called after all the important components of the library are loaded. This is called once only. The first and only argument passed is a reference to itself. | +| protect | boolean: false | This runs code within a protected call. To catch when errors happen see built in connections | +| stopOnError | boolean: false | This setting is used with protect. If an object crashes due to some error should it be paused? | +| priority | number: 0 | This sets the priority scheme. Look at the P-Charts below for examples. | +| auto_priority | boolean: false | **Note: This overrides any value set for priority!** If auto priority is enabled then priority scheme 3 is used and processes are considered for "recheck" after a certain amount of time. If a process isn't taking too long to complete anymore then it will be reset to core, if it starts to take a lot of time all of a sudden it will be set to idle. | +| auto_stretch | number: 1 | For use with auto_priority. Modifies the internal reperesentation of idle time by multiplying multi.Priority_Idle by the value given | +| auto_delay | number: 3 | For use with auto_priority. This changes the time in seconds that process are "rechecked" | +| auto_lowerbound | number: multi.Priority_Idle | For use with auto_priority. The lowerbound is what is considered to be idle time. A higher value combined with auto_stretch allows one to fine tune how pirority is managed. | + +# P-Chart: Priority 1 + +P1 follows a forumla that resembles this: ~n=I*PRank where n is the amount of steps given to an object with PRank and where I is the idle time see chart below. The aim of this priority scheme was to make core objects run fastest while letting idle processes get decent time as well. + +| Priority: n | PRank | Formula | +|-|-|-| +| Core: 3322269 | 7 | n = ~**I***7 | +| High: 2847660 | 6 | n = ~**I***6 | +| Above_Normal: 2373050 | 5 | n = ~**I***5 | +| Normal: 1898440 | 4 | n = ~**I***4 | +| Below_Normal: 1423830 | 3 | n = ~**I***3 | +| Low: 949220 | 2 | n = ~**I***2 | +| **I**dle: 474610 | 1 | n = ~**I***1 | + +**General Rule:** ~n=**I***PRank + +# P-Chart: Priority 2 + +P2 follows a formula that resembles this: ~n=n*4 where n starts as the initial idle time, see chart below. The goal of this one was to make core process’ higher while keeping idle process’ low. + +| Priority: n | +|-|-| +| Core: 6700821| +| High: 1675205| +| Above_Normal: 418801| +| Normal: 104700| +| Below_Normal: 26175| +| Low: 6543| +| **I**dle: 1635| + +**General Rule:** `~n=n*4` Where the inital n = **I** + +# P-Chart: Priority 3 +P3 Ignores using a basic formula and instead bases its processing time on the amount of cpu time is there. If cpu-time is low and a process is set at a lower priority it will get its time reduced. There is no formula, at idle almost all process work at the same speed! + +There are 2 settings for this: Core and Idle. If a process takes too long then it is set to idle. Otherwise it will stay core. + +Example of settings: +```lua +settings = { + preLoop = function(m) + print("All settings have been loaded!") + end, + protect = false, + stopOnError = false, + priority = 0, + auto_priority = false, + auto_stretch = 1, + auto_delay = 3, + auto_lowerbound = multi.Priority_Idle +} + +-- Below are how the runners work + +multi:mainloop(settings) + +-- or + +multi:threadloop(settings) + +-- or + +while true do + multi:uManager(settings) +end +``` + + +Multi constructors - Multi-Objs +------------------------------- +**Processors** +proc = multi:newProcessor(**STRING:** file [**nil**]) + +**Non-Actors** +timer = multi:newTimer() +conn = multi:newConnection(**BOOLEAN:** protect [**true**]) +nil = multi:newJob(**FUNCTION:** func, **STRING:** name) +range = multi:newRange() +cond = multi:newCondition(**FUNCTION:** func) + +**Actors** +event = multi:newEvent(**FUNCTION:** task) +updater = multi:newUpdater(**NUMBER:** skip [**1**]) +alarm = multi:newAlarm(**NUMBER:** [**0**]) +loop = multi:newLoop(**FUNCTION:** func) +tloop = multi:newTLoop(**FUNCTION:** func ,**NUMBER:** set [**1**]) +func = multi:newFunction(**FUNCTION:** func) +step = multi:newStep(**NUMBER:** start, **NUMBER:** reset, **NUMBER:** count [**1**], **NUMBER:** skip [**0**]) +tstep = multi:newStep(**NUMBER:** start, **NUMBER:** reset, **NUMBER:** count [**1**], **NUMBER:** set [**1**]) +trigger = multi:newTrigger(**FUNCTION:** func) +stamper = multi:newTimeStamper() +watcher = multi:newWatcher(**STRING** name) +watcher = multi:newWatcher(**TABLE** namespace, **STRING** name) +cobj = multi:newCustomObject(**TABLE** objRef, **BOOLEAN** isActor) + +Note: A lot of methods will return self as a return. This is due to the ability to chain that was added in 12.x.x + +Processor +--------- +proc = multi:newProcessor(**STRING:** file [**nil**]) +Creates a processor runner that acts like the multi runner. Actors and Non-Actors can be created on these objects. Pausing a process pauses all objects that are running on that process. + +An optional argument file is used if you want to load a file containing the processor data. +Note: This isn't portable on all areas where lua is used. Some interperters disable loadstring so it is not encouraged to use the file method for creating processors + +loop = Processor:getController() -- returns the loop that runs the "runner" that drives this processor +**self** = Processor:Start() -- Starts the processor +**self** = Processor:Pause() -- Pauses the processor +**self** = Processor:Resume() -- Resumes a paused processor +nil = Processor:Destroy() -- Destroys the processor and all of the Actors running on it + +Example +```lua +multi = require("multi") +proc = multi:newProcessor() +proc:newTLoop(function() -- create a t loop that runs every second + print("Hi!") +end,1) -- where we set the 1 second +proc:Start() -- let's start the processor +multi:mainloop() -- the main runner that drives everything +``` + +Timers +------ +timer = multi:newTimer() +Creates a timer object that can keep track of time + +**self** = timer:Start() -- Starts the timer +time_elapsed = timer:Get() -- Returns the time elapsed since timer:Start() was called +boolean = timer:isPaused() -- Returns if the timer is paused or not +**self** = timer:Pause() -- Pauses the timer, it skips time that would be counted during the time that it is paused +**self** = timer:Resume() -- Resumes a paused timer. **See note below** +**self** = timer:tofile(**STRING** path) -- Saves the object to a file at location path + +**Note:** If a timer was paused after 1 second then resumed a second later and Get() was called a second later, timer would have 2 seconds counted though 3 really have passed. + +Connections +----------- +Arguable my favorite object in this library, next to threads + +conn = multi:newConnection(**BOOLEAN:** protect [**true**]) +Creates a connection object and defaults to a protective state. All calls will run within pcall() + +**self** = conn:HoldUT(**NUMBER** n [**0**]) -- Will hold futhur execution of the thread until the connection was triggered. If n is supplied the connection must be triggered n times before it will allow ececution to continue. +**self** = conn:FConnect(**FUNCTION** func) -- Creates a connection that is forced to execute when Fire() is called. **Deprecated** +returns or nil = conn:Fire(...) -- Triggers the connection with arguments ..., "returns" if non-nil is a table containing return values from the triggered connections. +**self** = conn:Bind(**TABLE** t) -- sets the table to hold the connections. Leaving it alone is best unless you know what you are doing +**self** = conn:Remove() -- removes the bind that was put in place. This will also destroy all connections that existed before. +Link = conn:connect(**FUNCTION** func, **STRING** name [**nil**], **NUMBER** [**#conns+1**]) -- Connects to the object using function func which will recieve the arguments passed by Fire(...). You can name a connection, which allows you to use conn:getConnection(name). Names must be unique! num is simple the position in the order in which connections are triggered. The return Link is the link to the connected event that was made. You can remove this event or even trigger it specifically if need be. +Link:Fire(...) -- Fires the created event +bool = Link:Destroy() -- returns true if success. +subConn = conn:getConnection(**STRING** name, **BOOLEAN** ingore) -- returns the sub connection which matches name. +returns or nil subConn:Fire() -- "returns" if non-nil is a table containing return values from the triggered connections. +**self** = conn:tofile(**STRING** path) -- Saves the object to a file at location path + +The connect feature has some syntax sugar to it as seen below +Link = conn(**FUNCTION** func, **STRING** name [**nil**], **NUMBER** [**#conns+1**]) + +Example: +```lua +local multi = require("multi") +-- Let’s create the events +yawn={} +OnCustomSafeEvent=multi:newConnection(true) -- lets pcall the calls in case something goes wrong default +OnCustomEvent=multi:newConnection(false) -- let’s not pcall the calls and let errors happen. +OnCustomEvent:Bind(yawn) -- create the connection lookup data in yawn + +-- Let’s connect to them, a recent update adds a nice syntax to connect to these +cd1=OnCustomSafeEvent:Connect(function(arg1,arg2,...) + print("CSE1",arg1,arg2,...) +end,"bob") -- let’s give this connection a name +cd2=OnCustomSafeEvent:Connect(function(arg1,arg2,...) + print("CSE2",arg1,arg2,...) +end,"joe") -- let’s give this connection a name +cd3=OnCustomSafeEvent:Connect(function(arg1,arg2,...) + print("CSE3",arg1,arg2,...) +end) -- let’s not give this connection a name + +-- Using syntax sugar +OnCustomEvent(function(arg1,arg2,...) + print(arg1,arg2,...) +end) + +-- Now within some loop/other object you trigger the connection like +OnCustomEvent:Fire(1,2,"Hello!!!") -- fire all connections + +-- You may have noticed that some events have names! See the following example! +OnCustomSafeEvent:getConnection("bob"):Fire(1,100,"Bye!") -- fire only bob! +OnCustomSafeEvent:getConnection("joe"):Fire(1,100,"Hello!") -- fire only joe!! +OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all!!! + +-- Connections have more to them than that though! +-- As seen above cd1-cd3 these are hooks to the connection object. This allows you to remove a connection +-- For Example: +cd1:Remove() -- remove this connection from the master connection object +print("------") +OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!! +-- To remove all connections use: +OnCustomSafeEvent:Remove() +print("------") +OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!! +``` + +Jobs +---- +nil = multi:newJob(**FUNCTION:** func, **STRING:** name) -- Adds a job to a queue of jobs that get executed after some time. func is the job that is being ran, name is the name of the job. +nil = multi:setJobSpeed(**NUMBER** n) -- seconds between when each job should be done. +bool, number = multi:hasJobs() -- returns true if there are jobs to be processed. And the number of jobs to be processed +num = multi:getJobs() -- returns the number of jobs left to be processed. +number = multi:removeJob(name) -- removes all jobs of name, name. Returns the number of jobs removed + +**Note:** Jobs may be turned into actual objects in the future. + +Example: +```lua +local multi = require("multi") +print(multi:hasJobs()) +multi:setJobSpeed(1) -- set job speed to 1 second +multi:newJob(function() + print("A job!") +end,"test") + +multi:newJob(function() + print("Another job!") + multi:removeJob("test") -- removes all jobs with name "test" +end,"test") + +multi:newJob(function() + print("Almost done!") +end,"test") + +multi:newJob(function() + print("Final job!") +end,"test") +print(multi:hasJobs()) +print("There are "..multi:getJobs().." jobs in the queue!") +multi:mainloop() +``` + +Ranges +------ + +Conditions +---------- \ No newline at end of file diff --git a/README.md b/README.md index 68a9cd1..a2c970d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# multi Version: 12.2.2 Some more bug fixes +# multi Version: 12.3.0 Documentation and bug fixes My multitasking library for lua. It is a pure lua binding, if you ignore the integrations and the love2d compat. If you find any bugs or have any issues, please let me know . **If you don't see a table of contents try using the ReadMe.html file. It is easier to navigate than readme**
diff --git a/changes.md b/changes.md index 1ed4365..f1b4f67 100644 --- a/changes.md +++ b/changes.md @@ -1,5 +1,11 @@ #Changes [TOC] +Update 12.3.0 So you documented it finally +------------- +Fixed: Tons of bugs, I actually went through the entire library and did a full test of everything while writing the documentation. +Changed: A few things to make things more clear when using the library +Added: ... + Update 12.2.2 Time for some more bug fixes! ------------- Fixed: multi.Stop() not actually stopping due to the new pirority management scheme and preformance boost changes. diff --git a/multi/init.lua b/multi/init.lua index b75d144..654d469 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -24,8 +24,8 @@ SOFTWARE. local bin = pcall(require,"bin") local multi = {} local clock = os.clock -multi.Version = "12.2.2" -multi._VERSION = "12.2.2" +multi.Version = "12.3.0" +multi._VERSION = "12.3.0" multi.stage = "stable" multi.__index = multi multi.Mainloop = {} @@ -277,7 +277,7 @@ function multi.timer(func,...) return t,unpack(args) end function multi:IsAnActor() - return ({watcher=true,tstep=true,step=true,updater=true,loop=true,alarm=true,event=true})[self.Type] + return self.Act~=nil end function multi:OnMainConnect(func) table.insert(self.func,func) @@ -301,11 +301,14 @@ function multi:getJobs() return #self.Jobs end function multi:removeJob(name) + local count = 0 for i=#self.Jobs,1,-1 do if self.Jobs[i][2]==name then table.remove(self.Jobs,i) + count = count + 1 end end + return count end function multi:FreeMainEvent() self.func={} @@ -469,7 +472,7 @@ function multi:newBase(ins) end return c end -function multi:newProcess(file) +function multi:newProcessor(file) if not(self.Type=='mainprocess') then error('Can only create an interface on the multi obj') return false end local c = {} setmetatable(c, self) @@ -510,14 +513,15 @@ function multi:newProcess(file) return self end function c:Remove() - self:Destroy() + self:__Destroy() self.l:Destroy() - return self end if file then self.Cself=c loadstring('local process=multi.Cself '..io.open(file,'rb'):read('*all'))() end + self.__Destroy = self.Destroy + self.Destroy = Remove self:create(c) --~ c:IngoreObject() return c @@ -599,7 +603,7 @@ function multi:newConnection(protect) function c:getConnection(name,ingore) if ingore then return self.connections[name] or { - Fire=function() end -- if the connection doesn't exist lets call all of them or silently ignore + Fire=function() return end -- if the connection doesn't exist lets call all of them or silently ignore } else return self.connections[name] or self @@ -753,476 +757,10 @@ function multi:newCondition(func) end multi.OnPreLoad=multi:newConnection() multi.NewCondition=multi.newCondition -function multi:threadloop(settings) - multi.scheduler:Destroy() -- destroy is an interesting thing... if you dont set references to nil, then you only remove it from the mainloop - local Threads=multi:linkDomain("Threads") - local Globals=multi:linkDomain("Globals") - local counter=0 - local tick = 0 - while true do - tick = tick + 1 - if tick == 1024 then - tick = 0 - multi:uManager(settings) - end - counter=counter+1 - for i=#Threads,1,-1 do - ret={} - if coroutine.status(Threads[i].thread)=="dead" then - table.remove(Threads,i) - else - if Threads[i].timer:Get()>=Threads[i].sleep then - if Threads[i].firstRunDone==false then - Threads[i].firstRunDone=true - Threads[i].timer:Start() - _,ret=coroutine.resume(Threads[i].thread,Threads[i].ref) - else - _,ret=coroutine.resume(Threads[i].thread,Globals) - end - if _==false then - multi.OnError:Fire(Threads[i],"Error in thread: <"..Threads[i].Name.."> "..ret) - end - if ret==true or ret==false then - ret={} - end - end - if ret then - if ret[1]=="_kill_" then - table.remove(Threads,i) - elseif ret[1]=="_sleep_" then - Threads[i].timer:Reset() - Threads[i].sleep=ret[2] - elseif ret[1]=="_skip_" then - Threads[i].timer:Reset() - Threads[i].sleep=math.huge - local event=multi:newEvent(function(evnt) return counter>=evnt.counter end) - event.link=Threads[i] - event.counter=counter+ret[2] - event:OnEvent(function(evnt) - evnt.link.sleep=0 - end) - elseif ret[1]=="_hold_" then - Threads[i].timer:Reset() - Threads[i].sleep=math.huge - local event=multi:newEvent(ret[2]) - event.link=Threads[i] - event:OnEvent(function(evnt) - evnt.link.sleep=0 - end) - elseif ret.Name then - Globals[ret.Name]=ret.Value - end - end - end - end - end -end -function multi:mainloop(settings) - multi.defaultSettings = settings or multi.defaultSettings - self.uManager=self.uManagerRef - multi.OnPreLoad:Fire() - local p_c,p_h,p_an,p_n,p_bn,p_l,p_i = self.Priority_Core,self.Priority_High,self.Priority_Above_Normal,self.Priority_Normal,self.Priority_Below_Normal,self.Priority_Low,self.Priority_Idle - local P_LB = p_i - if not isRunning then - local protect = false - local priority = false - local stopOnError = true - local delay = 3 - if settings then - priority = settings.priority - if settings.auto_priority then - priority = -1 - end - if settings.preLoop then - settings.preLoop(self) - end - if settings.stopOnError then - stopOnError = settings.stopOnError - end - if settings.auto_stretch then - p_i = p_i * settings.auto_stretch - end - if settings.auto_delay then - delay = settings.auto_delay - end - if settings.auto_lowerbound then - P_LB = settings.auto_lowerbound - end - protect = settings.protect - end - local t,tt = clock(),0 - isRunning=true - local lastTime = clock() - rawset(self,'Start',clock()) - mainloopActive = true - local Loop=self.Mainloop - local PS=self - local PStep = 1 - local autoP = 0 - local solid - local sRef - while mainloopActive do - if ncount ~= 0 then - for i = 1, ncount do - next[i]() - end - ncount = 0 - end - if priority == 1 then - for _D=#Loop,1,-1 do - for P=1,7 do - if Loop[_D] then - if (PS.PList[P])%Loop[_D].Priority==0 then - if Loop[_D].Active then - self.CID=_D - if not protect then - Loop[_D]:Act() - else - local status, err=pcall(Loop[_D].Act,Loop[_D]) - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - end - elseif priority == 2 then - for _D=#Loop,1,-1 do - if Loop[_D] then - if (PStep)%Loop[_D].Priority==0 then - if Loop[_D].Active then - self.CID=_D - if not protect then - Loop[_D]:Act() - else - local status, err=pcall(Loop[_D].Act,Loop[_D]) - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - PStep=PStep+1 - if PStep==p_i then - PStep=0 - end - elseif priority == 3 then - tt = clock()-t - t = clock() - for _D=#Loop,1,-1 do - if Loop[_D] then - if Loop[_D].Priority == p_c or (Loop[_D].Priority == p_h and tt<.5) or (Loop[_D].Priority == p_an and tt<.125) or (Loop[_D].Priority == p_n and tt<.063) or (Loop[_D].Priority == p_bn and tt<.016) or (Loop[_D].Priority == p_l and tt<.003) or (Loop[_D].Priority == p_i and tt<.001) then - if Loop[_D].Active then - self.CID=_D - if not protect then - Loop[_D]:Act() - else - local status, err=pcall(Loop[_D].Act,Loop[_D]) - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - elseif priority == -1 then - for _D=#Loop,1,-1 do - sRef = Loop[_D] - if Loop[_D] then - if (sRef.Priority == p_c) or PStep==0 then - if sRef.Active then - self.CID=_D - if not protect then - if sRef.solid then - sRef:Act() - solid = true - else - time = multi.timer(sRef.Act,sRef) - sRef.solid = true - solid = false - end - if Loop[_D] and not solid then - if time == 0 then - Loop[_D].Priority = p_c - else - Loop[_D].Priority = P_LB - end - end - else - if Loop[_D].solid then - Loop[_D]:Act() - solid = true - else - time, status, err=multi.timer(pcall,Loop[_D].Act,Loop[_D]) - Loop[_D].solid = true - solid = false - end - if Loop[_D] and not solid then - if time == 0 then - Loop[_D].Priority = p_c - else - Loop[_D].Priority = P_LB - end - end - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - PStep=PStep+1 - if PStep>p_i then - PStep=0 - if clock()-lastTime>delay then - lastTime = clock() - for i = 1,#Loop do - Loop[i]:ResetPriority() - end - end - end - else - for _D=#Loop,1,-1 do - if Loop[_D] then - if Loop[_D].Active then - self.CID=_D - if not protect then - Loop[_D]:Act() - else - local status, err=pcall(Loop[_D].Act,Loop[_D]) - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - end - else - return "Already Running!" - end -end -function multi:uManager(settings) - multi.OnPreLoad:Fire() - multi.defaultSettings = settings or multi.defaultSettings - self.t,self.tt = clock(),0 - if settings then - priority = settings.priority - if settings.auto_priority then - priority = -1 - end - if settings.preLoop then - settings.preLoop(self) - end - if settings.stopOnError then - stopOnError = settings.stopOnError - end - multi.defaultSettings.p_i = self.Priority_Idle - if settings.auto_stretch then - multi.defaultSettings.p_i = settings.auto_stretch*self.Priority_Idle - end - multi.defaultSettings.delay = settings.auto_delay or 3 - multi.defaultSettings.auto_lowerbound = settings.auto_lowerbound or self.Priority_Idle - protect = settings.protect - end - self.uManager=self.uManagerRef -end -function multi:uManagerRef(settings) - if self.Active then - if ncount ~= 0 then - for i = 1, ncount do - next[i]() - end - ncount = 0 - end - local Loop=self.Mainloop - local PS=self - if multi.defaultSettings.priority==1 then - for _D=#Loop,1,-1 do - for P=1,7 do - if Loop[_D] then - if (PS.PList[P])%Loop[_D].Priority==0 then - if Loop[_D].Active then - self.CID=_D - if not multi.defaultSettings.protect then - Loop[_D]:Act() - else - local status, err=pcall(Loop[_D].Act,Loop[_D]) - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if multi.defaultSettings.stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - end - elseif multi.defaultSettings.priority==2 then - for _D=#Loop,1,-1 do - if Loop[_D] then - if (PS.PStep)%Loop[_D].Priority==0 then - if Loop[_D].Active then - self.CID=_D - if not multi.defaultSettings.protect then - Loop[_D]:Act() - else - local status, err=pcall(Loop[_D].Act,Loop[_D]) - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if multi.defaultSettings.stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - PS.PStep=PS.PStep+1 - if PS.PStep>self.Priority_Idle then - PS.PStep=0 - end - elseif priority == 3 then - self.tt = clock()-self.t - self.t = clock() - for _D=#Loop,1,-1 do - if Loop[_D] then - if Loop[_D].Priority == self.Priority_Core or (Loop[_D].Priority == self.Priority_High and tt<.5) or (Loop[_D].Priority == self.Priority_Above_Normal and tt<.125) or (Loop[_D].Priority == self.Priority_Normal and tt<.063) or (Loop[_D].Priority == self.Priority_Below_Normal and tt<.016) or (Loop[_D].Priority == self.Priority_Low and tt<.003) or (Loop[_D].Priority == self.Priority_Idle and tt<.001) then - if Loop[_D].Active then - self.CID=_D - if not protect then - Loop[_D]:Act() - else - local status, err=pcall(Loop[_D].Act,Loop[_D]) - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if multi.defaultSettings.stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - elseif priority == -1 then - for _D=#Loop,1,-1 do - local sRef = Loop[_D] - if Loop[_D] then - if (sRef.Priority == self.Priority_Core) or PStep==0 then - if sRef.Active then - self.CID=_D - if not protect then - if sRef.solid then - sRef:Act() - solid = true - else - time = multi.timer(sRef.Act,sRef) - sRef.solid = true - solid = false - end - if Loop[_D] and not solid then - if time == 0 then - Loop[_D].Priority = self.Priority_Core - else - Loop[_D].Priority = multi.defaultSettings.auto_lowerbound - end - end - else - if Loop[_D].solid then - Loop[_D]:Act() - solid = true - else - time, status, err=multi.timer(pcall,Loop[_D].Act,Loop[_D]) - Loop[_D].solid = true - solid = false - end - if Loop[_D] and not solid then - if time == 0 then - Loop[_D].Priority = self.Priority_Core - else - Loop[_D].Priority = multi.defaultSettings.auto_lowerbound - end - end - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - if multi.defaultSettings.stopOnError then - Loop[_D]:Destroy() - end - end - end - end - end - end - end - self.PStep=self.PStep+1 - if self.PStep>multi.defaultSettings.p_i then - self.PStep=0 - if clock()-self.lastTime>multi.defaultSettings.delay then - self.lastTime = clock() - for i = 1,#Loop do - Loop[i]:ResetPriority() - end - end - end - else - for _D=#Loop,1,-1 do - if Loop[_D] then - if Loop[_D].Active then - self.CID=_D - if not multi.defaultSettings.protect then - Loop[_D]:Act() - else - local status, err=pcall(Loop[_D].Act,Loop[_D]) - if err then - Loop[_D].error=err - self.OnError:Fire(Loop[_D],err) - end - end - end - end - end - end - end -end --Core Actors -function multi:newCustomObject(objRef,t) +function multi:newCustomObject(objRef,isActor) local c={} - if t=='process' then + if isActor then c=self:newBase() if type(objRef)=='table' then table.merge(c,objRef) @@ -2327,6 +1865,473 @@ function multi:newThreadedEvent(name,task) self:create(c) return c end +-- Multi runners +function multi:threadloop(settings) + multi.scheduler:Destroy() -- destroy is an interesting thing... if you dont set references to nil, then you only remove it from the mainloop + local Threads=multi:linkDomain("Threads") + local Globals=multi:linkDomain("Globals") + local counter=0 + local tick = 0 + while true do + tick = tick + 1 + if tick == 1024 then + tick = 0 + multi:uManager(settings) + end + counter=counter+1 + for i=#Threads,1,-1 do + ret={} + if coroutine.status(Threads[i].thread)=="dead" then + table.remove(Threads,i) + else + if Threads[i].timer:Get()>=Threads[i].sleep then + if Threads[i].firstRunDone==false then + Threads[i].firstRunDone=true + Threads[i].timer:Start() + _,ret=coroutine.resume(Threads[i].thread,Threads[i].ref) + else + _,ret=coroutine.resume(Threads[i].thread,Globals) + end + if _==false then + multi.OnError:Fire(Threads[i],"Error in thread: <"..Threads[i].Name.."> "..ret) + end + if ret==true or ret==false then + ret={} + end + end + if ret then + if ret[1]=="_kill_" then + table.remove(Threads,i) + elseif ret[1]=="_sleep_" then + Threads[i].timer:Reset() + Threads[i].sleep=ret[2] + elseif ret[1]=="_skip_" then + Threads[i].timer:Reset() + Threads[i].sleep=math.huge + local event=multi:newEvent(function(evnt) return counter>=evnt.counter end) + event.link=Threads[i] + event.counter=counter+ret[2] + event:OnEvent(function(evnt) + evnt.link.sleep=0 + end) + elseif ret[1]=="_hold_" then + Threads[i].timer:Reset() + Threads[i].sleep=math.huge + local event=multi:newEvent(ret[2]) + event.link=Threads[i] + event:OnEvent(function(evnt) + evnt.link.sleep=0 + end) + elseif ret.Name then + Globals[ret.Name]=ret.Value + end + end + end + end + end +end +function multi:mainloop(settings) + multi.defaultSettings = settings or multi.defaultSettings + self.uManager=self.uManagerRef + multi.OnPreLoad:Fire() + local p_c,p_h,p_an,p_n,p_bn,p_l,p_i = self.Priority_Core,self.Priority_High,self.Priority_Above_Normal,self.Priority_Normal,self.Priority_Below_Normal,self.Priority_Low,self.Priority_Idle + local P_LB = p_i + if not isRunning then + local protect = false + local priority = false + local stopOnError = true + local delay = 3 + if settings then + priority = settings.priority + if settings.auto_priority then + priority = -1 + end + if settings.preLoop then + settings.preLoop(self) + end + if settings.stopOnError then + stopOnError = settings.stopOnError + end + if settings.auto_stretch then + p_i = p_i * settings.auto_stretch + end + if settings.auto_delay then + delay = settings.auto_delay + end + if settings.auto_lowerbound then + P_LB = settings.auto_lowerbound + end + protect = settings.protect + end + local t,tt = clock(),0 + isRunning=true + local lastTime = clock() + rawset(self,'Start',clock()) + mainloopActive = true + local Loop=self.Mainloop + local PS=self + local PStep = 1 + local autoP = 0 + local solid + local sRef + while mainloopActive do + if ncount ~= 0 then + for i = 1, ncount do + next[i]() + end + ncount = 0 + end + if priority == 1 then + for _D=#Loop,1,-1 do + for P=1,7 do + if Loop[_D] then + if (PS.PList[P])%Loop[_D].Priority==0 then + if Loop[_D].Active then + self.CID=_D + if not protect then + Loop[_D]:Act() + else + local status, err=pcall(Loop[_D].Act,Loop[_D]) + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + end + elseif priority == 2 then + for _D=#Loop,1,-1 do + if Loop[_D] then + if (PStep)%Loop[_D].Priority==0 then + if Loop[_D].Active then + self.CID=_D + if not protect then + Loop[_D]:Act() + else + local status, err=pcall(Loop[_D].Act,Loop[_D]) + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + PStep=PStep+1 + if PStep==p_i then + PStep=0 + end + elseif priority == 3 then + tt = clock()-t + t = clock() + for _D=#Loop,1,-1 do + if Loop[_D] then + if Loop[_D].Priority == p_c or (Loop[_D].Priority == p_h and tt<.5) or (Loop[_D].Priority == p_an and tt<.125) or (Loop[_D].Priority == p_n and tt<.063) or (Loop[_D].Priority == p_bn and tt<.016) or (Loop[_D].Priority == p_l and tt<.003) or (Loop[_D].Priority == p_i and tt<.001) then + if Loop[_D].Active then + self.CID=_D + if not protect then + Loop[_D]:Act() + else + local status, err=pcall(Loop[_D].Act,Loop[_D]) + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + elseif priority == -1 then + for _D=#Loop,1,-1 do + sRef = Loop[_D] + if Loop[_D] then + if (sRef.Priority == p_c) or PStep==0 then + if sRef.Active then + self.CID=_D + if not protect then + if sRef.solid then + sRef:Act() + solid = true + else + time = multi.timer(sRef.Act,sRef) + sRef.solid = true + solid = false + end + if Loop[_D] and not solid then + if time == 0 then + Loop[_D].Priority = p_c + else + Loop[_D].Priority = P_LB + end + end + else + if Loop[_D].solid then + Loop[_D]:Act() + solid = true + else + time, status, err=multi.timer(pcall,Loop[_D].Act,Loop[_D]) + Loop[_D].solid = true + solid = false + end + if Loop[_D] and not solid then + if time == 0 then + Loop[_D].Priority = p_c + else + Loop[_D].Priority = P_LB + end + end + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + PStep=PStep+1 + if PStep>p_i then + PStep=0 + if clock()-lastTime>delay then + lastTime = clock() + for i = 1,#Loop do + Loop[i]:ResetPriority() + end + end + end + else + for _D=#Loop,1,-1 do + if Loop[_D] then + if Loop[_D].Active then + self.CID=_D + if not protect then + Loop[_D]:Act() + else + local status, err=pcall(Loop[_D].Act,Loop[_D]) + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + end + else + return "Already Running!" + end +end +function multi:uManager(settings) + multi.OnPreLoad:Fire() + multi.defaultSettings = settings or multi.defaultSettings + self.t,self.tt = clock(),0 + if settings then + priority = settings.priority + if settings.auto_priority then + priority = -1 + end + if settings.preLoop then + settings.preLoop(self) + end + if settings.stopOnError then + stopOnError = settings.stopOnError + end + multi.defaultSettings.p_i = self.Priority_Idle + if settings.auto_stretch then + multi.defaultSettings.p_i = settings.auto_stretch*self.Priority_Idle + end + multi.defaultSettings.delay = settings.auto_delay or 3 + multi.defaultSettings.auto_lowerbound = settings.auto_lowerbound or self.Priority_Idle + protect = settings.protect + end + self.uManager=self.uManagerRef +end +function multi:uManagerRef(settings) + if self.Active then + if ncount ~= 0 then + for i = 1, ncount do + next[i]() + end + ncount = 0 + end + local Loop=self.Mainloop + local PS=self + if multi.defaultSettings.priority==1 then + for _D=#Loop,1,-1 do + for P=1,7 do + if Loop[_D] then + if (PS.PList[P])%Loop[_D].Priority==0 then + if Loop[_D].Active then + self.CID=_D + if not multi.defaultSettings.protect then + Loop[_D]:Act() + else + local status, err=pcall(Loop[_D].Act,Loop[_D]) + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if multi.defaultSettings.stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + end + elseif multi.defaultSettings.priority==2 then + for _D=#Loop,1,-1 do + if Loop[_D] then + if (PS.PStep)%Loop[_D].Priority==0 then + if Loop[_D].Active then + self.CID=_D + if not multi.defaultSettings.protect then + Loop[_D]:Act() + else + local status, err=pcall(Loop[_D].Act,Loop[_D]) + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if multi.defaultSettings.stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + PS.PStep=PS.PStep+1 + if PS.PStep>self.Priority_Idle then + PS.PStep=0 + end + elseif priority == 3 then + self.tt = clock()-self.t + self.t = clock() + for _D=#Loop,1,-1 do + if Loop[_D] then + if Loop[_D].Priority == self.Priority_Core or (Loop[_D].Priority == self.Priority_High and tt<.5) or (Loop[_D].Priority == self.Priority_Above_Normal and tt<.125) or (Loop[_D].Priority == self.Priority_Normal and tt<.063) or (Loop[_D].Priority == self.Priority_Below_Normal and tt<.016) or (Loop[_D].Priority == self.Priority_Low and tt<.003) or (Loop[_D].Priority == self.Priority_Idle and tt<.001) then + if Loop[_D].Active then + self.CID=_D + if not protect then + Loop[_D]:Act() + else + local status, err=pcall(Loop[_D].Act,Loop[_D]) + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if multi.defaultSettings.stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + elseif priority == -1 then + for _D=#Loop,1,-1 do + local sRef = Loop[_D] + if Loop[_D] then + if (sRef.Priority == self.Priority_Core) or PStep==0 then + if sRef.Active then + self.CID=_D + if not protect then + if sRef.solid then + sRef:Act() + solid = true + else + time = multi.timer(sRef.Act,sRef) + sRef.solid = true + solid = false + end + if Loop[_D] and not solid then + if time == 0 then + Loop[_D].Priority = self.Priority_Core + else + Loop[_D].Priority = multi.defaultSettings.auto_lowerbound + end + end + else + if Loop[_D].solid then + Loop[_D]:Act() + solid = true + else + time, status, err=multi.timer(pcall,Loop[_D].Act,Loop[_D]) + Loop[_D].solid = true + solid = false + end + if Loop[_D] and not solid then + if time == 0 then + Loop[_D].Priority = self.Priority_Core + else + Loop[_D].Priority = multi.defaultSettings.auto_lowerbound + end + end + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + if multi.defaultSettings.stopOnError then + Loop[_D]:Destroy() + end + end + end + end + end + end + end + self.PStep=self.PStep+1 + if self.PStep>multi.defaultSettings.p_i then + self.PStep=0 + if clock()-self.lastTime>multi.defaultSettings.delay then + self.lastTime = clock() + for i = 1,#Loop do + Loop[i]:ResetPriority() + end + end + end + else + for _D=#Loop,1,-1 do + if Loop[_D] then + if Loop[_D].Active then + self.CID=_D + if not multi.defaultSettings.protect then + Loop[_D]:Act() + else + local status, err=pcall(Loop[_D].Act,Loop[_D]) + if err then + Loop[_D].error=err + self.OnError:Fire(Loop[_D],err) + end + end + end + end + end + end + end +end -- State Saving Stuff function multi:IngoreObject() self.Ingore=true diff --git a/test.lua b/test.lua index e1e1cba..efb6804 100644 --- a/test.lua +++ b/test.lua @@ -1,36 +1,23 @@ package.path="?/init.lua;?.lua;"..package.path -multi = require("multi") -local GLOBAL, THREAD = require("multi.integration.lanesManager").init() -conn = multi:newSystemThreadedConnection("test"):init() -multi:newSystemThread("Work",function() - local multi = require("multi") - conn = THREAD.waitFor("test"):init() - conn(function(...) - print(...) - end) - multi:newTLoop(function() - conn:Fire("meh2") - end,1) - multi:mainloop() -end) -multi.OnError(function(a,b,c) - print(c) -end) -multi:newTLoop(function() - conn:Fire("meh") -end,1) -conn(function(...) - print(">",...) -end) +local multi = require("multi") +print(multi:hasJobs()) +multi:setJobSpeed(1) -- set job speed to 1 second +multi:newJob(function() + print("A job!") +end,"test") ---~ jq = multi:newSystemThreadedJobQueue() ---~ jq:registerJob("test",function(a) ---~ return "Hello",a ---~ end) ---~ jq.OnJobCompleted(function(ID,...) ---~ print(ID,...) ---~ end) ---~ for i=1,16 do ---~ jq:pushJob("test",5) ---~ end +multi:newJob(function() + print("Another job!") + multi:removeJob("test") -- removes all jobs with name "test" +end,"test") + +multi:newJob(function() + print("Almost done!") +end,"test") + +multi:newJob(function() + print("Final job!") +end,"test") +print(multi:hasJobs()) +print("There are "..multi:getJobs().." jobs in the queue!") multi:mainloop()