From dfc75fa56c32d9c947623912eedbd762dc29d14c Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Sun, 23 Sep 2018 23:14:49 -0400 Subject: [PATCH 01/20] Started working on documentation --- Documentation.html | 415 ++++++++++++++++++++ Documentation.md | 281 ++++++++++++- README.md | 2 +- changes.md | 6 + multi/init.lua | 955 +++++++++++++++++++++++---------------------- test.lua | 53 +-- 6 files changed, 1202 insertions(+), 510 deletions(-) create mode 100644 Documentation.html 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() -- 2.43.0 From 86845f423088655fb7bffafe28b2bf77afaf86fa Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Wed, 26 Sep 2018 13:02:19 -0400 Subject: [PATCH 02/20] well version will now be 13.0.0 --- Documentation.md | 234 +++++++++++++++++++++++++++++++++++++---------- changes.md | 17 +++- multi/init.lua | 58 +++--------- test.lua | 25 +---- 4 files changed, 213 insertions(+), 121 deletions(-) diff --git a/Documentation.md b/Documentation.md index 2fa8daa..204a522 100644 --- a/Documentation.md +++ b/Documentation.md @@ -1,9 +1,11 @@ +Mulit Version: 12.3.0 + Table of contents ----------------- [TOC] Multi static variables ------------------------ -`multi.Version = "12.3.0"` +`multi.Version` -- The current version of the library `multi.Priority_Core` -- Highest level of pirority that can be given to a process `multi.Priority_High` `multi.Priority_Above_Normal` @@ -14,9 +16,9 @@ Multi static variables 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: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 -------------- @@ -105,45 +107,43 @@ end Multi constructors - Multi-Objs ------------------------------- **Processors** -proc = multi:newProcessor(**STRING:** file [**nil**]) +`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) +`timer = multi:newTimer()` +`conn = multi:newConnection([BOOLEAN protect true])` +`nil = multi:newJob(FUNCTION func, STRING name)` +`func = multi:newFunction(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) +`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])` +`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**]) +`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 +`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 @@ -174,23 +174,22 @@ Connections ----------- Arguable my favorite object in this library, next to threads -conn = multi:newConnection(**BOOLEAN:** protect [**true**]) +`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. +`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. returns or nil = conn:Fire(...) -- Triggers the connection with arguments ..., "returns" if non-nil is a table containing return values from the triggered connections. [**Deprecated:** Planned removal in 14.x.x] +`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 num #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 +`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**]) +`link = conn(FUNCTION func, [STRING name nil], [NUMBER #conns+1])` Example: ```lua @@ -239,11 +238,11 @@ 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 +`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(STRING name)` -- removes all jobs of name, name. Returns the number of jobs removed **Note:** Jobs may be turned into actual objects in the future. @@ -273,8 +272,143 @@ print("There are "..multi:getJobs().." jobs in the queue!") multi:mainloop() ``` -Ranges ------- +Functions +--------- +`func = multi:newFunction(FUNCTION func)` +These objects used to have more of a *function* before corutine based threads came around, but the main purpose now is the ablity to have pausable function calls + +`... = func(...)` -- This is how you call your function. The first argument passed is itself when your function is triggered. See example. +`self = func:Pause()` +`self = func:Resume()` + +Note: A paused function will return: nil, true + +Example: +```lua +local multi = require("multi") +printOnce = multi:newFunction(function(self,msg) + print(msg) + self:Pause() + return "I won't work anymore" +end) +a=printOnce("Hello World!") +b,c=printOnce("Hello World!") +print(a,b,c) +``` +Universal Actor functions +------------------------- +All of these functions are found on actors +`self = multiObj:Pause()` -- Pauses the actor from running +`self = multiObj:Resume()` -- Resumes the actor that was paused +`nil = multiObj:Destroy()` -- Removes the object from the mainloop +`bool = multiObj:isPaused()` -- Returns true if the object is paused, false otherwise +`string = multiObj:getType()` -- Returns the type of the object +`self = multiObj:SetTime(n)` -- Sets a timer, and creates a special "timemaster" actor, which will timeout unless ResolveTimer is called +`self = multiObj:ResolveTimer(...)` -- Stops the timer that was put onto the multiObj from timing out +`self = multiObj:OnTimedOut(func)` -- If ResolveTimer was not called in time this event will be triggered. The function connected to it get a refrence of the original object that the timer was created on as the first argument. +`self = multiObj:OnTimerResolved(func)` -- This event is triggered when the timer gets resolved. Same argument as above is passed, but the variable arguments that are accepted in resolvetimer are also passed as well. +`self = multiObj:Reset(n)` -- In the cases where it isn't obvious what it does, it acts as Resume() + +Events +------ +`event = multi:newEvent(FUNCTION task)` +The object that started it all. These are simply actors that wait for a condition to take place, then auto triggers an event. The event when triggered once isn't triggered again unless you Reset() it. + +`self = SetTask(FUNCTION func)` -- This function is not needed if you supplied task at construction time +`self = OnEvent(FUNCTION func)` -- Connects to the OnEvent event passes argument self to the connectee + +Example: +```lua +local multi = require("multi") +count=0 +-- A loop object is used to demostrate how one could use an event object. +loop=multi:newLoop(function(self,dt) + count=count+1 +end) +event=multi:newEvent(function() return count==100 end) -- set the event +event:OnEvent(function(self) -- connect to the event object + loop:Destroy() -- destroys the loop from running! + print("Stopped that loop!",count) +end) -- events like alarms need to be reset the Reset() command works here as well +multi:mainloop() +``` + +Updates +------- +`updater = multi:newUpdater([NUMBER skip 1])` -- set the amount of steps that are skipped +Updaters are a mix between both loops and steps. They were a way to add basic priority management to loops (until a better way was added). Now they aren't as useful, but if you do not want the performance hit of turning on priority then they are useful to auro skip some loops. Note: The performance hit due to priority management is not as bas as it used to be. + +`self = updater:SetSkip(NUMBER n)` -- sets the amount of steps that are skipped +`self = OnUpdate(FUNCTION func)` -- connects to the main trigger of the updater which is called every nth step + +Example: +```lua +local multi = require("multi") +updater=multi:newUpdater(5000) -- simple, think of a loop with the skip feature of a step +updater:OnUpdate(function(self) + print("updating...") +end) +multi:mainloop() +``` + +Alarms +------ +`alarm = multi:newAlarm([NUMBER 0])` -- creates an alarm which waits n seconds +Alarms ring after a certain amount of time, but you need to reset the alarm every time it rings! Use a TLoop if you do not want to have to reset. + +`self = alarm:Reset([NUMBER sec current_time_set])` -- Allows one to reset an alarm, optional argument to change the time until the next ring. +`self = alarm:OnRing(FUNCTION func` -- Allows one to connect to the alarm event which is triggerd after a certain amount of time has passed. + +```lua +local multi = require("multi") +alarm=multi:newAlarm(3) -- in seconds can go to .001 uses the built in os.clock() +alarm:OnRing(function(a) + print("3 Seconds have passed!") + a:Reset(n) -- if n were nil it will reset back to 3, or it would reset to n seconds +end) +multi:mainloop() +``` + +Loops +----- +`loop = multi:newLoop(FUNCTION func)` -- func the main connection that you can connect to. Is optional, but you can also use OnLoop(func) to connect as well. +Loops are events that happen over and over until paused. They act like a while loop. + +`self = OnLoop(FUNCTION func)` -- func the main connection that you can connect to. Alllows multiple connections to one loop if need be. + +TLoops +------ +`tloop = multi:newTLoop(FUNCTION func ,NUMBER: [set 1])` + +Steps +----- +`step = multi:newStep(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0])` + +TSteps +------ +`tstep = multi:newStep(NUMBER start, NUMBER reset, [NUMBER count 1], [NUMBER set 1])` + +Triggers +-------- +`trigger = multi:newTrigger(FUNCTION: func)` + +Time Stampers WIP +----------------- +`stamper = multi:newTimeStamper()` + +Watchers +-------- +`watcher = multi:newWatcher(STRING name)` +`watcher = multi:newWatcher(TABLE namespace, STRING name)` + +Custom Objects +-------------- +`cobj = multi:newCustomObject(TABLE objRef, BOOLEAN isActor)` + +Coroutine based Threading +------------------------- +This was made due to the limitations of multiObj:hold(), which no longer exists. When this library was in its infancy and before I knew about coroutines, I actually tried to emulate what coroutines did in pure lua. (Was ok, but only allowed for one pause of code ececution) Many objects were given threaded variants though, none are really needed since the base thread objects ant thread.* can be used to emulate those. + +System Threads - Multi-core +--------------------------- -Conditions ----------- \ No newline at end of file diff --git a/changes.md b/changes.md index f1b4f67..98bd1a9 100644 --- a/changes.md +++ b/changes.md @@ -1,17 +1,26 @@ #Changes [TOC] -Update 12.3.0 So you documented it finally +Update 13.0.0 So you documented it, finally, but it's sad to see some things go isn't it? ------------- 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 +Changed: +- A few things to make things more clear when using the library +- The way functions returned paused status. Before it would return "PAUSED" now it returns nil, true if paused + + +Removed: +- Ranges and conditions -- corutine based threads can dmulate what these objects did and much better! +- + + Added: ... -Update 12.2.2 Time for some more bug fixes! +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. Thats all for this update -Update 12.2.1 Time for some bug fixes! +Update 12.2.1 Time for some bug fixes! ------------- Fixed: SystemThreadedJobQueues - You can now make as many job queues as you want! Just a warning when using a large amount of cores for the queue it takes a second or 2 to set up the jobqueues for data transfer. I am unsure if this is a lanes thing or not, but love2d has no such delay when setting up the jobqueue! diff --git a/multi/init.lua b/multi/init.lua index 654d469..fbe8466 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.3.0" -multi._VERSION = "12.3.0" +multi.Version = "13.0.0" +multi._VERSION = "13.0.0" multi.stage = "stable" multi.__index = multi multi.Mainloop = {} @@ -184,21 +184,6 @@ multi.GetParentProcess=multi.getParentProcess function multi.Stop() mainloopActive=false end -function multi:condition(cond) - if not self.CD then - self:Pause() - self.held=true - self.CD=cond.condition - elseif not(cond.condition()) then - self.held=false - self:Resume() - self.CD=nil - return false - end - self.Parent:Do_Order() - return true -end -multi.Condition=multi.condition function multi:isHeld() return self.held end @@ -281,6 +266,7 @@ function multi:IsAnActor() end function multi:OnMainConnect(func) table.insert(self.func,func) + return self end function multi:reallocate(o,n) n=n or #o.Mainloop+1 @@ -321,7 +307,7 @@ function multi:connectFinal(func) elseif self.Type=='step' or self.Type=='tstep' then self:OnEnd(func) else - print("Warning!!! "..self.Type.." doesn't contain a Final Connection State! Use "..self.Type..":Break(function) to trigger it's final event!") + print("Warning!!! "..self.Type.." doesn't contain a Final Connection State! Use "..self.Type..":Break(func) to trigger it's final event!") self:OnBreak(func) end end @@ -369,7 +355,7 @@ function multi:SetTime(n) self:Destroy() end end - return c + return self end multi.ResetTime=multi.SetTime function multi:ResolveTimer(...) @@ -729,34 +715,7 @@ function multi.nextStep(func) next[#self.next+1] = func end end -function multi:newRange() - local selflink=self - local temp={ - getN = function(self) selflink:Do_Order() self.n=self.n+self.c if self.n>self.b then self.Link.held=false self.Link:Resume() return nil end return self.n end, - } - setmetatable(temp,{ - __call=function(self,a,b,c) - self.c=c or 1 - self.n=a-self.c - self.a=a - self.b=b - self.Link=selflink--.Parent.Mainloop[selflink.CID] or - self.Link:Pause() - self.Link.held=true - return function() return self:getN() end - end - }) - self:create(temp) - return temp -end -multi.NewRange=multi.newRange -function multi:newCondition(func) - local c={['condition']=func,Type="condition"} - self:create(c) - return c -end multi.OnPreLoad=multi:newConnection() -multi.NewCondition=multi.newCondition --Core Actors function multi:newCustomObject(objRef,isActor) local c={} @@ -889,7 +848,12 @@ function multi:newFunction(func) c.func=func mt={ __index=multi, - __call=function(self,...) if self.Active then return self:func(...) end local t={...} return "PAUSED" end + __call=function(self,...) + if self.Active then + return self:func(...) + end + return nil,true + end } c.Parent=self function c:Pause() diff --git a/test.lua b/test.lua index efb6804..694a227 100644 --- a/test.lua +++ b/test.lua @@ -1,23 +1,8 @@ package.path="?/init.lua;?.lua;"..package.path 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!") +alarm=multi:newAlarm(3) -- in seconds can go to .001 uses the built in os.clock() +alarm:OnRing(function(a) + print("3 Seconds have passed!") + a:Reset(n) -- if n were nil it will reset back to 3, or it would reset to n seconds +end) multi:mainloop() -- 2.43.0 From 72dcc95400b30d41590a1b45e2826fa8f7f25ba2 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Wed, 26 Sep 2018 20:33:24 -0400 Subject: [PATCH 03/20] updated readme for 13.0.0 --- README.md | 810 +----------------------------------------------------- 1 file changed, 5 insertions(+), 805 deletions(-) diff --git a/README.md b/README.md index a2c970d..93fd18c 100644 --- a/README.md +++ b/README.md @@ -1,9 +1,7 @@ -# multi Version: 12.3.0 Documentation and bug fixes +# multi Version: 13.0.0 Documentation finally done 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**
-[TOC] - INSTALLING ---------- Note: The latest version of Lua lanes is required if you want to make use of system threads on lua 5.1+. I will update the dependencies for Lua rocks since this library should work fine on lua 5.1+ You also need the lua-net library and the bin library. all installed automatically using luarocks. however you can do this manually if lanes and luasocket are installed. Links: @@ -18,9 +16,6 @@ If you want to use the system threads, then you'll need to install lanes! ``` luarocks install multi ``` -Note: Soon you may be able to run multitasking code on multiple machines, network parallelism. This however will have to wait until I hammer out some bugs within the core of system threading itself. - -See the rambling section to get an idea of how this will work. Discord ------- @@ -29,20 +24,15 @@ https://discord.gg/U8UspuA
Planned features/TODO --------------------- -- [ ] Make practical examples that show how you can solve real problems -- [ ] Finish the wiki stuff. (11% done) -- It's been at 11% for so long. I really need to get on this! +- [ ] Finish Documentation - [ ] Test for unknown bugs -- This is always going on - [x] ~~Network Parallelism~~ This was fun, I have some more plans for this as well -Known Bugs/Issues ------------------ -~~A bug concerns the SystemThreadedJobQueue, only 1 can be used for now. Might change in a future update~~ :D Fixed - Usage:
----- ```lua -- Basic usage Alarms: Have been moved to the core of the library require("multi") would work as well -require("multi") -- gets the entire library +local multi = require("multi") -- gets the entire library alarm=multi:newAlarm(3) -- in seconds can go to .001 uses the built in os.clock() alarm:OnRing(function(a) print("3 Seconds have passed!") @@ -50,797 +40,7 @@ alarm:OnRing(function(a) end) multi:mainloop() -- the main loop of the program, multi:umanager() exists as well to allow integration in other loops Ex: love2d love.update function. More on this binding in the wiki! ``` -The library is modular, so you only need to require what you need to. Because of this, the global environment is altered
-There are many useful objects that you can use
-Check out the wiki for detailed usage, but here are the objects:
-- Process#
-- Queue#
-- Alarm
-- Loop
-- Event
-- Step
-- Range
-- TStep
-- TLoop
-- Condition
-- Connection
-- Timer
-- Updater
-- Thread*
-- Trigger
-- Task
-- Job
-- Function
-- Watcher
-Note: *Both a process and queue act like the multi namespace but allows for some cool things. Because they use the other objects an example on them will be done last*
-*Uses the built in coroutine features of lua, these have an interesting interaction with the other means of multi-tasking
-Triggers are kind of useless after the creation of the Connection
-Watchers have no real purpose as well I made it just because.
- -# Examples of each object being used
-We already showed alarms in action so let’s move on to a Loop object - -Throughout these examples I am going to do some strange things to show other features of the library! - -LOOPS ------ -```lua --- Loops: Have been moved to the core of the library require("multi") would work as well -require("multi") -- gets the entire library -count=0 -loop=multi:newLoop(function(self,dt) -- dt is delta time and self are a reference to itself - count=count+1 - if count > 10 then - self:Break() -- All methods on the multi objects are upper camel case, whereas methods on the multi or process/queuer namespace are lower camel case - -- self:Break() will stop the loop and trigger the OnBreak(func) method - -- Stopping is the act of Pausing and deactivating the object! All objects can have the multiobj:Break() command on it! - else - print("Loop #"..count.."!") - end -end) -loop:OnBreak(function(self) - print("You broke me :(") -end) -multi:mainloop() -``` -# Output -Loop #1!
-Loop #2!
-Loop #3!
-Loop #4!
-Loop #5!
-Loop #6!
-Loop #7!
-Loop #8!
-Loop #9!
-Loop #10!
-You broke me :(
- - -With loops out of the way lets go down the line - -This library aims to be Async like. Everything is still on one thread *unless you are using the lanes integration module WIP* (A stable WIP, more on that later) - -EVENTS ------- -```lua --- Events, these were the first objects introduced into the library. I seldomly use them in their pure form though, but later you'll see their advance uses! --- Events on their own don't really do much... We are going to need 2 objects at least to get something going -require("multi") -- gets the entire library -count=0 --- let’s use the loop again to add to count! -loop=multi:newLoop(function(self,dt) - count=count+1 -end) -event=multi:newEvent(function() return count==100 end) -- set the event -event:OnEvent(function(self) -- connect to the event object - loop:Pause() -- pauses the loop from running! - print("Stopped that loop!") -end) -- events like alarms need to be reset the Reset() command works here as well -multi:mainloop() -``` -# Output -Stopped that loop! - -STEPS ------ -```lua -require("multi") --- Steps, are like for loops but non-blocking... You can run a loop to infinity and everything will still run I will combine Steps with Ranges in this example. -step1=multi:newStep(1,10,1,0) -- Some explaining is due. Argument 1 is the Start # Argument 2 is the ResetAt # (inclusive) Argument 3 is the count # (in our case we are counting by +1, this can be -1 but you need to adjust your start and resetAt numbers) --- The 4th Argument is for skipping. This is useful for timing and for basic priority management. A priority management system is included! -step2=multi:newStep(10,1,-1,1) -- a second step, notice the slight changes! -step1:OnStart(function(self) - print("Step Started!") -end) -step1:OnStep(function(self,pos) - if pos<=10 then -- The step only goes to 10 - print("Stepping... "..pos) - else - print("How did I get here?") - end -end) -step1:OnEnd(function(self) - print("Done!") - -- We finished here, but I feel like we could have reused this step in some way... I could use Reset() , but what if I wanted to change it... - if self.endAt==10 then -- lets only loop once - self:Update(1,11,1,0) -- oh now we can reach that else condition! - end - -- Note Update() will restart the step! -end) - --- step2 is bored let’s give it some love :P -step2.range=step2:newRange() -- Set up a range object to have a nested step in a sense! Each nest requires a new range --- it is in your interest not to share ranges between objects! You can however do it if it suits your needs though -step2:OnStep(function(self,pos) - -- for 1=1,math.huge do - -- print("I am holding the code up because I can!") - --end - -- We don’t want to hold things up, but we want to nest. - -- Note a range is not necessary if the nested for loop has a small range, if however, the range is rather large you may want to allow other objects to do some work - for i in self.range(1,100) do - print(pos,i) -- Now our nested for loop is using a range object which allows for other objects to get some CPU time while this one is running - end -end) --- TSteps are just like alarms and steps mixed together, the only difference in construction is the 4th Argument. On a TStep that argument controls time. The default is 1 --- The Reset(n) works just like you would figure! -step3=multi:newTStep(1,10,.5,2) -- lets go from 1 to 10 counting by .5 every 2 seconds -step3:OnStep(function(self,pos) - print("Ok "..pos.."!") -end) -multi:mainloop() -``` -# Output - -Note: the output on this one is huge!!! So, I had to ... some parts! You need to run this for yourself to see what is going on!
-Step Started!
-Stepping... 1
-10 1
-Stepping... 2
-10 2
-Stepping... 3
-10 3
-...
-Ok 9.5!
-Ok 10!
- -TLOOPS ------- -```lua -require("multi") --- TLoops are loops that run ever n second. We will also look at condition objects as well --- Here we are going to modify the old loop to be a little different -count=0 -loop=multi:newTLoop(function(self) -- We are only going to count with this loop but doing so using a condition! - while self:condition(self.cond) do - count=count+1 - end - print("Count is "..count.."!") - self:Destroy() -- Lets destroy this object, casting it to the dark abyss MUHAHAHA!!! - -- the reference to this object will be a phantom object that does nothing! -end,1) -- Notice the ',1' after the function! This is where you put your time value! -loop.cond=multi:newCondition(function() return count<=100 end) -- conditions need a bit of work before I am happy with them -multi:mainloop() -``` -# Output -Count is 101! - -Connections ------------ -These are my favorite objects and you'll see why. They are very useful objects for ASync connections! - -```lua -require("multi") --- Let’s create the events -yawn={} -- ill just leave that there -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... We are good at coding though so let’s get a speed advantage by not pcalling. Pcalling is useful for plugins and stuff that may have been coded badly and you can ignore those connections if need be. -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 - --- no need for connect, but I kept that function because of backwards compatibility. -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!!! -``` -# Output -1 2 Hello!!!
-CSE1 1 100 Bye!
-CSE2 1 100 Hello!
-CSE1 1 100 Hi Ya Folks!!!
-CSE2 1 100 Hi Ya Folks!!!
-CSE3 1 100 Hi Ya Folks!!!
-CSE2 1 100 Hi Ya Folks!!!
-CSE3 1 100 Hi Ya Folks!!!
-
- -You may think timers should be bundled with alarms, but they are a bit different and have cool features
-TIMERS ------- -```lua --- You see the thing is that all time-based objects use timers e.g. Alarms, TSteps, and Loops. Timers are more low level! -require("multi") -local clock = os.clock -function sleep(n) -- seconds - local t0 = clock() - while clock() - t0 <= n do end -end -- we will use this later! - -timer=multi:newTimer() -timer:Start() --- let’s do a mock alarm -set=3 -- 3 seconds -a=0 -while timer:Get()<=set do - -- waiting... - a=a+1 -end -print(set.." second(s) have passed!") --- Timers can do one more thing that is interesting and that is pausing them! -timer:Pause() -print(timer:Get()) -- should be really close to 'set' -sleep(3) -print(timer:Get()) -- should be really close to 'set' -timer:Resume() -sleep(1) -print(timer:Get()) -- should be really close to the value of set + 1 -timer:Pause() -print(timer:Get()) -- should be really close to 'set' -sleep(3) -print(timer:Get()) -- should be really close to 'set' -timer:Resume() -sleep(1) -print(timer:Get()) -- should be really close to the value of set + 2 -``` -# Output -Note: This will make more sense when you run it for yourself
-3 second(s) have passed!
-3.001
-3.001
-4.002
-4.002
-4.002
-5.003
- -UPDATER -------- -```lua --- Updaters: Have been moved to the core of the library require("multi") would work as well -require("multi") -updater=multi:newUpdater(5) -- simple, think of a look with the skip feature of a step -updater:OnUpdate(function(self) - --print("updating...") -end) --- Here every 5 steps the updater will do stuff! --- But I feel it is now time to touch into priority management, so let’s get into basic priority stuff and get into a more advance version of it ---[[ -multi.Priority_Core -- Highest form of priority -multi.Priority_High -multi.Priority_Above_Normal -multi.Priority_Normal -- The default form of priority -multi.Priority_Below_Normal -multi.Priority_Low -multi.Priority_Idle -- Lowest form of priority - -Note: These only take effect when you enable priority, otherwise everything is at a core like level! -We aren't going to use regular objects to test priority, but rather benchmarks! -to set priority on an object though you would do -multiobj:setPriority(one of the above) -]] --- let’s bench for 3 seconds using the 3 forms of priority! First no Priority -multi:benchMark(3,nil,"Regular Bench: "):OnBench(function() -- the onbench() allows us to do each bench after each other! - print("P1\n---------------") - multi:enablePriority() - multi:benchMark(3,multi.Priority_Core,"Core:") - multi:benchMark(3,multi.Priority_High,"High:") - multi:benchMark(3,multi.Priority_Above_Normal,"Above_Normal:") - multi:benchMark(3,multi.Priority_Normal,"Normal:") - multi:benchMark(3,multi.Priority_Below_Normal,"Below_Normal:") - multi:benchMark(3,multi.Priority_Low,"Low:") - multi:benchMark(3,multi.Priority_Idle,"Idle:"):OnBench(function() - print("P2\n---------------") - -- Finally, the 3rd form - multi:enablePriority2() - multi:benchMark(3,multi.Priority_Core,"Core:") - multi:benchMark(3,multi.Priority_High,"High:") - multi:benchMark(3,multi.Priority_Above_Normal,"Above_Normal:") - multi:benchMark(3,multi.Priority_Normal,"Normal:") - multi:benchMark(3,multi.Priority_Below_Normal,"Below_Normal:") - multi:benchMark(3,multi.Priority_Low,"Low:") - multi:benchMark(3,multi.Priority_Idle,"Idle:") - end) -end) -multi:mainloop() -- Notice how the past few examples did not need this, well only actors need to be in a loop! More on this in the wiki. -``` -# Output -Note: These numbers will vary drastically depending on your compiler and CPU power
-Regular Bench: 2094137 Steps in 3 second(s)!
-P1
-Below_Normal: 236022 Steps in 3 second(s)!
-Normal: 314697 Steps in 3 second(s)!
-Above_Normal: 393372 Steps in 3 second(s)!
-High: 472047 Steps in 3 second(s)!
-Core: 550722 Steps in 3 second(s)!
-Low: 157348 Steps in 3 second(s)!
-Idle: 78674 Steps in 3 second(s)!
-P2
-Core: 994664 Steps in 3 second(s)!
-High: 248666 Steps in 3 second(s)!
-Above_Normal: 62166 Steps in 3 second(s)!
-Normal: 15541 Steps in 3 second(s)!
-Below_Normal: 3885 Steps in 3 second(s)!
-Idle: 242 Steps in 3 second(s)!
-Low: 971 Steps in 3 second(s)!
- -Notice: Even though I started each bench at the same time the order that they finished differed the order is likely to vary on your machine as well!
- -Processes ---------- -A process allows you to group the Actor objects within a controllable interface -```lua -require("multi") -proc=multi:newProcess() -- takes an optional file as an argument, but for this example we aren't going to use that --- a process works just like the multi object! -b=0 -loop=proc:newTLoop(function(self) - a=a+1 - proc:Pause() -- pauses the CPU cycler for this processor! Individual objects are not paused, however because they aren't getting CPU time they act as if they were paused -end,.1) -updater=proc:newUpdater(multi.Priority_Idle) -- priority can be used in skip arguments as well to manage priority without enabling it! -updater:OnUpdate(function(self) - b=b+1 -end) -a=0 -- a counter -loop2=proc:newLoop(function(self,dt) - print("Let’s Go!") - self:hold(3) -- this will keep this object from doing anything! Note: You can only have one hold active at a time! Multiple are possible, but results may not be as they seem see * for how hold works - -- Within a process using hold will keep it alive until the hold is satisfied! - print("Done being held for 1 second") - self:hold(function() return a>10 end) - print("A is now: "..a.." b is also: "..b) - self:Destroy() - self.Parent:Pause() -- let’s say you don't have the reference to the process! - os.exit() -end) --- Notice this is now being created on the multi namespace -event=multi:newEvent(function() return os.clock()>=1 end) -event:OnEvent(function(self) - proc:Resume() - self:Destroy() -end) -proc:Start() -multi:mainloop() -``` -# Output -Let’s Go!
-Done being held for 1 second
-A is now: 29 b is also: 479
- -**Hold: This method works as follows** -```lua -function multi:hold(task) - self:Pause() -- pause the current object - self.held=true -- set held - if type(task)=='number' then -- a sleep cmd - local timer=multi:newTimer() - timer:Start() - while timer:Get() -Ring ring!!!
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-Done
-# Actual Output -Done
-1
-2
-3
-4
-5
-6
-7
-8
-9
-10
-Ring ring!!!
- -Threads -------- -These fix the hold problem that you get with regular objects, and they work the same! They even have some extra features that make them really useful.
-```lua -require("multi") -test=multi:newThreadedProcess("main") -- you can thread processors and all Actors see note for a list of actors you can thread! -test2=multi:newThreadedProcess("main2") -count=0 -test:newLoop(function(self,dt) - count=count+1 - thread.sleep(.01) -end) -test2:newLoop(function(self,dt) - print("Hello!") - thread.sleep(1) -- sleep for some time -end) --- threads take a name object then the rest as normal -step=multi:newThreadedTStep("step",1,10) -step:OnStep(function(self,p) - print("step",p) - thread.skip(21) -- skip n cycles -end) -step:OnEnd(function() - print("Killing thread!") - thread.kill() -- kill the thread -end) -loop=multi:newThreadedLoop("loop",function(self,dt) - print(dt) - thread.sleep(1.1) -end) -loop2=multi:newThreadedLoop("loop",function(self,dt) - print(dt) - thread.hold(function() return count>=100 end) - print("Count is "..count) - os.exit() -end) -alarm=multi:newThreadedAlarm("alarm",1) -alarm:OnRing(function(self) - print("Ring") - self:Reset() -end) -multi:mainloop() -``` -# Output -Ring
-0.992
-0.992
-Hello!
-step 1
-step 2
-Hello!
-Ring
-2.092
-step 3
-Hello!
-Ring
-Count is 100
-Threadable Actors +Known Bugs/Issues ----------------- -- Alarms -- Events -- Loop/TLoop -- Process -- Step/TStep - -Functions ---------- -If you ever wanted to pause a function then great now you can -The use of the Function object allows one to have a method that can run free in a sense -```lua -require("multi") -func=multi:newFunction(function(self,arg1,arg2,...) - self:Pause() - return arg1 -end) -print(func("Hello")) -print(func("Hello2")) -- returns PAUSED allows for the calling of functions that should only be called once. returns PAUSED instantly if paused -func:Resume() -print(func("Hello3")) -``` -# Output -Hello
-PAUSED
-Hello3
- -ThreadedUpdater ---------------- - -```lua --- Works the same as a regular updater! -require("multi") -multi:newThreadedUpdater("Test",10000):OnUpdate(function(self) - print(self.pos) -end) -multi:mainloop() -``` -# Output -1
-2
-...
-.inf
- -Triggers --------- -Triggers were what I used before connections became a thing, also Function objects are a lot like triggers and can be paused as well, while triggers cannot...
-They are simple to use, but in most cases you are better off using a connection
-```lua -require("multi") --- They work like connections but can only have one event binded to them -trig=multi:newTrigger(function(self,a,b,c,...) - print(a,b,c,...) -end) -trig:Fire(1,2,3) -trig:Fire(1,2,3,"Hello",true) -``` - -# Output -1 2 3
-1 2 3 Hello true
- -Tasks ------ -Tasks allow you to run a block of code before the multi mainloop does it thing. Tasks still have a use but depending on how you program they aren't needed.
-```lua -require("multi") -multi:newTask(function() - print("Hi!") -end) -multi:newLoop(function(self,dt) - print("Which came first the task or the loop?") - self:Break() -end) -multi:newTask(function() - print("Hello there!") -end) -multi:mainloop() -``` -# Output -Hi!
-Hello there!
-Which came first the task or the loop?
- -As seen in the example above the tasks were done before anything else in the mainloop! This is useful when making libraries around the multitasking features and you need things to happen in a certain order!
- -Jobs ----- -Jobs were a strange feature that was created for throttling connections! When I was building an IRC bot around this library I couldn't have messages posting too fast due to restrictions. Jobs allowed functions to be added to a queue that were executed after a certain amount of time has passed -```lua -require("multi") -- jobs use alarms I am pondering if alarms should be added to the core or if jobs should use timers instead... --- jobs are built into the core of the library so no need to require them -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() -``` -# Output -false 0
-true 4
-There are 4 jobs in the queue!
-A job!

-Another job!
- -Watchers --------- -Watchers allow you to monitor a variable and trigger an event when the variable has changed! -```lua -require("multi") -a=0 -watcher=multi:newWatcher(_G,"a") -- watch a in the global environment -watcher:OnValueChanged(function(self,old,new) - print(old,new) -end) -tloop=multi:newTLoop(function(self) - a=a+1 -end,1) -multi:mainloop() -``` -# Output -0 1
-1 2
-2 3
-...
-.inf-1 inf
- -Timeout management ------------------- -```lua --- Note: I used a tloop, so I could control the output of the program a bit. -require("multi") -a=0 -inc=1 -- change to 0 to see it not met at all, 1 if you want to see the first condition not met but the second and 2 if you want to see it meet the condition on the first go. -loop=multi:newTLoop(function(self) - print("Looping...") - a=a+inc - if a==14 then - self:ResolveTimer("1","2","3") -- ... any number of arguments can be passed to the resolve handler - -- this will also automatically pause the object that it is binded to - end -end,.1) -loop:SetTime(1) -loop:OnTimerResolved(function(self,a,b,c) -- the handler will return the self and the passed arguments - print("We did it!",a,b,c) -end) -loop:OnTimedOut(function(self) - if not TheSecondTry then - print("Loop timed out!",self.Type,"Trying again...") - self:ResetTime(2) - self:Resume() - TheSecondTry=true - else - print("We just couldn't do it!") -- print if we don't get anything working - end -end) -multi:mainloop() -``` -# Output (Change the value inc as indicated in the comment to see the outcomes!) -Looping...
-Looping...
-Looping...
-Looping...
-Looping...
-Looping...
-Looping...
-Looping...
-Looping...
-Loop timed out! tloop Trying again...
-Looping...
-Looping...
-Looping...
-Looping...
-Looping...
-We did it! 1 2 3
- -Rambling --------- -5/23/18: -When it comes to running code across different systems we run into a problem. It takes time to send objects from one matching to another. In the beginning only, local networks will be supported. I may add support to send commands to another network to do computing. Like having your own lua cloud. userdata will never be allowed to run on other machines. It is not possible unless the library you are using allows userdata to be turned into a string and back into an object. With this feature you want to send a command that will take time or needs tons of them done millions+, reason being networks are not that "fast" and only simple objects can be sent. If you mirror your environment then you can do some cool things. - -The planned structure will be something like this: -multi-Single Threaded Multitasking -multi-Threads -multi-System Threads -multi-Network threads - -where netThreads can contain systemThreads which can intern contain both Threads and single threaded multitasking - -Nothing has been built yet, but the system will work something like this: -#host: -```lua -sGLOBAL, nGlobal,sThread=require("multi.integration.networkManager").init() -- This will determine if one is using lanes,love2d, or luvit -multi:Host("MainSystem") -- tell the network that this is the main system. Uses broadcast so that nodes know how to find the host! -nThread = multi:newNetworkThread("NetThread_1",function(...) - -- basic usage - nGLOBAL["RemoteVaraible"] = true -- will sync data to all nodes and the host - sGLOBAL["LocalMachineVaraible"] = true -- will sync data to all system threads on the local machine - return "Hello Network!" -- send "Hello Network" back to the host node -end) -multi:mainloop() -``` -#node -```lua -GLOBAL,sThread=require("multi.integration.networkManager").init() -- This will determine if one is using lanes,love2d, or luvit -node = multi:newNode("NodeName","MainSystem") -- Search the network for the host, connect to it and be ready for requests! --- On the main thread, a simple multi:newNetworkThread thread and non-system threads, you can access global data without an issue. When dealing with system threads is when you have a problem. -node:setLog{ - maxLines = 10000, - cleanOnInterval = true, - cleanInterval = "day", -- every day Supports(day, week, month, year) - noLog = false -- default is false, make true if you do not need a log -} -node:settings{ - maxJobs = 100, -- Job queues will respect this as well as the host when it is figuring out which node is under the least load. Default: 0 or infinite - sendLoadInterval = 60 -- every 60 seconds update the host of the nodes load - sendLoad = true -- default is true, tells the server how stressed the system is -} -multi:mainloop() --- Note: the node will contain a log of all the commands that it gets. A file called "NodeName.log" will contain the info. You can set the limit by lines or file size. Also, you can set it to clear the log every interval of time if an error does not exist. All errors are both logged and sent to the host as well. You can have more than one host and more than one node(duh :P). -``` -The goal of the node is to set up a simple and easy way to run commands on a remote machine. - -There are 2 main ways you can use this feature. 1. One node per machine with system threads being able to use the full processing power of the machine. 2. Multiple nodes on one machine where each node is acting like its own thread. And of course, a mix of the two is indeed possible. - - -Love2d Sleeping reduces the CPU time making my load detection think the system is under more load, thus preventing it from sleeping... I will investigate other means. As of right now it will not eat all your CPU if threads are active. For now, I suggest killing threads that aren't needed anymore. On lanes threads at idle use 0% CPU and it is amazing. A state machine may solve what I need though. One state being idle state that sleeps and only goes into the active state if a job request or data is sent to it... after some time of not being under load it will switch back into the idle state... We'll see what happens. - -Love2d doesn't like to send functions through channels. By default, it does not support this. I achieve this by dumping the function and loadstring it on the thread. This however is slow. For the System Threaded Job Queue, I had to change my original idea of sending functions as jobs. The current way you do it now is register a job functions once and then call that job across the thread through a queue. Each worker thread pops from the queue and returns the job. The Job ID is automatically updated and allows you to keep track of the order that the data comes in. A table with # indexes can be used to organize the data... - -Regarding benchmarking. If you see my bench marks and are wondering they are 10x better it’s because I am using luajit for my tests. I highly recommend using luajit for my library, but lua 5.1 will work just as well, but not as fast. - -So, while working on the jobQueue:doToAll() method I figured out why love2d's threaded tables were acting up when more than 1 thread was sharing the table. It turns out 1 thread was eating all the pops from the queue and starved all the other queues... I’ll need to use the same trick I did with GLOBAL to fix the problem... However, at the rate I am going threading in love will become way slower. I might use the regular GLOBAL to manage data internally for threadedtables... - -I have been using this (EventManager --> MultiManager --> now multi) for my own purposes and started making this when I first started learning lua. You can see how the code changed and evolved throughout the years. I tried to include all the versions that still existed on my HDD. - -I added my old versions to this library... It started out as the EventManager and was kind of crappy, but it was the start to this library. It kept getting better and better until it became what it is today. There are some features that no longer exist in the latest version, but they were remove because they were useless... I added these files to the GitHub so for those interested can see into my mind in a sense and see how I developed the library before I used GitHub. - -The first version of the EventManager was function based not object based and benched at about 2000 steps per second... Yeah that was bad... I used loadstring and it was a mess... Look and see how it grew throughout the years I think it may interest some of you guys! +Currently no bugs that I know of :D -- 2.43.0 From 040d8842a26895f96f5302eb1a03598bf0f79f42 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Tue, 6 Nov 2018 06:39:14 -0500 Subject: [PATCH 04/20] still writing this thing --- Documentation.md | 388 ++++++++++++++++++++++++--- changes.md | 16 +- examples/RegisteredFunctions.dat | 0 examples/network-node1.lua | 21 +- multi/compat/love2d.lua | 8 +- multi/init.lua | 69 ++++- multi/integration/networkManager.lua | 72 ++--- test.lua | 14 +- 8 files changed, 492 insertions(+), 96 deletions(-) create mode 100644 examples/RegisteredFunctions.dat diff --git a/Documentation.md b/Documentation.md index 204a522..08b931a 100644 --- a/Documentation.md +++ b/Documentation.md @@ -1,4 +1,4 @@ -Mulit Version: 12.3.0 +Current Multi Version: 13.0.0 Table of contents ----------------- @@ -57,7 +57,7 @@ P1 follows a forumla that resembles this: ~n=I*PRank where n is the amount of st 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| @@ -114,6 +114,7 @@ Multi constructors - Multi-Objs `conn = multi:newConnection([BOOLEAN protect true])` `nil = multi:newJob(FUNCTION func, STRING name)` `func = multi:newFunction(FUNCTION func)` +`trigger = multi:newTrigger(FUNCTION: func)` **Actors** `event = multi:newEvent(FUNCTION task)` @@ -156,7 +157,7 @@ proc:Start() -- let's start the processor multi:mainloop() -- the main runner that drives everything ``` -Timers +Non-Actor: Timers ------ timer = multi:newTimer() Creates a timer object that can keep track of time @@ -170,7 +171,7 @@ boolean = timer:isPaused() -- Returns if the timer is paused or not **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 +Non-Actor: Connections ----------- Arguable my favorite object in this library, next to threads @@ -236,7 +237,7 @@ print("------") OnCustomSafeEvent:Fire(1,100,"Hi Ya Folks!!!") -- fire them all again!!! ``` -Jobs +Non-Actor: 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. @@ -272,7 +273,7 @@ print("There are "..multi:getJobs().." jobs in the queue!") multi:mainloop() ``` -Functions +Non-Actor: Functions --------- `func = multi:newFunction(FUNCTION func)` These objects used to have more of a *function* before corutine based threads came around, but the main purpose now is the ablity to have pausable function calls @@ -295,6 +296,13 @@ a=printOnce("Hello World!") b,c=printOnce("Hello World!") print(a,b,c) ``` + +Non-Actor: Triggers +-------- +`trigger = multi:newTrigger(FUNCTION: func(...))` -- A trigger is the precursor of connection objects. The main difference is that only one function can be binded to the trigger. +`self = trigger:Fire(...)` -- Fires the function that was connected to the trigger and passes the arguments supplied in Fire to the function given. + + Universal Actor functions ------------------------- All of these functions are found on actors @@ -309,7 +317,7 @@ All of these functions are found on actors `self = multiObj:OnTimerResolved(func)` -- This event is triggered when the timer gets resolved. Same argument as above is passed, but the variable arguments that are accepted in resolvetimer are also passed as well. `self = multiObj:Reset(n)` -- In the cases where it isn't obvious what it does, it acts as Resume() -Events +Actor: Events ------ `event = multi:newEvent(FUNCTION task)` The object that started it all. These are simply actors that wait for a condition to take place, then auto triggers an event. The event when triggered once isn't triggered again unless you Reset() it. @@ -333,7 +341,7 @@ end) -- events like alarms need to be reset the Reset() command works here as we multi:mainloop() ``` -Updates +Actor: Updates ------- `updater = multi:newUpdater([NUMBER skip 1])` -- set the amount of steps that are skipped Updaters are a mix between both loops and steps. They were a way to add basic priority management to loops (until a better way was added). Now they aren't as useful, but if you do not want the performance hit of turning on priority then they are useful to auro skip some loops. Note: The performance hit due to priority management is not as bas as it used to be. @@ -351,7 +359,7 @@ end) multi:mainloop() ``` -Alarms +Actor: Alarms ------ `alarm = multi:newAlarm([NUMBER 0])` -- creates an alarm which waits n seconds Alarms ring after a certain amount of time, but you need to reset the alarm every time it rings! Use a TLoop if you do not want to have to reset. @@ -359,6 +367,7 @@ Alarms ring after a certain amount of time, but you need to reset the alarm ever `self = alarm:Reset([NUMBER sec current_time_set])` -- Allows one to reset an alarm, optional argument to change the time until the next ring. `self = alarm:OnRing(FUNCTION func` -- Allows one to connect to the alarm event which is triggerd after a certain amount of time has passed. +Example: ```lua local multi = require("multi") alarm=multi:newAlarm(3) -- in seconds can go to .001 uses the built in os.clock() @@ -369,45 +378,360 @@ end) multi:mainloop() ``` -Loops +Actor: Loops ----- `loop = multi:newLoop(FUNCTION func)` -- func the main connection that you can connect to. Is optional, but you can also use OnLoop(func) to connect as well. Loops are events that happen over and over until paused. They act like a while loop. `self = OnLoop(FUNCTION func)` -- func the main connection that you can connect to. Alllows multiple connections to one loop if need be. -TLoops ------- -`tloop = multi:newTLoop(FUNCTION func ,NUMBER: [set 1])` +Example: +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +local a = 0 +loop = multi:newLoop(function() + a = a + 1 + if a == 1000 then + print("a = 1000") + loop:Pause() + end +end) +multi:mainloop() +``` -Steps +Actor: TLoops +------ +`tloop = multi:newTLoop(FUNCTION func ,NUMBER: [set 1])` -- TLoops are pretty much the same as loops. The only difference is that they take set which is how long it waits, in seconds, before triggering function func. + +`self = OnLoop(FUNCTION func)` -- func the main connection that you can connect to. Alllows multiple connections to one TLoop if need be. + +Example: +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +local a = 0 +loop = multi:newTLoop(function() + a = a + 1 + if a == 10 then + print("a = 10") + loop:Pause() + end +end,1) +multi:mainloop() +``` + +Actor: Steps ----- -`step = multi:newStep(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0])` +`step = multi:newStep(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0])` -- Steps were originally introduced to bs used as for loops that can run parallel with other code. When using steps think of it like this: `for i=start,reset,count do` When the skip argument is given, each time the step object is given cpu cycles it will be skipped by n cycles. So if skip is 1 every other cpu cycle will be alloted to the step object. -TSteps +`self = step:OnStart(FUNCTION func(self))` -- This connects a function to an event that is triggered everytime a step starts. +`self = step:OnStep(FUNCTION func(self,i))` -- This connects a function to an event that is triggered every step or cycle that is alloted to the step object +`self = step:OnEnd(FUNCTION func(self))` -- This connects a function to an event that is triggered when a step reaches its goal +`self = step:Update(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0])` -- Update can be used to change the goals of the step. You should call step:Reset() after using Update to restart the step. + +Example: +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +multi:newStep(1,10,1,0):OnStep(function(step,pos) + print(step,pos) +end):OnEnd(fucntion(step) + step:Destroy() +end) +multi:mainloop() +``` + +Actor: TSteps ------ -`tstep = multi:newStep(NUMBER start, NUMBER reset, [NUMBER count 1], [NUMBER set 1])` +`tstep = multi:newStep(NUMBER start, NUMBER reset, [NUMBER count 1], [NUMBER set 1])` -- TSteps work just like steps, the only difference is that instead of skip, we have set which is how long in seconds it should wait before triggering the OnStep() event. -Triggers +`self = tstep:OnStart(FUNCTION func(self))` -- This connects a function to an event that is triggered everytime a step starts. +`self = tstep:OnStep(FUNCTION func(self,i))` -- This connects a function to an event that is triggered every step or cycle that is alloted to the step object +`self = tstep:OnEnd(FUNCTION func(self))` -- This connects a function to an event that is triggered when a step reaches its goal +`self = tstep:Update(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER set 1])` -- Update can be used to change the goals of the step. You should call step:Reset() after using Update to restart the step. +`self = tstep:Reset([NUMBER n set])` -- Allows you to reset a tstep that has ended, but also can change the time between each trigger. + +Example: +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +multi:newTStep(1,10,1,1):OnStep(function(step,pos) + print(step,pos) +end):OnEnd(fucntion(step) + step:Destroy() +end) +multi:mainloop() +``` + +Actor: Time Stampers +------------- +`stamper = multi:newTimeStamper()` -- This allows for long time spans as well as short time spans. +`stamper = stamper:OhSecond(NUMBER second, FUNCTION func)` -- This takes a value between 0 and 59. This event is called once every second! Not once every second! If you want seconds then use alarms*****! 0 is the start of every minute and 59 is the end of every minute. +`stamper = stamper:OhMinute(NUMBER minute, FUNCTION func)` -- This takes a value between 0 and 59. This event is called once every hour*****! Same concept as OnSecond() +`stamper = stamper:OhHour(NUMBER hour, FUNCTION func)` -- This takes a value between 0 and 23. This event is called once every day*****! 0 is midnight and 23 is 11pm if you use 12 hour based time. +`stamper = stamper:OnDay(STRING/NUMBER day, FUNCTION func)` -- So the days work like this 'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'. When in string form this is called every week. When in number form this is called every month*****! +There is a gotcha though with this. Months can have 28,29,30, and 31 days to it, which means that something needs to be done when dealing with the last few days of a month. I am aware of this issue and am looking into a solution that is simple and readable. I thought about allowing negitive numbers to allow one to eaisly use the last day of a month. -1 is the last day of the month where -2 is the second to last day of the month. You can go as low as -28 if you want, but this provides a nice way to do something near the end of the month that is lua like. +`stamper = stamper:OnMonth(NUMBER month,FUNCTION func)` -- This takes a value between 1 and 12. 1 being January and 12 being December. Called once per year*****. +`stamper = stamper:OnYear(NUMBER year,FUNCTION func)` -- This takes a number yy. for example 18 do not use yyyy format! Odds are you will not see this method triggered more than once, unless science figures out the whole life extension thing. But every century this event is triggered*****! I am going to be honest though, the odds of a system never reseting for 100 years is very unlikely, so if I used 18 (every 18th year in each century every time i load my program this event will be triggered). Does it actually work? I have no idea tbh it should, but can i prove that without actually testing it? Yes by using fake data thats how. +`stamper = stamper:OnTime(NUMBER hour,NUMBER minute,NUMBER second,FUNCTION func)` -- This takes in a time to trigger, hour, minute, second. This triggeres once a day at a certain time! Sort of like setting an alarm! You can combine events to get other effects like this! +`stamper = stamper:OnTime(STRING time,FUNCTION func)` -- This takes a string time that should be formatted like this: "hh:mm:ss" hours minutes and seconds must be given as parameters! Otherwise functions as above! + +*****If your program crashes or is rebooted than the data in RAM letting the code know that the function was already called will be reset! This means that if an event set to be triggered on Monday then you reboot the code it will retrigger that event on the same day if the code restarts. In a future update I am planning of writing to the disk for OnHour/Day/Week/Year events. This will be an option that can be set on the object. + +Examples: +**OnSecond** +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +local a = 0 +ts:OnSecond(0,function() + a=a+1 + print("New Minute: "..a.." <"..os.date("%M")..">") +end) +multi:mainloop() +``` +**OnMinute** +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +local a = 0 +ts:OnSecond(0,function() + a=a+1 + print("New Hour: "..a.." <"..os.date("%I")..">") +end) +multi:mainloop() +``` +**OnHour** +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +ts:OnHour(0,function() + print("New Day") +end) +multi:mainloop() + +``` +**OnDay** +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +ts:OnDay("Thu",function() + print("It's thursday!") +end) +multi:mainloop() +``` +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +ts:OnDay(2,function() + print("Second day of the month!") +end) +multi:mainloop() +``` +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +ts:OnDay(-1,function() + print("Last day of the month!") +end) +multi:mainloop() +``` +**OnYear** +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +ts:OnYear(19,function() -- They gonna wonder if they run this in 2018 why it no work :P + print("We did it!") +end) +multi:mainloop() +``` +**OnTime** +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +ts:OnTime(12,1,0,function() + print("Whooooo") +end) +multi:mainloop() +``` +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +ts = multi:newTimeStamper() +ts:OnTime("12:04:00",function() + print("Whooooo") +end) +multi:mainloop() +``` + +Actor: Watchers -------- -`trigger = multi:newTrigger(FUNCTION: func)` +**Deprecated: ** This object was removed due to its uselessness. Metatables will work much better for what is being done. Perhaps in the future i will remake this method to use metamethods instead of basic watching every step. This will most likely be removed in the next version of the library or changed to use metatables and metamethods. +`watcher = multi:newWatcher(STRING name)` -- Watches a variable on the global namespace +`watcher = multi:newWatcher(TABLE namespace, STRING name)` -- Watches a variable inside of a table +`watcher = watcher::OnValueChanged(Function func(self, old_value, current_value))` -Time Stampers WIP ------------------ -`stamper = multi:newTimeStamper()` - -Watchers --------- -`watcher = multi:newWatcher(STRING name)` -`watcher = multi:newWatcher(TABLE namespace, STRING name)` - -Custom Objects +Example +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +test = {a=0} +watcher = multi:newWatcher(test,"a") +watcher:OnValueChanged(function(self, old_value, current_value) + print(old_value,current_value) +end) +multi:newTLoop(function() + test.a=test.a + 1 +end,.5) +multi:mainloop() +``` +Actor: Custom Object -------------- -`cobj = multi:newCustomObject(TABLE objRef, BOOLEAN isActor)` +`cobj = multi:newCustomObject(TABLE objRef, BOOLEAN isActor [false])` -- Allows you to create your own multiobject that runs each allotted step. This allows you to create your own object that works with all the features that each built in multi object does. If isActor is set to true you must have an `Act` method in your table. See example below. If an object is not an actor than the `Act` method will not be automatically called for you. -Coroutine based Threading +Example: +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +local work = false +ticktock = multi:newCustomObject({ + timer = multi:newTimer(), + Act = function(self) + if self.timer:Get()>=1 then + work = not work + if work then + self.OnTick:Fire() + else + self.OnTock:Fire() + end + self.timer:Reset() + end + end, + OnTick = multi:newConnection(), + OnTock = multi:newConnection(), +},true) +ticktock.OnTick(function() + print("Tick") +end) +ticktock.OnTock(function() + print("Tock") +end) +multi:mainloop() +``` + +Coroutine based Threading (CBT) ------------------------- -This was made due to the limitations of multiObj:hold(), which no longer exists. When this library was in its infancy and before I knew about coroutines, I actually tried to emulate what coroutines did in pure lua. (Was ok, but only allowed for one pause of code ececution) Many objects were given threaded variants though, none are really needed since the base thread objects ant thread.* can be used to emulate those. +This was made due to the limitations of multiObj:hold(), which no longer exists. When this library was in its infancy and before I knew about coroutines, I actually tried to emulate what coroutines did in pure lua. +The threaded bariants of the non threaded objects do exist, but there isn't too much of a need to use them. + +The main benefits of using the coroutine based threads it the thread.* namespace which gives you the ability to easily run code side by side. + +A quick not on how threads are managed in the library. The library contains a scheduler which keeps track of coroutines and manages them. Coroutines take some time then give off processing to another coroutine. Which means there are some methods that you need to use in order to hand off cpu time to other coroutines or the main thread. + +threads.* +--------- +`thread.sleep(NUMBER n)` -- Holds execution of the thread until a certain amount of time has passed +`thread.hold(FUNCTION func)` -- Hold execttion until the function returns true +`thread.skip(NUMBER n)` -- How many cycles should be skipped until I execute again +`thread.kill()` -- Kills the thread +`thread.yeild()` -- Is the same as using thread.skip(0) or thread.sleep(0), hands off control until the next cycle +`thread.isThread()` -- Returns true if the current running code is inside of a coroutine based thread +`thread.getCores()` -- Returns the number of cores that the current system has. (used for system threads) +`thread.set(STRING name, VARIABLE val)` -- A global interface where threads can talk with eachother. sets a variable with name and its value +`thread.get(STRING name)` -- Gets the data stored in name +`thread.waitFor(STRING name)` -- Holds executon of a thread until variable name exists +`thread.testFor(STRING name,VARIABLE val,STRING sym)` -- holds execution untile variable name exists and is compared to val +sym can be equal to: "=", "==", "<", ">", "<=", or ">=" the way comparisan works is: "`return val sym valTested`" + +CBT: Thread +----------- + +`multi:newThread(STRING name,FUNCTION func)` -- Creates a new thread with name and function. +Note: newThread() returns nothing. Threads are opperated hands off everything that happens, does so inside of its functions. + +Threads simplify many things that you would use non CBT objects for. I almost solely use CBT for my current programming. I will slso show the above custom object using threads instead. Yes its cool and can be done. + +Examples: +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +multi:newThread("Example of basic usage",function() + while true do + thread.sleep(1) + print("We just made an alarm!") + end +end) +multi:mainloop() +``` +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") + +function multi:newTickTock() + local work = false + local _alive = true + local OnTick = multi:newConnection() + local OnTock = multi:newConnection() + local c =multi:newCustomObject{ + OnTick = OnTick, + OnTock = OnTock, + Destroy = function() + _alive = false -- Threads at least how they work here now need a bit of data management for cleaning up objects. When a thread either finishes its execution of thread.kill() is called everything is removed from the scheduler letting lua know that it can garbage collect + end + } + multi:newThread("TickTocker",function() + while _alive do + thread.sleep(1) + work = not work + if work then + OnTick:Fire() + else + OnTock:Fire() + end + end + thread.kill() -- When a thread gets to the end of it's ececution it will automatically be ended, but having this method is good to show what is going on with your code. + end) + return c +end +ticktock = multi:newTickTock() +ticktock.OnTick(function() + print("Tick") + -- The thread.* namespace works in all events that +end) +ticktock.OnTock(function() + print("Tock") +end) +multi:mainloop() +``` + +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") + +multi:newThread("TickTocker",function() + print("Waiting for variable a to exist...") + ret,ret2 = thread.hold(function() + return a~=nil, "test!" + end) + print(ret,ret2) -- The hold method returns the arguments when the first argument is true. This methods return feature is rather new and took more work then you think to get working. Since threads +end) +multi:newAlarm(3):OnRing(function() a = true end) -- allows a to exist + +multi:mainloop() +``` + +CBT: Process +----------- +`process = multi:newThreadedProcess(STRING name)` -- Creates a process object that is able allows all processes created on it to use the thread.* namespace System Threads - Multi-core --------------------------- diff --git a/changes.md b/changes.md index 98bd1a9..540a064 100644 --- a/changes.md +++ b/changes.md @@ -4,14 +4,28 @@ Update 13.0.0 So you documented it, finally, but it's sad to see some things go ------------- 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 +- A few things, to make concepts in the library more clear - The way functions returned paused status. Before it would return "PAUSED" now it returns nil, true if paused +- Modified the connection object to allow for some more syntaxial suger! +Connection Example: +```lua +loop = multi:newTLoop(function(self) + self:OnLoops() -- new way to Fire a connection! Only works when used on a multi object, bin objects, or any object that contains a Type parameter +end,1) +loop.OnLoops = multi:newConnection() +loop.OnLoops(function() + print("Looping") +end) +multi:mainloop() +``` Removed: - Ranges and conditions -- corutine based threads can dmulate what these objects did and much better! - +Fixed: +- There were some bugs in the networkmanager.lua file. Desrtoy -> Destroy some misspellings. Added: ... diff --git a/examples/RegisteredFunctions.dat b/examples/RegisteredFunctions.dat new file mode 100644 index 0000000..e69de29 diff --git a/examples/network-node1.lua b/examples/network-node1.lua index 4a828a5..8d3b0ca 100644 --- a/examples/network-node1.lua +++ b/examples/network-node1.lua @@ -5,30 +5,13 @@ nGLOBAL = require("multi.integration.networkManager").init() node = multi:newNode{ crossTalk = false, -- default value, allows nodes to talk to eachother. WIP NOT READY YET! allowRemoteRegistering = true, -- allows you to register functions from the master on the node, default is false - name = nil, --"TESTNODE", -- default value is nil, if nil a random name is generated. Naming nodes are important if you assign each node on a network with a different task + name = "MASTERPC", -- default value is nil, if nil a random name is generated. Naming nodes are important if you assign each node on a network with a different task --noBroadCast = true, -- if using the node manager, set this to true to save on some cpu cycles --managerDetails = {"localhost",12345}, -- connects to the node manager if one exists } function RemoteTest(a,b,c) -- a function that we will be executing remotely - --print("Yes I work!",a,b,c) - multi:newThread("waiter",function() - print("Hello!") - while true do - thread.sleep(2) - node:pushTo("Main","This is a test") - end - end) + print("Yes I work!",a,b,c) end -multi:newThread("some-test",function() - local dat = node:pop() - while true do - thread.skip(10) - if dat then - print(dat) - end - dat = node:pop() - end -end,"NODE_TESTNODE") settings = { priority = 0, -- 1 or 2 stopOnError = true, diff --git a/multi/compat/love2d.lua b/multi/compat/love2d.lua index fe77db1..917a561 100644 --- a/multi/compat/love2d.lua +++ b/multi/compat/love2d.lua @@ -37,6 +37,7 @@ multi.OnMouseMoved = multi:newConnection() multi.OnDraw = multi:newConnection() multi.OnTextInput = multi:newConnection() multi.OnUpdate = multi:newConnection() +multi.OnQuit = multi:newConnection() multi.OnPreLoad(function() local function Hook(func,conn) if love[func]~=nil then @@ -51,6 +52,7 @@ multi.OnPreLoad(function() end end end + Hook("quit",multi.OnQuit) Hook("keypressed",multi.OnKeyPressed) Hook("keyreleased",multi.OnKeyReleased) Hook("mousepressed",multi.OnMousePressed) @@ -67,4 +69,8 @@ multi.OnPreLoad(function() end end) end) -return multi \ No newline at end of file +multi.OnQuit(function() + multi.Stop() + love.event.quit() +end) +return multi diff --git a/multi/init.lua b/multi/init.lua index fbe8466..1f000bc 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -551,10 +551,18 @@ function multi:newTimer() self:create(c) return c end -function multi:newConnection(protect) +function multi:newConnection(protect,func) local c={} + c.callback = func c.Parent=self - setmetatable(c,{__call=function(self,...) return self:connect(...) end}) + setmetatable(c,{__call=function(self,...) + local t = ... + if type(t)=="table" and t.Type ~= nil then + return self:Fire(args,select(2,...)) + else + return self:connect(...) + end + end}) c.Type='connector' c.func={} c.ID=0 @@ -661,6 +669,9 @@ function multi:newConnection(protect) if name then self.connections[name]=temp end + if self.callback then + self.callback(temp) + end return temp end c.Connect=c.connect @@ -1057,7 +1068,29 @@ function multi:newTStep(start,reset,count,set) return c end function multi:newTimeStamper() - local c=self:newBase() + local c=self:newUpdater(self.Priority_Idle) + c:OnUpdate(function() + c:Run() + end) + local feb = 28 + local leap = tonumber(os.date("%Y"))%4==0 and (tonumber(os.date("%Y"))%100~=0 or tonumber(os.date("%Y"))%400==0) + if leap then + feb = 29 + end + local dInM = { + ["01"] = 31, + ["02"] = feb, + ["03"] = 31, + ["04"] = 30, + ["05"] = 31, + ["06"] = 30, + ["07"] = 31, -- This is dumb, why do we still follow this double 31 days!? + ["08"] = 31, + ["09"] = 30, + ["10"] = 31, + ["11"] = 30, + ["12"] = 31, + } c.Type='timestamper' c.Priority=self.Priority_Idle c.hour = {} @@ -1067,7 +1100,7 @@ function multi:newTimeStamper() c.day = {} c.month = {} c.year = {} - function c:Act() + function c:Run() for i=1,#self.hour do if self.hour[i][1]==os.date("%H") and self.hour[i][3] then self.hour[i][2](self) @@ -1101,10 +1134,14 @@ function multi:newTimeStamper() self.day[i][3]=true end else - if string.format("%02d",self.day[i][1])==os.date("%d") and self.day[i][3] then + local dday = self.day[i][1] + if dday < 0 then + dday = dInM[os.date("%m")]+(dday+1) + end + if string.format("%02d",dday)==os.date("%d") and self.day[i][3] then self.day[i][2](self) self.day[i][3]=false - elseif string.format("%02d",self.day[i][1])~=os.date("%d") and not self.day[i][3] then + elseif string.format("%02d",dday)~=os.date("%d") and not self.day[i][3] then self.day[i][3]=true end end @@ -1243,8 +1280,23 @@ function thread.waitFor(name) thread.hold(function() return thread.get(name)~=nil end) return thread.get(name) end -function thread.testFor(name,val,sym) - thread.hold(function() return thread.get(name)~=nil end) +function thread.testFor(name,_val,sym) + thread.hold(function() + local val = thread.get(name)~=nil + if val then + if sym == "==" or sym == "=" then + return _val==val + elseif sym == ">" then + return _val>val + elseif sym == "<" then + return _val=" then + return _val>=val + end + end + end) return thread.get(name) end function multi:newTBase(name) @@ -1676,7 +1728,6 @@ function multi:newThreadedProcess(name) setmetatable(c, multi) function c:newBase(ins) local ct = {} - setmetatable(ct, self.Parent) ct.Active=true ct.func={} ct.ender={} diff --git a/multi/integration/networkManager.lua b/multi/integration/networkManager.lua index ca620de..40e7f3c 100644 --- a/multi/integration/networkManager.lua +++ b/multi/integration/networkManager.lua @@ -23,7 +23,7 @@ SOFTWARE. ]] local multi = require("multi") local net = require("net") -require("bin") +local bin = require("bin") bin.setBitsInterface(infinabits) -- the bits interface does not work so well, another bug to fix -- Commands that the master and node will respect, max of 256 commands @@ -142,17 +142,20 @@ function multi:nodeManager(port) server.OnDataRecieved(function(server,data,cid,ip,port) local cmd = data:sub(1,1) if cmd == "R" then - multi:newTLoop(function(loop) - if server.timeouts[cid]==true then - server.OnNodeRemoved:Fire(server.nodes[cid]) - server.nodes[cid] = nil - server.timeouts[cid] = nil - loop:Destroy() - return + multi:newThread("Test",function(loop) + while true do + if server.timeouts[cid]==true then + server.OnNodeRemoved:Fire(server.nodes[cid]) + server.nodes[cid] = nil + server.timeouts[cid] = nil + thread.kill() + else + server.timeouts[cid] = true + server:send(cid,"ping") + end + thread.sleep(1) end - server.timeouts[cid] = true - server:send(cid,"ping") - end,1) + end) server.nodes[cid]=data:sub(2,-1) server.OnNodeAdded:Fire(server.nodes[cid]) elseif cmd == "G" then @@ -436,12 +439,15 @@ function multi:newMaster(settings) -- You will be able to have more than one mas name = self:getRandomNode() end if name==nil then - multi:newTLoop(function(loop) - if name~=nil then - self:sendTo(name,char(CMD_TASK)..len..aData..len2..fData) - loop:Desrtoy() + multi:newThread("Test",function(loop) + while true do + if name~=nil then + self:sendTo(name,char(CMD_TASK)..len..aData..len2..fData) + thread.kill() + end + thread.sleep(.1) end - end,.1) + end) else self:sendTo(name,char(CMD_TASK)..len..aData..len2..fData) end @@ -455,12 +461,15 @@ function multi:newMaster(settings) -- You will be able to have more than one mas name = "NODE_"..name end if self.connections[name]==nil then - multi:newTLoop(function(loop) - if self.connections[name]~=nil then - self.connections[name]:send(data) - loop:Destroy() + multi:newThread("Test",function(loop) + while true do + if self.connections[name]~=nil then + self.connections[name]:send(data) + thread.kill() + end + thread.sleep(.1) end - end,.1) + end) else self.connections[name]:send(data) end @@ -495,16 +504,19 @@ function multi:newMaster(settings) -- You will be able to have more than one mas client.OnClientReady(function() client:send(char(CMD_INITMASTER)..master.name) -- Tell the node that you are a master trying to connect if not settings.managerDetails then - multi:newTLoop(function(loop) - if master.timeouts[name]==true then - master.timeouts[name] = nil - master.connections[name] = nil - loop:Destroy() - return + multi:newThread("Test",function(loop) + while true do + if master.timeouts[name]==true then + master.timeouts[name] = nil + master.connections[name] = nil + thread.kill() + else + master.timeouts[name] = true + master:sendTo(name,char(CMD_PING)) + end + thread.sleep(1) end - master.timeouts[name] = true - master:sendTo(name,char(CMD_PING)) - end,1) + end) end client.name = name client.OnDataRecieved(function(client,data) diff --git a/test.lua b/test.lua index 694a227..b39089c 100644 --- a/test.lua +++ b/test.lua @@ -1,8 +1,14 @@ package.path="?/init.lua;?.lua;"..package.path local multi = require("multi") -alarm=multi:newAlarm(3) -- in seconds can go to .001 uses the built in os.clock() -alarm:OnRing(function(a) - print("3 Seconds have passed!") - a:Reset(n) -- if n were nil it will reset back to 3, or it would reset to n seconds + +multi:newThread("TickTocker",function() + print("Waiting for variable a to exist...") + ret,ret2 = thread.hold(function() + return a~=nil, "test!" + end) + print(ret,ret2) -- The hold method returns the arguments when the first argument is true. This methods return feature is rather new and took more work then you think to get working. Since threads end) +multi:newAlarm(3):OnRing(function() a = true end) -- allows a to exist + multi:mainloop() + -- 2.43.0 From 9b5acbd23e4b43b44f0c865e941b7e50bbdbd147 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Wed, 14 Nov 2018 10:27:39 -0500 Subject: [PATCH 05/20] 13.0.0 Will be out soooooooon I promise Anyway added some new objects, removed a few and wrote some more documentation... all thats left is system threads and network threads(nodes) --- Documentation.md | 72 ++++++- changes.md | 41 +++- multi/init.lua | 511 ++++++++++++----------------------------------- test.lua | 20 +- 4 files changed, 240 insertions(+), 404 deletions(-) diff --git a/Documentation.md b/Documentation.md index 08b931a..57dcdab 100644 --- a/Documentation.md +++ b/Documentation.md @@ -729,10 +729,74 @@ multi:newAlarm(3):OnRing(function() a = true end) -- allows a to exist multi:mainloop() ``` -CBT: Process ------------ +CBT: Threaded Process +--------------------- `process = multi:newThreadedProcess(STRING name)` -- Creates a process object that is able allows all processes created on it to use the thread.* namespace -System Threads - Multi-core ---------------------------- +`nil = process:getController()` -- Returns nothing there is no "controller" when using threaded processes +`self = process:Start()` -- Starts the processor +`self = process:Pause()` -- Pauses the processor +`self = process:Resume()` -- Resumes a paused processor +`self = process:Kill()` -- Kills/Destroys the process thread +`self = process:Remove()` -- Destroys/Kills the processor and all of the Actors running on it +`self = process:Sleep(NUMBER n)` -- Forces a process to sleep for n amount of time +`self = process:Hold(FUNCTION/NUMBER n)` -- Forces a process to either test a condition or sleep. +Everything eles works as if you were using the multi.* interface. You can create multi objects on the process and the objects are able to use the thread.* interface. + +Note: When using Hold/Sleep/Skip on an object created inside of a threaded process, you actually hold the entire process! Which means all objects on that process will be stopping until the conditions are met! + +Example: +```lua +test = multi:newThreadedProcess("test") +test:newLoop(function() + print("HI!") +end) +test:newLoop(function() + print("HI2!") + thread.sleep(.5) +end) +multi:newAlarm(3):OnRing(function() + test:Sleep(10) +end) +test:Start() +multi:mainloop() +``` + +CBT: Hyper Threaded Process +--------------------------- +`process = multi:newHyperThreadedProcess(STRING name)` -- Creates a process object that is able allows all processes created on it to use the thread.* namespace. Hold/Sleep/Skip can be used in each multi obj created without stopping each other object that is running. + +`nil = process:getController()` -- Returns nothing there is no "controller" when using threaded processes +`self = process:Start()` -- Starts the processor +`self = process:Pause()` -- Pauses the processor +`self = process:Resume()` -- Resumes a paused processor +`self = process:Kill()` -- Kills/Destroys the process thread +`self = process:Remove()` -- Destroys/Kills the processor and all of the Actors running on it +`self = process:Sleep(NUMBER n)` -- Forces a process to sleep for n amount of time +`self = process:Hold(FUNCTION/NUMBER n)` -- Forces a process to either test a condition or sleep. + +Example: +```lua +test = multi:newHyperThreadedProcess("test") +test:newLoop(function() + print("HI!") +end) +test:newLoop(function() + print("HI2!") + thread.sleep(.5) +end) +multi:newAlarm(3):OnRing(function() + test:Sleep(10) +end) +test:Start() +multi:mainloop() +``` +Same example as above, but notice how this works opposed to the non hyper version + +System Threads - Multi-Integration +---------------------------------- + + +Network Threads - Multi-Integration +----------------------------------- diff --git a/changes.md b/changes.md index 540a064..65136bb 100644 --- a/changes.md +++ b/changes.md @@ -2,7 +2,7 @@ [TOC] Update 13.0.0 So you documented it, finally, but it's sad to see some things go isn't it? ------------- -Fixed: Tons of bugs, I actually went through the entire library and did a full test of everything while writing the documentation. +Fixed: Tons of bugs, I actually went through the entire library and did a full test of everything, I mean everything, while writing the documentation. Changed: - A few things, to make concepts in the library more clear - The way functions returned paused status. Before it would return "PAUSED" now it returns nil, true if paused @@ -22,12 +22,47 @@ multi:mainloop() Removed: - Ranges and conditions -- corutine based threads can dmulate what these objects did and much better! -- +- Due to the creation of hyper threaded processes the following objects are no more! +-- ~~multi:newThreadedEvent()~~ +-- ~~multi:newThreadedLoop()~~ +-- ~~multi:newThreadedTLoop()~~ +-- ~~multi:newThreadedStep()~~ +-- ~~multi:newThreadedTStep()~~ +-- ~~multi:newThreadedAlarm()~~ +-- ~~multi:newThreadedUpdater()~~ + +These didn't have much use in their previous form, but with the addition of hyper threaded processes the goals that these objects aimed to solve are now possible using a process Fixed: - There were some bugs in the networkmanager.lua file. Desrtoy -> Destroy some misspellings. -Added: ... +Added: +- multi:newHyperThreadedProcess(STRING name) -- This is a version of the threaded process that gives each object created its own coroutine based thread which means you can use thread.* without affecting other objects created within the hyper threaded processes. +- multi:newConnector() -- A simple object that allows you to use the new connection Fire syntax without using a multi obj + +```lua +package.path="?/init.lua;?.lua;"..package.path +local multi = require("multi") +conn = multi:newConnector() +conn.OnTest = multi:newConnection() +conn.OnTest(function() + print("Yes!") +end) +test = multi:newHyperThreadedProcess("test") +test:newTLoop(function() + print("HI!") + conn:OnTest() +end,1) +test:newLoop(function() + print("HI2!") + thread.sleep(.5) +end) +multi:newAlarm(3):OnRing(function() + test:Sleep(10) +end) +test:Start() +multi:mainloop() +``` Update 12.2.2 Time for some more bug fixes! ------------- diff --git a/multi/init.lua b/multi/init.lua index 1f000bc..e9bb5bf 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -430,7 +430,7 @@ function multi:isDone() end multi.IsDone=multi.isDone function multi:create(ref) - multi.OnObjectCreated:Fire(ref) + multi.OnObjectCreated:Fire(ref,self) end --Constructors [CORE] function multi:newBase(ins) @@ -551,6 +551,10 @@ function multi:newTimer() self:create(c) return c end +function multi:newConnector() + local c = {Type = "connector"} + return c +end function multi:newConnection(protect,func) local c={} c.callback = func @@ -800,26 +804,28 @@ function multi:newAlarm(set) local c=self:newBase() c.Type='alarm' c.Priority=self.Priority_Low - c.timer=self:newTimer() c.set=set or 0 + local count = 0 + local t = clock() function c:Act() - if self.timer:Get()>=self.set then + if clock()-t>=self.set then self:Pause() self.Active=false for i=1,#self.func do self.func[i](self) end + t = clock() end end function c:Resume() self.Parent.Resume(self) - self.timer:Resume() + t = count + t return self end function c:Reset(n) if n then self.set=n end self:Resume() - self.timer:Reset() + t = clock() return self end function c:OnRing(func) @@ -827,7 +833,7 @@ function multi:newAlarm(set) return self end function c:Pause() - self.timer:Pause() + count = clock() self.Parent.Pause(self) return self end @@ -1432,299 +1438,10 @@ multi.scheduler:OnLoop(function(self) end) multi.scheduler:Pause() multi.OnError=multi:newConnection() -function multi:newThreadedAlarm(name,set) - local c=self:newTBase(name) - c.Type='alarmThread' - c.timer=self:newTimer() - c.set=set or 0 - function c:Resume() - self.rest=false - self.timer:Resume() - return self - end - function c:Reset(n) - if n then self.set=n end - self.rest=false - self.timer:Reset(n) - return self - end - function c:OnRing(func) - table.insert(self.func,func) - return self - end - function c:Pause() - self.timer:Pause() - self.rest=true - return self - end - c.rest=false - c.updaterate=multi.Priority_Low -- skips - c.restRate=0 -- secs - multi:newThread(name,function(ref) - while true do - if c.rest then - thread.sleep(c.restRate) -- rest a bit more when a thread is paused - else - if c.timer:Get()>=c.set then - c:Pause() - for i=1,#c.func do - c.func[i](c) - end - end - thread.skip(c.updaterate) -- lets rest a bit - end - end - end) - self:create(c) - return c -end -function multi:newThreadedUpdater(name,skip) - local c=self:newTBase(name) - c.Type='updaterThread' - c.pos=1 - c.skip=skip or 1 - function c:Resume() - self.rest=false - return self - end - function c:Pause() - self.rest=true - return self - end - c.OnUpdate=self.OnMainConnect - c.rest=false - c.updaterate=0 - c.restRate=.75 - multi:newThread(name,function(ref) - while true do - if c.rest then - thread.sleep(c.restRate) -- rest a bit more when a thread is paused - else - for i=1,#c.func do - c.func[i](c) - end - c.pos=c.pos+1 - thread.skip(c.skip) - end - end - end) - self:create(c) - return c -end -function multi:newThreadedTStep(name,start,reset,count,set) - local c=self:newTBase(name) - local think=1 - c.Type='tstepThread' - c.start=start or 1 - local reset = reset or math.huge - c.endAt=reset - c.pos=start or 1 - c.skip=skip or 0 - c.count=count or 1*think - c.funcE={} - c.timer=os.clock() - c.set=set or 1 - c.funcS={} - function c:Update(start,reset,count,set) - self.start=start or self.start - self.pos=self.start - self.endAt=reset or self.endAt - self.set=set or self.set - self.count=count or self.count or 1 - self.timer=os.clock() - self:Resume() - return self - end - function c:Resume() - self.rest=false - return self - end - function c:Pause() - self.rest=true - return self - end - function c:OnStart(func) - table.insert(self.funcS,func) - return self - end - function c:OnStep(func) - table.insert(self.func,func) - return self - end - function c:OnEnd(func) - table.insert(self.funcE,func) - return self - end - function c:Break() - self.Active=nil - return self - end - function c:Reset(n) - if n then self.set=n end - self.timer=os.clock() - self:Resume() - return self - end - c.updaterate=0--multi.Priority_Low -- skips - c.restRate=0 - multi:newThread(name,function(ref) - while true do - if c.rest then - thread.sleep(c.restRate) -- rest a bit more when a thread is paused - else - if os.clock()-c.timer>=c.set then - c:Reset() - if c.pos==c.start then - for fe=1,#c.funcS do - c.funcS[fe](c) - end - end - for i=1,#c.func do - c.func[i](c,c.pos) - end - c.pos=c.pos+c.count - if c.pos-c.count==c.endAt then - c:Pause() - for fe=1,#c.funcE do - c.funcE[fe](c) - end - c.pos=c.start - end - end - thread.skip(c.updaterate) -- lets rest a bit - end - end - end) - self:create(c) - return c -end -function multi:newThreadedTLoop(name,func,n) - local c=self:newTBase(name) - c.Type='tloopThread' - c.restN=n or 1 - if func then - c.func={func} - end - function c:Resume() - self.rest=false - return self - end - function c:Pause() - self.rest=true - return self - end - function c:OnLoop(func) - table.insert(self.func,func) - return self - end - c.rest=false - c.updaterate=0 - c.restRate=.75 - multi:newThread(name,function(ref) - while true do - if c.rest then - thread.sleep(c.restRate) -- rest a bit more when a thread is paused - else - for i=1,#c.func do - c.func[i](c) - end - thread.sleep(c.restN) -- lets rest a bit - end - end - end) - self:create(c) - return c -end -function multi:newThreadedStep(name,start,reset,count,skip) - local c=self:newTBase(name) - local think=1 - c.Type='stepThread' - c.pos=start or 1 - c.endAt=reset or math.huge - c.skip=skip or 0 - c.spos=0 - c.count=count or 1*think - c.funcE={} - c.funcS={} - c.start=start or 1 - if start~=nil and reset~=nil then - if start>reset then - think=-1 - end - end - function c:Resume() - self.rest=false - return self - end - function c:Pause() - self.rest=true - return self - end - c.Reset=c.Resume - function c:OnStart(func) - table.insert(self.funcS,func) - return self - end - function c:OnStep(func) - table.insert(self.func,1,func) - return self - end - function c:OnEnd(func) - table.insert(self.funcE,func) - return self - end - function c:Break() - self.rest=true - return self - end - function c:Update(start,reset,count,skip) - self.start=start or self.start - self.endAt=reset or self.endAt - self.skip=skip or self.skip - self.count=count or self.count - self:Resume() - return self - end - c.updaterate=0 - c.restRate=.1 - multi:newThread(name,function(ref) - while true do - if c.rest then - ref:sleep(c.restRate) -- rest a bit more when a thread is paused - else - if c~=nil then - if c.spos==0 then - if c.pos==c.start then - for fe=1,#c.funcS do - c.funcS[fe](c) - end - end - for i=1,#c.func do - c.func[i](c,c.pos) - end - c.pos=c.pos+c.count - if c.pos-c.count==c.endAt then - c:Pause() - for fe=1,#c.funcE do - c.funcE[fe](c) - end - c.pos=c.start - end - end - end - c.spos=c.spos+1 - if c.spos>=c.skip then - c.spos=0 - end - ref:sleep(c.updaterate) -- lets rest a bit - end - end - end) - self:create(c) - return c -end function multi:newThreadedProcess(name) local c = {} + local holding = false + local kill = false setmetatable(c, multi) function c:newBase(ins) local ct = {} @@ -1743,7 +1460,7 @@ function multi:newThreadedProcess(name) c.Active=true c.func={} c.Id=0 - c.Type='process' + c.Type='threadedprocess' c.Mainloop={} c.Garbage={} c.Children={} @@ -1775,109 +1492,127 @@ function multi:newThreadedProcess(name) self.ref:kill() return self end - function c:kill() - err=coroutine.yield({"_kill_"}) - if err then - error("Failed to kill a thread! Exiting...") + function c:Kill() + kill = true + return self + end + function c:Sleep(n) + holding = true + if type(n)=="number" then + multi:newAlarm(n):OnRing(function(a) + holding = false + a:Destroy() + end) + elseif type(n)=="function" then + multi:newEvent(n):OnEvent(function(e) + holding = false + e:Destroy() + end) end return self end - function c:sleep(n) - if type(n)=="function" then - ret=coroutine.yield({"_hold_",n}) - elseif type(n)=="number" then - n = tonumber(n) or 0 - ret=coroutine.yield({"_sleep_",n}) - else - error("Invalid Type for sleep!") - end - return self - end - c.hold=c.sleep + c.Hold=c.Sleep multi:newThread(name,function(ref) while true do - if c.rest then - ref:Sleep(c.restRate) -- rest a bit more when a thread is paused - else - c:uManager() - ref:sleep(c.updaterate) -- lets rest a bit - end + thread.hold(function() + return not(holding) + end) + c:uManager() end end) return c end -function multi:newThreadedLoop(name,func) - local c=self:newTBase(name) - c.Type='loopThread' - c.Start=os.clock() - if func then - c.func={func} - end - function c:Resume() - self.rest=false - return self - end - function c:Pause() - self.rest=true - return self - end - function c:OnLoop(func) - table.insert(self.func,func) - return self - end - c.rest=false - c.updaterate=0 - c.restRate=.75 - multi:newThread(name,function(ref) - while true do - if c.rest then - thread.sleep(c.restRate) -- rest a bit more when a thread is paused - else - for i=1,#c.func do - c.func[i](os.clock()-self.Start,c) - end - thread.sleep(c.updaterate) -- lets rest a bit - end - end - end) - self:create(c) - return c -end -function multi:newThreadedEvent(name,task) - local c=self:newTBase(name) - c.Type='eventThread' - c.Task=task or function() end - function c:OnEvent(func) - table.insert(self.func,func) - return self - end - function c:Resume() - self.rest=false - return self - end - function c:Pause() - self.rest=true - return self - end - c.rest=false - c.updaterate=0 - c.restRate=1 - multi:newThread(name,function(ref) - while true do - if c.rest then - ref:sleep(c.restRate) -- rest a bit more when a thread is paused - else - if c.Task(self) then - for _E=1,#c.func do - c.func[_E](c) +function multi:newHyperThreadedProcess(name) + if not name then error("All threads must have a name!") end + local c = {} + setmetatable(c, multi) + local ind = 0 + local holding = true + local kill = false + function c:newBase(ins) + local ct = {} + ct.Active=true + ct.func={} + ct.ender={} + ct.Id=0 + ct.Act=function() end + ct.Parent=self + ct.held=false + ct.ref=self.ref + ind = ind + 1 + multi:newThread("Proc <"..name.."> #"..ind,function() + while true do + thread.hold(function() + return not(holding) + end) + if kill then + err=coroutine.yield({"_kill_"}) + if err then + error("Failed to kill a thread! Exiting...") end - c:Pause() end - ref:sleep(c.updaterate) -- lets rest a bit + ct:Act() end + end) + return ct + end + c.Parent=self + c.Active=true + c.func={} + c.Id=0 + c.Type='hyperthreadedprocess' + c.Mainloop={} + c.Garbage={} + c.Children={} + c.Active=true + c.Id=-1 + c.Rest=0 + c.updaterate=.01 + c.restRate=.1 + c.Jobs={} + c.queue={} + c.jobUS=2 + c.rest=false + function c:getController() + return nil + end + function c:Start() + holding = false + return self + end + function c:Resume() + holding = false + return self + end + function c:Pause() + holding = true + return self + end + function c:Remove() + self.ref:kill() + return self + end + function c:Kill() + kill = true + return self + end + function c:Sleep(b) + holding = true + if type(b)=="number" then + local t = os.clock() + multi:newAlarm(b):OnRing(function(a) + holding = false + a:Destroy() + end) + elseif type(b)=="function" then + multi:newEvent(b):OnEvent(function(e) + holding = false + e:Destroy() + end) end - end) - self:create(c) + return self + end + c.Hold=c.Sleep return c end -- Multi runners diff --git a/test.lua b/test.lua index b39089c..a603ea6 100644 --- a/test.lua +++ b/test.lua @@ -1,14 +1,16 @@ package.path="?/init.lua;?.lua;"..package.path local multi = require("multi") - -multi:newThread("TickTocker",function() - print("Waiting for variable a to exist...") - ret,ret2 = thread.hold(function() - return a~=nil, "test!" - end) - print(ret,ret2) -- The hold method returns the arguments when the first argument is true. This methods return feature is rather new and took more work then you think to get working. Since threads +test = multi:newHyperThreadedProcess("test") +test:newLoop(function() + print("HI!") end) -multi:newAlarm(3):OnRing(function() a = true end) -- allows a to exist - +test:newLoop(function() + print("HI2!") + thread.sleep(.5) +end) +multi:newAlarm(3):OnRing(function() + test:Sleep(10) +end) +test:Start() multi:mainloop() -- 2.43.0 From 0abd8183b5d3f00e354152aab8762752290be073 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Fri, 4 Jan 2019 13:59:38 -0500 Subject: [PATCH 06/20] still working on the doc, soon --- .gitignore | 1 + Documentation.md | 60 ++++++++++++++++++++++++++++++++++-- multi/integration/shared.lua | 2 +- 3 files changed, 60 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 2d543a2..82d723d 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,4 @@ lanestestclient.lua lanestest.lua sample-node.lua sample-master.lua +Ayn Rand - The Virtue of Selfishness-Mg4QJheclsQ.m4a diff --git a/Documentation.md b/Documentation.md index 57dcdab..094c1c7 100644 --- a/Documentation.md +++ b/Documentation.md @@ -794,9 +794,65 @@ multi:mainloop() ``` Same example as above, but notice how this works opposed to the non hyper version -System Threads - Multi-Integration ----------------------------------- +System Threads (ST) - Multi-Integration Getting Started +------------------------------------------------------- +The system threads need to be required seperatly. +```lua +local GLOBAL, THREAD = require("multi.integration.lanesManager").init() -- We will talk about the global and thread interface that is returned +GLOBAL, THREAD = require("multi.integration.loveManager").init() +GLOBAL, THREAD = require("luvitManager")-- There is a catch to this* +``` +Using this integration modifies some methods that the multi library has. +`multi:canSystemThread()` -- Returns true is system threading is possible +`multi:getPlatform()` -- Returns (for now) either "lanes", "love2d" and "luvit" +This variable is created on the main thread only inside of the multi namespace: multi.isMainThread = true +This is used to know which thread is the main thread. When network threads are being discussed there is a gotcha that needs to be addressed. +*** GLOBAL and THREAD do not work currently when using the luvit integration + +ST - THREAD namespace +--------------------- +`THREAD.set(STRING name, VALUE val)` -- Sets a value in GLOBAL +`THREAD.get(STRING name)` -- Gets a value in GLOBAL +`THREAD.waitFor(STRING name)` -- Waits for a value in GLOBAL to exist +`THREAD.testFor(STRING name, VALUE val, STRING sym)` -- **NOT YET IMPLEMENTED** +`THREAD.getCores()` -- Returns the number of actual system threads/cores +`THREAD.kill()` -- Kills the thread +`THREAD.getName()` -- Returns the name of the working thread +`THREAD.sleep(NUMBER n)` -- Sleeps for an amount of time stopping the current thread +`THREAD.hold(FUNCTION func)` -- Holds the current thread until a condition is met + +ST - GLOBAL namespace +--------------------- +Treat global like a table. +```lua +GLOBAL["name"] = "Ryan" +print(GLOBAL["name"]) +``` +Removes the need to use THREAD.set() and THREAD.get() +ST - System Threads +------------------- + +ST - SystemThreadedQueue +------------------------ + +ST - SystemThreadedConnection +----------------------------- + +ST - SystemThreadedBenchmark +---------------------------- + +ST - SystemThreadedConsole +-------------------------- + +ST - SystemThreadedTable +------------------------ + +ST - SystemThreadedJobQueue +--------------------------- + +ST - SystemThreadedExecute +-------------------------- Network Threads - Multi-Integration ----------------------------------- diff --git a/multi/integration/shared.lua b/multi/integration/shared.lua index 1d16d2a..fb1f599 100644 --- a/multi/integration/shared.lua +++ b/multi/integration/shared.lua @@ -193,7 +193,7 @@ function multi:newSystemThreadedConnection(name,protect) GLOBAL[name]=c return c end -function multi:systemThreadedBenchmark(n) +function multi:SystemThreadedBenchmark(n) n=n or 1 local cores=multi.integration.THREAD.getCores() local queue=multi:newSystemThreadedQueue("THREAD_BENCH_QUEUE"):init() -- 2.43.0 From 4040e0d9d0f9afe784e9f38044b49711e05b5e1f Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Mon, 28 Jan 2019 15:53:32 -0500 Subject: [PATCH 07/20] some more updates --- .gitignore | 1 + Documentation.md | 172 +++++++++++++- changes.md | 44 +++- multi/init.lua | 339 ++++++++++++++++----------- multi/integration/networkManager.lua | 9 +- multi/integration/shared.lua | 9 +- test.lua | 48 +++- 7 files changed, 445 insertions(+), 177 deletions(-) diff --git a/.gitignore b/.gitignore index 82d723d..c288235 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ lanestest.lua sample-node.lua sample-master.lua Ayn Rand - The Virtue of Selfishness-Mg4QJheclsQ.m4a +Atlas Shrugged by Ayn Rand Audiobook-9s2qrEau63E.webm diff --git a/Documentation.md b/Documentation.md index 094c1c7..ad80448 100644 --- a/Documentation.md +++ b/Documentation.md @@ -765,7 +765,7 @@ multi:mainloop() CBT: Hyper Threaded Process --------------------------- -`process = multi:newHyperThreadedProcess(STRING name)` -- Creates a process object that is able allows all processes created on it to use the thread.* namespace. Hold/Sleep/Skip can be used in each multi obj created without stopping each other object that is running. +`process = multi:newHyperThreadedProcess(STRING name)` -- Creates a process object that is able allows all processes created on it to use the thread.* namespace. Hold/Sleep/Skip can be used in each multi obj created without stopping each other object that is running, but allows for one to pause/halt a process and stop all objects running in that process. `nil = process:getController()` -- Returns nothing there is no "controller" when using threaded processes `self = process:Start()` -- Starts the processor @@ -798,7 +798,7 @@ System Threads (ST) - Multi-Integration Getting Started ------------------------------------------------------- The system threads need to be required seperatly. ```lua -local GLOBAL, THREAD = require("multi.integration.lanesManager").init() -- We will talk about the global and thread interface that is returned +local GLOBAL, THREAD = require("multi.integration.lanesManager").init()# -- We will talk about the global and thread interface that is returned GLOBAL, THREAD = require("multi.integration.loveManager").init() GLOBAL, THREAD = require("luvitManager")-- There is a catch to this* ``` @@ -808,14 +808,15 @@ Using this integration modifies some methods that the multi library has. This variable is created on the main thread only inside of the multi namespace: multi.isMainThread = true This is used to know which thread is the main thread. When network threads are being discussed there is a gotcha that needs to be addressed. -*** GLOBAL and THREAD do not work currently when using the luvit integration +`*` GLOBAL and THREAD do not work currently when using the luvit integration +`#`So you may have noticed that when using the lanes manager you need to make the global and thread local, this is due to how lanes copies local variables between states. Also love2d does not require this, actually things will break if this is done! Keep these non local since the way threading is handled at the lower level is much different anyway so GLOBAL and THREAD is automatically set up for use within a spawned thread! ST - THREAD namespace --------------------- `THREAD.set(STRING name, VALUE val)` -- Sets a value in GLOBAL `THREAD.get(STRING name)` -- Gets a value in GLOBAL `THREAD.waitFor(STRING name)` -- Waits for a value in GLOBAL to exist -`THREAD.testFor(STRING name, VALUE val, STRING sym)` -- **NOT YET IMPLEMENTED** +`THREAD.testFor(STRING name, VALUE val, STRING sym)` -- **NOT YET IMPLEMENTED** but planned `THREAD.getCores()` -- Returns the number of actual system threads/cores `THREAD.kill()` -- Kills the thread `THREAD.getName()` -- Returns the name of the working thread @@ -832,25 +833,176 @@ print(GLOBAL["name"]) Removes the need to use THREAD.set() and THREAD.get() ST - System Threads ------------------- +`systemThread = multi:newSystemThread(STRING thread_name,FUNCTION spawned_function,ARGUMENTS ...)` -- Spawns a thread with a certain name. +`systemThread:kill()` -- kills a thread; can only be called in the main thread! + +System Threads are the feature that allows a user to interact with systen threads. It differs from regular coroutine based thread in how it can interact with variables. When using system threads the GLOBAL table is the "only way"* to send data. Spawning a System thread is really simple once all the required libraries are in place. See example below: + +```lua +local multi = require("multi") -- keep this global when using lanes or implicitly define multi within the spawned thread +local GLOBAL, THREAD = require("multi.integration.lanesManager").init() +multi:newSystemThread("Example thread",function() + local multi = require("multi") -- we are in a thread so lets not refer to that upvalue! + print("We have spawned a thread!") + -- we could do work but theres no need to we can save that for other examples + print("Lets have a non ending loop!") + while true do + -- If this was not in a thread execution would halt for the entire process + end +end,"A message that we are passing") -- There are restrictions on what can be passed! + +tloop = multi:newTLoop(function() + print("I'm still kicking!") +end,1) +multi:mainloop() +``` + +*This isn't entirely true, as of right now the compatiablity with the lanes library and love2d engine have their own methods to share data, but if you would like to have your code work in both enviroments then using the GLOBAL table and the data structures provided by the multi library will ensure this happens. If you do not plan on having support for both platforms then feel free to use linda's in lanes and channels in love2d. + +Note: luvit currently has very basic support, it only allows the spawning of system threads, but no way to send data back and forth as of yet. I do not know if this is doable or not, but I will keep looking into it. If I can somehow emulate System Threaded Queues and the GLOBAL tabke then all other datastructures will work! + +ST - System Threaded Objects +---------------------------- +Great we are able to spawn threads, but unless your working with a process that works on passed data and then uses a socket or writes to the disk I can't do to much with out being able to pass data between threads. This section we will look at how we can share objects between threads. In order to keep the compatibility between both love2d and lanes I had to format the system threaded objects in a strange way, but they are consistant and should work on both enviroments. + +When creating objects with a name they are automatically exposed to the GLOBAL table. Which means you can retrieve them from a spawned thread. For example we have a queue object, which will be discussed in more detail next. + +```lua +-- Exposing a queue +multi = require("multi") +local GLOBAL, THREAD = require("multi.integration.lanesManager").init() -- The standard setup above +queue = multi:newSystemThreadedQueue("myQueue"):init() -- We create and initiate the queue for the main thread +queue:push("This is a test!") -- We push some data onto the queue that other threads can consume and do stuff with +multi:newSystemThread("Example thread",function() -- Create a system thread + queue = THREAD.waitFor("myQueue"):init() -- Get the queue. It is good pratice to use the waitFor command when getting objects. If it doesn't exist yet we wait for it, preventing future errors. It is possible for the data to not ve present when a thread is looking for it! Especally when using the love2d module, my fault needs some rewriting data passing on the GLOBAL is quite slow, but the queue internally uses channels so after it is exposed you should have good speeds! + local data = queue:pop() -- Get the data + print(data) -- print the data +end) +multi:mainloop() +``` ST - SystemThreadedQueue ------------------------ +`queue(nonInit) = multi:newSystemThreadedQueue(STRING name)` -- You must enter a name! +`queue = queue:init()` -- initiates the queue, without doing this it will not work +`void = queue:push(DATA data)` -- Pushes data into a queue that all threads that have been shared have access to +`data = queue:pop()` -- pops data from the queue removing it from all threads +`data = queue:peek()` -- looks at data that is on the queue, but dont remove it from the queue -ST - SystemThreadedConnection ------------------------------ +This object the System Threaded Queue is the basis for all other data structures that a user has access to within the "shared" objects. -ST - SystemThreadedBenchmark ----------------------------- +General tips when using a queue. You can always pop from a queue without worrying if another thread poped that same data, BUT if you are peeking at a queue there is the possibility that another thread popped the data while you are peeking and this could cause an issue, depends on what you are doing though. It's important to keep this in mind when using queues. + +Let's get into some examples: +```lua +multi = require("multi") +thread_names = {"Thread_A","Thread_B","Thread_C","Thread_D"} +local GLOBAL, THREAD = require("multi.integration.lanesManager").init() +queue = multi:newSystemThreadedQueue("myQueue"):init() +for _,n in pairs(thread_names) do + multi:newSystemThread(n,function() + queue = THREAD.waitFor("myQueue"):init() + local name = THREAD.getName() + local data = queue:pop() + while data do + print(name.." "..data) + data = queue:pop() + end + end) +end +for i=1,100 do + queue:push(math.random(1,1000)) +end +multi:newEvent(function() -- Felt like using the event object, I hardly use them for anything non internal + return not queue:peek() +end):OnEvent(function() + print("No more data within the queue!") + os.exit() +end) +multi:mainloop() +``` + +You have probable noticed that the output from this is a total mess! Well I though so too, and created the system threaded console! ST - SystemThreadedConsole -------------------------- +`console(nonInit) = multi:newSystemThreadedConsole(STRING name)` -- Creates a console object called name. The name is mandatory! +`concole = console:inti()` -- initiates the console object +`console:print(...)` -- prints to the console +`console:write(msg)` -- writes to the console, to be fair you wouldn't want to use this one. -ST - SystemThreadedTable ------------------------- +The console makes printing from threads much cleaner. We will use the same example from above with the console implemented and compare the outputs and how readable they now are! + +```lua +multi = require("multi") +thread_names = {"Thread_A","Thread_B","Thread_C","Thread_D"} +local GLOBAL, THREAD = require("multi.integration.lanesManager").init() +multi:newSystemThreadedConsole("console"):init() +queue = multi:newSystemThreadedQueue("myQueue"):init() +for _,n in pairs(thread_names) do + multi:newSystemThread(n,function() + local queue = THREAD.waitFor("myQueue"):init() + local console = THREAD.waitFor("console"):init() + local name = THREAD.getName() + local data = queue:pop() + while data do + --THREAD.sleep(.1) -- uncomment this to see them all work + console:print(name.." "..data) + data = queue:pop() + end + end) +end +for i=1,100 do + queue:push(math.random(1,1000)) +end +multi:newEvent(function() + return not queue:peek() +end):OnEvent(function() + multi:newAlarm(.1):OnRing(function() -- Well the mainthread has to read from an internal queue so we have to wait a sec + print("No more data within the queue!") + os.exit() + end) +end) +multi:mainloop() +``` + +As you see the output here is so much cleaner, but we have a small gotcha, you probably noticed that I used an alarm to delay the exiting of the program for a bit. This is due to how the console object works, I send all the print data into a queue that the main thread then reads and prints out when it looks at the queue. This should not be an issue since you gain so much by having clean outputs! + +Another thing to note, because system threads are put to work one thread at a time, really quick though, the first thread that is loaded is able to complete the tasks really fast, its just printing after all. If you want to see all the threads working uncomment the code with THREAD.sleep(.1) ST - SystemThreadedJobQueue --------------------------- +ST - SystemThreadedConnection - WIP* +----------------------------- +`connection(nonInit) = multi:newSystemThreadedConnection(name,protect)` -- creates a connecion object +`connection = connection:init()` -- initaties the connection object +`connectionID = connection:connect(FUNCTION func)` -- works like the regular connect function +`void = connection:holdUT(NUMBER/FUNCTION n)` -- works just like the regular holdut function +`void = connection:Remove()` -- works the same as the default +`voic = connection:Fire(ARGS ...)` -- works the same as the default + +In the current form a connection object requires that the multi:mainloop() is running on the threads that are sharing this object! By extention since SystemThreadedTables rely on SystemThreadedConnections they have the same requirements. Both objects should not be used for now. + +Since the current object is not in a stable condition, I will not be providing examples of how to use it just yet! + +*The main issue we have with the connection objects in this form is proper comunication and memory managament between threads. For example if a thread crashes or no longer exists the current apporach to how I manage the connection objects will cause all connections to halt. This feature is still being worked on and has many bugs to be patched out. for now only use for testing purposes. + +ST - SystemThreadedTable - WIP* +------------------------ + +ST - SystemThreadedBenchmark +---------------------------- +`bench = multi:SystemThreadedBenchmark(NUMBER seconds)` -- runs a benchmark for a certain amount of time +`bench:OnBench(FUNCTION callback(NUMBER steps/second))` +```lua +multi = require("multi") +local GLOBAL, THREAD = require("multi.integration.lanesManager").init() +multi:SystemThreadedBenchmark(1).OnBench(function(...) + print(...) +end) +multi:mainloop() +``` ST - SystemThreadedExecute -------------------------- diff --git a/changes.md b/changes.md index 65136bb..46c6d04 100644 --- a/changes.md +++ b/changes.md @@ -1,10 +1,10 @@ #Changes [TOC] -Update 13.0.0 So you documented it, finally, but it's sad to see some things go isn't it? +Update 13.0.0 So you documented it, finally! New additions/changes/ and fixes ------------- Fixed: Tons of bugs, I actually went through the entire library and did a full test of everything, I mean everything, while writing the documentation. Changed: -- A few things, to make concepts in the library more clear +- A few things, to make concepts in the library more clear. - The way functions returned paused status. Before it would return "PAUSED" now it returns nil, true if paused - Modified the connection object to allow for some more syntaxial suger! @@ -20,8 +20,18 @@ end) multi:mainloop() ``` +Function Example: +```lua +func = multi:newFunction(function(self,a,b) + self:Pause() + return 1,2,3 +end) +print(func()) -- returns: 1, 2, 3 +print(func()) -- nil, true +``` + Removed: -- Ranges and conditions -- corutine based threads can dmulate what these objects did and much better! +- Ranges and conditions -- corutine based threads can emulate what these objects did and much better! - Due to the creation of hyper threaded processes the following objects are no more! -- ~~multi:newThreadedEvent()~~ -- ~~multi:newThreadedLoop()~~ @@ -30,15 +40,22 @@ Removed: -- ~~multi:newThreadedTStep()~~ -- ~~multi:newThreadedAlarm()~~ -- ~~multi:newThreadedUpdater()~~ +-- ~~multi:newTBase()~~ -- Acted as the base for creating the other objects These didn't have much use in their previous form, but with the addition of hyper threaded processes the goals that these objects aimed to solve are now possible using a process Fixed: - There were some bugs in the networkmanager.lua file. Desrtoy -> Destroy some misspellings. +- Massive object management bugs which caused performance to drop like a rock. Remember to Destroy objects when no longer using them. I should probably start working on a garbage collector for these objects! +- Found a bug with processors not having the Destroy() function implemented properly. Added: - multi:newHyperThreadedProcess(STRING name) -- This is a version of the threaded process that gives each object created its own coroutine based thread which means you can use thread.* without affecting other objects created within the hyper threaded processes. -- multi:newConnector() -- A simple object that allows you to use the new connection Fire syntax without using a multi obj +- multi:newConnector() -- A simple object that allows you to use the new connection Fire syntax without using a multi obj or the standard object format that I follow. +- multi:purge() -- Removes all references to objects that are contained withing the processes list of tasks to do. Doing this will stop all objects from functioning. Calling Resume on an object should make it work again. +- multi:getTasksDetails(STRING format) -- Simple function, will get massive updates in the future, as of right now It will print out the current processes that are running; listing their type, uptime, and priority. More useful additions will be added in due time. Format can be either a string "s" or "t" see below for the table format +- multi:endTask(TID) -- Use multi:getTasksDetails("t") to get the tid of a task +- multi:enableLoadDetection() -- Since load detection puts some strain on the system (very little) I decided to make it something that has to be enabled. Once on it cant be turned off! ```lua package.path="?/init.lua;?.lua;"..package.path @@ -54,7 +71,7 @@ test:newTLoop(function() conn:OnTest() end,1) test:newLoop(function() - print("HI2!") + print("HEY!") thread.sleep(.5) end) multi:newAlarm(3):OnRing(function() @@ -63,7 +80,22 @@ end) test:Start() multi:mainloop() ``` - +Table format for getTasksDetails(STRING format) +```lua +{ + {TID = 1,Type="",Priority="",Uptime=0} + {TID = 2,Type="",Priority="",Uptime=0} + ... + {TID = n,Type="",Priority="",Uptime=0} + ThreadCount = 0 + threads={ + [Thread_Name]={ + Uptime = 0 + } + } +} +``` +**Note:** After adding the getTasksDetails() function I noticed many areas where threads, and tasks were not being cleaned up and fixed the leaks. I also found out that a lot of tasks were starting by default and made them enable only. If you compare the benchmark from this version to last version you;ll notice a signifacant increase in performance. 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 e9bb5bf..88535c7 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -61,11 +61,20 @@ multi.Priority_Normal = 64 multi.Priority_Below_Normal = 256 multi.Priority_Low = 1024 multi.Priority_Idle = 4096 +multi.PriorityResolve = { + [1]="Core", + [4]="High", + [16]="Above Normal", + [64]="Normal", + [256]="Below Normal", + [1024]="Low", + [4096]="Idle", +} multi.PStep = 1 multi.PList = {multi.Priority_Core,multi.Priority_High,multi.Priority_Above_Normal,multi.Priority_Normal,multi.Priority_Below_Normal,multi.Priority_Low,multi.Priority_Idle} --^^^^ multi.PriorityTick=1 -- Between 1, 2 and 4 -multi.Priority=multi.Priority_Core +multi.Priority=multi.Priority_High multi.threshold=256 multi.threstimed=.001 function multi.queuefinal(self) @@ -240,6 +249,75 @@ function multi:benchMark(sec,p,pt) self.tt=function() end return temp end +function multi.Round(num, numDecimalPlaces) + local mult = 10^(numDecimalPlaces or 0) + return math.floor(num * mult + 0.5) / mult +end +function multi.AlignTable(tab) + local longest = {} + local columns = #tab[1] + local rows = #tab + for i=1, columns do + longest[i] = -math.huge + end + for i = 1,rows do + for j = 1,columns do + tab[i][j] = tostring(tab[i][j]) + if #tab[i][j]>longest[j] then + longest[j] = #tab[i][j] + end + end + end + for i = 1,rows do + for j = 1,columns do + if tab[i][j]~=nil and #tab[i][j]" + end + table.insert(str,{v.Type:sub(1,1):upper()..v.Type:sub(2,-1)..name,multi.Round(os.clock()-v.creationTime,3),self.PriorityResolve[v.Priority],i}) + end + + local s = multi.AlignTable(str) + dat = "" + for i=1,#multi.scheduler.Threads do + dat = dat .. "\n" + end + return "Load on manager: "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\n\n"..s.."\n\n"..dat + elseif t == "t" or t == "table" then + str = {ThreadCount = #multi.scheduler.Threads,MemoryUsage = math.ceil(collectgarbage("count")).." KB"} + str.threads = {} + for i,v in pairs(self.Mainloop) do + str[#str+1]={Type=v.Type,Name=v.Name,Uptime=os.clock()-v.creationTime,Priority=self.PriorityResolve[v.Priority],TID = i} + end + for i=1,#multi.scheduler.Threads do + str.threads[multi.scheduler.Threads[i].Name]={Uptime = os.clock()-multi.scheduler.Threads[i].creationTime} + end + return str + end +end +function multi:endTask(TID) + self.Mainloop[TID]:Destroy() +end function multi.startFPSMonitior() if not multi.runFPS then multi.doFPS(s) @@ -432,6 +510,9 @@ multi.IsDone=multi.isDone function multi:create(ref) multi.OnObjectCreated:Fire(ref,self) end +function multi:setName(name) + self.Name = name + end --Constructors [CORE] function multi:newBase(ins) if not(self.Type=='mainprocess' or self.Type=='process' or self.Type=='queue') then error('Can only create an object on multi or an interface obj') return false end @@ -451,6 +532,7 @@ function multi:newBase(ins) c.Act=function() end c.Parent=self c.held=false + c.creationTime = os.clock() if ins then table.insert(self.Mainloop,ins,c) else @@ -469,29 +551,33 @@ function multi:newProcessor(file) c.Mainloop={} c.Garbage={} c.Children={} - c.Active=true + c.Active=false c.Id=-1 c.Rest=0 c.Jobs={} c.queue={} c.jobUS=2 - c.l=self:newLoop(function(self,dt) c:uManager() end) - c.l:Pause() + c.l=self:newLoop(function(self,dt) + if self.link.Active then + c:uManager() + end + end) + c.l.link = c + c.l.Type = "process" function c:getController() return c.l end function c:Start() - if self.l then - self.l:Resume() - end + self.Active = true return self end function c:Resume() - if self.l then - self.l:Resume() - end + self.Active = false return self end + function c:setName(name) + c.Name = name + end function c:Pause() if self.l then self.l:Pause() @@ -506,8 +592,8 @@ function multi:newProcessor(file) self.Cself=c loadstring('local process=multi.Cself '..io.open(file,'rb'):read('*all'))() end - self.__Destroy = self.Destroy - self.Destroy = Remove + c.__Destroy = self.Destroy + c.Destroy = c.Remove self:create(c) --~ c:IngoreObject() return c @@ -962,6 +1048,7 @@ function multi:newTLoop(func,set) c.set=set or 0 c.timer=self:newTimer() c.life=0 + c:setPriority("Low") if func then c.func={func} end @@ -1305,20 +1392,9 @@ function thread.testFor(name,_val,sym) end) return thread.get(name) end -function multi:newTBase(name) - local c = {} - c.name=name - c.Active=true - c.func={} - c.ender={} - c.Id=0 - c.Parent=self - c.important={} - c.held=false - c.ToString=multi.ToString - c.ToFile=multi.ToFile - return c -end +multi:setDomainName("Threads") +multi:setDomainName("Globals") +local initT = false function multi:newThread(name,func) local c={} c.ref={} @@ -1327,7 +1403,7 @@ function multi:newThread(name,func) c.sleep=1 c.Type="thread" c.firstRunDone=false - c.timer=multi.scheduler:newTimer() + c.timer=multi:newTimer() c.ref.Globals=self:linkDomain("Globals") function c.ref:send(name,val) ret=coroutine.yield({Name=name,Value=val}) @@ -1358,85 +1434,90 @@ function multi:newThread(name,func) self.Globals=v end table.insert(self:linkDomain("Threads"),c) - if not multi.scheduler:isActive() then - multi.scheduler:Resume() + if initT==false then + multi.initThreads() end + c.creationTime = os.clock() end -multi:setDomainName("Threads") -multi:setDomainName("Globals") -multi.scheduler=multi:newLoop() -multi.scheduler.Type="scheduler" -function multi.scheduler:setStep(n) - self.skip=tonumber(n) or 24 -end -multi.scheduler.skip=0 -multi.scheduler.counter=0 -multi.scheduler.Threads=multi:linkDomain("Threads") -multi.scheduler.Globals=multi:linkDomain("Globals") -multi.scheduler:OnLoop(function(self) - self.counter=self.counter+1 - for i=#self.Threads,1,-1 do - ret={} - if coroutine.status(self.Threads[i].thread)=="dead" then - table.remove(self.Threads,i) - else - if self.Threads[i].timer:Get()>=self.Threads[i].sleep then - if self.Threads[i].firstRunDone==false then - self.Threads[i].firstRunDone=true - self.Threads[i].timer:Start() - if unpack(self.Threads[i].returns or {}) then - _,ret=coroutine.resume(self.Threads[i].thread,unpack(self.Threads[i].returns)) +function multi.initThreads() + initT = true + multi.scheduler=multi:newLoop() + multi.scheduler.Type="scheduler" + function multi.scheduler:setStep(n) + self.skip=tonumber(n) or 24 + end + multi.scheduler.skip=0 + multi.scheduler.counter=0 + multi.scheduler.Threads=multi:linkDomain("Threads") + multi.scheduler.Globals=multi:linkDomain("Globals") + multi.scheduler:OnLoop(function(self) + self.counter=self.counter+1 + for i=#self.Threads,1,-1 do + ret={} + if coroutine.status(self.Threads[i].thread)=="dead" then + table.remove(self.Threads,i) + else + if self.Threads[i].timer:Get()>=self.Threads[i].sleep then + if self.Threads[i].firstRunDone==false then + self.Threads[i].firstRunDone=true + self.Threads[i].timer:Start() + if unpack(self.Threads[i].returns or {}) then + _,ret=coroutine.resume(self.Threads[i].thread,unpack(self.Threads[i].returns)) + else + _,ret=coroutine.resume(self.Threads[i].thread,self.Threads[i].ref) + end else - _,ret=coroutine.resume(self.Threads[i].thread,self.Threads[i].ref) + if unpack(self.Threads[i].returns or {}) then + _,ret=coroutine.resume(self.Threads[i].thread,unpack(self.Threads[i].returns)) + else + _,ret=coroutine.resume(self.Threads[i].thread,self.Globals) + end end - else - if unpack(self.Threads[i].returns or {}) then - _,ret=coroutine.resume(self.Threads[i].thread,unpack(self.Threads[i].returns)) - else - _,ret=coroutine.resume(self.Threads[i].thread,self.Globals) + if _==false then + self.Parent.OnError:Fire(self.Threads[i],"Error in thread: <"..self.Threads[i].Name.."> "..ret) + end + if ret==true or ret==false then + print("Thread Ended!!!") + ret={} end end - if _==false then - self.Parent.OnError:Fire(self.Threads[i],"Error in thread: <"..self.Threads[i].Name.."> "..ret) - end - if ret==true or ret==false then - print("Thread Ended!!!") - ret={} - end - end - if ret then - if ret[1]=="_kill_" then - table.remove(self.Threads,i) - elseif ret[1]=="_sleep_" then - self.Threads[i].timer:Reset() - self.Threads[i].sleep=ret[2] - elseif ret[1]=="_skip_" then - self.Threads[i].timer:Reset() - self.Threads[i].sleep=math.huge - local event=multi:newEvent(function(evnt) return multi.scheduler.counter>=evnt.counter end) - event.link=self.Threads[i] - event.counter=self.counter+ret[2] - event:OnEvent(function(evnt) - evnt.link.sleep=0 - end) - elseif ret[1]=="_hold_" then - self.Threads[i].timer:Reset() - self.Threads[i].sleep=math.huge - local event=multi:newEvent(ret[2]) - event.returns = nil - event.link=self.Threads[i] - event:OnEvent(function(evnt) - evnt.link.sleep=0 - evnt.link.returns = evnt.returns - end) - elseif ret.Name then - self.Globals[ret.Name]=ret.Value + if ret then + if ret[1]=="_kill_" then + table.remove(self.Threads,i) + elseif ret[1]=="_sleep_" then + self.Threads[i].timer:Reset() + self.Threads[i].sleep=ret[2] + elseif ret[1]=="_skip_" then + self.Threads[i].timer:Reset() + self.Threads[i].sleep=math.huge + local event=multi:newEvent(function(evnt) return multi.scheduler.counter>=evnt.counter end) + event.link=self.Threads[i] + event.counter=self.counter+ret[2] + event:OnEvent(function(evnt) + evnt.link.sleep=0 + evnt:Destroy() + end) + elseif ret[1]=="_hold_" then + self.Threads[i].timer:Reset() + self.Threads[i].sleep=math.huge + local event=multi:newEvent(ret[2]) + event.returns = nil + event.link=self.Threads[i] + event:OnEvent(function(evnt) + evnt.link.sleep=0 + evnt.link.returns = evnt.returns + multi.nextStep(function() + evnt:Destroy() + end) + end) + elseif ret.Name then + self.Globals[ret.Name]=ret.Value + end end end end - end -end) -multi.scheduler:Pause() + end) +end multi.OnError=multi:newConnection() function multi:newThreadedProcess(name) local c = {} @@ -1663,6 +1744,7 @@ function multi:threadloop(settings) event.counter=counter+ret[2] event:OnEvent(function(evnt) evnt.link.sleep=0 + evnt:Destroy() end) elseif ret[1]=="_hold_" then Threads[i].timer:Reset() @@ -1671,6 +1753,7 @@ function multi:threadloop(settings) event.link=Threads[i] event:OnEvent(function(evnt) evnt.link.sleep=0 + evnt:Destroy() end) elseif ret.Name then Globals[ret.Name]=ret.Value @@ -2087,7 +2170,6 @@ function multi:IngoreObject() self.Ingore=true return self end -multi.scheduler:IngoreObject() function multi:ToString() if self.Ingore then return end local t=self.Type @@ -2268,34 +2350,6 @@ function multi:newFromString(str) local item=self:newLoop() table.merge(item,data) return item - elseif t=="eventThread" then -- GOOD - local item=self:newThreadedEvent(data.name) - table.merge(item,data) - return item - elseif t=="loopThread" then -- GOOD - local item=self:newThreadedLoop(data.name) - table.merge(item,data) - return item - elseif t=="stepThread" then -- GOOD - local item=self:newThreadedStep(data.name) - table.merge(item,data) - return item - elseif t=="tloopThread" then -- GOOD - local item=self:newThreadedTLoop(data.name,nil,data.restN) - table.merge(item,data) - return item - elseif t=="tstepThread" then -- GOOD - local item=self:newThreadedTStep(data.name) - table.merge(item,data) - return item - elseif t=="updaterThread" then -- GOOD - local item=self:newThreadedUpdater(data.name) - table.merge(item,data) - return item - elseif t=="alarmThread" then -- GOOD - local item=self:newThreadedAlarm(data.name) - table.merge(item,data) - return item end end function multi:Important(varname) @@ -2322,19 +2376,22 @@ end function multi:setDefualtStateFlag(opt) -- end -multi.dStepA = 0 -multi.dStepB = 0 -multi.dSwap = 0 -multi.deltaTarget = .05 -multi.load_updater = multi:newUpdater(2) -multi.load_updater:Pause() -multi.load_updater:OnUpdate(function(self) - if self.Parent.dSwap == 0 then - self.Parent.dStepA = os.clock() - self.Parent.dSwap = 1 - else - self.Parent.dSwap = 0 - self.Parent.dStepB = os.clock() - end -end) +function multi:enableLoadDetection() + if multi.load_updater then return end + multi.dStepA = 0 + multi.dStepB = 0 + multi.dSwap = 0 + multi.deltaTarget = .05 + multi.load_updater = multi:newUpdater(2) + multi.load_updater:setName("LoadDetector") + multi.load_updater:OnUpdate(function(self) + if self.Parent.dSwap == 0 then + self.Parent.dStepA = os.clock() + self.Parent.dSwap = 1 + else + self.Parent.dSwap = 0 + self.Parent.dStepB = os.clock() + end + end) +end return multi diff --git a/multi/integration/networkManager.lua b/multi/integration/networkManager.lua index 40e7f3c..7268842 100644 --- a/multi/integration/networkManager.lua +++ b/multi/integration/networkManager.lua @@ -142,7 +142,7 @@ function multi:nodeManager(port) server.OnDataRecieved(function(server,data,cid,ip,port) local cmd = data:sub(1,1) if cmd == "R" then - multi:newThread("Test",function(loop) + multi:newThread("Node Client Manager",function(loop) while true do if server.timeouts[cid]==true then server.OnNodeRemoved:Fire(server.nodes[cid]) @@ -175,6 +175,7 @@ function multi:nodeManager(port) end -- The main driving force of the network manager: Nodes function multi:newNode(settings) + multi:enableLoadDetection() settings = settings or {} -- Here we have to use the net library to broadcast our node across the network math.randomseed(os.time()) @@ -439,7 +440,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas name = self:getRandomNode() end if name==nil then - multi:newThread("Test",function(loop) + multi:newThread("Network Thread Manager",function(loop) while true do if name~=nil then self:sendTo(name,char(CMD_TASK)..len..aData..len2..fData) @@ -461,7 +462,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas name = "NODE_"..name end if self.connections[name]==nil then - multi:newThread("Test",function(loop) + multi:newThread("Node Data Link Controller",function(loop) while true do if self.connections[name]~=nil then self.connections[name]:send(data) @@ -504,7 +505,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas client.OnClientReady(function() client:send(char(CMD_INITMASTER)..master.name) -- Tell the node that you are a master trying to connect if not settings.managerDetails then - multi:newThread("Test",function(loop) + multi:newThread("Node Data Link Controller",function(loop) while true do if master.timeouts[name]==true then master.timeouts[name] = nil diff --git a/multi/integration/shared.lua b/multi/integration/shared.lua index fb1f599..217326f 100644 --- a/multi/integration/shared.lua +++ b/multi/integration/shared.lua @@ -211,6 +211,7 @@ function multi:SystemThreadedBenchmark(n) multi:benchMark(n):OnBench(function(self,count) queue:push(count) sThread.kill() + error("Thread was killed!") end) multi:mainloop() end,n) @@ -247,10 +248,10 @@ function multi:newSystemThreadedConsole(name) end local cc={} if multi.isMainThread then - if GLOBAL["__SYSTEM_CONSLOE__"] then - cc.stream = sThread.waitFor("__SYSTEM_CONSLOE__"):init() + if GLOBAL["__SYSTEM_CONSOLE__"] then + cc.stream = sThread.waitFor("__SYSTEM_CONSOLE__"):init() else - cc.stream = multi:newSystemThreadedQueue("__SYSTEM_CONSLOE__"):init() + cc.stream = multi:newSystemThreadedQueue("__SYSTEM_CONSOLE__"):init() multi:newLoop(function() local data = cc.stream:pop() if data then @@ -264,7 +265,7 @@ function multi:newSystemThreadedConsole(name) end) end else - cc.stream = sThread.waitFor("__SYSTEM_CONSLOE__"):init() + cc.stream = sThread.waitFor("__SYSTEM_CONSOLE__"):init() end function cc:write(msg) self.stream:push({"w",tostring(msg)}) diff --git a/test.lua b/test.lua index a603ea6..7f5969f 100644 --- a/test.lua +++ b/test.lua @@ -1,16 +1,40 @@ package.path="?/init.lua;?.lua;"..package.path -local multi = require("multi") -test = multi:newHyperThreadedProcess("test") -test:newLoop(function() - print("HI!") +multi = require("multi") +local GLOBAL,THREAD = require("multi.integration.lanesManager").init() +nGLOBAL = require("multi.integration.networkManager").init() +local a +function multi:setName(name) + self.Name = name +end +local clock = os.clock +function sleep(n) -- seconds + local t0 = clock() + while clock() - t0 <= n do end +end +master = multi:newMaster{ + name = "Main", -- the name of the master + --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections + managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) +} +master.OnError(function(name,err) + print(name.." has encountered an error: "..err) end) -test:newLoop(function() - print("HI2!") - thread.sleep(.5) +master.OnNodeConnected(function(name) + multi:newThread("Main Thread Data Sender",function() + while true do + thread.sleep(.5) + conn = master:execute("TASK_MAN",name, multi:getTasksDetails()) + end + end,5) end) -multi:newAlarm(3):OnRing(function() - test:Sleep(10) +multi.OnError(function(...) + print(...) end) -test:Start() -multi:mainloop() - +multi:mainloop{ + protect = false +} +--~ print(multi.AlignTable{ +--~ {"Name","Type","Number"}, +--~ {"Test","This is a type","1.34"}, +--~ {"Test Hello","This is another type","143.43"}, +--~ }) -- 2.43.0 From 110055ffc4f19752f04a0ed7a7a324b629d09aee Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Tue, 29 Jan 2019 12:22:09 -0500 Subject: [PATCH 08/20] changing workstations --- test.lua | 5 ----- 1 file changed, 5 deletions(-) diff --git a/test.lua b/test.lua index 7f5969f..570724b 100644 --- a/test.lua +++ b/test.lua @@ -33,8 +33,3 @@ end) multi:mainloop{ protect = false } ---~ print(multi.AlignTable{ ---~ {"Name","Type","Number"}, ---~ {"Test","This is a type","1.34"}, ---~ {"Test Hello","This is another type","143.43"}, ---~ }) -- 2.43.0 From b5bc0f8e9114cddecfcf9f2f68ead78229b05363 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Tue, 29 Jan 2019 12:23:11 -0500 Subject: [PATCH 09/20] updated rockspec --- rockspecs/multi-12.2-1.rockspec | 1 - 1 file changed, 1 deletion(-) diff --git a/rockspecs/multi-12.2-1.rockspec b/rockspecs/multi-12.2-1.rockspec index c1c70d0..c110b89 100644 --- a/rockspecs/multi-12.2-1.rockspec +++ b/rockspecs/multi-12.2-1.rockspec @@ -16,7 +16,6 @@ dependencies = { "lua >= 5.1", "bin", "lanes", - "lua-net" } build = { type = "builtin", -- 2.43.0 From 41725dc01f7e8835d89e8a43a6ec55202c02bb80 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Tue, 29 Jan 2019 23:27:36 -0500 Subject: [PATCH 10/20] more fixes and tons of bugs to be worked on --- Documentation.md | 2 +- changes.md | 5 ++++- multi/init.lua | 1 + multi/integration/networkManager.lua | 6 +++--- rockspecs/multi-13.0-0.rockspec | 31 ++++++++++++++++++++++++++++ test.lua | 18 +++++++++------- 6 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 rockspecs/multi-13.0-0.rockspec diff --git a/Documentation.md b/Documentation.md index ad80448..bb41eb6 100644 --- a/Documentation.md +++ b/Documentation.md @@ -1003,7 +1003,7 @@ multi:SystemThreadedBenchmark(1).OnBench(function(...) end) multi:mainloop() ``` -ST - SystemThreadedExecute +ST - SystemThreadedExecute WIP* Might remove -------------------------- Network Threads - Multi-Integration diff --git a/changes.md b/changes.md index 46c6d04..976731a 100644 --- a/changes.md +++ b/changes.md @@ -1,6 +1,6 @@ #Changes [TOC] -Update 13.0.0 So you documented it, finally! New additions/changes/ and fixes +Update 13.0.0 So you documented it, finally! If I had a dollar for each time I found a bug working on 13.0.0 I'd be quite wealthy by now. How much lag could one expect when I've been coding with my own library wrong this entire time? ------------- Fixed: Tons of bugs, I actually went through the entire library and did a full test of everything, I mean everything, while writing the documentation. Changed: @@ -48,6 +48,9 @@ Fixed: - There were some bugs in the networkmanager.lua file. Desrtoy -> Destroy some misspellings. - Massive object management bugs which caused performance to drop like a rock. Remember to Destroy objects when no longer using them. I should probably start working on a garbage collector for these objects! - Found a bug with processors not having the Destroy() function implemented properly. +- Found an issue with the rockspec which is due to the networkManager additon. The net Library and the multi Library are now codependent if using that feature. Going forward you will have to now install the network library separately +- Insane proformance bug found in the networkManager file, where each connection to a node created a new thread (VERY BAD) If say you connected to 100s of threads, you would lose a lot of processing power due to a bad implementation of this feature. But it goes futhur than this, the net library also creates a new thread for each connection made, so times that initial 100 by about 3, you end up with a system that quickly eats itself. I have to do tons of rewriting of everything. Yet another setback for the 13.0.0 release +- Fixed an issue where any argument greater than 256^2/65536 bytes is sent the networkmanager would soft crash. This was fixed by increading the limit to 256^4/4294967296 bytes. The fix was changing a 2 to a 4. Arguments greater than 256^4 would be impossible in 32 bit lua, and highly unlikely even in lua 64 bit. Perhaps someone is reading an entire file into ram and then sending the entire file that they read over a socket for some reason all at once!? Added: - multi:newHyperThreadedProcess(STRING name) -- This is a version of the threaded process that gives each object created its own coroutine based thread which means you can use thread.* without affecting other objects created within the hyper threaded processes. diff --git a/multi/init.lua b/multi/init.lua index 88535c7..78fc909 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -1808,6 +1808,7 @@ function multi:mainloop(settings) local solid local sRef while mainloopActive do + --print(mainloopActive) if ncount ~= 0 then for i = 1, ncount do next[i]() diff --git a/multi/integration/networkManager.lua b/multi/integration/networkManager.lua index 7268842..b9e65be 100644 --- a/multi/integration/networkManager.lua +++ b/multi/integration/networkManager.lua @@ -287,7 +287,7 @@ function multi:newNode(settings) local temp = bin.new(dat) local len = temp:getBlock("n",1) local name = temp:getBlock("s",len) - len = temp:getBlock("n",2) + len = temp:getBlock("n",4) local args = temp:getBlock("s",len) _G[name](unpack(resolveData(args))) elseif cmd == CMD_TASK then @@ -356,7 +356,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas if settings.managerDetails then local client = net:newTCPClient(settings.managerDetails[1],settings.managerDetails[2]) if not client then - print("Cannot connect to the node manager! Ensuring broadcast listening is enabled!") settings.noBroadCast = false + print("Warning: Cannot connect to the node manager! Ensuring broadcast listening is enabled!") settings.noBroadCast = false else client.OnDataRecieved(function(client,data) local cmd = data:sub(1,1) @@ -406,7 +406,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas temp:addBlock(CMD_CALL,1) temp:addBlock(#name,1) temp:addBlock(name,#name) - temp:addBlock(#args,2) + temp:addBlock(#args,4) temp:addBlock(args,#args) master:sendTo(node,temp.data) end diff --git a/rockspecs/multi-13.0-0.rockspec b/rockspecs/multi-13.0-0.rockspec new file mode 100644 index 0000000..1885be2 --- /dev/null +++ b/rockspecs/multi-13.0-0.rockspec @@ -0,0 +1,31 @@ +package = "multi" +version = "13.0-0" +source = { + url = "git://github.com/rayaman/multi.git", + tag = "v13.0.0", +} +description = { + summary = "Lua Multi tasking library", + detailed = [[ + This library contains many methods for multi tasking. From simple side by side code using multi-objs, to using coroutine based Threads and System threads(When you have lua lanes installed or are using love2d) + ]], + homepage = "https://github.com/rayaman/multi", + license = "MIT" +} +dependencies = { + "lua >= 5.1", + "bin", + "lanes", +} +build = { + type = "builtin", + modules = { + ["multi.init"] = "multi/init.lua", + ["multi.compat.love2d"] = "multi/compat/love2d.lua", + ["multi.integration.lanesManager"] = "multi/integration/lanesManager.lua", + ["multi.integration.loveManager"] = "multi/integration/loveManager.lua", + ["multi.integration.luvitManager"] = "multi/integration/luvitManager.lua", + ["multi.integration.networkManager"] = "multi/integration/networkManager.lua", + ["multi.integration.shared"] = "multi/integration/shared.lua" + } +} \ No newline at end of file diff --git a/test.lua b/test.lua index 570724b..d60b7a4 100644 --- a/test.lua +++ b/test.lua @@ -14,18 +14,22 @@ end master = multi:newMaster{ name = "Main", -- the name of the master --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections - managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) +--~ managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) } master.OnError(function(name,err) print(name.." has encountered an error: "..err) end) -master.OnNodeConnected(function(name) - multi:newThread("Main Thread Data Sender",function() - while true do - thread.sleep(.5) - conn = master:execute("TASK_MAN",name, multi:getTasksDetails()) +local connlist = {} +multi:newThread("NodeUpdater",function() + while true do + thread.sleep(1) + for i=1,#connlist do + conn = master:execute("TASK_MAN",connlist[i], multi:getTasksDetails()) end - end,5) + end +end) +master.OnNodeConnected(function(name) + table.insert(connlist,name) end) multi.OnError(function(...) print(...) -- 2.43.0 From 03c8f364b285a1fd6b4e6707859307bc9f5fbc26 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Wed, 30 Jan 2019 15:17:12 -0500 Subject: [PATCH 11/20] fixed rockspec This does not mean its ready for use, but if you lurk and wanna try it out go ahead --- rockspecs/multi-13.0-0.rockspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rockspecs/multi-13.0-0.rockspec b/rockspecs/multi-13.0-0.rockspec index 1885be2..9737df5 100644 --- a/rockspecs/multi-13.0-0.rockspec +++ b/rockspecs/multi-13.0-0.rockspec @@ -20,7 +20,7 @@ dependencies = { build = { type = "builtin", modules = { - ["multi.init"] = "multi/init.lua", + ["multi"] = "multi/init.lua", ["multi.compat.love2d"] = "multi/compat/love2d.lua", ["multi.integration.lanesManager"] = "multi/integration/lanesManager.lua", ["multi.integration.loveManager"] = "multi/integration/loveManager.lua", -- 2.43.0 From 0fd604e356473cc74d1fe702c1b53c8fda718da6 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Thu, 31 Jan 2019 12:25:41 -0500 Subject: [PATCH 12/20] sigh processors have some bugs working on more bugs... I estimate about 2 months of work for the next version haha --- Documentation.md | 4 +- changes.md | 13 +++++- multi/init.lua | 105 ++++++++++++++++++++++++++++++----------------- test.lua | 71 +++++++++++++++++++------------- 4 files changed, 123 insertions(+), 70 deletions(-) diff --git a/Documentation.md b/Documentation.md index bb41eb6..6d9b438 100644 --- a/Documentation.md +++ b/Documentation.md @@ -633,9 +633,9 @@ Coroutine based Threading (CBT) This was made due to the limitations of multiObj:hold(), which no longer exists. When this library was in its infancy and before I knew about coroutines, I actually tried to emulate what coroutines did in pure lua. The threaded bariants of the non threaded objects do exist, but there isn't too much of a need to use them. -The main benefits of using the coroutine based threads it the thread.* namespace which gives you the ability to easily run code side by side. +The main benefits of using the coroutine based threads is the thread.* namespace which gives you the ability to easily run code side by side. -A quick not on how threads are managed in the library. The library contains a scheduler which keeps track of coroutines and manages them. Coroutines take some time then give off processing to another coroutine. Which means there are some methods that you need to use in order to hand off cpu time to other coroutines or the main thread. +A quick note on how threads are managed in the library. The library contains a scheduler which keeps track of coroutines and manages them. Coroutines take some time then give off processing to another coroutine. Which means there are some methods that you need to use in order to hand off cpu time to other coroutines or the main thread. You must hand off cpu time when inside of a non ending loop or your code will hang. Threads also have a slight delay before starting, about 3 seconds. threads.* --------- diff --git a/changes.md b/changes.md index 976731a..d880d4d 100644 --- a/changes.md +++ b/changes.md @@ -2,6 +2,9 @@ [TOC] Update 13.0.0 So you documented it, finally! If I had a dollar for each time I found a bug working on 13.0.0 I'd be quite wealthy by now. How much lag could one expect when I've been coding with my own library wrong this entire time? ------------- +**Quick note** on the 13.0.0 update: +This update I went all in finding bugs and improving proformance within the library. I added some new features and the new task manager, which I used as a way to debug the library was a great help, so much so thats it is now a permanent feature. It's been about half a year since my last update, but so much work needed to be done. I hope you can find a use in your code to use my library. I am extremely proud of my work; 7 years of development, I learned so much about lua and programming through the creation of this library. It was fun, but there will always be more to add and bugs crawling there way in. I can't wait to see where this library goes in the future! + Fixed: Tons of bugs, I actually went through the entire library and did a full test of everything, I mean everything, while writing the documentation. Changed: - A few things, to make concepts in the library more clear. @@ -53,12 +56,13 @@ Fixed: - Fixed an issue where any argument greater than 256^2/65536 bytes is sent the networkmanager would soft crash. This was fixed by increading the limit to 256^4/4294967296 bytes. The fix was changing a 2 to a 4. Arguments greater than 256^4 would be impossible in 32 bit lua, and highly unlikely even in lua 64 bit. Perhaps someone is reading an entire file into ram and then sending the entire file that they read over a socket for some reason all at once!? Added: -- multi:newHyperThreadedProcess(STRING name) -- This is a version of the threaded process that gives each object created its own coroutine based thread which means you can use thread.* without affecting other objects created within the hyper threaded processes. +- Documentation, the purpose of 13.0.0, orginally going to be 12.2.3, but due to the amount of bugs and features I added couldn't become that. I actually still did my tests in the 12.2.3 branch in github. +- multi:newHyperThreadedProcess(STRING name) -- This is a version of the threaded process that gives each object created its own coroutine based thread which means you can use thread.* without affecting other objects created within the hyper threaded processes. Though, creating a self contained single thread is a better idea which when I eventually create the wiki page I'll discuss - multi:newConnector() -- A simple object that allows you to use the new connection Fire syntax without using a multi obj or the standard object format that I follow. - multi:purge() -- Removes all references to objects that are contained withing the processes list of tasks to do. Doing this will stop all objects from functioning. Calling Resume on an object should make it work again. - multi:getTasksDetails(STRING format) -- Simple function, will get massive updates in the future, as of right now It will print out the current processes that are running; listing their type, uptime, and priority. More useful additions will be added in due time. Format can be either a string "s" or "t" see below for the table format - multi:endTask(TID) -- Use multi:getTasksDetails("t") to get the tid of a task -- multi:enableLoadDetection() -- Since load detection puts some strain on the system (very little) I decided to make it something that has to be enabled. Once on it cant be turned off! +- multi:enableLoadDetection() -- Reworked how load detection works. It gives better values now, but it still needs some work before I am happy with it ```lua package.path="?/init.lua;?.lua;"..package.path @@ -99,6 +103,11 @@ Table format for getTasksDetails(STRING format) } ``` **Note:** After adding the getTasksDetails() function I noticed many areas where threads, and tasks were not being cleaned up and fixed the leaks. I also found out that a lot of tasks were starting by default and made them enable only. If you compare the benchmark from this version to last version you;ll notice a signifacant increase in performance. + +**Going forward:** +- Add something + + 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 78fc909..d6bdd29 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -125,10 +125,59 @@ end function multi:setThrestimed(n) self.deltaTarget=n or .1 end +function multi:enableLoadDetection() + if multi.maxSpd then return end + -- here we are going to run a quick benchMark solo + local temp = multi:newProcessor() + temp:Start() + local t = os.clock() + local stop = false + temp:benchMark(.01):OnBench(function(time,steps) + stop = steps*1.1 + end) + while not stop do + temp:uManager() + end + temp:__Destroy() + multi.maxSpd = stop +end +local MaxLoad = nil +function multi:setLoad(n) + MaxLoad = n +end +local busy = false +local lastVal = 0 function multi:getLoad() - if multi.load_updater:isPaused() then multi.load_updater:Resume() return 0 end - local val = math.abs(self.dStepA-self.dStepB)/multi.deltaTarget*100 - if val > 100 then return 100 else return val end + if not multi.maxSpd then multi:enableLoadDetection() end + if busy then return lastVal end + local val = nil + if thread.isThread() then + local bench + multi:benchMark(.01):OnBench(function(time,steps) + bench = steps + end) + thread.hold(function() + return bench + end) + bench = bench^1.5 + val = math.ceil((1-(bench/(multi.maxSpd/1.5)))*100) + else + busy = true + local bench + multi:benchMark(.01):OnBench(function(time,steps) + bench = steps + end) + while not bench do + multi:uManager() + end + bench = bench^1.5 + val = math.ceil((1-(bench/(multi.maxSpd/1.5)))*100) + busy = false + end + if val<0 then val = 0 end + if val > 100 then val = 100 end + lastVal = val + return val end function multi:setDomainName(name) self[name]={} @@ -282,9 +331,6 @@ function multi.AlignTable(tab) return table.concat(str,"\n") end function multi:getTasksDetails(t) - if not multi.load_updater then - multi:enableLoadDetection() - end if t == "string" or not t then str = { {"Type","Uptime","Priority","TID"} @@ -296,7 +342,6 @@ function multi:getTasksDetails(t) end table.insert(str,{v.Type:sub(1,1):upper()..v.Type:sub(2,-1)..name,multi.Round(os.clock()-v.creationTime,3),self.PriorityResolve[v.Priority],i}) end - local s = multi.AlignTable(str) dat = "" for i=1,#multi.scheduler.Threads do @@ -563,7 +608,7 @@ function multi:newProcessor(file) end end) c.l.link = c - c.l.Type = "process" + c.l.Type = "processor" function c:getController() return c.l end @@ -585,15 +630,19 @@ function multi:newProcessor(file) return self end function c:Remove() - self:__Destroy() - self.l:Destroy() + if self.Type == "process" then + self:__Destroy() + self.l:Destroy() + else + self:__Destroy() + end end if file then self.Cself=c loadstring('local process=multi.Cself '..io.open(file,'rb'):read('*all'))() end - c.__Destroy = self.Destroy - c.Destroy = c.Remove + -- c.__Destroy = self.Destroy + -- c.Destroy = c.Remove self:create(c) --~ c:IngoreObject() return c @@ -813,7 +862,7 @@ function multi.nextStep(func) if not next then next = {func} else - next[#self.next+1] = func + next[#next+1] = func end end multi.OnPreLoad=multi:newConnection() @@ -1808,12 +1857,12 @@ function multi:mainloop(settings) local solid local sRef while mainloopActive do - --print(mainloopActive) - if ncount ~= 0 then - for i = 1, ncount do - next[i]() + if next then + local DD = table.remove(next,1) + while DD do + DD() + DD = table.remove(next,1) end - ncount = 0 end if priority == 1 then for _D=#Loop,1,-1 do @@ -2313,7 +2362,7 @@ function multi:newFromString(str) end return self elseif t=="process" then - local temp=multi:newProcess() + local temp=multi:newProcessor() local objs=handle:getBlock("n",4) for i=1,objs do temp:newFromString(handle:getBlock("s",(handle:getBlock("n",4)))) @@ -2377,22 +2426,4 @@ end function multi:setDefualtStateFlag(opt) -- end -function multi:enableLoadDetection() - if multi.load_updater then return end - multi.dStepA = 0 - multi.dStepB = 0 - multi.dSwap = 0 - multi.deltaTarget = .05 - multi.load_updater = multi:newUpdater(2) - multi.load_updater:setName("LoadDetector") - multi.load_updater:OnUpdate(function(self) - if self.Parent.dSwap == 0 then - self.Parent.dStepA = os.clock() - self.Parent.dSwap = 1 - else - self.Parent.dSwap = 0 - self.Parent.dStepB = os.clock() - end - end) -end return multi diff --git a/test.lua b/test.lua index d60b7a4..4c2bca3 100644 --- a/test.lua +++ b/test.lua @@ -1,39 +1,52 @@ package.path="?/init.lua;?.lua;"..package.path multi = require("multi") -local GLOBAL,THREAD = require("multi.integration.lanesManager").init() -nGLOBAL = require("multi.integration.networkManager").init() -local a -function multi:setName(name) - self.Name = name -end -local clock = os.clock -function sleep(n) -- seconds - local t0 = clock() - while clock() - t0 <= n do end -end -master = multi:newMaster{ - name = "Main", -- the name of the master - --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections +--~ local GLOBAL,THREAD = require("multi.integration.lanesManager").init() +--~ nGLOBAL = require("multi.integration.networkManager").init() +--~ local a +--~ function multi:setName(name) +--~ self.Name = name +--~ end +--~ local clock = os.clock +--~ function sleep(n) -- seconds +--~ local t0 = clock() +--~ while clock() - t0 <= n do end +--~ end +--~ master = multi:newMaster{ +--~ name = "Main", -- the name of the master +--~ --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections --~ managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) -} -master.OnError(function(name,err) - print(name.." has encountered an error: "..err) -end) -local connlist = {} -multi:newThread("NodeUpdater",function() +--~ } +--~ master.OnError(function(name,err) +--~ print(name.." has encountered an error: "..err) +--~ end) +--~ local connlist = {} +--~ multi:newThread("NodeUpdater",function() +--~ while true do +--~ thread.sleep(1) +--~ for i=1,#connlist do +--~ conn = master:execute("TASK_MAN",connlist[i], multi:getTasksDetails()) +--~ end +--~ end +--~ end) +--~ master.OnNodeConnected(function(name) +--~ table.insert(connlist,name) +--~ end) +--~ multi.OnError(function(...) +--~ print(...) +--~ end) +--~ for i=1,20 do +--~ multi:newLoop(function() +--~ for i=1,500 do +--~ -- +--~ end +--~ end) +--~ end +multi:newThread("Test",function() while true do thread.sleep(1) - for i=1,#connlist do - conn = master:execute("TASK_MAN",connlist[i], multi:getTasksDetails()) - end + print(multi:getTasksDetails()) end end) -master.OnNodeConnected(function(name) - table.insert(connlist,name) -end) -multi.OnError(function(...) - print(...) -end) multi:mainloop{ protect = false } -- 2.43.0 From 20647f47388185785174bf3d4f1632205abcede6 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Fri, 1 Feb 2019 10:43:00 -0500 Subject: [PATCH 13/20] more updates --- changes.md | 2 + multi/init.lua | 72 +++++++++++++++----------- multi/integration/networkManager.lua | 27 ++++------ test.lua | 76 ++++++++++++---------------- 4 files changed, 88 insertions(+), 89 deletions(-) diff --git a/changes.md b/changes.md index d880d4d..6c69832 100644 --- a/changes.md +++ b/changes.md @@ -54,6 +54,8 @@ Fixed: - Found an issue with the rockspec which is due to the networkManager additon. The net Library and the multi Library are now codependent if using that feature. Going forward you will have to now install the network library separately - Insane proformance bug found in the networkManager file, where each connection to a node created a new thread (VERY BAD) If say you connected to 100s of threads, you would lose a lot of processing power due to a bad implementation of this feature. But it goes futhur than this, the net library also creates a new thread for each connection made, so times that initial 100 by about 3, you end up with a system that quickly eats itself. I have to do tons of rewriting of everything. Yet another setback for the 13.0.0 release - Fixed an issue where any argument greater than 256^2/65536 bytes is sent the networkmanager would soft crash. This was fixed by increading the limit to 256^4/4294967296 bytes. The fix was changing a 2 to a 4. Arguments greater than 256^4 would be impossible in 32 bit lua, and highly unlikely even in lua 64 bit. Perhaps someone is reading an entire file into ram and then sending the entire file that they read over a socket for some reason all at once!? +- Fixed an issue with processors not properly destroying objects within them and not being destroyable themselves +- Fixed a bug where pause and resume would duplicate objects! Not good Added: - Documentation, the purpose of 13.0.0, orginally going to be 12.2.3, but due to the amount of bugs and features I added couldn't become that. I actually still did my tests in the 12.2.3 branch in github. diff --git a/multi/init.lua b/multi/init.lua index d6bdd29..dde3b0b 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -34,7 +34,6 @@ multi.ender = {} multi.Children = {} multi.Active = true multi.fps = 60 -multi.Id = -1 multi.Type = "mainprocess" multi.Rest = 0 multi._type = type @@ -138,7 +137,7 @@ function multi:enableLoadDetection() while not stop do temp:uManager() end - temp:__Destroy() + temp:Destroy() multi.maxSpd = stop end local MaxLoad = nil @@ -335,19 +334,28 @@ function multi:getTasksDetails(t) str = { {"Type","Uptime","Priority","TID"} } + local count = 0 for i,v in pairs(self.Mainloop) do local name = v.Name or "" if name~="" then name = " <"..name..">" end + count = count + 1 table.insert(str,{v.Type:sub(1,1):upper()..v.Type:sub(2,-1)..name,multi.Round(os.clock()-v.creationTime,3),self.PriorityResolve[v.Priority],i}) end + if count == 0 then + table.insert(str,{"Currently no processes running!","","",""}) + end local s = multi.AlignTable(str) dat = "" - for i=1,#multi.scheduler.Threads do - dat = dat .. "\n" + if multi.scheduler then + for i=1,#multi.scheduler.Threads do + dat = dat .. "\n" + end + return "Load on "..({[true]="SubProcess<"..(self.Name or "Unnamed")..">",[false]="MainProcess"})[self.Type=="process"]..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\n\n"..s.."\n\n"..dat + else + return "Load on "..({[true]="SubProcess<"..(self.Name or "Unnamed")..">",[false]="MainProcess"})[self.Type=="process"]..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: 0\n\n"..s end - return "Load on manager: "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\n\n"..s.."\n\n"..dat elseif t == "t" or t == "table" then str = {ThreadCount = #multi.scheduler.Threads,MemoryUsage = math.ceil(collectgarbage("count")).." KB"} str.threads = {} @@ -503,8 +511,12 @@ function multi:Pause() print("You cannot pause the main process. Doing so will stop all methods and freeze your program! However if you still want to use multi:_Pause()") else self.Active=false - if self.Parent.Mainloop[self.Id]~=nil then - table.remove(self.Parent.Mainloop,self.Id) + local loop = self.Parent.Mainloop + for i=1,#loop do + if loop[i] == self then + table.remove(loop,i) + break + end end end return self @@ -517,9 +529,8 @@ function multi:Resume() c[i]:Resume() end else - if self:isPaused() then + if self.Active==false then table.insert(self.Parent.Mainloop,self) - self.Id=#self.Parent.Mainloop self.Active=true end end @@ -556,8 +567,10 @@ function multi:create(ref) multi.OnObjectCreated:Fire(ref,self) end function multi:setName(name) - self.Name = name - end + self.Name = name + return self +end +multi.SetName = multi.setName --Constructors [CORE] function multi:newBase(ins) if not(self.Type=='mainprocess' or self.Type=='process' or self.Type=='queue') then error('Can only create an object on multi or an interface obj') return false end @@ -573,7 +586,6 @@ function multi:newBase(ins) c.funcTMR={} c.ender={} c.important={} - c.Id=0 c.Act=function() end c.Parent=self c.held=false @@ -597,7 +609,6 @@ function multi:newProcessor(file) c.Garbage={} c.Children={} c.Active=false - c.Id=-1 c.Rest=0 c.Jobs={} c.queue={} @@ -621,7 +632,8 @@ function multi:newProcessor(file) return self end function c:setName(name) - c.Name = name + c.l.Name = name + return self end function c:Pause() if self.l then @@ -637,6 +649,18 @@ function multi:newProcessor(file) self:__Destroy() end end + function c:Destroy() + if self == c then + self.l:Destroy() + else + for i = #c.Mainloop,1,-1 do + if c.Mainloop[i] == self then + table.remove(c.Mainloop,i) + break + end + end + end + end if file then self.Cself=c loadstring('local process=multi.Cself '..io.open(file,'rb'):read('*all'))() @@ -836,7 +860,6 @@ function multi:newJob(func,name) end c.Active=true c.func={} - c.Id=0 c.Parent=self c.Type='job' c.trigfunc=func or function() end @@ -1578,7 +1601,6 @@ function multi:newThreadedProcess(name) ct.Active=true ct.func={} ct.ender={} - ct.Id=0 ct.Act=function() end ct.Parent=self ct.held=false @@ -1589,13 +1611,11 @@ function multi:newThreadedProcess(name) c.Parent=self c.Active=true c.func={} - c.Id=0 c.Type='threadedprocess' c.Mainloop={} c.Garbage={} c.Children={} c.Active=true - c.Id=-1 c.Rest=0 c.updaterate=.01 c.restRate=.1 @@ -1664,7 +1684,6 @@ function multi:newHyperThreadedProcess(name) ct.Active=true ct.func={} ct.ender={} - ct.Id=0 ct.Act=function() end ct.Parent=self ct.held=false @@ -1689,13 +1708,11 @@ function multi:newHyperThreadedProcess(name) c.Parent=self c.Active=true c.func={} - c.Id=0 c.Type='hyperthreadedprocess' c.Mainloop={} c.Garbage={} c.Children={} c.Active=true - c.Id=-1 c.Rest=0 c.updaterate=.01 c.restRate=.1 @@ -2052,11 +2069,12 @@ function multi:uManager(settings) end function multi:uManagerRef(settings) if self.Active then - if ncount ~= 0 then - for i = 1, ncount do - next[i]() + if next then + local DD = table.remove(next,1) + while DD do + DD() + DD = table.remove(next,1) end - ncount = 0 end local Loop=self.Mainloop local PS=self @@ -2236,8 +2254,6 @@ function multi:ToString() important=self.important, Active=self.Active, ender=self.ender, - -- IDK if these need to be present... - -- Id=self.Id, held=self.held, } else @@ -2248,8 +2264,6 @@ function multi:ToString() funcTMR=self.funcTMR, important=self.important, ender=self.ender, - -- IDK if these need to be present... - -- Id=self.Id, held=self.held, } end diff --git a/multi/integration/networkManager.lua b/multi/integration/networkManager.lua index b9e65be..10ea9a8 100644 --- a/multi/integration/networkManager.lua +++ b/multi/integration/networkManager.lua @@ -42,6 +42,7 @@ local CMD_CONSOLE = 0x0B local char = string.char local byte = string.byte +-- Process to hold all of the networkManager's muilt objects -- Helper for piecing commands local function pieceCommand(cmd,...) @@ -440,14 +441,11 @@ function multi:newMaster(settings) -- You will be able to have more than one mas name = self:getRandomNode() end if name==nil then - multi:newThread("Network Thread Manager",function(loop) - while true do - if name~=nil then - self:sendTo(name,char(CMD_TASK)..len..aData..len2..fData) - thread.kill() - end - thread.sleep(.1) - end + multi:newEvent(function() return name~=nil end):OnEvent(function(evnt) + self:sendTo(name,char(CMD_TASK)..len..aData..len2..fData) + evnt:Destroy() + end):SetName("DelayedSendTask"):SetName("DelayedSendTask"):SetTime(8):OnTimedOut(function(self) + self:Destroy() end) else self:sendTo(name,char(CMD_TASK)..len..aData..len2..fData) @@ -462,14 +460,11 @@ function multi:newMaster(settings) -- You will be able to have more than one mas name = "NODE_"..name end if self.connections[name]==nil then - multi:newThread("Node Data Link Controller",function(loop) - while true do - if self.connections[name]~=nil then - self.connections[name]:send(data) - thread.kill() - end - thread.sleep(.1) - end + multi:newEvent(function() return self.connections[name]~=nil end):OnEvent(function(evnt) + self.connections[name]:send(data) + evnt:Destroy() + end):SetName("DelayedSendTask"):SetTime(8):OnTimedOut(function(self) + self:Destroy() end) else self.connections[name]:send(data) diff --git a/test.lua b/test.lua index 4c2bca3..b8bf1fd 100644 --- a/test.lua +++ b/test.lua @@ -1,52 +1,40 @@ package.path="?/init.lua;?.lua;"..package.path multi = require("multi") ---~ local GLOBAL,THREAD = require("multi.integration.lanesManager").init() ---~ nGLOBAL = require("multi.integration.networkManager").init() ---~ local a ---~ function multi:setName(name) ---~ self.Name = name ---~ end ---~ local clock = os.clock ---~ function sleep(n) -- seconds ---~ local t0 = clock() ---~ while clock() - t0 <= n do end ---~ end ---~ master = multi:newMaster{ ---~ name = "Main", -- the name of the master ---~ --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections ---~ managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) ---~ } ---~ master.OnError(function(name,err) ---~ print(name.." has encountered an error: "..err) ---~ end) ---~ local connlist = {} ---~ multi:newThread("NodeUpdater",function() ---~ while true do ---~ thread.sleep(1) ---~ for i=1,#connlist do ---~ conn = master:execute("TASK_MAN",connlist[i], multi:getTasksDetails()) ---~ end ---~ end ---~ end) ---~ master.OnNodeConnected(function(name) ---~ table.insert(connlist,name) ---~ end) ---~ multi.OnError(function(...) ---~ print(...) ---~ end) ---~ for i=1,20 do ---~ multi:newLoop(function() ---~ for i=1,500 do ---~ -- ---~ end ---~ end) ---~ end -multi:newThread("Test",function() +local GLOBAL,THREAD = require("multi.integration.lanesManager").init() +nGLOBAL = require("multi.integration.networkManager").init() +local a +function multi:setName(name) + self.Name = name +end +local clock = os.clock +function sleep(n) -- seconds + local t0 = clock() + while clock() - t0 <= n do end +end +master = multi:newMaster{ + name = "Main", -- the name of the master + --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections + managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) +} +master.OnError(function(name,err) + print(name.." has encountered an error: "..err) +end) +local connlist = {} +multi:newThread("NodeUpdater",function() while true do - thread.sleep(1) - print(multi:getTasksDetails()) + thread.sleep(.1) + for i=1,#connlist do + conn = master:execute("TASK_MAN",connlist[i], multi:getTasksDetails()) + end end end) +master.OnNodeConnected(function(name) + table.insert(connlist,name) +end) +multi.OnError(function(...) + print(...) +end) + multi:mainloop{ protect = false } -- 2.43.0 From 28a4e37c51f1900b290d4fe0a6777d7bb5daf599 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Fri, 1 Feb 2019 22:43:32 -0500 Subject: [PATCH 14/20] more tweaks --- multi/init.lua | 26 +++++++++++++++++--------- test.lua | 6 +----- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/multi/init.lua b/multi/init.lua index dde3b0b..323e9c7 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -329,6 +329,8 @@ function multi.AlignTable(tab) end return table.concat(str,"\n") end +local priorityTable = {[0]="Round-Robin",[1]="Just-Right",[2]="Top-heavy",[3]="Timed-Based-Balancer"} +local ProcessName = {[true]="SubProcess",[false]="MainProcess"} function multi:getTasksDetails(t) if t == "string" or not t then str = { @@ -352,12 +354,17 @@ function multi:getTasksDetails(t) for i=1,#multi.scheduler.Threads do dat = dat .. "\n" end - return "Load on "..({[true]="SubProcess<"..(self.Name or "Unnamed")..">",[false]="MainProcess"})[self.Type=="process"]..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\n\n"..s.."\n\n"..dat + return "Load on "..ProcessName[self.Type=="process"].."<"..(self.Name or "Unnamed")..">"..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s.."\n\n"..dat else - return "Load on "..({[true]="SubProcess<"..(self.Name or "Unnamed")..">",[false]="MainProcess"})[self.Type=="process"]..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: 0\n\n"..s + return "Load on "..({[true]="SubProcess<"..(self.Name or "Unnamed")..">",[false]="MainProcess"})[self.Type=="process"]..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: 0\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s end elseif t == "t" or t == "table" then - str = {ThreadCount = #multi.scheduler.Threads,MemoryUsage = math.ceil(collectgarbage("count")).." KB"} + str = { + ThreadCount = #multi.scheduler.Threads, + MemoryUsage = math.ceil(collectgarbage("count")).." KB", + PriorityScheme = priorityTable[multi.defaultSettings.priority or 0], + SystemLoad = multi.Round(multi:getLoad(),2) + } str.threads = {} for i,v in pairs(self.Mainloop) do str[#str+1]={Type=v.Type,Name=v.Name,Uptime=os.clock()-v.creationTime,Priority=self.PriorityResolve[v.Priority],TID = i} @@ -868,7 +875,7 @@ function multi:newJob(func,name) end table.insert(self.Jobs,{c,name}) if self.JobRunner==nil then - self.JobRunner=self:newAlarm(self.jobUS) + self.JobRunner=self:newAlarm(self.jobUS):setName("multi.jobHandler") self.JobRunner:OnRing(function(self) if #self.Parent.Jobs>0 then if self.Parent.Jobs[1] then @@ -933,6 +940,7 @@ function multi:newEvent(task) table.insert(self.func,func) return self end + self:setPriority("core") self:create(c) return c end @@ -1513,7 +1521,7 @@ function multi:newThread(name,func) end function multi.initThreads() initT = true - multi.scheduler=multi:newLoop() + multi.scheduler=multi:newLoop():setName("multi.thread") multi.scheduler.Type="scheduler" function multi.scheduler:setStep(n) self.skip=tonumber(n) or 24 @@ -1652,12 +1660,12 @@ function multi:newThreadedProcess(name) multi:newAlarm(n):OnRing(function(a) holding = false a:Destroy() - end) + end):setName("multi.TPSleep") elseif type(n)=="function" then multi:newEvent(n):OnEvent(function(e) holding = false e:Destroy() - end) + end):setName("multi.TPHold") end return self end @@ -1750,12 +1758,12 @@ function multi:newHyperThreadedProcess(name) multi:newAlarm(b):OnRing(function(a) holding = false a:Destroy() - end) + end):setName("multi.HTPSleep") elseif type(b)=="function" then multi:newEvent(b):OnEvent(function(e) holding = false e:Destroy() - end) + end):setName("multi.HTPHold") end return self end diff --git a/test.lua b/test.lua index b8bf1fd..081b606 100644 --- a/test.lua +++ b/test.lua @@ -3,9 +3,6 @@ multi = require("multi") local GLOBAL,THREAD = require("multi.integration.lanesManager").init() nGLOBAL = require("multi.integration.networkManager").init() local a -function multi:setName(name) - self.Name = name -end local clock = os.clock function sleep(n) -- seconds local t0 = clock() @@ -22,7 +19,7 @@ end) local connlist = {} multi:newThread("NodeUpdater",function() while true do - thread.sleep(.1) + thread.sleep(1) for i=1,#connlist do conn = master:execute("TASK_MAN",connlist[i], multi:getTasksDetails()) end @@ -34,7 +31,6 @@ end) multi.OnError(function(...) print(...) end) - multi:mainloop{ protect = false } -- 2.43.0 From 11cd2805c8fa688df9ba211a7dddbc7132c50a69 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Sun, 3 Feb 2019 22:43:52 -0500 Subject: [PATCH 15/20] bug fixes and features --- Documentation.md | 3 ++ changes.md | 5 +-- multi/init.lua | 29 ++++++++++++----- multi/integration/lanesManager.lua | 52 +++++++++++++++++++++++++----- multi/integration/loveManager.lua | 10 +++++- multi/integration/shared.lua | 3 ++ test.lua | 21 +++++++++--- 7 files changed, 99 insertions(+), 24 deletions(-) diff --git a/Documentation.md b/Documentation.md index 6d9b438..6eba7d6 100644 --- a/Documentation.md +++ b/Documentation.md @@ -316,6 +316,7 @@ All of these functions are found on actors `self = multiObj:OnTimedOut(func)` -- If ResolveTimer was not called in time this event will be triggered. The function connected to it get a refrence of the original object that the timer was created on as the first argument. `self = multiObj:OnTimerResolved(func)` -- This event is triggered when the timer gets resolved. Same argument as above is passed, but the variable arguments that are accepted in resolvetimer are also passed as well. `self = multiObj:Reset(n)` -- In the cases where it isn't obvious what it does, it acts as Resume() +`self = multiObj:SetName(STRING name)` Actor: Events ------ @@ -822,6 +823,7 @@ ST - THREAD namespace `THREAD.getName()` -- Returns the name of the working thread `THREAD.sleep(NUMBER n)` -- Sleeps for an amount of time stopping the current thread `THREAD.hold(FUNCTION func)` -- Holds the current thread until a condition is met +`THREAD.getID()` -- returns a unique ID for the current thread. This varaiable is visible to the main thread as well by accessing it through the returned thread object. OBJ.Id ST - GLOBAL namespace --------------------- @@ -835,6 +837,7 @@ ST - System Threads ------------------- `systemThread = multi:newSystemThread(STRING thread_name,FUNCTION spawned_function,ARGUMENTS ...)` -- Spawns a thread with a certain name. `systemThread:kill()` -- kills a thread; can only be called in the main thread! +`systemThread.OnError(FUNCTION(systemthread,errMsg,errorMsgWithThreadName))` System Threads are the feature that allows a user to interact with systen threads. It differs from regular coroutine based thread in how it can interact with variables. When using system threads the GLOBAL table is the "only way"* to send data. Spawning a System thread is really simple once all the required libraries are in place. See example below: diff --git a/changes.md b/changes.md index 6c69832..26fa215 100644 --- a/changes.md +++ b/changes.md @@ -1,6 +1,6 @@ #Changes [TOC] -Update 13.0.0 So you documented it, finally! If I had a dollar for each time I found a bug working on 13.0.0 I'd be quite wealthy by now. How much lag could one expect when I've been coding with my own library wrong this entire time? +Update 13.0.0 So you documented it, finally! If I had a dollar for each time I found a bug working on 13.0.0 I'd be quite wealthy by now. ------------- **Quick note** on the 13.0.0 update: This update I went all in finding bugs and improving proformance within the library. I added some new features and the new task manager, which I used as a way to debug the library was a great help, so much so thats it is now a permanent feature. It's been about half a year since my last update, but so much work needed to be done. I hope you can find a use in your code to use my library. I am extremely proud of my work; 7 years of development, I learned so much about lua and programming through the creation of this library. It was fun, but there will always be more to add and bugs crawling there way in. I can't wait to see where this library goes in the future! @@ -10,6 +10,7 @@ Changed: - A few things, to make concepts in the library more clear. - The way functions returned paused status. Before it would return "PAUSED" now it returns nil, true if paused - Modified the connection object to allow for some more syntaxial suger! +- System threads now trigger an OnError connection that is a member of the object itself. multi.OnError() is no longer triggered for a system thread that crashes! Connection Example: ```lua @@ -65,6 +66,7 @@ Added: - multi:getTasksDetails(STRING format) -- Simple function, will get massive updates in the future, as of right now It will print out the current processes that are running; listing their type, uptime, and priority. More useful additions will be added in due time. Format can be either a string "s" or "t" see below for the table format - multi:endTask(TID) -- Use multi:getTasksDetails("t") to get the tid of a task - multi:enableLoadDetection() -- Reworked how load detection works. It gives better values now, but it still needs some work before I am happy with it +- THREAD.getID() -- returns a unique ID for the current thread. This varaiable is visible to the main thread as well by accessing it through the returned thread object. OBJ.Id Do not confuse this with thread.* this refers to the system threading interface ```lua package.path="?/init.lua;?.lua;"..package.path @@ -109,7 +111,6 @@ Table format for getTasksDetails(STRING format) **Going forward:** - Add something - 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 323e9c7..c811d25 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -28,6 +28,7 @@ multi.Version = "13.0.0" multi._VERSION = "13.0.0" multi.stage = "stable" multi.__index = multi +multi.Name = "multi.Root" multi.Mainloop = {} multi.Garbage = {} multi.ender = {} @@ -159,7 +160,7 @@ function multi:getLoad() return bench end) bench = bench^1.5 - val = math.ceil((1-(bench/(multi.maxSpd/1.5)))*100) + val = math.ceil((1-(bench/(multi.maxSpd/2.2)))*100) else busy = true local bench @@ -170,7 +171,7 @@ function multi:getLoad() multi:uManager() end bench = bench^1.5 - val = math.ceil((1-(bench/(multi.maxSpd/1.5)))*100) + val = math.ceil((1-(bench/(multi.maxSpd/2.2)))*100) busy = false end if val<0 then val = 0 end @@ -330,11 +331,11 @@ function multi.AlignTable(tab) return table.concat(str,"\n") end local priorityTable = {[0]="Round-Robin",[1]="Just-Right",[2]="Top-heavy",[3]="Timed-Based-Balancer"} -local ProcessName = {[true]="SubProcess",[false]="MainProcess"} +local ProcessName = {[true]="SubProcessor",[false]="MainProcessor"} function multi:getTasksDetails(t) if t == "string" or not t then str = { - {"Type","Uptime","Priority","TID"} + {"Type \n" + end + end if multi.scheduler then for i=1,#multi.scheduler.Threads do dat = dat .. "\n" end - return "Load on "..ProcessName[self.Type=="process"].."<"..(self.Name or "Unnamed")..">"..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s.."\n\n"..dat + return "Load on "..ProcessName[self.Type=="process"].."<"..(self.Name or "Unnamed")..">"..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\nSystemThreads Running: "..#(multi.SystemThreads or {}).."\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s.."\n\n"..dat..dat2 else - return "Load on "..({[true]="SubProcess<"..(self.Name or "Unnamed")..">",[false]="MainProcess"})[self.Type=="process"]..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: 0\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s + return "Load on "..ProcessName[self.Type=="process"].."<"..(self.Name or "Unnamed")..">"..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: 0\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s..dat2 end elseif t == "t" or t == "table" then str = { @@ -366,12 +373,18 @@ function multi:getTasksDetails(t) SystemLoad = multi.Round(multi:getLoad(),2) } str.threads = {} + str.systemthreads = {} for i,v in pairs(self.Mainloop) do str[#str+1]={Type=v.Type,Name=v.Name,Uptime=os.clock()-v.creationTime,Priority=self.PriorityResolve[v.Priority],TID = i} end for i=1,#multi.scheduler.Threads do str.threads[multi.scheduler.Threads[i].Name]={Uptime = os.clock()-multi.scheduler.Threads[i].creationTime} end + if multi.canSystemThread then + for i=1,#multi.SystemThreads do + str.systemthreads[multi.SystemThreads[i].Name]={Uptime = os.clock()-multi.SystemThreads[i].creationTime} + end + end return str end end @@ -1576,7 +1589,7 @@ function multi.initThreads() event:OnEvent(function(evnt) evnt.link.sleep=0 evnt:Destroy() - end) + end):setName("multi.thread.skip") elseif ret[1]=="_hold_" then self.Threads[i].timer:Reset() self.Threads[i].sleep=math.huge @@ -1589,7 +1602,7 @@ function multi.initThreads() multi.nextStep(function() evnt:Destroy() end) - end) + end):setName("multi.thread.hold") elseif ret.Name then self.Globals[ret.Name]=ret.Value end diff --git a/multi/integration/lanesManager.lua b/multi/integration/lanesManager.lua index ea201f4..5a97347 100644 --- a/multi/integration/lanesManager.lua +++ b/multi/integration/lanesManager.lua @@ -32,6 +32,8 @@ end -- Step 1 get lanes lanes=require("lanes").configure() local multi = require("multi") -- get it all and have it on all lanes +multi.SystemThreads = {} +local thread = thread multi.isMainThread=true function multi:canSystemThread() return true @@ -93,6 +95,9 @@ end function THREAD.getName() return THREAD_NAME end +function THREAD.getID() + return THREAD_ID +end --[[ Step 4 We need to get sleeping working to handle timing... We want idle wait, not busy wait Idle wait keeps the CPU running better where busy wait wastes CPU cycles... Lanes does not have a sleep method however, a linda recieve will in fact be a idle wait! So we use that and wrap it in a nice package]] @@ -109,17 +114,32 @@ function THREAD.hold(n) end local rand = math.random(1,10000000) -- Step 5 Basic Threads! +-- local threads = {} +local count = 0 +local started function multi:newSystemThread(name,func,...) + multi.InitSystemThreadErrorHandler() rand = math.random(1,10000000) local c={} local __self=c c.name=name + c.Name = name + c.Id = count + local THREAD_ID = count + count = count + 1 c.Type="sthread" + c.creationTime = os.clock() local THREAD_NAME=name local function func2(...) + local multi = require("multi") _G["THREAD_NAME"]=THREAD_NAME + _G["THREAD_ID"]=THREAD_ID math.randomseed(rand) func(...) + if _G.__Needs_Multi then + multi:mainloop() + end + THREAD.kill() end c.thread=lanes.gen("*", func2)(...) function c:kill() @@ -127,16 +147,32 @@ function multi:newSystemThread(name,func,...) self.thread:cancel() print("Thread: '"..self.name.."' has been stopped!") end - c.status=multi:newUpdater(multi.Priority_IDLE) - c.status.link=c - c.status:OnUpdate(function(self) - local v,err,t=self.link.thread:join(.001) - if err then - multi.OnError:Fire(self.link,err,"Error in systemThread: '"..self.link.name.."' <"..err..">") - self:Destroy() + table.insert(multi.SystemThreads,c) + c.OnError = multi:newConnection() + return c +end +function multi.InitSystemThreadErrorHandler() + if started then return end + multi:newThread("ThreadErrorHandler",function() + local deadThreads = {} + local threads = multi.SystemThreads + while true do + thread.sleep(.5) -- switching states often takes a huge hit on performance. half a second to tell me there is an error is good enough. + for i=#threads,1,-1 do + local v,err,t=threads[i].thread:join(.001) + if err then + if err:find("Thread was killed!") then + table.remove(threads,i) + else + threads[i].OnError:Fire(threads[i],err,"Error in systemThread: '"..threads[i].name.."' <"..err..">") + table.remove(threads,i) + table.insert(deadThreads,threads[i].Id) + GLOBAL["__DEAD_THREADS__"]=deadThreads + end + end + end end end) - return c end print("Integrated Lanes!") multi.integration={} -- for module creators diff --git a/multi/integration/loveManager.lua b/multi/integration/loveManager.lua index bc886eb..50c4e6c 100644 --- a/multi/integration/loveManager.lua +++ b/multi/integration/loveManager.lua @@ -34,6 +34,7 @@ multi.integration.love2d.ThreadBase=[[ tab={...} __THREADID__=table.remove(tab,1) __THREADNAME__=table.remove(tab,1) +THREAD_ID=table.remove(tab,1) require("love.filesystem") require("love.system") require("love.timer") @@ -217,6 +218,9 @@ isMainThread=true function THREAD.getName() return __THREADNAME__ end +function THREAD.getID() + return THREAD_ID +end function ToStr(val, name, skipnewlines, depth) skipnewlines = skipnewlines or false depth = depth or 0 @@ -295,12 +299,16 @@ local function randomString(n) end return str end +local count = 0 function multi:newSystemThread(name,func,...) -- the main method local c={} c.name=name + c.Name = name c.ID=c.name.."" + c.Id=count + count = count + 1 c.thread=love.thread.newThread(multi.integration.love2d.ThreadBase:gsub("INSERT_USER_CODE",dump(func))) - c.thread:start(c.ID,c.name,...) + c.thread:start(c.ID,c.name,,...) function c:kill() multi.integration.GLOBAL["__DIEPLZ"..self.ID.."__"]="__DIEPLZ"..self.ID.."__" end diff --git a/multi/integration/shared.lua b/multi/integration/shared.lua index 217326f..2149456 100644 --- a/multi/integration/shared.lua +++ b/multi/integration/shared.lua @@ -123,6 +123,7 @@ function multi:newSystemThreadedConnection(name,protect) local qsm = multi:newSystemThreadedQueue(name.."THREADED_CALLSYNCM"):init() local qs = multi:newSystemThreadedQueue(name.."THREADED_CALLSYNC"):init() function c:init() + _G.__Needs_Multi = true local multi = require("multi") if multi:getPlatform()=="love2d" then GLOBAL=_G.GLOBAL @@ -241,6 +242,7 @@ function multi:newSystemThreadedConsole(name) local sThread=multi.integration.THREAD local GLOBAL=multi.integration.GLOBAL function c:init() + _G.__Needs_Multi = true local multi = require("multi") if multi:getPlatform()=="love2d" then GLOBAL=_G.GLOBAL @@ -288,6 +290,7 @@ function multi:newSystemThreadedTable(name) local sThread=multi.integration.THREAD local GLOBAL=multi.integration.GLOBAL function c:init() -- create an init function so we can mimic on both love2d and lanes + _G.__Needs_Multi = true local multi = require("multi") if multi:getPlatform()=="love2d" then GLOBAL=_G.GLOBAL diff --git a/test.lua b/test.lua index 081b606..9a0de0a 100644 --- a/test.lua +++ b/test.lua @@ -5,12 +5,12 @@ nGLOBAL = require("multi.integration.networkManager").init() local a local clock = os.clock function sleep(n) -- seconds - local t0 = clock() - while clock() - t0 <= n do end + local t0 = clock() + while clock() - t0 <= n do end end master = multi:newMaster{ name = "Main", -- the name of the master - --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections +--~ --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) } master.OnError(function(name,err) @@ -28,9 +28,20 @@ end) master.OnNodeConnected(function(name) table.insert(connlist,name) end) -multi.OnError(function(...) - print(...) +--~ multi:newThread("TaskView",function() +--~ while true do +--~ thread.sleep(1) +--~ print(multi:getTasksDetails()) +--~ end +--~ end) +multi:newSystemThread("SystemThread",function() + local multi = require("multi") + print(THREAD.getName(),THREAD.getID()) + THREAD.sleep(8) +end).OnError(function(a,b,c) + print("ERROR:",b) end) +--~ print(multi:getTasksDetails()) multi:mainloop{ protect = false } -- 2.43.0 From 8f0f404c36281b87202c787d60d45d859902dc02 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Wed, 6 Feb 2019 00:22:32 -0500 Subject: [PATCH 16/20] small changes Im still have tons of work to do --- changes.md | 1 + multi/init.lua | 3 ++- multi/integration/lanesManager.lua | 5 +++-- multi/integration/shared.lua | 30 ++++++++++++++++++++---------- test.lua | 29 +++++++++++++++-------------- 5 files changed, 41 insertions(+), 27 deletions(-) diff --git a/changes.md b/changes.md index 26fa215..4206721 100644 --- a/changes.md +++ b/changes.md @@ -57,6 +57,7 @@ Fixed: - Fixed an issue where any argument greater than 256^2/65536 bytes is sent the networkmanager would soft crash. This was fixed by increading the limit to 256^4/4294967296 bytes. The fix was changing a 2 to a 4. Arguments greater than 256^4 would be impossible in 32 bit lua, and highly unlikely even in lua 64 bit. Perhaps someone is reading an entire file into ram and then sending the entire file that they read over a socket for some reason all at once!? - Fixed an issue with processors not properly destroying objects within them and not being destroyable themselves - Fixed a bug where pause and resume would duplicate objects! Not good +- Noticed that the switching of lua states, corutine based threading, is slow when done often. Code your threads to have an idler of .5 seconds between sleep states. After doing this to a few built in threads I've seen a nice drop in performance. 68%-100% to 0%-40% when using the jobqueue. If you dont need the hold feature then use a multi object! Sleeping can be done in a multi object using timers and alarms. Though if your aim is speed timers are a bit faster than alarms, if you really want to boost speed then local clock = os.clock and use the clock function to do your timings yourself Added: - Documentation, the purpose of 13.0.0, orginally going to be 12.2.3, but due to the amount of bugs and features I added couldn't become that. I actually still did my tests in the 12.2.3 branch in github. diff --git a/multi/init.lua b/multi/init.lua index c811d25..b2dabb3 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -335,7 +335,7 @@ local ProcessName = {[true]="SubProcessor",[false]="MainProcessor"} function multi:getTasksDetails(t) if t == "string" or not t then str = { - {"Type ","Uptime","Priority","TID"} } local count = 0 for i,v in pairs(self.Mainloop) do @@ -1489,6 +1489,7 @@ multi:setDomainName("Threads") multi:setDomainName("Globals") local initT = false function multi:newThread(name,func) + if not func then return end local c={} c.ref={} c.Name=name diff --git a/multi/integration/lanesManager.lua b/multi/integration/lanesManager.lua index 5a97347..6395b1e 100644 --- a/multi/integration/lanesManager.lua +++ b/multi/integration/lanesManager.lua @@ -116,7 +116,7 @@ local rand = math.random(1,10000000) -- Step 5 Basic Threads! -- local threads = {} local count = 0 -local started +local started = false function multi:newSystemThread(name,func,...) multi.InitSystemThreadErrorHandler() rand = math.random(1,10000000) @@ -152,7 +152,8 @@ function multi:newSystemThread(name,func,...) return c end function multi.InitSystemThreadErrorHandler() - if started then return end + if started==true then return end + started = true multi:newThread("ThreadErrorHandler",function() local deadThreads = {} local threads = multi.SystemThreads diff --git a/multi/integration/shared.lua b/multi/integration/shared.lua index 2149456..2a3cb61 100644 --- a/multi/integration/shared.lua +++ b/multi/integration/shared.lua @@ -112,6 +112,7 @@ function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a chann end return c end +-- NEEDS WORK function multi:newSystemThreadedConnection(name,protect) local c={} local sThread=multi.integration.THREAD @@ -284,6 +285,7 @@ function multi:newSystemThreadedConsole(name) GLOBAL[c.name]=c return c end +-- NEEDS WORK function multi:newSystemThreadedTable(name) local c={} c.name=name -- set the name this is important for identifying what is what @@ -334,8 +336,9 @@ function multi:newSystemThreadedJobQueue(a,b) local sThread=multi.integration.THREAD local c = {} c.numberofcores = 4 + c.idle = nil c.name = "SYSTEM_THREADED_JOBQUEUE_"..jobqueuecount - -- This is done to keep backwards compatability for older code + -- This is done to keep backwards compatibility for older code if type(a)=="string" and not(b) then c.name = a elseif type(a)=="number" and not (b) then @@ -363,6 +366,7 @@ function multi:newSystemThreadedJobQueue(a,b) end c.tempQueue = {} function c:pushJob(name,...) + c.idle = os.clock() if not self.isReady then table.insert(c.tempQueue,{self.jobnum,name,...}) self.jobnum=self.jobnum+1 @@ -429,12 +433,9 @@ function multi:newSystemThreadedJobQueue(a,b) end end end) - multi:newThread("Idler",function() - while true do - if os.clock()-lastjob>1 then - sThread.sleep(.1) - end - thread.sleep(.001) + multi:newLoop(function() + if os.clock()-lastjob>1 then + sThread.sleep(.1) end end) setmetatable(_G,{ @@ -447,11 +448,12 @@ function multi:newSystemThreadedJobQueue(a,b) end end,c.name) end - multi:newThread("counter",function() + local clock = os.clock + multi:newThread("JQ-"..c.name.." Manager",function() print("thread started") local _count = 0 while _count= 15 then + c.idle = nil + end + end dat = queueJD:pop() if dat then + c.idle = clock() c.OnJobCompleted:Fire(unpack(dat)) end + thread.skip() end end) return c diff --git a/test.lua b/test.lua index 9a0de0a..2264108 100644 --- a/test.lua +++ b/test.lua @@ -10,7 +10,7 @@ function sleep(n) -- seconds end master = multi:newMaster{ name = "Main", -- the name of the master ---~ --noBroadCast = true, -- if using the node manager, set this to true to avoid double connections + noBroadCast = true, -- if using the node manager, set this to true to avoid double connections managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) } master.OnError(function(name,err) @@ -28,20 +28,21 @@ end) master.OnNodeConnected(function(name) table.insert(connlist,name) end) ---~ multi:newThread("TaskView",function() ---~ while true do ---~ thread.sleep(1) ---~ print(multi:getTasksDetails()) ---~ end ---~ end) -multi:newSystemThread("SystemThread",function() - local multi = require("multi") - print(THREAD.getName(),THREAD.getID()) - THREAD.sleep(8) -end).OnError(function(a,b,c) - print("ERROR:",b) +multi.OnError(function(...) + print(...) end) ---~ print(multi:getTasksDetails()) +multi:newSystemThreadedConsole("console"):init() +jQueue = multi:newSystemThreadedJobQueue("MainJobQueue") +jQueue.OnReady:holdUT() +jQueue:doToAll(function() + console = THREAD.waitFor("console"):init() +end) +jQueue:registerJob("TEST",function(a,b,c) + console:print(a,b,c) + return "This is a test" +end) +jQueue:pushJob("TEST",123,"Hello",false) +--~ multi:benchMark(1,nil,"Bench:") multi:mainloop{ protect = false } -- 2.43.0 From 4272397678be61283144c2ab5e96f5a6f2e27647 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Fri, 8 Feb 2019 22:19:13 -0500 Subject: [PATCH 17/20] ST-Connections Fixed Fixed system threaded connections Also added some features to monitor threads Each thread now has its own ID even the main thread which has an id of 0! --- changes.md | 11 +- multi/init.lua | 37 +++--- multi/integration/lanesManager.lua | 32 +++-- multi/integration/loveManager.lua | 4 +- multi/integration/luvitManager.lua | 2 +- multi/integration/networkManager.lua | 20 +-- multi/integration/shared.lua | 177 ++++++++++++++------------- test.lua | 159 ++++++++++++++++++++++-- 8 files changed, 296 insertions(+), 146 deletions(-) diff --git a/changes.md b/changes.md index 4206721..97cba6e 100644 --- a/changes.md +++ b/changes.md @@ -50,7 +50,7 @@ These didn't have much use in their previous form, but with the addition of hype Fixed: - There were some bugs in the networkmanager.lua file. Desrtoy -> Destroy some misspellings. -- Massive object management bugs which caused performance to drop like a rock. Remember to Destroy objects when no longer using them. I should probably start working on a garbage collector for these objects! +- Massive object management bugs which caused performance to drop like a rock. - Found a bug with processors not having the Destroy() function implemented properly. - Found an issue with the rockspec which is due to the networkManager additon. The net Library and the multi Library are now codependent if using that feature. Going forward you will have to now install the network library separately - Insane proformance bug found in the networkManager file, where each connection to a node created a new thread (VERY BAD) If say you connected to 100s of threads, you would lose a lot of processing power due to a bad implementation of this feature. But it goes futhur than this, the net library also creates a new thread for each connection made, so times that initial 100 by about 3, you end up with a system that quickly eats itself. I have to do tons of rewriting of everything. Yet another setback for the 13.0.0 release @@ -58,16 +58,21 @@ Fixed: - Fixed an issue with processors not properly destroying objects within them and not being destroyable themselves - Fixed a bug where pause and resume would duplicate objects! Not good - Noticed that the switching of lua states, corutine based threading, is slow when done often. Code your threads to have an idler of .5 seconds between sleep states. After doing this to a few built in threads I've seen a nice drop in performance. 68%-100% to 0%-40% when using the jobqueue. If you dont need the hold feature then use a multi object! Sleeping can be done in a multi object using timers and alarms. Though if your aim is speed timers are a bit faster than alarms, if you really want to boost speed then local clock = os.clock and use the clock function to do your timings yourself +- multi:newSystemThreadedConnection(name,protect) -- I did it! It works and I believe all the gotchas are fixed as well. +-- Issue one, if a thread died that was connected to that connection all connections would stop since the queue would get clogged! FIXED +-- There is one thing, the connection does have some handshakes that need to be done before it functions as normal! Added: -- Documentation, the purpose of 13.0.0, orginally going to be 12.2.3, but due to the amount of bugs and features I added couldn't become that. I actually still did my tests in the 12.2.3 branch in github. +- Documentation, the purpose of 13.0.0, orginally going to be 12.2.3, but due to the amount of bugs and features added it couldn't be a simple bug fix update. - multi:newHyperThreadedProcess(STRING name) -- This is a version of the threaded process that gives each object created its own coroutine based thread which means you can use thread.* without affecting other objects created within the hyper threaded processes. Though, creating a self contained single thread is a better idea which when I eventually create the wiki page I'll discuss - multi:newConnector() -- A simple object that allows you to use the new connection Fire syntax without using a multi obj or the standard object format that I follow. - multi:purge() -- Removes all references to objects that are contained withing the processes list of tasks to do. Doing this will stop all objects from functioning. Calling Resume on an object should make it work again. - multi:getTasksDetails(STRING format) -- Simple function, will get massive updates in the future, as of right now It will print out the current processes that are running; listing their type, uptime, and priority. More useful additions will be added in due time. Format can be either a string "s" or "t" see below for the table format - multi:endTask(TID) -- Use multi:getTasksDetails("t") to get the tid of a task - multi:enableLoadDetection() -- Reworked how load detection works. It gives better values now, but it still needs some work before I am happy with it -- THREAD.getID() -- returns a unique ID for the current thread. This varaiable is visible to the main thread as well by accessing it through the returned thread object. OBJ.Id Do not confuse this with thread.* this refers to the system threading interface +- THREAD.getID() -- returns a unique ID for the current thread. This varaiable is visible to the main thread as well by accessing it through the returned thread object. OBJ.Id Do not confuse this with thread.* this refers to the system threading interface. Each thread, including the main thread has a threadID the main thread has an ID of 0! +- multi.print(...) works like normal print, but only prints if the setting print is set to true +- setting: `print` enables multi.print() to work ```lua package.path="?/init.lua;?.lua;"..package.path diff --git a/multi/init.lua b/multi/init.lua index b2dabb3..3167e23 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -110,18 +110,6 @@ function table.merge(t1, t2) end return t1 end -_print=print -function print(...) - if not __SUPPRESSPRINTS then - _print(...) - end -end -_write=io.write -function io.write(...) - if not __SUPPRESSWRITES then - _write(...) - end -end function multi:setThrestimed(n) self.deltaTarget=n or .1 end @@ -251,7 +239,7 @@ function multi.executeFunction(name,...) if type(_G[name])=='function' then _G[name](...) else - print('Error: Not a function') + multi.print('Error: Not a function') end end function multi:getChildren() @@ -283,7 +271,7 @@ function multi:benchMark(sec,p,pt) local temp=self:newLoop(function(self,t) if t>sec then if pt then - print(pt.." "..c.." Steps in "..sec.." second(s)!") + multi.print(pt.." "..c.." Steps in "..sec.." second(s)!") end self.tt(sec,c) self:Destroy() @@ -458,7 +446,7 @@ function multi:connectFinal(func) elseif self.Type=='step' or self.Type=='tstep' then self:OnEnd(func) else - print("Warning!!! "..self.Type.." doesn't contain a Final Connection State! Use "..self.Type..":Break(func) to trigger it's final event!") + multi.print("Warning!!! "..self.Type.." doesn't contain a Final Connection State! Use "..self.Type..":Break(func) to trigger it's final event!") self:OnBreak(func) end end @@ -528,7 +516,7 @@ end -- Timer stuff done function multi:Pause() if self.Type=='mainprocess' then - print("You cannot pause the main process. Doing so will stop all methods and freeze your program! However if you still want to use multi:_Pause()") + multi.print("You cannot pause the main process. Doing so will stop all methods and freeze your program! However if you still want to use multi:_Pause()") else self.Active=false local loop = self.Parent.Mainloop @@ -795,7 +783,7 @@ function multi:newConnection(protect,func) table.remove(temp,1) table.insert(ret,temp) else - print(temp[2]) + multi.print(temp[2]) end else table.insert(ret,{self.func[i][1](...)}) @@ -1395,7 +1383,7 @@ end function multi:newWatcher(namespace,name) local function WatcherObj(ns,n) if self.Type=='queue' then - print("Cannot create a watcher on a queue! Creating on 'multi' instead!") + multi.print("Cannot create a watcher on a queue! Creating on 'multi' instead!") self=multi end local c=self:newBase() @@ -1423,7 +1411,7 @@ function multi:newWatcher(namespace,name) elseif type(namespace)=='table' and (type(name)=='string' or 'number') then return WatcherObj(namespace,name) else - print('Warning, invalid arguments! Nothing returned!') + multi.print('Warning, invalid arguments! Nothing returned!') end end -- Threading stuff @@ -1485,6 +1473,11 @@ function thread.testFor(name,_val,sym) end) return thread.get(name) end +function multi.print(...) + if multi.defaultSettings.print then + print(...) + end +end multi:setDomainName("Threads") multi:setDomainName("Globals") local initT = false @@ -1571,7 +1564,7 @@ function multi.initThreads() self.Parent.OnError:Fire(self.Threads[i],"Error in thread: <"..self.Threads[i].Name.."> "..ret) end if ret==true or ret==false then - print("Thread Ended!!!") + multi.print("Thread Ended!!!") ret={} end end @@ -2264,7 +2257,7 @@ function multi:ToString() if self.Ingore then return end local t=self.Type local data; - print(t) + multi.print(t) if t:sub(-6)=="Thread" then data={ Type=t, @@ -2341,7 +2334,7 @@ function multi:ToString() set=self.set, }) elseif t=="watcher" then - print("Currently cannot sterilize a watcher object!") + multi.print("Currently cannot sterilize a watcher object!") -- needs testing -- table.merge(data,{ -- ns=self.ns, diff --git a/multi/integration/lanesManager.lua b/multi/integration/lanesManager.lua index 6395b1e..e96368a 100644 --- a/multi/integration/lanesManager.lua +++ b/multi/integration/lanesManager.lua @@ -41,10 +41,10 @@ end function multi:getPlatform() return "lanes" end --- Step 2 set up the linda objects +-- Step 2 set up the Linda objects local __GlobalLinda = lanes.linda() -- handles global stuff local __SleepingLinda = lanes.linda() -- handles sleeping stuff --- For convience a GLOBAL table will be constructed to handle requests +-- For convenience a GLOBAL table will be constructed to handle requests local GLOBAL={} setmetatable(GLOBAL,{ __index=function(t,k) @@ -54,7 +54,7 @@ setmetatable(GLOBAL,{ __GlobalLinda:set(k,v) end, }) --- Step 3 rewrite the thread methods to use lindas +-- Step 3 rewrite the thread methods to use Lindas local THREAD={} function THREAD.set(name,val) __GlobalLinda:set(name,val) @@ -84,6 +84,9 @@ end function THREAD.getCores() return THREAD.__CORES end +function THREAD.getThreads() + return GLOBAL.__THREADS__ +end if os.getOS()=="windows" then THREAD.__CORES=tonumber(os.getenv("NUMBER_OF_PROCESSORS")) else @@ -98,6 +101,7 @@ end function THREAD.getID() return THREAD_ID end +_G.THREAD_ID = 0 --[[ Step 4 We need to get sleeping working to handle timing... We want idle wait, not busy wait Idle wait keeps the CPU running better where busy wait wastes CPU cycles... Lanes does not have a sleep method however, a linda recieve will in fact be a idle wait! So we use that and wrap it in a nice package]] @@ -114,9 +118,10 @@ function THREAD.hold(n) end local rand = math.random(1,10000000) -- Step 5 Basic Threads! --- local threads = {} -local count = 0 +local threads = {} +local count = 1 local started = false +local livingThreads = {} function multi:newSystemThread(name,func,...) multi.InitSystemThreadErrorHandler() rand = math.random(1,10000000) @@ -125,6 +130,7 @@ function multi:newSystemThread(name,func,...) c.name=name c.Name = name c.Id = count + livingThreads[count] = {true,name} local THREAD_ID = count count = count + 1 c.Type="sthread" @@ -143,19 +149,19 @@ function multi:newSystemThread(name,func,...) end c.thread=lanes.gen("*", func2)(...) function c:kill() - --self.status:Destroy() self.thread:cancel() - print("Thread: '"..self.name.."' has been stopped!") + multi.print("Thread: '"..self.name.."' has been stopped!") end table.insert(multi.SystemThreads,c) c.OnError = multi:newConnection() + GLOBAL["__THREADS__"]=livingThreads return c end +multi.OnSystemThreadDied = multi:newConnection() function multi.InitSystemThreadErrorHandler() if started==true then return end started = true multi:newThread("ThreadErrorHandler",function() - local deadThreads = {} local threads = multi.SystemThreads while true do thread.sleep(.5) -- switching states often takes a huge hit on performance. half a second to tell me there is an error is good enough. @@ -163,19 +169,23 @@ function multi.InitSystemThreadErrorHandler() local v,err,t=threads[i].thread:join(.001) if err then if err:find("Thread was killed!") then + livingThreads[threads[i].Id] = {false,threads[i].Name} + multi.OnSystemThreadDied:Fire(threads[i].Id) + GLOBAL["__THREADS__"]=livingThreads table.remove(threads,i) else threads[i].OnError:Fire(threads[i],err,"Error in systemThread: '"..threads[i].name.."' <"..err..">") + livingThreads[threads[i].Id] = {false,threads[i].Name} + multi.OnSystemThreadDied:Fire(threads[i].Id) + GLOBAL["__THREADS__"]=livingThreads table.remove(threads,i) - table.insert(deadThreads,threads[i].Id) - GLOBAL["__DEAD_THREADS__"]=deadThreads end end end end end) end -print("Integrated Lanes!") +multi.print("Integrated Lanes!") multi.integration={} -- for module creators multi.integration.GLOBAL=GLOBAL multi.integration.THREAD=THREAD diff --git a/multi/integration/loveManager.lua b/multi/integration/loveManager.lua index 50c4e6c..9b19dc2 100644 --- a/multi/integration/loveManager.lua +++ b/multi/integration/loveManager.lua @@ -316,7 +316,7 @@ function multi:newSystemThread(name,func,...) -- the main method end function love.threaderror( thread, errorstr ) multi.OnError:Fire(thread,errorstr) - print("Error in systemThread: "..tostring(thread)..": "..errorstr) + multi.print("Error in systemThread: "..tostring(thread)..": "..errorstr) end local THREAD={} function THREAD.set(name,val) @@ -374,7 +374,7 @@ updater:OnUpdate(function(self) end end) require("multi.integration.shared") -print("Integrated Love2d!") +multi.print("Integrated Love2d!") return { init=function(t) if t then diff --git a/multi/integration/luvitManager.lua b/multi/integration/luvitManager.lua index 401501c..6e7fba4 100644 --- a/multi/integration/luvitManager.lua +++ b/multi/integration/luvitManager.lua @@ -114,7 +114,7 @@ local function _INIT(luvitThread,timer) luvitThread.start(entry,package.path,name,c.func,...) return c end - print("Integrated Luvit!") + multi.print("Integrated Luvit!") multi.integration={} -- for module creators multi.integration.GLOBAL=GLOBAL multi.integration.THREAD=THREAD diff --git a/multi/integration/networkManager.lua b/multi/integration/networkManager.lua index 10ea9a8..21c847a 100644 --- a/multi/integration/networkManager.lua +++ b/multi/integration/networkManager.lua @@ -194,21 +194,21 @@ function multi:newNode(settings) node.hasFuncs = {} node.OnError = multi:newConnection() node.OnError(function(node,err,master) - print("ERROR",err,node.name) + multi.print("ERROR",err,node.name) local temp = bin.new() temp:addBlock(#node.name,2) temp:addBlock(node.name) temp:addBlock(#err,2) temp:addBlock(err) for i,v in pairs(node.connections) do - print(i) + multi.print(i) v[1]:send(v[2],char(CMD_ERROR)..temp.data,v[3]) end end) if settings.managerDetails then local c = net:newTCPClient(settings.managerDetails[1],settings.managerDetails[2]) if not c then - print("Cannot connect to the node manager! Ensuring broadcast is enabled!") settings.noBroadCast = false + multi.print("Cannot connect to the node manager! Ensuring broadcast is enabled!") settings.noBroadCast = false else c.OnDataRecieved(function(self,data) if data == "ping" then @@ -220,7 +220,7 @@ function multi:newNode(settings) end if not settings.preload then if node.functions:getSize()~=0 then - print("We have function(s) to preload!") + multi.print("We have function(s) to preload!") local len = node.functions:getBlock("n",1) local name,func while len do @@ -270,14 +270,14 @@ function multi:newNode(settings) node.queue:push(resolveData(dat)) elseif cmd == CMD_REG then if not settings.allowRemoteRegistering then - print(ip..": has attempted to register a function when it is currently not allowed!") + multi.print(ip..": has attempted to register a function when it is currently not allowed!") return end local temp = bin.new(dat) local len = temp:getBlock("n",1) local name = temp:getBlock("s",len) if node.hasFuncs[name] then - print("Function already preloaded onto the node!") + multi.print("Function already preloaded onto the node!") return end len = temp:getBlock("n",2) @@ -304,13 +304,13 @@ function multi:newNode(settings) node.OnError:Fire(node,err,server) end elseif cmd == CMD_INITNODE then - print("Connected with another node!") + multi.print("Connected with another node!") node.connections[dat]={server,ip,port} multi.OnGUpdate(function(k,v) server:send(ip,table.concat{char(CMD_GLOBAL),k,"|",v},port) end)-- set this up elseif cmd == CMD_INITMASTER then - print("Connected to the master!",dat) + multi.print("Connected to the master!",dat) node.connections[dat]={server,ip,port} multi.OnGUpdate(function(k,v) server:send(ip,table.concat{char(CMD_GLOBAL),k,"|",v},port) @@ -357,7 +357,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas if settings.managerDetails then local client = net:newTCPClient(settings.managerDetails[1],settings.managerDetails[2]) if not client then - print("Warning: Cannot connect to the node manager! Ensuring broadcast listening is enabled!") settings.noBroadCast = false + multi.print("Warning: Cannot connect to the node manager! Ensuring broadcast listening is enabled!") settings.noBroadCast = false else client.OnDataRecieved(function(client,data) local cmd = data:sub(1,1) @@ -550,7 +550,7 @@ function multi:newMaster(settings) -- You will be able to have more than one mas return master end -- The init function that gets returned -print("Integrated Network Parallelism") +multi.print("Integrated Network Parallelism") return {init = function() return GLOBAL end} diff --git a/multi/integration/shared.lua b/multi/integration/shared.lua index 2a3cb61..fe6a00b 100644 --- a/multi/integration/shared.lua +++ b/multi/integration/shared.lua @@ -113,88 +113,88 @@ function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a chann return c end -- NEEDS WORK -function multi:newSystemThreadedConnection(name,protect) - local c={} - local sThread=multi.integration.THREAD - local GLOBAL=multi.integration.GLOBAL - c.name = name or error("You must supply a name for this object!") - c.protect = protect or false - c.count = 0 - multi:newSystemThreadedQueue(name.."THREADED_CALLFIRE"):init() - local qsm = multi:newSystemThreadedQueue(name.."THREADED_CALLSYNCM"):init() - local qs = multi:newSystemThreadedQueue(name.."THREADED_CALLSYNC"):init() - function c:init() - _G.__Needs_Multi = true - local multi = require("multi") - if multi:getPlatform()=="love2d" then - GLOBAL=_G.GLOBAL - sThread=_G.sThread - end - local conns = 0 - local qF = sThread.waitFor(self.name.."THREADED_CALLFIRE"):init() - local qSM = sThread.waitFor(self.name.."THREADED_CALLSYNCM"):init() - local qS = sThread.waitFor(self.name.."THREADED_CALLSYNC"):init() - qSM:push("OK") - local conn = {} - conn.obj = multi:newConnection(self.protect) - setmetatable(conn,{__call=function(self,...) return self:connect(...) end}) - function conn:connect(func) - return self.obj(func) - end - function conn:holdUT(n) - self.obj:holdUT(n) - end - function conn:Remove() - self.obj:Remove() - end - function conn:Fire(...) - local args = {multi.randomString(8),...} - for i = 1, conns do - qF:push(args) - end - end - local lastID = "" - local lastCount = 0 - multi:newThread("syncer",function() - while true do - thread.skip(1) - local fire = qF:peek() - local count = qS:peek() - if fire and fire[1]~=lastID then - lastID = fire[1] - qF:pop() - table.remove(fire,1) - conn.obj:Fire(unpack(fire)) - end - if count and count[1]~=lastCount then - conns = count[2] - lastCount = count[1] - qs:pop() - end - end - end) - return conn - end - multi:newThread("connSync",function() - while true do - thread.skip(1) - local syncIN = qsm:pop() - if syncIN then - if syncIN=="OK" then - c.count = c.count + 1 - else - c.count = c.count - 1 - end - local rand = math.random(1,1000000) - for i = 1, c.count do - qs:push({rand,c.count}) - end - end - end - end) - GLOBAL[name]=c - return c -end +-- function multi:newSystemThreadedConnection(name,protect) + -- local c={} + -- local sThread=multi.integration.THREAD + -- local GLOBAL=multi.integration.GLOBAL + -- c.name = name or error("You must supply a name for this object!") + -- c.protect = protect or false + -- c.count = 0 + -- multi:newSystemThreadedQueue(name.."THREADED_CALLFIRE"):init() + -- local qsm = multi:newSystemThreadedQueue(name.."THREADED_CALLSYNCM"):init() + -- local qs = multi:newSystemThreadedQueue(name.."THREADED_CALLSYNC"):init() + -- function c:init() + -- _G.__Needs_Multi = true + -- local multi = require("multi") + -- if multi:getPlatform()=="love2d" then + -- GLOBAL=_G.GLOBAL + -- sThread=_G.sThread + -- end + -- local conns = 0 + -- local qF = sThread.waitFor(self.name.."THREADED_CALLFIRE"):init() + -- local qSM = sThread.waitFor(self.name.."THREADED_CALLSYNCM"):init() + -- local qS = sThread.waitFor(self.name.."THREADED_CALLSYNC"):init() + -- qSM:push("OK") + -- local conn = {} + -- conn.obj = multi:newConnection(self.protect) + -- setmetatable(conn,{__call=function(self,...) return self:connect(...) end}) + -- function conn:connect(func) + -- return self.obj(func) + -- end + -- function conn:holdUT(n) + -- self.obj:holdUT(n) + -- end + -- function conn:Remove() + -- self.obj:Remove() + -- end + -- function conn:Fire(...) + -- local args = {multi.randomString(8),...} + -- for i = 1, conns do + -- qF:push(args) + -- end + -- end + -- local lastID = "" + -- local lastCount = 0 + -- multi:newThread("syncer",function() + -- while true do + -- thread.skip(1) + -- local fire = qF:peek() + -- local count = qS:peek() + -- if fire and fire[1]~=lastID then + -- lastID = fire[1] + -- qF:pop() + -- table.remove(fire,1) + -- conn.obj:Fire(unpack(fire)) + -- end + -- if count and count[1]~=lastCount then + -- conns = count[2] + -- lastCount = count[1] + -- qs:pop() + -- end + -- end + -- end) + -- return conn + -- end + -- multi:newThread("connSync",function() + -- while true do + -- thread.skip(1) + -- local syncIN = qsm:pop() + -- if syncIN then + -- if syncIN=="OK" then + -- c.count = c.count + 1 + -- else + -- c.count = c.count - 1 + -- end + -- local rand = math.random(1,1000000) + -- for i = 1, c.count do + -- qs:push({rand,c.count}) + -- end + -- end + -- end + -- end) + -- GLOBAL[name]=c + -- return c +-- end function multi:SystemThreadedBenchmark(n) n=n or 1 local cores=multi.integration.THREAD.getCores() @@ -265,7 +265,7 @@ function multi:newSystemThreadedConsole(name) print(unpack(data)) end end - end) + end):setName("ST.consoleSyncer") end else cc.stream = sThread.waitFor("__SYSTEM_CONSOLE__"):init() @@ -330,6 +330,7 @@ function multi:newSystemThreadedTable(name) return c end local jobqueuecount = 0 +local jqueues = {} function multi:newSystemThreadedJobQueue(a,b) jobqueuecount=jobqueuecount+1 local GLOBAL=multi.integration.GLOBAL @@ -350,6 +351,10 @@ function multi:newSystemThreadedJobQueue(a,b) c.name = b c.numberofcores = a end + if jqueues[c.name] then + error("A job queue by the name: "..c.name.." already exists!") + end + jqueues[c.name] = true c.isReady = false c.jobnum=1 c.OnJobCompleted = multi:newConnection() @@ -378,8 +383,9 @@ function multi:newSystemThreadedJobQueue(a,b) end end function c:doToAll(func) + local r = multi.randomString(12) for i = 1, self.numberofcores do - queueDA:push{multi.randomString(12),func} + queueDA:push{r,func} end end for i=1,c.numberofcores do @@ -450,7 +456,6 @@ function multi:newSystemThreadedJobQueue(a,b) end local clock = os.clock multi:newThread("JQ-"..c.name.." Manager",function() - print("thread started") local _count = 0 while _count= 15 then c.idle = nil end + thread.skip() end dat = queueJD:pop() if dat then c.idle = clock() c.OnJobCompleted:Fire(unpack(dat)) end - thread.skip() end end) return c diff --git a/test.lua b/test.lua index 2264108..1054c42 100644 --- a/test.lua +++ b/test.lua @@ -31,18 +31,155 @@ end) multi.OnError(function(...) print(...) end) -multi:newSystemThreadedConsole("console"):init() -jQueue = multi:newSystemThreadedJobQueue("MainJobQueue") -jQueue.OnReady:holdUT() -jQueue:doToAll(function() - console = THREAD.waitFor("console"):init() + + +local conncount = 0 +function multi:newSystemThreadedConnection(name,protect) + conncount = conncount + 1 + local c={} + c.name = name or error("You must provide a name for the connection object!") + c.protect = protect or false + c.idle = nil + local sThread=multi.integration.THREAD + local GLOBAL=multi.integration.GLOBAL + local connSync = multi:newSystemThreadedQueue(c.name.."_CONN_SYNC") + local connFire = multi:newSystemThreadedQueue(c.name.."_CONN_FIRE") + function c:init() + local multi = require("multi") + if love then -- lets make sure we don't reference up-values if using love2d + GLOBAL=_G.GLOBAL + sThread=_G.sThread + end + local conn = {} + conn.obj = multi:newConnection() + setmetatable(conn,{ + __call=function(self,...) + return self:connect(...) + end + }) + local ID = sThread.getID() + local sync = sThread.waitFor(self.name.."_CONN_SYNC"):init() + local fire = sThread.waitFor(self.name.."_CONN_FIRE"):init() + local connections = {} + if not multi.isMainThread then + connections = {0} + end + sync:push{"INIT",ID} -- Register this as an active connection! + function conn:connect(func) + return self.obj(func) + end + function conn:holdUT(n) + self.obj:holdUT(n) + end + function conn:Remove() + self.obj:Remove() + end + function conn:Fire(...) + for i = 1,#connections do + fire:push{connections[i],ID,{...}} + end + end + -- FIRE {TO,FROM,{ARGS}} + local data + multi:newLoop(function() + data = fire:peek() + if type(data)=="table" and data[1]==ID then + if data[2]==ID and conn.IgnoreSelf then + fire:pop() + return + end + fire:pop() + conn.obj:Fire(unpack(data[3])) + end + -- We need to hangle syncs here as well + data = sync:peek() + if data~=nil and data[1]=="SYNCA" and data[2]==ID then + sync:pop() + table.insert(connections,data[3]) + end + if type(data)=="table" and data[1]=="SYNCR" and data[2]==ID then + sync:pop() + for i=1,#connections do + if connections[i] == data[3] then + table.remove(connections,i) + end + end + end + end) + return conn + end + local cleanUp = {} + multi.OnSystemThreadDied(function(ThreadID) + for i=1,#syncs do + connSync:push{"SYNCR",syncs[i],ThreadID} + end + cleanUp[ThreadID] = true + end) + multi:newThread(c.name.." Connection Handler",function() + local data + local clock = os.clock + local syncs = {} + while true do + if not c.idle then + thread.sleep(.1) + else + if clock() - c.idle >= 15 then + c.idle = nil + end + thread.skip() + end + data = connSync:peek() + if data~= nil and data[1]=="INIT" then + connSync:pop() + c.idle = clock() + table.insert(syncs,data[2]) + for i=1,#syncs do + connSync:push{"SYNCA",syncs[i],data[2]} + end + end + data = connFire:peek() + if data~=nil and cleanUp[data[1]] then + local meh = data[1] + connFire:pop() -- lets remove dead thread stuff + multi:newAlarm(15):OnRing(function(a) + cleanUp[meh] = nil + end) + end + end + end) + GLOBAL[c.name]=c + return c +end + + +local conn = multi:newSystemThreadedConnection("conn"):init() +conn(function(...) + print("MAIN",...) end) -jQueue:registerJob("TEST",function(a,b,c) - console:print(a,b,c) - return "This is a test" +conn.IgnoreSelf = true +multi:newSystemThread("meh",function() + local multi = require("multi") + local conn = THREAD.waitFor("conn"):init() + conn.IgnoreSelf = true + conn(function(...) + print("THREAD:",...) + end) + multi:newAlarm(1):OnRing(function(a) + conn:Fire("Does this work?") + a:Destroy() + end) + multi.OnError(function(...) + print(...) + end) + multi:mainloop() +end).OnError(function(...) + print(...) +end) +multi:newAlarm(2):OnRing(function(a) + conn:Fire("What about this one?") + a:Destroy() end) -jQueue:pushJob("TEST",123,"Hello",false) ---~ multi:benchMark(1,nil,"Bench:") multi:mainloop{ - protect = false + protect = false, +--~ print = true } -- 2.43.0 From 391625674a9444e1262cbc81b78324583de540b8 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Sat, 9 Feb 2019 23:32:26 -0500 Subject: [PATCH 18/20] ST - connections work Finally, but more bugs found Need to fix, sigh why node manager no work in love2d. Both luajit, both luasocket, both net and multi library. works in non love, but not love I wanna just melt, there is no reason why no work --- changes.md | 3 + multi/init.lua | 44 ++++-- multi/integration/loveManager.lua | 44 +++++- multi/integration/shared.lua | 215 ++++++++++++++++++------------ sample-nodeManager.lua | 12 ++ test.lua | 155 +-------------------- 6 files changed, 222 insertions(+), 251 deletions(-) create mode 100644 sample-nodeManager.lua diff --git a/changes.md b/changes.md index 97cba6e..1c103c7 100644 --- a/changes.md +++ b/changes.md @@ -73,6 +73,9 @@ Added: - THREAD.getID() -- returns a unique ID for the current thread. This varaiable is visible to the main thread as well by accessing it through the returned thread object. OBJ.Id Do not confuse this with thread.* this refers to the system threading interface. Each thread, including the main thread has a threadID the main thread has an ID of 0! - multi.print(...) works like normal print, but only prints if the setting print is set to true - setting: `print` enables multi.print() to work +- STC: IgnoreSelf defaults to false, if true a Fire command will not be sent to the self +- STC: OnConnectionAdded(function(connID)) -- Is fired when a connection is added you can use STC:FireTo(id,...) to trigger a specific connection. Works like the named non threaded connections, only the id's are genereated for you. +- STC: FireTo(id,...) -- Described above. ```lua package.path="?/init.lua;?.lua;"..package.path diff --git a/multi/init.lua b/multi/init.lua index 3167e23..b0a7a10 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -28,7 +28,7 @@ multi.Version = "13.0.0" multi._VERSION = "13.0.0" multi.stage = "stable" multi.__index = multi -multi.Name = "multi.Root" +multi.Name = "multi.root" multi.Mainloop = {} multi.Garbage = {} multi.ender = {} @@ -121,7 +121,7 @@ function multi:enableLoadDetection() local t = os.clock() local stop = false temp:benchMark(.01):OnBench(function(time,steps) - stop = steps*1.1 + stop = steps end) while not stop do temp:uManager() @@ -135,6 +135,7 @@ function multi:setLoad(n) end local busy = false local lastVal = 0 +local bb = 0 function multi:getLoad() if not multi.maxSpd then multi:enableLoadDetection() end if busy then return lastVal end @@ -143,6 +144,7 @@ function multi:getLoad() local bench multi:benchMark(.01):OnBench(function(time,steps) bench = steps + bb = steps end) thread.hold(function() return bench @@ -154,6 +156,7 @@ function multi:getLoad() local bench multi:benchMark(.01):OnBench(function(time,steps) bench = steps + bb = steps end) while not bench do multi:uManager() @@ -165,7 +168,7 @@ function multi:getLoad() if val<0 then val = 0 end if val > 100 then val = 100 end lastVal = val - return val + return val,bb*100 end function multi:setDomainName(name) self[name]={} @@ -197,6 +200,13 @@ function multi:setPriority(s) end self.solid = true end + if not self.PrioritySet then + self.defPriority = self.Priority + self.PrioritySet = true + end +end +function multi:ResetPriority() + self.Priority = self.defPriority end -- System function os.getOS() @@ -345,20 +355,24 @@ function multi:getTasksDetails(t) dat2 = dat2.."\n" end end + local load,steps = multi:getLoad() if multi.scheduler then for i=1,#multi.scheduler.Threads do dat = dat .. "\n" end - return "Load on "..ProcessName[self.Type=="process"].."<"..(self.Name or "Unnamed")..">"..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\nSystemThreads Running: "..#(multi.SystemThreads or {}).."\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s.."\n\n"..dat..dat2 + return "Load on "..ProcessName[self.Type=="process"].."<"..(self.Name or "Unnamed")..">"..": "..multi.Round(load,2).."%\nCycles Per Second Per Task: "..steps.."\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: "..#multi.scheduler.Threads.."\nSystemThreads Running: "..#(multi.SystemThreads or {}).."\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s.."\n\n"..dat..dat2 else - return "Load on "..ProcessName[self.Type=="process"].."<"..(self.Name or "Unnamed")..">"..": "..multi.Round(multi:getLoad(),2).."%\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: 0\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s..dat2 + return "Load on "..ProcessName[self.Type=="process"].."<"..(self.Name or "Unnamed")..">"..": "..multi.Round(load,2).."%\nCycles Per Second Per Task: "..steps.."\n\nMemory Usage: "..math.ceil(collectgarbage("count")).." KB\nThreads Running: 0\nPriority Scheme: "..priorityTable[multi.defaultSettings.priority or 0].."\n\n"..s..dat2 end elseif t == "t" or t == "table" then + local load,steps = multi:getLoad() str = { + ProcessName = (self.Name or "Unnamed"), ThreadCount = #multi.scheduler.Threads, MemoryUsage = math.ceil(collectgarbage("count")).." KB", PriorityScheme = priorityTable[multi.defaultSettings.priority or 0], - SystemLoad = multi.Round(multi:getLoad(),2) + SystemLoad = multi.Round(load,2), + CyclesPerSecondPerTask = steps, } str.threads = {} str.systemthreads = {} @@ -970,7 +984,7 @@ end function multi:newAlarm(set) local c=self:newBase() c.Type='alarm' - c.Priority=self.Priority_Low + c:setPriority("Low") c.set=set or 0 local count = 0 local t = clock() @@ -1174,7 +1188,7 @@ function multi:newTStep(start,reset,count,set) local c=self:newBase() think=1 c.Type='tstep' - c.Priority=self.Priority_Low + c:setPriority("Low") c.start=start or 1 local reset = reset or math.huge c.endAt=reset @@ -1266,7 +1280,7 @@ function multi:newTimeStamper() ["12"] = 31, } c.Type='timestamper' - c.Priority=self.Priority_Idle + c:setPriority("Idle") c.hour = {} c.minute = {} c.second = {} @@ -1886,8 +1900,8 @@ function multi:mainloop(settings) local PS=self local PStep = 1 local autoP = 0 - local solid - local sRef + local solid,sRef + local cc=0 while mainloopActive do if next then local DD = table.remove(next,1) @@ -1947,8 +1961,12 @@ function multi:mainloop(settings) PStep=0 end elseif priority == 3 then - tt = clock()-t - t = clock() + cc=cc+1 + if cc == 1000 then + tt = clock()-t + t = clock() + cc=0 + end 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 diff --git a/multi/integration/loveManager.lua b/multi/integration/loveManager.lua index 9b19dc2..19bc2b4 100644 --- a/multi/integration/loveManager.lua +++ b/multi/integration/loveManager.lua @@ -168,6 +168,9 @@ end function sThread.getName() return __THREADNAME__ end +function sThread.getID() + return THREAD_ID +end function sThread.kill() error("Thread was killed!") end @@ -196,6 +199,7 @@ func=loadDump([=[INSERT_USER_CODE]=])(unpack(tab)) multi:mainloop() ]] GLOBAL={} -- Allow main thread to interact with these objects as well +_G.THREAD_ID = 0 __proxy__={} setmetatable(GLOBAL,{ __index=function(t,k) @@ -215,6 +219,7 @@ setmetatable(GLOBAL,{ THREAD={} -- Allow main thread to interact with these objects as well multi.integration.love2d.mainChannel=love.thread.getChannel("__MainChan__") isMainThread=true +multi.SystemThreads = {} function THREAD.getName() return __THREADNAME__ end @@ -299,16 +304,19 @@ local function randomString(n) end return str end -local count = 0 +local count = 1 +local livingThreads = {} function multi:newSystemThread(name,func,...) -- the main method + multi.InitSystemThreadErrorHandler() local c={} c.name=name c.Name = name c.ID=c.name.."" c.Id=count count = count + 1 + livingThreads[count] = {true,name} c.thread=love.thread.newThread(multi.integration.love2d.ThreadBase:gsub("INSERT_USER_CODE",dump(func))) - c.thread:start(c.ID,c.name,,...) + c.thread:start(c.ID,c.name,THREAD_ID,...) function c:kill() multi.integration.GLOBAL["__DIEPLZ"..self.ID.."__"]="__DIEPLZ"..self.ID.."__" end @@ -341,8 +349,7 @@ end __channels__={} multi.integration.GLOBAL=GLOBAL multi.integration.THREAD=THREAD -updater=multi:newUpdater() -updater:OnUpdate(function(self) +updater=multi:newLoop(function(self) local data=multi.integration.love2d.mainChannel:pop() while data do if type(data)=="string" then @@ -373,6 +380,35 @@ updater:OnUpdate(function(self) data=multi.integration.love2d.mainChannel:pop() end end) +multi.OnSystemThreadDied = multi:newConnection() +local started = false +function multi.InitSystemThreadErrorHandler() + if started==true then return end + started = true + multi:newThread("ThreadErrorHandler",function() + local threads = multi.SystemThreads + while true do + thread.sleep(.5) -- switching states often takes a huge hit on performance. half a second to tell me there is an error is good enough. + for i=#threads,1,-1 do + local v,err,t=threads[i].thread:join(.001) + if err then + if err:find("Thread was killed!") then + livingThreads[threads[i].Id] = {false,threads[i].Name} + multi.OnSystemThreadDied:Fire(threads[i].Id) + GLOBAL["__THREADS__"]=livingThreads + table.remove(threads,i) + else + threads[i].OnError:Fire(threads[i],err,"Error in systemThread: '"..threads[i].name.."' <"..err..">") + livingThreads[threads[i].Id] = {false,threads[i].Name} + multi.OnSystemThreadDied:Fire(threads[i].Id) + GLOBAL["__THREADS__"]=livingThreads + table.remove(threads,i) + end + end + end + end + end) +end require("multi.integration.shared") multi.print("Integrated Love2d!") return { diff --git a/multi/integration/shared.lua b/multi/integration/shared.lua index fe6a00b..e1fd6d1 100644 --- a/multi/integration/shared.lua +++ b/multi/integration/shared.lua @@ -112,89 +112,138 @@ function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a chann end return c end --- NEEDS WORK --- function multi:newSystemThreadedConnection(name,protect) - -- local c={} - -- local sThread=multi.integration.THREAD - -- local GLOBAL=multi.integration.GLOBAL - -- c.name = name or error("You must supply a name for this object!") - -- c.protect = protect or false - -- c.count = 0 - -- multi:newSystemThreadedQueue(name.."THREADED_CALLFIRE"):init() - -- local qsm = multi:newSystemThreadedQueue(name.."THREADED_CALLSYNCM"):init() - -- local qs = multi:newSystemThreadedQueue(name.."THREADED_CALLSYNC"):init() - -- function c:init() - -- _G.__Needs_Multi = true - -- local multi = require("multi") - -- if multi:getPlatform()=="love2d" then - -- GLOBAL=_G.GLOBAL - -- sThread=_G.sThread - -- end - -- local conns = 0 - -- local qF = sThread.waitFor(self.name.."THREADED_CALLFIRE"):init() - -- local qSM = sThread.waitFor(self.name.."THREADED_CALLSYNCM"):init() - -- local qS = sThread.waitFor(self.name.."THREADED_CALLSYNC"):init() - -- qSM:push("OK") - -- local conn = {} - -- conn.obj = multi:newConnection(self.protect) - -- setmetatable(conn,{__call=function(self,...) return self:connect(...) end}) - -- function conn:connect(func) - -- return self.obj(func) - -- end - -- function conn:holdUT(n) - -- self.obj:holdUT(n) - -- end - -- function conn:Remove() - -- self.obj:Remove() - -- end - -- function conn:Fire(...) - -- local args = {multi.randomString(8),...} - -- for i = 1, conns do - -- qF:push(args) - -- end - -- end - -- local lastID = "" - -- local lastCount = 0 - -- multi:newThread("syncer",function() - -- while true do - -- thread.skip(1) - -- local fire = qF:peek() - -- local count = qS:peek() - -- if fire and fire[1]~=lastID then - -- lastID = fire[1] - -- qF:pop() - -- table.remove(fire,1) - -- conn.obj:Fire(unpack(fire)) - -- end - -- if count and count[1]~=lastCount then - -- conns = count[2] - -- lastCount = count[1] - -- qs:pop() - -- end - -- end - -- end) - -- return conn - -- end - -- multi:newThread("connSync",function() - -- while true do - -- thread.skip(1) - -- local syncIN = qsm:pop() - -- if syncIN then - -- if syncIN=="OK" then - -- c.count = c.count + 1 - -- else - -- c.count = c.count - 1 - -- end - -- local rand = math.random(1,1000000) - -- for i = 1, c.count do - -- qs:push({rand,c.count}) - -- end - -- end - -- end - -- end) - -- GLOBAL[name]=c - -- return c --- end + +function multi:newSystemThreadedConnection(name,protect) + local c={} + c.name = name or error("You must provide a name for the connection object!") + c.protect = protect or false + c.idle = nil + local sThread=multi.integration.THREAD + local GLOBAL=multi.integration.GLOBAL + local connSync = multi:newSystemThreadedQueue(c.name.."_CONN_SYNC") + local connFire = multi:newSystemThreadedQueue(c.name.."_CONN_FIRE") + function c:init() + local multi = require("multi") + if love then -- lets make sure we don't reference up-values if using love2d + GLOBAL=_G.GLOBAL + sThread=_G.sThread + end + local conn = {} + conn.obj = multi:newConnection() + setmetatable(conn,{ + __call=function(self,...) + return self:connect(...) + end + }) + local ID = sThread.getID() + local sync = sThread.waitFor(self.name.."_CONN_SYNC"):init() + local fire = sThread.waitFor(self.name.."_CONN_FIRE"):init() + local connections = {} + if not multi.isMainThread then + connections = {0} + end + sync:push{"INIT",ID} -- Register this as an active connection! + function conn:connect(func) + return self.obj(func) + end + function conn:holdUT(n) + self.obj:holdUT(n) + end + function conn:Remove() + self.obj:Remove() + end + function conn:Fire(...) + for i = 1,#connections do + fire:push{connections[i],ID,{...}} + end + end + function conn:FireTo(to,...) + local good = false + for i = 1,#connections do + if connections[i]==to then + good = true + break + end + end + if not good then return multi.print("NonExisting Connection!") end + fire:push{to,ID,{...}} + end + -- FIRE {TO,FROM,{ARGS}} + local data + local clock = os.clock + conn.OnConnectionAdded = multi:newConnection() + multi:newLoop(function() + data = fire:peek() + if type(data)=="table" and data[1]==ID then + if data[2]==ID and conn.IgnoreSelf then + fire:pop() + return + end + fire:pop() + conn.obj:Fire(unpack(data[3])) + end + data = sync:peek() + if data~=nil and data[1]=="SYNCA" and data[2]==ID then + sync:pop() + multi.nextStep(function() + conn.OnConnectionAdded:Fire(data[3]) + end) + table.insert(connections,data[3]) + end + if type(data)=="table" and data[1]=="SYNCR" and data[2]==ID then + sync:pop() + for i=1,#connections do + if connections[i] == data[3] then + table.remove(connections,i) + end + end + end + end):setName("STConn.syncer") + return conn + end + local cleanUp = {} + multi.OnSystemThreadDied(function(ThreadID) + for i=1,#syncs do + connSync:push{"SYNCR",syncs[i],ThreadID} + end + cleanUp[ThreadID] = true + end) + multi:newThread(c.name.." Connection-Handler",function() + local data + local clock = os.clock + local syncs = {} + while true do + if not c.idle then + thread.sleep(.5) + else + if clock() - c.idle >= 15 then + c.idle = nil + end + thread.skip() + end + data = connSync:peek() + if data~= nil and data[1]=="INIT" then + connSync:pop() + c.idle = clock() + table.insert(syncs,data[2]) + for i=1,#syncs do + connSync:push{"SYNCA",syncs[i],data[2]} + end + end + data = connFire:peek() + if data~=nil and cleanUp[data[1]] then + local meh = data[1] + connFire:pop() -- lets remove dead thread stuff + multi:newAlarm(15):OnRing(function(a) + cleanUp[meh] = nil + end) + end + end + end) + GLOBAL[c.name]=c + return c +end + function multi:SystemThreadedBenchmark(n) n=n or 1 local cores=multi.integration.THREAD.getCores() diff --git a/sample-nodeManager.lua b/sample-nodeManager.lua new file mode 100644 index 0000000..7d57596 --- /dev/null +++ b/sample-nodeManager.lua @@ -0,0 +1,12 @@ +package.path="?/init.lua;?.lua;"..package.path +multi = require("multi") +local GLOBAL, THREAD = require("multi.integration.lanesManager").init() +nGLOBAL = require("multi.integration.networkManager").init() +multi:nodeManager(12345) -- Host a node manager on port: 12345 +print("Node Manager Running...") +settings = { + priority = 0, -- 1 or 2 + protect = false, +} +multi:mainloop(settings) +-- Thats all you need to run the node manager, everything else is done automatically diff --git a/test.lua b/test.lua index 1054c42..bca0dc5 100644 --- a/test.lua +++ b/test.lua @@ -11,7 +11,7 @@ end master = multi:newMaster{ name = "Main", -- the name of the master noBroadCast = true, -- if using the node manager, set this to true to avoid double connections - managerDetails = {"192.168.1.4",12345}, -- the details to connect to the node manager (ip,port) + managerDetails = {"localhost",12345}, -- the details to connect to the node manager (ip,port) } master.OnError(function(name,err) print(name.." has encountered an error: "..err) @@ -21,165 +21,18 @@ multi:newThread("NodeUpdater",function() while true do thread.sleep(1) for i=1,#connlist do - conn = master:execute("TASK_MAN",connlist[i], multi:getTasksDetails()) + master:execute("TASK_MAN",connlist[i], multi:getTasksDetails()) end end end) master.OnNodeConnected(function(name) + print("Connected to the node") table.insert(connlist,name) end) multi.OnError(function(...) print(...) end) - - -local conncount = 0 -function multi:newSystemThreadedConnection(name,protect) - conncount = conncount + 1 - local c={} - c.name = name or error("You must provide a name for the connection object!") - c.protect = protect or false - c.idle = nil - local sThread=multi.integration.THREAD - local GLOBAL=multi.integration.GLOBAL - local connSync = multi:newSystemThreadedQueue(c.name.."_CONN_SYNC") - local connFire = multi:newSystemThreadedQueue(c.name.."_CONN_FIRE") - function c:init() - local multi = require("multi") - if love then -- lets make sure we don't reference up-values if using love2d - GLOBAL=_G.GLOBAL - sThread=_G.sThread - end - local conn = {} - conn.obj = multi:newConnection() - setmetatable(conn,{ - __call=function(self,...) - return self:connect(...) - end - }) - local ID = sThread.getID() - local sync = sThread.waitFor(self.name.."_CONN_SYNC"):init() - local fire = sThread.waitFor(self.name.."_CONN_FIRE"):init() - local connections = {} - if not multi.isMainThread then - connections = {0} - end - sync:push{"INIT",ID} -- Register this as an active connection! - function conn:connect(func) - return self.obj(func) - end - function conn:holdUT(n) - self.obj:holdUT(n) - end - function conn:Remove() - self.obj:Remove() - end - function conn:Fire(...) - for i = 1,#connections do - fire:push{connections[i],ID,{...}} - end - end - -- FIRE {TO,FROM,{ARGS}} - local data - multi:newLoop(function() - data = fire:peek() - if type(data)=="table" and data[1]==ID then - if data[2]==ID and conn.IgnoreSelf then - fire:pop() - return - end - fire:pop() - conn.obj:Fire(unpack(data[3])) - end - -- We need to hangle syncs here as well - data = sync:peek() - if data~=nil and data[1]=="SYNCA" and data[2]==ID then - sync:pop() - table.insert(connections,data[3]) - end - if type(data)=="table" and data[1]=="SYNCR" and data[2]==ID then - sync:pop() - for i=1,#connections do - if connections[i] == data[3] then - table.remove(connections,i) - end - end - end - end) - return conn - end - local cleanUp = {} - multi.OnSystemThreadDied(function(ThreadID) - for i=1,#syncs do - connSync:push{"SYNCR",syncs[i],ThreadID} - end - cleanUp[ThreadID] = true - end) - multi:newThread(c.name.." Connection Handler",function() - local data - local clock = os.clock - local syncs = {} - while true do - if not c.idle then - thread.sleep(.1) - else - if clock() - c.idle >= 15 then - c.idle = nil - end - thread.skip() - end - data = connSync:peek() - if data~= nil and data[1]=="INIT" then - connSync:pop() - c.idle = clock() - table.insert(syncs,data[2]) - for i=1,#syncs do - connSync:push{"SYNCA",syncs[i],data[2]} - end - end - data = connFire:peek() - if data~=nil and cleanUp[data[1]] then - local meh = data[1] - connFire:pop() -- lets remove dead thread stuff - multi:newAlarm(15):OnRing(function(a) - cleanUp[meh] = nil - end) - end - end - end) - GLOBAL[c.name]=c - return c -end - - -local conn = multi:newSystemThreadedConnection("conn"):init() -conn(function(...) - print("MAIN",...) -end) -conn.IgnoreSelf = true -multi:newSystemThread("meh",function() - local multi = require("multi") - local conn = THREAD.waitFor("conn"):init() - conn.IgnoreSelf = true - conn(function(...) - print("THREAD:",...) - end) - multi:newAlarm(1):OnRing(function(a) - conn:Fire("Does this work?") - a:Destroy() - end) - multi.OnError(function(...) - print(...) - end) - multi:mainloop() -end).OnError(function(...) - print(...) -end) -multi:newAlarm(2):OnRing(function(a) - conn:Fire("What about this one?") - a:Destroy() -end) multi:mainloop{ protect = false, ---~ print = true + print = true } -- 2.43.0 From 2fad69a1ecfc5b74acb8bbf637269b2e7448a9fa Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Mon, 25 Feb 2019 11:19:01 -0500 Subject: [PATCH 19/20] small tweaks There are some things that are left to be done. love2d support needs some work and there is one issue that I am trying to fix with this library. Should be able to release soonish --- multi/compat/love2d.lua | 2 +- multi/init.lua | 2 +- multi/integration/lanesManager.lua | 2 +- multi/integration/loveManager.lua | 2 +- multi/integration/luvitManager.lua | 2 +- multi/integration/networkManager.lua | 2 +- multi/integration/shared.lua | 2 +- 7 files changed, 7 insertions(+), 7 deletions(-) diff --git a/multi/compat/love2d.lua b/multi/compat/love2d.lua index 917a561..7048deb 100644 --- a/multi/compat/love2d.lua +++ b/multi/compat/love2d.lua @@ -1,7 +1,7 @@ --[[ MIT License -Copyright (c) 2018 Ryan Ward +Copyright (c) 2019 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 diff --git a/multi/init.lua b/multi/init.lua index b0a7a10..19894e1 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -1,7 +1,7 @@ --[[ MIT License -Copyright (c) 2018 Ryan Ward +Copyright (c) 2019 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 diff --git a/multi/integration/lanesManager.lua b/multi/integration/lanesManager.lua index e96368a..2172f7c 100644 --- a/multi/integration/lanesManager.lua +++ b/multi/integration/lanesManager.lua @@ -1,7 +1,7 @@ --[[ MIT License -Copyright (c) 2018 Ryan Ward +Copyright (c) 2019 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 diff --git a/multi/integration/loveManager.lua b/multi/integration/loveManager.lua index 19bc2b4..02f2f9e 100644 --- a/multi/integration/loveManager.lua +++ b/multi/integration/loveManager.lua @@ -1,7 +1,7 @@ --[[ MIT License -Copyright (c) 2018 Ryan Ward +Copyright (c) 2019 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 diff --git a/multi/integration/luvitManager.lua b/multi/integration/luvitManager.lua index 6e7fba4..862783d 100644 --- a/multi/integration/luvitManager.lua +++ b/multi/integration/luvitManager.lua @@ -1,7 +1,7 @@ --[[ MIT License -Copyright (c) 2018 Ryan Ward +Copyright (c) 2019 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 diff --git a/multi/integration/networkManager.lua b/multi/integration/networkManager.lua index 21c847a..7102b7d 100644 --- a/multi/integration/networkManager.lua +++ b/multi/integration/networkManager.lua @@ -1,7 +1,7 @@ --[[ MIT License -Copyright (c) 2018 Ryan Ward +Copyright (c) 2019 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 diff --git a/multi/integration/shared.lua b/multi/integration/shared.lua index e1fd6d1..6d420ba 100644 --- a/multi/integration/shared.lua +++ b/multi/integration/shared.lua @@ -1,7 +1,7 @@ --[[ MIT License -Copyright (c) 2018 Ryan Ward +Copyright (c) 2019 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 -- 2.43.0 From 6714878ae930b5a36dbc91d94679a820c60f8412 Mon Sep 17 00:00:00 2001 From: Ryan Ward Date: Fri, 22 Mar 2019 21:20:30 -0400 Subject: [PATCH 20/20] 13.0.0 Stability testing To be released soon --- .gitignore | 2 + Documentation.html | 935 ++++++++++++++++++++++++++++++++++++++++++++- changes.html | 166 ++++++-- changes.md | 17 +- test.lua | 60 +-- 5 files changed, 1102 insertions(+), 78 deletions(-) diff --git a/.gitignore b/.gitignore index c288235..0aff785 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ sample-node.lua sample-master.lua Ayn Rand - The Virtue of Selfishness-Mg4QJheclsQ.m4a Atlas Shrugged by Ayn Rand Audiobook-9s2qrEau63E.webm +test.lua +test.lua diff --git a/Documentation.html b/Documentation.html index f876dbe..c44921d 100644 --- a/Documentation.html +++ b/Documentation.html @@ -9,7 +9,7 @@ -

Table of contents

    +

    Current Multi Version: 13.0.0

    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.

    +

    Multi static variables

    multi.Version — The current version of the library
    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.

    @@ -268,7 +471,7 @@ multi:threadloop(settings) whiletruedo 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 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)
    func = multi:newFunction(FUNCTION func)
    trigger = multi:newTrigger(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])
    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

    functionend,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:

    Non-Actor: 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.

    Non-Actor: 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. returns or nil = conn:Fire(…) — Triggers the connection with arguments …, “returns” if non-nil is a table containing return values from the triggered connections. [Deprecated: Planned removal in 14.x.x]
    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 num #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:

    1,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:

    Non-Actor: 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(STRING 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:

    functionprint(multi:hasJobs())
     print("There are "..multi:getJobs().." jobs in the queue!")
     multi:mainloop()
    -

    Ranges

    Conditions

    +

    Non-Actor: Functions

    func = multi:newFunction(FUNCTION func)
    These objects used to have more of a function before corutine based threads came around, but the main purpose now is the ablity to have pausable function calls

    ... = func(...) — This is how you call your function. The first argument passed is itself when your function is triggered. See example.
    self = func:Pause()
    self = func:Resume()

    Note: A paused function will return: nil, true

    Example:

    local multi = require("multi")
    +printOnce = multi:newFunction(function(self,msg)
    +    print(msg)
    +    self:Pause()
    +    return "I won't work anymore"
    +end)
    +a=printOnce("Hello World!")
    +b,c=printOnce("Hello World!")
    +print(a,b,c)
    +

    Non-Actor: Triggers

    trigger = multi:newTrigger(FUNCTION: func(...)) — A trigger is the precursor of connection objects. The main difference is that only one function can be binded to the trigger.
    self = trigger:Fire(...) — Fires the function that was connected to the trigger and passes the arguments supplied in Fire to the function given.

    Universal Actor functions

    All of these functions are found on actors
    self = multiObj:Pause() — Pauses the actor from running
    self = multiObj:Resume() — Resumes the actor that was paused
    nil = multiObj:Destroy() — Removes the object from the mainloop
    bool = multiObj:isPaused() — Returns true if the object is paused, false otherwise
    string = multiObj:getType() — Returns the type of the object
    self = multiObj:SetTime(n) — Sets a timer, and creates a special “timemaster” actor, which will timeout unless ResolveTimer is called
    self = multiObj:ResolveTimer(...) — Stops the timer that was put onto the multiObj from timing out
    self = multiObj:OnTimedOut(func) — If ResolveTimer was not called in time this event will be triggered. The function connected to it get a refrence of the original object that the timer was created on as the first argument.
    self = multiObj:OnTimerResolved(func) — This event is triggered when the timer gets resolved. Same argument as above is passed, but the variable arguments that are accepted in resolvetimer are also passed as well.
    self = multiObj:Reset(n) — In the cases where it isn’t obvious what it does, it acts as Resume()
    self = multiObj:SetName(STRING name)

    Actor: Events

    event = multi:newEvent(FUNCTION task)
    The object that started it all. These are simply actors that wait for a condition to take place, then auto triggers an event. The event when triggered once isn’t triggered again unless you Reset() it.

    self = SetTask(FUNCTION func) — This function is not needed if you supplied task at construction time
    self = OnEvent(FUNCTION func) — Connects to the OnEvent event passes argument self to the connectee

    Example:

    local multi = require("multi")
    +count=0
    +-- A loop object is used to demostrate how one could use an event object.
    +loop=multi:newLoop(function(self,dt)
    +    count=count+1
    +end)
    +event=multi:newEvent(function() return count==100 end) -- set the event
    +event:OnEvent(function(self) -- connect to the event object
    +    loop:Destroy() -- destroys the loop from running!
    +    print("Stopped that loop!",count)
    +end) -- events like alarms need to be reset the Reset() command works here as well
    +multi:mainloop()
    +

    Actor: Updates

    updater = multi:newUpdater([NUMBER skip 1]) — set the amount of steps that are skipped
    Updaters are a mix between both loops and steps. They were a way to add basic priority management to loops (until a better way was added). Now they aren’t as useful, but if you do not want the performance hit of turning on priority then they are useful to auro skip some loops. Note: The performance hit due to priority management is not as bas as it used to be.

    self = updater:SetSkip(NUMBER n) — sets the amount of steps that are skipped
    self = OnUpdate(FUNCTION func) — connects to the main trigger of the updater which is called every nth step

    Example:

    local multi = require("multi")
    +updater=multi:newUpdater(5000) -- simple, think of a loop with the skip feature of a step
    +updater:OnUpdate(function(self)
    +    print("updating...")
    +end)
    +multi:mainloop()
    +

    Actor: Alarms

    alarm = multi:newAlarm([NUMBER 0]) — creates an alarm which waits n seconds
    Alarms ring after a certain amount of time, but you need to reset the alarm every time it rings! Use a TLoop if you do not want to have to reset.

    self = alarm:Reset([NUMBER sec current_time_set]) — Allows one to reset an alarm, optional argument to change the time until the next ring.
    self = alarm:OnRing(FUNCTION func — Allows one to connect to the alarm event which is triggerd after a certain amount of time has passed.

    Example:

    local multi = require("multi")
    +alarm=multi:newAlarm(3) -- in seconds can go to .001 uses the built in os.clock()
    +alarm:OnRing(function(a)
    +    print("3 Seconds have passed!")
    +    a:Reset(n) -- if n were nil it will reset back to 3, or it would reset to n seconds
    +end)
    +multi:mainloop()
    +

    Actor: Loops

    loop = multi:newLoop(FUNCTION func) — func the main connection that you can connect to. Is optional, but you can also use OnLoop(func) to connect as well.
    Loops are events that happen over and over until paused. They act like a while loop.

    self = OnLoop(FUNCTION func) — func the main connection that you can connect to. Alllows multiple connections to one loop if need be.

    Example:

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +local a = 0
    +loop = multi:newLoop(function()
    +    a = a + 1
    +    if a == 1000 then
    +        print("a = 1000")
    +        loop:Pause()
    +    end
    +end)
    +multi:mainloop()
    +

    Actor: TLoops

    tloop = multi:newTLoop(FUNCTION func ,NUMBER: [set 1]) — TLoops are pretty much the same as loops. The only difference is that they take set which is how long it waits, in seconds, before triggering function func.

    self = OnLoop(FUNCTION func) — func the main connection that you can connect to. Alllows multiple connections to one TLoop if need be.

    Example:

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +local a = 0
    +loop = multi:newTLoop(function()
    +    a = a + 1
    +    if a == 10 then
    +        print("a = 10")
    +        loop:Pause()
    +    end
    +end,1)
    +multi:mainloop()
    +

    Actor: Steps

    step = multi:newStep(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0]) — Steps were originally introduced to bs used as for loops that can run parallel with other code. When using steps think of it like this: for i=start,reset,count do When the skip argument is given, each time the step object is given cpu cycles it will be skipped by n cycles. So if skip is 1 every other cpu cycle will be alloted to the step object.

    self = step:OnStart(FUNCTION func(self)) — This connects a function to an event that is triggered everytime a step starts.
    self = step:OnStep(FUNCTION func(self,i)) — This connects a function to an event that is triggered every step or cycle that is alloted to the step object
    self = step:OnEnd(FUNCTION func(self)) — This connects a function to an event that is triggered when a step reaches its goal
    self = step:Update(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER skip 0]) — Update can be used to change the goals of the step. You should call step:Reset() after using Update to restart the step.

    Example:

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +multi:newStep(1,10,1,0):OnStep(function(step,pos)
    +    print(step,pos)
    +end):OnEnd(fucntion(step)
    +    step:Destroy()
    +end)
    +multi:mainloop()
    +

    Actor: TSteps

    tstep = multi:newStep(NUMBER start, NUMBER reset, [NUMBER count 1], [NUMBER set 1]) — TSteps work just like steps, the only difference is that instead of skip, we have set which is how long in seconds it should wait before triggering the OnStep() event.

    self = tstep:OnStart(FUNCTION func(self)) — This connects a function to an event that is triggered everytime a step starts.
    self = tstep:OnStep(FUNCTION func(self,i)) — This connects a function to an event that is triggered every step or cycle that is alloted to the step object
    self = tstep:OnEnd(FUNCTION func(self)) — This connects a function to an event that is triggered when a step reaches its goal
    self = tstep:Update(NUMBER start,*NUMBER reset, [NUMBER count 1], [NUMBER set 1]) — Update can be used to change the goals of the step. You should call step:Reset() after using Update to restart the step.
    self = tstep:Reset([NUMBER n set]) — Allows you to reset a tstep that has ended, but also can change the time between each trigger.

    Example:

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +multi:newTStep(1,10,1,1):OnStep(function(step,pos)
    +    print(step,pos)
    +end):OnEnd(fucntion(step)
    +    step:Destroy()
    +end)
    +multi:mainloop()
    +

    Actor: Time Stampers

    stamper = multi:newTimeStamper() — This allows for long time spans as well as short time spans.
    stamper = stamper:OhSecond(NUMBER second, FUNCTION func) — This takes a value between 0 and 59. This event is called once every second! Not once every second! If you want seconds then use alarms*! 0 is the start of every minute and 59 is the end of every minute.
    stamper = stamper:OhMinute(NUMBER minute, FUNCTION func) — This takes a value between 0 and 59. This event is called once every hour*! Same concept as OnSecond()
    stamper = stamper:OhHour(NUMBER hour, FUNCTION func) — This takes a value between 0 and 23. This event is called once every day*! 0 is midnight and 23 is 11pm if you use 12 hour based time.
    stamper = stamper:OnDay(STRING/NUMBER day, FUNCTION func) — So the days work like this ‘Sun’, ‘Mon’, ‘Tue’, ‘Wed’, ‘Thu’, ‘Fri’, ‘Sat’. When in string form this is called every week. When in number form this is called every month*!
    There is a gotcha though with this. Months can have 28,29,30, and 31 days to it, which means that something needs to be done when dealing with the last few days of a month. I am aware of this issue and am looking into a solution that is simple and readable. I thought about allowing negitive numbers to allow one to eaisly use the last day of a month. -1 is the last day of the month where -2 is the second to last day of the month. You can go as low as -28 if you want, but this provides a nice way to do something near the end of the month that is lua like.
    stamper = stamper:OnMonth(NUMBER month,FUNCTION func) — This takes a value between 1 and 12. 1 being January and 12 being December. Called once per year*.
    stamper = stamper:OnYear(NUMBER year,FUNCTION func) — This takes a number yy. for example 18 do not use yyyy format! Odds are you will not see this method triggered more than once, unless science figures out the whole life extension thing. But every century this event is triggered*! I am going to be honest though, the odds of a system never reseting for 100 years is very unlikely, so if I used 18 (every 18th year in each century every time i load my program this event will be triggered). Does it actually work? I have no idea tbh it should, but can i prove that without actually testing it? Yes by using fake data thats how.
    stamper = stamper:OnTime(NUMBER hour,NUMBER minute,NUMBER second,FUNCTION func) — This takes in a time to trigger, hour, minute, second. This triggeres once a day at a certain time! Sort of like setting an alarm! You can combine events to get other effects like this!
    stamper = stamper:OnTime(STRING time,FUNCTION func) — This takes a string time that should be formatted like this: “hh:mm:ss” hours minutes and seconds must be given as parameters! Otherwise functions as above!

    *If your program crashes or is rebooted than the data in RAM letting the code know that the function was already called will be reset! This means that if an event set to be triggered on Monday then you reboot the code it will retrigger that event on the same day if the code restarts. In a future update I am planning of writing to the disk for OnHour/Day/Week/Year events. This will be an option that can be set on the object.

    Examples:
    OnSecond

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +local a = 0
    +ts:OnSecond(0,function()
    +    a=a+1
    +    print("New Minute: "..a.." <"..os.date("%M")..">")
    +end)
    +multi:mainloop()
    +

    OnMinute

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +local a = 0
    +ts:OnSecond(0,function()
    +    a=a+1
    +    print("New Hour: "..a.." <"..os.date("%I")..">")
    +end)
    +multi:mainloop()
    +

    OnHour

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +ts:OnHour(0,function()
    +    print("New Day")
    +end)
    +multi:mainloop()
    +

    OnDay

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +ts:OnDay("Thu",function()
    +    print("It's thursday!")
    +end)
    +multi:mainloop()
    +
    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +ts:OnDay(2,function()
    +    print("Second day of the month!")
    +end)
    +multi:mainloop()
    +
    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +ts:OnDay(-1,function()
    +    print("Last day of the month!")
    +end)
    +multi:mainloop()
    +

    OnYear

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +ts:OnYear(19,function() -- They gonna wonder if they run this in 2018 why it no work :P
    +    print("We did it!")
    +end)
    +multi:mainloop()
    +

    OnTime

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +ts:OnTime(12,1,0,function()
    +    print("Whooooo")
    +end)
    +multi:mainloop()
    +
    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +ts = multi:newTimeStamper()
    +ts:OnTime("12:04:00",function()
    +    print("Whooooo")
    +end)
    +multi:mainloop()
    +

    Actor: Watchers

    Deprecated: This object was removed due to its uselessness. Metatables will work much better for what is being done. Perhaps in the future i will remake this method to use metamethods instead of basic watching every step. This will most likely be removed in the next version of the library or changed to use metatables and metamethods.
    watcher = multi:newWatcher(STRING name) — Watches a variable on the global namespace
    watcher = multi:newWatcher(TABLE namespace, STRING name) — Watches a variable inside of a table
    watcher = watcher::OnValueChanged(Function func(self, old_value, current_value))

    Example

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +test = {a=0}
    +watcher = multi:newWatcher(test,"a")
    +watcher:OnValueChanged(function(self, old_value, current_value)
    +    print(old_value,current_value)
    +end)
    +multi:newTLoop(function()
    +    test.a=test.a + 1
    +end,.5)
    +multi:mainloop()
    +

    Actor: Custom Object

    cobj = multi:newCustomObject(TABLE objRef, BOOLEAN isActor [false]) — Allows you to create your own multiobject that runs each allotted step. This allows you to create your own object that works with all the features that each built in multi object does. If isActor is set to true you must have an Act method in your table. See example below. If an object is not an actor than the Act method will not be automatically called for you.

    Example:

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +local work = false
    +ticktock = multi:newCustomObject({
    +    timer = multi:newTimer(),
    +    Act = function(self)
    +        if self.timer:Get()>=1 then
    +            work = not work
    +            if work then
    +                self.OnTick:Fire()
    +            else
    +                self.OnTock:Fire()
    +            end
    +            self.timer:Reset()
    +        end
    +    end,
    +    OnTick = multi:newConnection(),
    +    OnTock = multi:newConnection(),
    +},true)
    +ticktock.OnTick(function()
    +    print("Tick")
    +end)
    +ticktock.OnTock(function()
    +    print("Tock")
    +end)
    +multi:mainloop()
    +

    Coroutine based Threading (CBT)

    This was made due to the limitations of multiObj:hold(), which no longer exists. When this library was in its infancy and before I knew about coroutines, I actually tried to emulate what coroutines did in pure lua.
    The threaded bariants of the non threaded objects do exist, but there isn’t too much of a need to use them.

    The main benefits of using the coroutine based threads is the thread.* namespace which gives you the ability to easily run code side by side.

    A quick note on how threads are managed in the library. The library contains a scheduler which keeps track of coroutines and manages them. Coroutines take some time then give off processing to another coroutine. Which means there are some methods that you need to use in order to hand off cpu time to other coroutines or the main thread. You must hand off cpu time when inside of a non ending loop or your code will hang. Threads also have a slight delay before starting, about 3 seconds.

    threads.*

    thread.sleep(NUMBER n) — Holds execution of the thread until a certain amount of time has passed
    thread.hold(FUNCTION func) — Hold execttion until the function returns true
    thread.skip(NUMBER n) — How many cycles should be skipped until I execute again
    thread.kill() — Kills the thread
    thread.yeild() — Is the same as using thread.skip(0) or thread.sleep(0), hands off control until the next cycle
    thread.isThread() — Returns true if the current running code is inside of a coroutine based thread
    thread.getCores() — Returns the number of cores that the current system has. (used for system threads)
    thread.set(STRING name, VARIABLE val) — A global interface where threads can talk with eachother. sets a variable with name and its value
    thread.get(STRING name) — Gets the data stored in name
    thread.waitFor(STRING name) — Holds executon of a thread until variable name exists
    thread.testFor(STRING name,VARIABLE val,STRING sym) — holds execution untile variable name exists and is compared to val
    sym can be equal to: “=”, “==”, “<”, “>”, “<=”, or “>=” the way comparisan works is: “return val sym valTested

    CBT: Thread

    multi:newThread(STRING name,FUNCTION func) — Creates a new thread with name and function.
    Note: newThread() returns nothing. Threads are opperated hands off everything that happens, does so inside of its functions.

    Threads simplify many things that you would use non CBT objects for. I almost solely use CBT for my current programming. I will slso show the above custom object using threads instead. Yes its cool and can be done.

    Examples:

    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +multi:newThread("Example of basic usage",function()
    +    while true do
    +        thread.sleep(1)
    +        print("We just made an alarm!")
    +    end
    +end)
    +multi:mainloop()
    +
    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +
    +function multi:newTickTock()
    +    local work = false
    +    local _alive = true
    +    local OnTick = multi:newConnection()
    +    local OnTock = multi:newConnection()
    +    local c =multi:newCustomObject{
    +        OnTick = OnTick,
    +        OnTock = OnTock,
    +        Destroy = function()
    +            _alive = false -- Threads at least how they work here now need a bit of data management for cleaning up objects. When a thread either finishes its execution of thread.kill() is called everything is removed from the scheduler letting lua know that it can garbage collect
    +        end
    +    }
    +    multi:newThread("TickTocker",function()
    +        while _alive do
    +            thread.sleep(1)
    +            work = not work
    +            if work then
    +                OnTick:Fire()
    +            else
    +                OnTock:Fire()
    +            end
    +        end
    +        thread.kill() -- When a thread gets to the end of it's ececution it will automatically be ended, but having this method is good to show what is going on with your code.
    +    end)
    +    return c
    +end
    +ticktock = multi:newTickTock()
    +ticktock.OnTick(function()
    +    print("Tick")
    +    -- The thread.* namespace works in all events that
    +end)
    +ticktock.OnTock(function()
    +    print("Tock")
    +end)
    +multi:mainloop()
    +
    package.path="?/init.lua;?.lua;"..package.path
    +local multi = require("multi")
    +
    +multi:newThread("TickTocker",function()
    +    print("Waiting for variable a to exist...")
    +    ret,ret2 = thread.hold(function()
    +        return a~=nil, "test!"
    +    end)
    +    print(ret,ret2) -- The hold method returns the arguments when the first argument is true. This methods return feature is rather new and took more work then you think to get working. Since threads
    +end)
    +multi:newAlarm(3):OnRing(function() a = true end) -- allows a to exist
    +
    +multi:mainloop()
    +

    CBT: Threaded Process

    process = multi:newThreadedProcess(STRING name) — Creates a process object that is able allows all processes created on it to use the thread.* namespace

    nil = process:getController() — Returns nothing there is no “controller” when using threaded processes
    self = process:Start() — Starts the processor
    self = process:Pause() — Pauses the processor
    self = process:Resume() — Resumes a paused processor
    self = process:Kill() — Kills/Destroys the process thread
    self = process:Remove() — Destroys/Kills the processor and all of the Actors running on it
    self = process:Sleep(NUMBER n) — Forces a process to sleep for n amount of time
    self = process:Hold(FUNCTION/NUMBER n) — Forces a process to either test a condition or sleep.

    Everything eles works as if you were using the multi. interface. You can create multi objects on the process and the objects are able to use the thread. interface.

    Note: When using Hold/Sleep/Skip on an object created inside of a threaded process, you actually hold the entire process! Which means all objects on that process will be stopping until the conditions are met!

    Example:

    test = multi:newThreadedProcess("test")
    +test:newLoop(function()
    +    print("HI!")
    +end)
    +test:newLoop(function()
    +    print("HI2!")
    +    thread.sleep(.5)
    +end)
    +multi:newAlarm(3):OnRing(function()
    +    test:Sleep(10)
    +end)
    +test:Start()
    +multi:mainloop()
    +

    CBT: Hyper Threaded Process

    process = multi:newHyperThreadedProcess(STRING name) — Creates a process object that is able allows all processes created on it to use the thread.* namespace. Hold/Sleep/Skip can be used in each multi obj created without stopping each other object that is running, but allows for one to pause/halt a process and stop all objects running in that process.

    nil = process:getController() — Returns nothing there is no “controller” when using threaded processes
    self = process:Start() — Starts the processor
    self = process:Pause() — Pauses the processor
    self = process:Resume() — Resumes a paused processor
    self = process:Kill() — Kills/Destroys the process thread
    self = process:Remove() — Destroys/Kills the processor and all of the Actors running on it
    self = process:Sleep(NUMBER n) — Forces a process to sleep for n amount of time
    self = process:Hold(FUNCTION/NUMBER n) — Forces a process to either test a condition or sleep.

    Example:

    test = multi:newHyperThreadedProcess("test")
    +test:newLoop(function()
    +    print("HI!")
    +end)
    +test:newLoop(function()
    +    print("HI2!")
    +    thread.sleep(.5)
    +end)
    +multi:newAlarm(3):OnRing(function()
    +    test:Sleep(10)
    +end)
    +test:Start()
    +multi:mainloop()
    +

    Same example as above, but notice how this works opposed to the non hyper version

    System Threads (ST) - Multi-Integration Getting Started

    The system threads need to be required seperatly.

    local GLOBAL, THREAD = require("multi.integration.lanesManager").init()# -- We will talk about the global and thread interface that is returned
    +GLOBAL, THREAD = require("multi.integration.loveManager").init()
    +GLOBAL, THREAD = require("luvitManager")-- There is a catch to this*
    +

    Using this integration modifies some methods that the multi library has.
    multi:canSystemThread() — Returns true is system threading is possible
    multi:getPlatform() — Returns (for now) either “lanes”, “love2d” and “luvit”
    This variable is created on the main thread only inside of the multi namespace: multi.isMainThread = true
    This is used to know which thread is the main thread. When network threads are being discussed there is a gotcha that needs to be addressed.

    * GLOBAL and THREAD do not work currently when using the luvit integration
    #So you may have noticed that when using the lanes manager you need to make the global and thread local, this is due to how lanes copies local variables between states. Also love2d does not require this, actually things will break if this is done! Keep these non local since the way threading is handled at the lower level is much different anyway so GLOBAL and THREAD is automatically set up for use within a spawned thread!

    ST - THREAD namespace

    THREAD.set(STRING name, VALUE val) — Sets a value in GLOBAL
    THREAD.get(STRING name) — Gets a value in GLOBAL
    THREAD.waitFor(STRING name) — Waits for a value in GLOBAL to exist
    THREAD.testFor(STRING name, VALUE val, STRING sym)NOT YET IMPLEMENTED but planned
    THREAD.getCores() — Returns the number of actual system threads/cores
    THREAD.kill() — Kills the thread
    THREAD.getName() — Returns the name of the working thread
    THREAD.sleep(NUMBER n) — Sleeps for an amount of time stopping the current thread
    THREAD.hold(FUNCTION func) — Holds the current thread until a condition is met
    THREAD.getID() — returns a unique ID for the current thread. This varaiable is visible to the main thread as well by accessing it through the returned thread object. OBJ.Id

    ST - GLOBAL namespace

    Treat global like a table.

    GLOBAL["name"] = "Ryan"
    +print(GLOBAL["name"])
    +

    Removes the need to use THREAD.set() and THREAD.get()

    ST - System Threads

    systemThread = multi:newSystemThread(STRING thread_name,FUNCTION spawned_function,ARGUMENTS ...) — Spawns a thread with a certain name.
    systemThread:kill() — kills a thread; can only be called in the main thread!
    systemThread.OnError(FUNCTION(systemthread,errMsg,errorMsgWithThreadName))

    System Threads are the feature that allows a user to interact with systen threads. It differs from regular coroutine based thread in how it can interact with variables. When using system threads the GLOBAL table is the “only way”* to send data. Spawning a System thread is really simple once all the required libraries are in place. See example below:

    local multi = require("multi") -- keep this global when using lanes or implicitly define multi within the spawned thread
    +local GLOBAL, THREAD = require("multi.integration.lanesManager").init()
    +multi:newSystemThread("Example thread",function()
    +    local multi = require("multi") -- we are in a thread so lets not refer to that upvalue!
    +    print("We have spawned a thread!")
    +    -- we could do work but theres no need to we can save that for other examples
    +    print("Lets have a non ending loop!")
    +    while true do
    +        -- If this was not in a thread execution would halt for the entire process
    +    end
    +end,"A message that we are passing") -- There are restrictions on what can be passed!
    +
    +tloop = multi:newTLoop(function()
    +    print("I'm still kicking!")
    +end,1)
    +multi:mainloop()
    +

    *This isn’t entirely true, as of right now the compatiablity with the lanes library and love2d engine have their own methods to share data, but if you would like to have your code work in both enviroments then using the GLOBAL table and the data structures provided by the multi library will ensure this happens. If you do not plan on having support for both platforms then feel free to use linda’s in lanes and channels in love2d.

    Note: luvit currently has very basic support, it only allows the spawning of system threads, but no way to send data back and forth as of yet. I do not know if this is doable or not, but I will keep looking into it. If I can somehow emulate System Threaded Queues and the GLOBAL tabke then all other datastructures will work!

    ST - System Threaded Objects

    Great we are able to spawn threads, but unless your working with a process that works on passed data and then uses a socket or writes to the disk I can’t do to much with out being able to pass data between threads. This section we will look at how we can share objects between threads. In order to keep the compatibility between both love2d and lanes I had to format the system threaded objects in a strange way, but they are consistant and should work on both enviroments.

    When creating objects with a name they are automatically exposed to the GLOBAL table. Which means you can retrieve them from a spawned thread. For example we have a queue object, which will be discussed in more detail next.

    -- Exposing a queue
    +multi = require("multi")
    +local GLOBAL, THREAD = require("multi.integration.lanesManager").init() -- The standard setup above
    +queue = multi:newSystemThreadedQueue("myQueue"):init() -- We create and initiate the queue for the main thread
    +queue:push("This is a test!") -- We push some data onto the queue that other threads can consume and do stuff with
    +multi:newSystemThread("Example thread",function() -- Create a system thread
    +    queue = THREAD.waitFor("myQueue"):init() -- Get the queue. It is good pratice to use the waitFor command when getting objects. If it doesn't exist yet we wait for it, preventing future errors. It is possible for the data to not ve present when a thread is looking for it! Especally when using the love2d module, my fault needs some rewriting data passing on the GLOBAL is quite slow, but the queue internally uses channels so after it is exposed you should have good speeds!
    +    local data = queue:pop() -- Get the data
    +    print(data) -- print the data
    +end)
    +multi:mainloop()
    +

    ST - SystemThreadedQueue

    queue(nonInit) = multi:newSystemThreadedQueue(STRING name) — You must enter a name!
    queue = queue:init() — initiates the queue, without doing this it will not work
    void = queue:push(DATA data) — Pushes data into a queue that all threads that have been shared have access to
    data = queue:pop() — pops data from the queue removing it from all threads
    data = queue:peek() — looks at data that is on the queue, but dont remove it from the queue

    This object the System Threaded Queue is the basis for all other data structures that a user has access to within the “shared” objects.

    General tips when using a queue. You can always pop from a queue without worrying if another thread poped that same data, BUT if you are peeking at a queue there is the possibility that another thread popped the data while you are peeking and this could cause an issue, depends on what you are doing though. It’s important to keep this in mind when using queues.

    Let’s get into some examples:

    multi = require("multi")
    +thread_names = {"Thread_A","Thread_B","Thread_C","Thread_D"}
    +local GLOBAL, THREAD = require("multi.integration.lanesManager").init()
    +queue = multi:newSystemThreadedQueue("myQueue"):init()
    +for _,n in pairs(thread_names) do
    +    multi:newSystemThread(n,function()
    +        queue = THREAD.waitFor("myQueue"):init()
    +        local name = THREAD.getName()
    +        local data = queue:pop()
    +        while data do
    +            print(name.." "..data)
    +            data = queue:pop()
    +        end
    +    end)
    +end
    +for i=1,100 do
    +    queue:push(math.random(1,1000))
    +end
    +multi:newEvent(function() -- Felt like using the event object, I hardly use them for anything non internal
    +    return not queue:peek()
    +end):OnEvent(function()
    +    print("No more data within the queue!")
    +    os.exit()
    +end)
    +multi:mainloop()
    +

    You have probable noticed that the output from this is a total mess! Well I though so too, and created the system threaded console!

    ST - SystemThreadedConsole

    console(nonInit) = multi:newSystemThreadedConsole(STRING name) — Creates a console object called name. The name is mandatory!
    concole = console:inti() — initiates the console object
    console:print(...) — prints to the console
    console:write(msg) — writes to the console, to be fair you wouldn’t want to use this one.

    The console makes printing from threads much cleaner. We will use the same example from above with the console implemented and compare the outputs and how readable they now are!

    multi = require("multi")
    +thread_names = {"Thread_A","Thread_B","Thread_C","Thread_D"}
    +local GLOBAL, THREAD = require("multi.integration.lanesManager").init()
    +multi:newSystemThreadedConsole("console"):init()
    +queue = multi:newSystemThreadedQueue("myQueue"):init()
    +for _,n in pairs(thread_names) do
    +    multi:newSystemThread(n,function()
    +        local queue = THREAD.waitFor("myQueue"):init()
    +        local console = THREAD.waitFor("console"):init()
    +        local name = THREAD.getName()
    +        local data = queue:pop()
    +        while data do
    +            --THREAD.sleep(.1) -- uncomment this to see them all work
    +            console:print(name.." "..data)
    +            data = queue:pop()
    +        end
    +    end)
    +end
    +for i=1,100 do
    +    queue:push(math.random(1,1000))
    +end
    +multi:newEvent(function()
    +    return not queue:peek()
    +end):OnEvent(function()
    +    multi:newAlarm(.1):OnRing(function() -- Well the mainthread has to read from an internal queue so we have to wait a sec
    +        print("No more data within the queue!")
    +        os.exit()
    +    end)
    +end)
    +multi:mainloop()
    +

    As you see the output here is so much cleaner, but we have a small gotcha, you probably noticed that I used an alarm to delay the exiting of the program for a bit. This is due to how the console object works, I send all the print data into a queue that the main thread then reads and prints out when it looks at the queue. This should not be an issue since you gain so much by having clean outputs!

    Another thing to note, because system threads are put to work one thread at a time, really quick though, the first thread that is loaded is able to complete the tasks really fast, its just printing after all. If you want to see all the threads working uncomment the code with THREAD.sleep(.1)

    ST - SystemThreadedJobQueue

    ST - SystemThreadedConnection - WIP*

    connection(nonInit) = multi:newSystemThreadedConnection(name,protect) — creates a connecion object
    connection = connection:init() — initaties the connection object
    connectionID = connection:connect(FUNCTION func) — works like the regular connect function
    void = connection:holdUT(NUMBER/FUNCTION n) — works just like the regular holdut function
    void = connection:Remove() — works the same as the default
    voic = connection:Fire(ARGS ...) — works the same as the default

    In the current form a connection object requires that the multi:mainloop() is running on the threads that are sharing this object! By extention since SystemThreadedTables rely on SystemThreadedConnections they have the same requirements. Both objects should not be used for now.

    Since the current object is not in a stable condition, I will not be providing examples of how to use it just yet!

    *The main issue we have with the connection objects in this form is proper comunication and memory managament between threads. For example if a thread crashes or no longer exists the current apporach to how I manage the connection objects will cause all connections to halt. This feature is still being worked on and has many bugs to be patched out. for now only use for testing purposes.

    ST - SystemThreadedTable - WIP*

    ST - SystemThreadedBenchmark

    bench = multi:SystemThreadedBenchmark(NUMBER seconds) — runs a benchmark for a certain amount of time
    bench:OnBench(FUNCTION callback(NUMBER steps/second))

    multi = require("multi")
    +local GLOBAL, THREAD = require("multi.integration.lanesManager").init()
    +multi:SystemThreadedBenchmark(1).OnBench(function(...)
    +    print(...)
    +end)
    +multi:mainloop()
    +

    ST - SystemThreadedExecute WIP* Might remove

    Network Threads - Multi-Integration

    diff --git a/changes.html b/changes.html index f1ea71b..4c957a4 100644 --- a/changes.html +++ b/changes.html @@ -12,209 +12,315 @@

    Changes

    -

    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.
    Thats all for this update

    Update 12.2.1 Time for some bug fixes!

    Fixed: SystemThreadedJobQueues

      +

      Update 13.0.0 Added some documentation, and some new features too check it out!

      Quick note on the 13.0.0 update:
      This update I went all in finding bugs and improving proformance within the library. I added some new features and the new task manager, which I used as a way to debug the library was a great help, so much so thats it is now a permanent feature. It’s been about half a year since my last update, but so much work needed to be done. I hope you can find a use in your code to use my library. I am extremely proud of my work; 7 years of development, I learned so much about lua and programming through the creation of this library. It was fun, but there will always be more to add and bugs crawling there way in. I can’t wait to see where this library goes in the future!

      Fixed: Tons of bugs, I actually went through the entire library and did a full test of everything, I mean everything, while writing the documentation.
      Changed:

        +
      • A few things, to make concepts in the library more clear.
      • The way functions returned paused status. Before it would return “PAUSED” now it returns nil, true if paused
      • Modified the connection object to allow for some more syntaxial suger!
      • System threads now trigger an OnError connection that is a member of the object itself. multi.OnError() is no longer triggered for a system thread that crashes!

      Connection Example:

      loop = multi:newTLoop(function(self)
      +    self:OnLoops() -- new way to Fire a connection! Only works when used on a multi object, bin objects, or any object that contains a Type variable
      +end,1)
      +loop.OnLoops = multi:newConnection()
      +loop.OnLoops(function()
      +    print("Looping")
      +end)
      +multi:mainloop()
      +

      Function Example:

      func = multi:newFunction(function(self,a,b)
      +    self:Pause()
      +    return 1,2,3
      +end)
      +print(func()) -- returns: 1, 2, 3
      +print(func()) -- nil, true
      +

      Removed:

        +
      • Ranges and conditions — corutine based threads can emulate what these objects did and much better!
      • Due to the creation of hyper threaded processes the following objects are no more!
        multi:newThreadedEvent()
        multi:newThreadedLoop()
        multi:newThreadedTLoop()
        multi:newThreadedStep()
        multi:newThreadedTStep()
        multi:newThreadedAlarm()
        multi:newThreadedUpdater()
        multi:newTBase() — Acted as the base for creating the other objects

      These didn’t have much use in their previous form, but with the addition of hyper threaded processes the goals that these objects aimed to solve are now possible using a process

      Fixed:

        +
      • There were some bugs in the networkmanager.lua file. Desrtoy -> Destroy some misspellings.
      • Massive object management bugs which caused performance to drop like a rock.
      • Found a bug with processors not having the Destroy() function implemented properly.
      • Found an issue with the rockspec which is due to the networkManager additon. The net Library and the multi Library are now codependent if using that feature. Going forward you will have to now install the network library separately
      • Insane proformance bug found in the networkManager file, where each connection to a node created a new thread (VERY BAD) If say you connected to 100s of threads, you would lose a lot of processing power due to a bad implementation of this feature. But it goes futhur than this, the net library also creates a new thread for each connection made, so times that initial 100 by about 3, you end up with a system that quickly eats itself. I have to do tons of rewriting of everything. Yet another setback for the 13.0.0 release (Im releasing 13.0.0 though this hasn’t been ironed out just yet)
      • Fixed an issue where any argument greater than 256^2 or 65536 bytes is sent the networkmanager would soft crash. This was fixed by increading the limit to 256^4 or 4294967296. The fix was changing a 2 to a 4. Arguments greater than 256^4 would be impossible in 32 bit lua, and highly unlikely even in lua 64 bit. Perhaps someone is reading an entire file into ram and then sending the entire file that they read over a socket for some reason all at once!?
      • Fixed an issue with processors not properly destroying objects within them and not being destroyable themselves
      • Fixed a bug where pause and resume would duplicate objects! Not good
      • Noticed that the switching of lua states, corutine based threading, is slower than multi-objs (Not by much though).
      • multi:newSystemThreadedConnection(name,protect) — I did it! It works and I believe all the gotchas are fixed as well.
        — Issue one, if a thread died that was connected to that connection all connections would stop since the queue would get clogged! FIXED
        — There is one thing, the connection does have some handshakes that need to be done before it functions as normal!

      Added:

        +
      • Documentation, the purpose of 13.0.0, orginally going to be 12.2.3, but due to the amount of bugs and features added it couldn’t be a simple bug fix update.
      • multi:newHyperThreadedProcess(STRING name) — This is a version of the threaded process that gives each object created its own coroutine based thread which means you can use thread.* without affecting other objects created within the hyper threaded processes. Though, creating a self contained single thread is a better idea which when I eventually create the wiki page I’ll discuss
      • multi:newConnector() — A simple object that allows you to use the new connection Fire syntax without using a multi obj or the standard object format that I follow.
      • multi:purge() — Removes all references to objects that are contained withing the processes list of tasks to do. Doing this will stop all objects from functioning. Calling Resume on an object should make it work again.
      • multi:getTasksDetails(STRING format) — Simple function, will get massive updates in the future, as of right now It will print out the current processes that are running; listing their type, uptime, and priority. More useful additions will be added in due time. Format can be either a string “s” or “t” see below for the table format
      • multi:endTask(TID) — Use multi:getTasksDetails(“t”) to get the tid of a task
      • multi:enableLoadDetection() — Reworked how load detection works. It gives better values now, but it still needs some work before I am happy with it
      • THREAD.getID() — returns a unique ID for the current thread. This varaiable is visible to the main thread as well by accessing it through the returned thread object. OBJ.Id Do not confuse this with thread.* this refers to the system threading interface. Each thread, including the main thread has a threadID the main thread has an ID of 0!
      • multi.print(…) works like normal print, but only prints if the setting print is set to true
      • setting: print enables multi.print() to work
      • STC: IgnoreSelf defaults to false, if true a Fire command will not be sent to the self
      • STC: OnConnectionAdded(function(connID)) — Is fired when a connection is added you can use STC:FireTo(id,…) to trigger a specific connection. Works like the named non threaded connections, only the id’s are genereated for you.
      • STC: FireTo(id,…) — Described above.
      package.path="?/init.lua;?.lua;"..package.path
      +local multi = require("multi")
      +conn = multi:newConnector()
      +conn.OnTest = multi:newConnection()
      +conn.OnTest(function()
      +    print("Yes!")
      +end)
      +test = multi:newHyperThreadedProcess("test")
      +test:newTLoop(function()
      +    print("HI!")
      +    conn:OnTest()
      +end,1)
      +test:newLoop(function()
      +    print("HEY!")
      +    thread.sleep(.5)
      +end)
      +multi:newAlarm(3):OnRing(function()
      +    test:Sleep(10)
      +end)
      +test:Start()
      +multi:mainloop()
      +

      Table format for getTasksDetails(STRING format)

      {
      +    {TID = 1,Type="",Priority="",Uptime=0}
      +    {TID = 2,Type="",Priority="",Uptime=0}
      +    ...
      +    {TID = n,Type="",Priority="",Uptime=0}
      +    ThreadCount = 0
      +    threads={
      +        [Thread_Name]={
      +            Uptime = 0
      +        }
      +    }
      +}
      +

      Note: After adding the getTasksDetails() function I noticed many areas where threads, and tasks were not being cleaned up and fixed the leaks. I also found out that a lot of tasks were starting by default and made them enable only. If you compare the benchmark from this version to last version you;ll notice a signifacant increase in performance.

      Going forward:

        +
      • Work on system threaded functions
      • work on the node manager
      • patch up bugs
      • finish documentstion

      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.
      Thats all for this update

      Update 12.2.1 Time for some bug fixes!

      Fixed: SystemThreadedJobQueues

      • You can now make as many job queues as you want! Just a warning when using a large amount of cores for the queue it takes a second or 2 to set up the jobqueues for data transfer. I am unsure if this is a lanes thing or not, but love2d has no such delay when setting up the jobqueue!
      • You now connect to the OnReady in the jobqueue object. No more holding everything else as you wait for a job queue to be ready
      • Jobqueues:doToAll now passes the queues multi interface as the first and currently only argument
      • No longer need to use jobqueue.OnReady() The code is smarter and will send the pushed jobs automatically when the threads are ready

      Fixed: SystemThreadedConnection

      • They work the exact same way as before, but actually work as expected now. The issue before was how i implemented it. Now each connection knows the number of instances of that object that ecist. This way I no longer have to do fancy timings that may or may not work. I can send exactly enough info for each connection to consume from the queue.

      Removed: multi:newQueuer

        -
      • This feature has no real use after corutine based threads were introduced. You can use those to get the same effect as the queuer and do it better too.

      Going forward:

        +
      • This feature has no real use after corutine based threads were introduced. You can use those to get the same effect as the queuer and do it better too.

      Going forwardGoing forward:

      • Will I ever finish steralization? Who knows, but being able to save state would be nice. The main issue is there is no simple way to save state. While I can provide methods to allow one to turn the objects into strings and back, there is no way for me to make your code work with it in a simple way. For now only the basic functions will be here.
      • I need to make better documentation for this library as well. In its current state, all I have are examples and not a list of what is what.

      Example

    Setting