Working on 14.0.0

A few more things to do and test before sending this version out
This commit is contained in:
Ryan Ward 2019-11-17 21:39:31 -05:00
parent aa92282b8a
commit c2488ed5ce
2 changed files with 53 additions and 1 deletions

View File

@ -1,6 +1,6 @@
# Changes
[TOC]
Update 14.0.0 Consistency and stability
Update 14.0.0 Consistency, stability and some new features
-------------
Added:
- multi.init() -- Initlizes the library! Must be called for multiple files to have the same handle. Example below
@ -12,6 +12,15 @@ Added:
-- tobj.OnError(self,error) -- returns a reference to self and the error as a string
-- **Limitations:** only 7 returns are possible! This was done because creating and destroying table objects are slow. Instead I capture the return values from coroutine.resume into local variables and only allowed it to collect 6 max.
- thread.run(function) -- Can only be used within a thread, creates another thread that can do work, but automatically returns whatever from the run function
- thread:newFunction(FUNCTION; func)
-- returns a function that gives you the option to wait or connect to the returns of the function.
-- func().wait() -- only works when within a coroutine based thread
-- func().connect() -- this can work outside of of a coroutine based thread
-- func() -- If your function does not return anything you dont have to use wait or connect at all and the function will return instantly. You could also use wait() to hold until the function does it thing
-- If the created function encounters an error, it will return nil, the error message!
- special variable multi.NIL was added to allow error handling in threaded functions.
-- multi.NIL can be used in to force a nil value when using thread.hold()
- All functions created in the root of a thread are now converted to threaded functions, which allow for wait and connect features
Fixed:
- Connections had a preformance issue where they would create a non function when using connection.getConnection() of a non existing label.

View File

@ -49,6 +49,7 @@ multi.clock = os.clock
multi.time = os.time
multi.LinkedPath = multi
multi.lastTime = clock()
math.randomseed(os.time())
local mainloopActive = false
local isRunning = false
local next
@ -82,6 +83,7 @@ multi.Priority=multi.Priority_High
multi.threshold=256
multi.threstimed=.001
function multi.init()
multi.NIL = {Type="NIL"}
return _G["$multi"].multi,_G["$multi"].thread
end
function multi.queuefinal(self)
@ -1533,6 +1535,32 @@ function thread.waitFor(name)
thread.hold(function() return thread.get(name)~=nil end)
return thread.get(name)
end
function thread:newFunction(func)
local c = {}
c.__call = function(self,...)
local rets, err
local t = multi:newThread("TempThread",func)
t.OnDeath(function(self,status,...) rets = {...} end)
t.OnError(function(self,e) err = e end)
return {
wait = function()
return thread.hold(function()
if err then
return multi.NIL, err
elseif rets then
return unpack(rets)
end
end)
end,
connect = function(f)
t.OnDeath(function(self,status,...) f(...) end)
t.OnError(function(self,err) f(self, err) end)
end
}
end
setmetatable(c,c)
return c
end
function thread.run(func)
local threaddata,t2,t3,t4,t5,t6
local t = multi:newThread("Temp_Thread",func)
@ -1577,6 +1605,18 @@ function multi:newThread(name,func)
if type(name) == "function" then
name = "Thread#"..threadCount
end
local env = {}
setmetatable(env,{
__index = _G,
__newindex = function(t,k,v)
if type(v)=="function" then
rawset(t,k,thread:newFunction(v))
else
rawset(t,k,v)
end
end
})
setfenv(func,env)
local c={}
c.ref={}
c.Name=name
@ -1720,6 +1760,9 @@ function multi.initThreads()
elseif threads[i].task == "hold" then
t0,t1,t2,t3,t4,t5,t6 = threads[i].func()
if t0 then
if t0==multi.NIL then
t0 = nil
end
threads[i].task = ""
threads[i].__ready = true
end