diff --git a/README.md b/README.md index 96179e6..c260577 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,4 @@ -# multi Version: 1.7.6 (Examples and more module creation support, small tweaks to cut down the amount of code needed to get things to work) -View Changes: https://github.com/rayaman/multi#changes +# multi Version: 1.8.0 (How much thread could a thread thread if a thread could thread thread?) **Note: The changes section has information on how to use the new features as they come out. Why put the infomation twice on the readme?**
My multitasking library for lua
@@ -11,24 +10,35 @@ If you find any bugs or have any issues please let me know :) ~~Also I will eventually add an example folder with a lot of examples for how you can use this library. Don't konw when that will be though :P~~ Added! -# Discord + +[TOC] + +Discord +------- For real-time assistance with my libraries! A place where you can ask questions and get help with any of my libraries
https://discord.gg/U8UspuA
-# Planned features/TODO -- [x] Add system threads for love2d that works like the lanesManager (loveManager, slight differences). -- [ ] Improve performance of the library -- [ ] Add more features to support module creators -- [ ] Make a framework for eaiser thread task distributing -- [ ] Fix Error handling on multi objects -- [ ] Add Remote Proxies **May or may not be completed** +Planned features/TODO +--------------------- +- [x] ~~Add system threads for love2d that works like the lanesManager (loveManager, slight differences).~~ +- [ ] Improve performance of the library -- It has increased a bit, but I feel I can get a little more out of it +- [x] ~~Add more features to support module creators~~ +- [x] ~~Make a framework for eaiser thread task distributing~~ +- [x] ~~Fix Error handling on threaded multi objects~~ Non threaded multiobjs will crash your program if they error though! Use multi:newThread() of multi:newSystemThread() if your code can error! Unless you use multi:protect() this however lowers performance! +- [x] ~~Add multi:OnError(function(obj,err))~~ - [ ] sThread.wrapper(obj) **May or may not be completed** -- [ ] SystemThreaded Actors +- [ ] SystemThreaded Actors -- After some tests i figured out a way to make this work... It will work slightly different though. This is due to the actor needing to be splittable... - [ ] LoadBalancing for system threads (Once SystemThreaded Actors are done) - [ ] Add more intergrations -- [ ] Finish the wiki stuff. (11% done)
-- [ ] Test for unknown bugs
+- [ ] Finish the wiki stuff. (11% done) +- [ ] Test for unknown bugs + +Known Bugs/Issues +----------------- +In regards to intergrations, thread cancellation works slightly different for love2d and lanes. Within love2d I was unable to (To lazy to...) not use the multi library within the thread. A fix for this is to call `multi:Stop()` when you are done with your threaded code! This may change however if I find a way to work around this. In love2d in order to mimic the GLOBAL table I needed the library to constantly sync tha data... You can use the sThread.waitFor(varname), or sThread.hold(func) methods to sync the globals, to get the value instead of using GLOBAL and this could work. If you want to go this route I suggest setting multi.isRunning=true to prevent the auto runner from doing its thing! This will make the multi manager no longer function, but thats the point :P + 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 @@ -72,7 +82,8 @@ We already showed alarms in action so lets move on to a Loop object Throughout these examples I am going to do some strange things in order to show other features of the library! -# LOOPS +LOOPS +----- ```lua -- Loops: Have been moved to the core of the library require("multi") would work as well require("multi") -- gets the entire library @@ -110,7 +121,8 @@ With loops out of the way lets go down the line This library aims to be Async like. In reality everything is still on one thread *unless you are using the lanes intergration module WIP* (More on that later) -# EVENTS +EVENTS +------ ```lua -- Events, these were the first objects introduced into the library. I seldomly use them in their pure form though, but later on you'll see their advance uses! -- Events on there own don't really do much... We are going to need 2 objects at least to get something going @@ -130,7 +142,8 @@ multi:mainloop() # Output Stopped that loop! -# STEPS +STEPS +----- ```lua require("multi") -- Steps, are like for loops but non blocking... You can run a loop to infintity and everything will still run I will combine Steps with Ranges in this example. @@ -191,7 +204,8 @@ Stepping... 3
Ok 9.5!
Ok 10!
-# TLOOPS +TLOOPS +------ ```lua require("multi") -- TLoops are loops that run ever n second. We will also look at condition objects as well @@ -211,7 +225,8 @@ multi:mainloop() # Output Count is 101! -# Connections +Connections +----------- These are my favorite objects and you'll see why. They are very useful objects for ASync connections! ```lua @@ -264,13 +279,13 @@ 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 +TIMERS +------ ```lua -- You see the thing is that all time based objects use timers eg. Alarms, TSteps, and Loops. Timers are more low level! require("multi") @@ -316,7 +331,8 @@ Note: This will make more sense when you run it for your self
4.002
5.003
-# UPDATER +UPDATER +------- ```lua -- Updaters: Have been moved to the core of the library require("multi") would work as well require("multi") @@ -369,7 +385,6 @@ multi:mainloop() -- Notice how the past few examples did not need this, well onl 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)!
@@ -378,7 +393,6 @@ 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)!
@@ -389,7 +403,8 @@ 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 +Processes +--------- A process allows you to group the Actor objects within a controlable interface ```lua require("multi") @@ -466,7 +481,8 @@ function multi:hold(task) end ``` -# Queuer (WIP) +Queuer (WIP) +------------ A queuer works just like a process however objects are processed in order that they were created... ```lua require("multi") @@ -514,7 +530,8 @@ Done
10
Ring ring!!!
-# Threads +Threads +------- These fix the hold problem that you get with regular objects, and they work exactly the same! They even have some extra features that make them really useful.
```lua _require=require -- lets play with the require method a bit @@ -576,14 +593,16 @@ step 3
Hello!
Ring
Count is 100
-# Threadable Actors +Threadable Actors +----------------- - Alarms - Events - Loop/TLoop - Process - Step/TStep -# Functions +Functions +--------- If you ever wanted to pause a function then great now you can The uses of the Function object allows one to have a method that can run free in a sense ```lua @@ -602,7 +621,8 @@ Hello
PAUSED
Hello3
-# ThreadedUpdater +ThreadedUpdater +--------------- ```lua -- Works the same as a regular updater! @@ -618,7 +638,8 @@ multi:mainloop() ...
.inf
-# Triggers +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 @@ -635,7 +656,8 @@ trig:Fire(1,2,3,"Hello",true) 1 2 3
1 2 3 Hello true
-# Tasks +Tasks +----- Tasks allow you to run a block of code before the multi mainloops does it thing. Tasks still have a use, but depending on how you program they aren't needed.
```lua require("multi") @@ -658,7 +680,8 @@ 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 +---- Jobs were a strange feature that was created for throttling connections! When I was building a 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... @@ -692,7 +715,8 @@ There are 4 jobs in the queue!
A job!

Another job!
-# Watchers +Watchers +-------- Watchers allow you to monitor a variable and trigger an event when the variable has changed! ```lua require("multi") @@ -714,6 +738,7 @@ multi:mainloop() .inf-1 inf
Timeout management +------------------ ```lua -- Note: I used a tloop so I could control the output of the program a bit. require("multi") @@ -761,7 +786,128 @@ Looping...
Looping...
We did it! 1 2 3
-# Changes +Changes +------- +Updated from 1.7.6 to 1.8.0
+Added:
+- multi:newSystemThreadedQueue() +- multi:systemThreadedBenchmark() +- More example files +- multi:canSystemThread() -- true if an intergration was added false otherwise (For module creation) +- Fixed a few bugs in the loveManager + +# Using multi:systemThreadedBenchmark() +```lua +package.path="?/init.lua;"..package.path +local GLOBAL,sThread=require("multi.intergration.lanesManager").init() +multi:systemThreadedBenchmark(3):OnBench(function(self,count) + print("First Bench: "..count) + multi:systemThreadedBenchmark(3,"All Threads: ") +end) +multi:mainloop() +``` + +# Using multi:newSystemThreadedQueue() +```lua +-- in love2d, this file will be in the same example folder as before, but is named main2.lua +require("core.Library") +GLOBAL,sThread=require("multi.intergration.loveManager").init() -- load the love2d version of the lanesManager and requires the entire multi library +--IMPORTANT +-- Do not make the above local, this is the one difference that the lanesManager does not have +-- If these are local the functions will have the upvalues put into them that do not exist on the threaded side +-- You will need to ensure that the function does not refer to any upvalues in its code. It will print an error if it does though +-- Also each thread has a .1 second delay! This is used to generate a random values for each thread! +require("core.GuiManager") +gui.ff.Color=Color.Black +function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a channel on both ends + local c={} + c.name=name + if love then + if love.thread then + function c:init() + self.chan=love.thread.getChannel(self.name) + function self:push(v) + self.chan:push(v) + end + function self:pop() + return self.chan:pop() + end + GLOBAL[self.name]=self + return self + end + return c + else + error("Make sure you required the love.thread module!") + end + else + c.linda=lanes.linda() + function c:push(v) + self.linda:send("Q",v) + end + function c:pop() + return ({self.linda:receive(0,"Q")})[2] + end + function c:init() + return self + end + GLOBAL[name]=c + end + return c +end +queue=multi:newSystemThreadedQueue("QUEUE"):init() +queue:push("This is a test") +queue:push("This is a test2") +queue:push("This is a test3") +queue:push("This is a test4") +multi:newSystemThread("test2",function() + queue=sThread.waitFor("QUEUE"):init() + data=queue:pop() + while data do + print(data) + data=queue:pop() + end + queue:push("DONE!") +end) +multi:newThread("test!",function() + thread.hold(function() return queue:pop() end) + t.text="Done!" +end) +t=gui:newTextLabel("no done yet!",0,0,300,100) +t:centerX() +t:centerY() +``` +# In Lanes +```lua +-- The code is compatible with each other, I just wanted to show different things you can do in both examples +-- This file can be found in the examples folder as lanesintergrationtest4.lua +local GLOBAL,sThread=require("multi.intergration.lanesManager").init() +queue=multi:newSystemThreadedQueue("QUEUE"):init() +queue:push("This is a test") +queue:push("This is a test2") +queue:push("This is a test3") +queue:push("This is a test4") +multi:newSystemThread("test2",function() + queue=sThread.waitFor("QUEUE"):init() + data=queue:pop() + while data do + print(data) + data=queue:pop() + end + queue:push("This is a test5") + queue:push("This is a test6") + queue:push("This is a test7") + queue:push("This is a test8") +end) +multi:newThread("test!",function() -- this is a lua thread + thread.sleep(.1) + data=queue:pop() + while data do + print(data) + data=queue:pop() + end +end) +multi:mainloop() +``` Updated from 1.7.5 to 1.7.6
Fixed: Typos like always diff --git a/examples/alarmTest.lua b/examples/alarmTest.lua new file mode 100644 index 0000000..632a8ee --- /dev/null +++ b/examples/alarmTest.lua @@ -0,0 +1,11 @@ +-- Tick Tock Example +require("multi") +alarm=multi:newAlarm(1) +alarm.state=-1 -- set the state to -1 +alarm.sounds={[-1]="Tick",[1]="Tock"} -- this makes changing between states easy and fast +alarm:OnRing(function(self) + print(self.sounds[self.state]) + self.state=self.state*-1 -- change the state in one line + self:Reset() -- Reset the alarm so it runs again +end) +multi:mainloop() diff --git a/examples/lanesintergratetest4.lua b/examples/lanesintergratetest4.lua new file mode 100644 index 0000000..34a651c --- /dev/null +++ b/examples/lanesintergratetest4.lua @@ -0,0 +1,27 @@ +local GLOBAL,sThread=require("multi.intergration.lanesManager").init() +queue=multi:newSystemThreadedQueue("QUEUE"):init() +queue:push("This is a test") +queue:push("This is a test2") +queue:push("This is a test3") +queue:push("This is a test4") +multi:newSystemThread("test2",function() + queue=sThread.waitFor("QUEUE"):init() + data=queue:pop() + while data do + print(data) + data=queue:pop() + end + queue:push("This is a test5") + queue:push("This is a test6") + queue:push("This is a test7") + queue:push("This is a test8") +end) +multi:newThread("test!",function() -- this is a lua thread + thread.sleep(.1) + data=queue:pop() + while data do + print(data) + data=queue:pop() + end +end) +multi:mainloop() diff --git a/examples/lanesintergratetest5.lua b/examples/lanesintergratetest5.lua new file mode 100644 index 0000000..ecd7d3c --- /dev/null +++ b/examples/lanesintergratetest5.lua @@ -0,0 +1,28 @@ +package.path="?/init.lua;"..package.path -- slightly different usage of the code +local GLOBAL,sThread=require("multi.intergration.lanesManager").init() +queue=multi:newSystemThreadedQueue("QUEUE") +queue:push(1) +queue:push(2) +queue:push(3) +queue:push(4) +queue:push(5) +queue:push(6) +multi:newSystemThread("STHREAD_1",function() + queue=sThread.waitFor("QUEUE"):init() + GLOBAL["QUEUE"]=nil + data=queue:pop() + while data do + print(data) + data=queue:pop() + end +end) +multi:newThread("THREAD_1",function() + while true do + if GLOBAL["QUEUE"]==nil then + print("Deleted a Global!") + break + end + thread.skip(1) + end +end) +multi:mainloop() diff --git a/examples/loopTest.lua b/examples/loopTest.lua new file mode 100644 index 0000000..138d8ba --- /dev/null +++ b/examples/loopTest.lua @@ -0,0 +1,9 @@ +-- like the while loop (kinda) +require("multi") +loop=multi:newLoop(function(self,dt) + if dt>10 then + print("Enough time has passed!") + self:Break() -- lets break this thing + end +end) +multi:mainloop() diff --git a/examples/love2d Threading Example/main2.lua b/examples/love2d Threading Example/main2.lua new file mode 100644 index 0000000..9528b5f --- /dev/null +++ b/examples/love2d Threading Example/main2.lua @@ -0,0 +1,65 @@ +require("core.Library") +GLOBAL,sThread=require("multi.intergration.loveManager").init() -- load the love2d version of the lanesManager and requires the entire multi library +--IMPORTANT +-- Do not make the above local, this is the one difference that the lanesManager does not have +-- If these are local the functions will have the upvalues put into them that do not exist on the threaded side +-- You will need to ensure that the function does not refer to any upvalues in its code. It will print an error if it does though +-- Also each thread has a .1 second delay! This is used to generate a random values for each thread! +require("core.GuiManager") +gui.ff.Color=Color.Black +function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a channel on both ends + local c={} + c.name=name + if love then + if love.thread then + function c:init() + self.chan=love.thread.getChannel(self.name) + function self:push(v) + self.chan:push(v) + end + function self:pop() + return self.chan:pop() + end + GLOBAL[self.name]=self + return self + end + return c + else + error("Make sure you required the love.thread module!") + end + else + c.linda=lanes.linda() + function c:push(v) + self.linda:send("Q",v) + end + function c:pop() + return ({self.linda:receive(0,"Q")})[2] + end + function c:init() + return self + end + GLOBAL[name]=c + end + return c +end +queue=multi:newSystemThreadedQueue("QUEUE"):init() +queue:push("This is a test") +queue:push("This is a test2") +queue:push("This is a test3") +queue:push("This is a test4") +multi:newSystemThread("test2",function() + queue=sThread.waitFor("QUEUE"):init() + data=queue:pop() + while data do + print(data) + data=queue:pop() + end + queue:push("DONE!") +end) +multi:newThread("test!",function() + thread.hold(function() return queue:pop() end) + t.text="Done!" +end) +t=gui:newTextLabel("no done yet!",0,0,300,100) +t:centerX() +t:centerY() diff --git a/examples/love2d Threading Example/main3.lua b/examples/love2d Threading Example/main3.lua new file mode 100644 index 0000000..1593f44 --- /dev/null +++ b/examples/love2d Threading Example/main3.lua @@ -0,0 +1,37 @@ +require("core.Library") +GLOBAL,sThread=require("multi.intergration.loveManager").init() -- load the love2d version of the lanesManager and requires the entire multi library +--IMPORTANT +-- Do not make the above local, this is the one difference that the lanesManager does not have +-- If these are local the functions will have the upvalues put into them that do not exist on the threaded side +-- You will need to ensure that the function does not refer to any upvalues in its code. It will print an error if it does though +-- Also each thread has a .1 second delay! This is used to generate a random values for each thread! +require("core.GuiManager") +gui.ff.Color=Color.Black +queue=multi:newSystemThreadedQueue("QUEUE"):init() +queue:push(1) +queue:push(2) +queue:push(3) +queue:push(4) +queue:push(5) +queue:push(6) +multi:newSystemThread("STHREAD_1",function() + queue=sThread.waitFor("QUEUE"):init() + GLOBAL["QUEUE"]=nil + data=queue:pop() + while data do + print(data) + data=queue:pop() + end +end) +multi:newThread("THREAD_1",function() + while true do + if GLOBAL["QUEUE"]==nil then + t.text="Deleted a Global!" + break + end + thread.skip() -- give cpu time to other processes + end +end) +t=gui:newTextLabel("no done yet!",0,0,300,100) +t:centerX() +t:centerY() diff --git a/multi/init.lua b/multi/init.lua index 66d82ba..22f74ef 100644 --- a/multi/init.lua +++ b/multi/init.lua @@ -45,7 +45,7 @@ function print(...) end end multi = {} -multi.Version={1,7,6} +multi.Version={1,8,0} multi.stage='stable' multi.__index = multi multi.Mainloop={} @@ -248,6 +248,9 @@ function multi:getPlatform() return "lanes" end end +function multi:canSystemThread() + return false +end --Processor function multi:getError() if self.error then @@ -417,7 +420,7 @@ function multi:protect() local status, err=pcall(Loop[_D].Act,Loop[_D]) if err and not(Loop[_D].error) then Loop[_D].error=err - self.OnError:Fire(err,Loop[_D]) + self.OnError:Fire(Loop[_D],err) end end end diff --git a/multi/intergration/lanesManager.lua b/multi/intergration/lanesManager.lua index 150fd00..c03d661 100644 --- a/multi/intergration/lanesManager.lua +++ b/multi/intergration/lanesManager.lua @@ -32,6 +32,9 @@ end lanes=require("lanes").configure() package.path="lua/?/init.lua;lua/?.lua;"..package.path require("multi.all") -- get it all and have it on all lanes +function multi:canSystemThread() + return true +end local multi=multi -- Step 2 set up the linda objects local __GlobalLinda = lanes.linda() -- handles global stuff @@ -103,9 +106,10 @@ function multi:newSystemThread(name,func) local c={} local __self=c c.name=name + c.Type="sthread" c.thread=lanes.gen("*", func)() function c:kill() - self.status:Destroy() + --self.status:Destroy() self.thread:cancel() print("Thread: '"..self.name.."' has been stopped!") end @@ -114,7 +118,8 @@ function multi:newSystemThread(name,func) c.status:OnUpdate(function(self) local v,err,t=self.link.thread:join(.001) if err then - print("Error in thread: '"..self.link.name.."' <"..err..">") + multi.OnError:Fire(self.link,err) + print("Error in systemThread: '"..self.link.name.."' <"..err..">") self:Destroy() end end) @@ -124,7 +129,8 @@ print("Intergrated Lanes!") multi.intergration={} -- for module creators multi.intergration.GLOBAL=GLOBAL multi.intergration.THREAD=THREAD -multi.intergration.lanes={} -- for module creators +multi.intergration.lanes={} multi.intergration.lanes.GLOBAL=GLOBAL -- for module creators multi.intergration.lanes.THREAD=THREAD -- for module creators +require("multi.intergration.shared.shared") return {init=function() return GLOBAL,THREAD end} diff --git a/multi/intergration/loveManager.lua b/multi/intergration/loveManager.lua index e8304d9..2e8e06f 100644 --- a/multi/intergration/loveManager.lua +++ b/multi/intergration/loveManager.lua @@ -1,7 +1,11 @@ require("multi.compat.love2d") +function multi:canSystemThread() + return true +end multi.intergration={} multi.intergration.love2d={} multi.intergration.love2d.ThreadBase=[[ +__THREADNAME__=({...})[1] require("love.filesystem") require("love.system") require("love.timer") @@ -43,6 +47,8 @@ function ToStr(val, name, skipnewlines, depth) tmp = tmp .. string.format("%q", val) elseif type(val) == "boolean" then tmp = tmp .. (val and "true" or "false") + elseif type(val) == "function" then + tmp = tmp .. "loadDump([===["..dump(val).."]===])" else tmp = tmp .. "\"[inserializeable datatype:" .. type(val) .. "]\"" end @@ -57,6 +63,8 @@ function resolveType(tp,d) return loadDump(d) elseif tp=="table" then return loadstring("return "..d)() + elseif tp=="nil" then + return nil else return d end @@ -67,7 +75,7 @@ function resolveData(v) data=ToStr(v) elseif type(v)=="function" then data=dump(v) - elseif type(v)=="string" or type(v)=="number" or type(v)=="bool" then + elseif type(v)=="string" or type(v)=="number" or type(v)=="bool" or type(v)=="nil" then data=tostring(v) end return data @@ -77,7 +85,7 @@ local function randomString(n) local c=os.clock() local a=0 while os.clock()" c.thread=love.thread.newThread(multi.intergration.love2d.ThreadBase:gsub("INSERT_USER_CODE",dump(func))) - c.thread:start() + c.thread:start(c.ID) + function c:kill() + multi.intergration.GLOBAL["__DIEPLZ"..self.ID.."__"]="__DIEPLZ"..self.ID.."__" + end return c end function love.threaderror( thread, errorstr ) - print("Error in "..tostring(thread)..": "..errorstr) + multi.OnError:Fire(thread,errorstr) + print("Error in systemThread: "..tostring(thread)..": "..errorstr) end local THREAD={} function THREAD.set(name,val) @@ -327,6 +361,7 @@ updater:OnUpdate(function(self) data=multi.intergration.love2d.mainChannel:pop() end end) +require("multi.intergration.shared.shared") print("Intergrated Love2d!") return { init=function(t) diff --git a/multi/intergration/shared/shared.lua b/multi/intergration/shared/shared.lua new file mode 100644 index 0000000..9a2d864 --- /dev/null +++ b/multi/intergration/shared/shared.lua @@ -0,0 +1,102 @@ +--[[ +MIT License + +Copyright (c) 2017 Ryan Ward + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +]] +function multi:newSystemThreadedQueue(name) -- in love2d this will spawn a channel on both ends + local c={} -- where we will store our object + c.name=name -- set the name this is important for the love2d side + if love then -- check love + if love.thread then -- make sure we can use the threading module + function c:init() -- create an init function so we can mimic on bith love2d and lanes + self.chan=love.thread.getChannel(self.name) -- create channel by the name self.name + function self:push(v) -- push to the channel + self.chan:push(v) + end + function self:pop() -- pop from the channel + return self.chan:pop() + end + GLOBAL[self.name]=self -- send the object to the thread through the global interface + return self -- return the object + end + return c + else + error("Make sure you required the love.thread module!") -- tell the user if he/she didn't require said module + end + else + c.linda=lanes.linda() -- lanes is a bit eaiser, create the linda on the main thread + function c:push(v) -- push to the queue + self.linda:send("Q",v) + end + function c:pop() -- pop the queue + return ({self.linda:receive(0,"Q")})[2] + end + function c:init() -- mimic the feature that love2d requires, so code can be consistent + return self + end + multi.intergration.GLOBAL[name]=c -- send the object to the thread through the global interface + end + return c +end +function multi:systemThreadedBenchmark(n,p) + n=n or 1 + local cores=multi.intergration.THREAD.getCores() + local queue=multi:newSystemThreadedQueue("QUEUE") + multi.intergration.GLOBAL["__SYSTEMBENCHMARK__"]=n + local sThread=multi.intergration.THREAD + local GLOBAL=multi.intergration.GLOBAL + for i=1,cores do + multi:newSystemThread("STHREAD_BENCH",function() + require("multi") + if multi:getPlatform()=="love2d" then + GLOBAL=_G.GLOBAL + sThread=_G.sThread + end -- we cannot have upvalues... in love2d globals not locals must be used + queue=sThread.waitFor("QUEUE"):init() -- always wait for when looking for a variable at the start of the thread! + multi:benchMark(sThread.waitFor("__SYSTEMBENCHMARK__")):OnBench(function(self,count) + queue:push(count) + multi:Stop() + end) + multi:mainloop() + end) + end + local c={} + c.tt=function() end + c.p=p + function c:OnBench(func) + self.tt=func + end + multi:newThread("THREAD_BENCH",function() + thread.sleep(n+.1) + GLOBAL["QUEUE"]=nil -- time to clean up + local num=0 + data=queue:pop() + while data do + num=num+data + data=queue:pop() + end + if p then + print(tostring(p)..num) + end + c.tt(c,num) + end) + return c +end diff --git a/multi/threading.lua b/multi/threading.lua index 4c0397a..4ae2add 100644 --- a/multi/threading.lua +++ b/multi/threading.lua @@ -30,13 +30,13 @@ else thread.__CORES=tonumber(io.popen("nproc --all"):read("*n")) end function thread.sleep(n) - coroutine.yield({"_sleep_",n}) + coroutine.yield({"_sleep_",n or 0}) end function thread.hold(n) - coroutine.yield({"_hold_",n}) + coroutine.yield({"_hold_",n or function() return true end}) end function thread.skip(n) - coroutine.yield({"_skip_",n}) + coroutine.yield({"_skip_",n or 0}) end function thread.kill() coroutine.yield({"_kill_",":)"}) @@ -79,6 +79,7 @@ function multi:newThread(name,func) c.Name=name c.thread=coroutine.create(func) c.sleep=1 + c.Type="thread" c.firstRunDone=false c.timer=multi.scheduler:newTimer() c.ref.Globals=self:linkDomain("Globals") @@ -141,6 +142,10 @@ multi.scheduler:OnUpdate(function(self) else _,ret=coroutine.resume(self.Threads[i].thread,self.Globals) end + if _==false then + self.Parent.OnError:Fire(self.Threads[i],ret) + print("Error in thread: <"..self.Threads[i].Name.."> "..ret) + end if ret==true or ret==false then print("Thread Ended!!!") ret={}