Added test for defer
This commit is contained in:
parent
f07b1ebb57
commit
42b4e0fcf4
3
init.lua
3
init.lua
@ -1340,7 +1340,7 @@ end
|
|||||||
function thread.hold(n, opt)
|
function thread.hold(n, opt)
|
||||||
thread._Requests()
|
thread._Requests()
|
||||||
local opt = opt or {}
|
local opt = opt or {}
|
||||||
if type(opt)=="table" then
|
if type(opt)=="table" and type(n) == "function" then
|
||||||
interval = opt.interval
|
interval = opt.interval
|
||||||
if opt.cycles then
|
if opt.cycles then
|
||||||
return yield(CMD, t_holdW, opt.cycles or 1, n or dFunc, interval)
|
return yield(CMD, t_holdW, opt.cycles or 1, n or dFunc, interval)
|
||||||
@ -1350,6 +1350,7 @@ function thread.hold(n, opt)
|
|||||||
return yield(CMD, t_skip, opt.skip or 1, nil, interval)
|
return yield(CMD, t_skip, opt.skip or 1, nil, interval)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if type(n) == "number" then
|
if type(n) == "number" then
|
||||||
thread.getRunningThread().lastSleep = clock()
|
thread.getRunningThread().lastSleep = clock()
|
||||||
return yield(CMD, t_sleep, n or 0, nil, interval)
|
return yield(CMD, t_sleep, n or 0, nil, interval)
|
||||||
|
|||||||
@ -137,9 +137,19 @@ local function INIT(__GlobalLinda, __SleepingLinda, __StatusLinda, __Console)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function THREAD.defer(func)
|
||||||
|
local m = {onexit = function() func() end}
|
||||||
|
if _VERSION >= "Lua 5.2" then
|
||||||
|
setmetatable(m, {__gc = m.onexit})
|
||||||
|
else
|
||||||
|
m.sentinel = newproxy(true)
|
||||||
|
getmetatable(m.sentinel).__gc = m.onexit
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return GLOBAL, THREAD
|
return GLOBAL, THREAD
|
||||||
end
|
end
|
||||||
|
|
||||||
return {init = function(g,s,st,c)
|
return {init = function(g,s,st,c,onexit)
|
||||||
return INIT(g,s,st,c)
|
return INIT(g,s,st,c,onexit)
|
||||||
end}
|
end}
|
||||||
@ -152,6 +152,10 @@ function INIT()
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function THREAD.defer(func)
|
||||||
|
multi.OnExit(func)
|
||||||
|
end
|
||||||
|
|
||||||
return GLOBAL, THREAD
|
return GLOBAL, THREAD
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -88,6 +88,8 @@ local function INIT(thread)
|
|||||||
THREAD.sleep = thread.sleep
|
THREAD.sleep = thread.sleep
|
||||||
|
|
||||||
THREAD.hold = thread.hold
|
THREAD.hold = thread.hold
|
||||||
|
|
||||||
|
THREAD.defer = thread.defer
|
||||||
|
|
||||||
function THREAD.setENV(env, name)
|
function THREAD.setENV(env, name)
|
||||||
name = name or "__env"
|
name = name or "__env"
|
||||||
|
|||||||
@ -44,13 +44,32 @@ multi:newThread("Scheduler Thread",function()
|
|||||||
-- multi:Stop()
|
-- multi:Stop()
|
||||||
-- os.exit(1)
|
-- os.exit(1)
|
||||||
-- end)
|
-- end)
|
||||||
|
|
||||||
queue = multi:newSystemThreadedQueue("Test_Queue"):init()
|
queue = multi:newSystemThreadedQueue("Test_Queue"):init()
|
||||||
|
defer_queue = multi:newSystemThreadedQueue("Defer_Queue"):init()
|
||||||
|
|
||||||
multi:newSystemThread("Test_Thread_0", function()
|
multi:newSystemThread("Test_Thread_0", function()
|
||||||
|
defer_queue = THREAD.waitFor("Defer_Queue"):init()
|
||||||
|
|
||||||
|
THREAD.defer(function()
|
||||||
|
defer_queue:push("done")
|
||||||
|
multi.print("This function was defered until the end of the threads life")
|
||||||
|
end)
|
||||||
|
|
||||||
|
multi.print("Testing defer, should print below this")
|
||||||
|
|
||||||
if THREAD_NAME~="Test_Thread_0" then
|
if THREAD_NAME~="Test_Thread_0" then
|
||||||
multi.error("The name should be Test_Thread_0",THREAD_NAME,THREAD_NAME,_G.THREAD_NAME)
|
multi.error("The name should be Test_Thread_0",THREAD_NAME,THREAD_NAME,_G.THREAD_NAME)
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
thread:newThread(function()
|
||||||
|
if thread.hold(function()
|
||||||
|
return defer_queue:pop() == "done"
|
||||||
|
end,{sleep=1}) == nil then
|
||||||
|
multi.error("Thread.defer didn't work!")
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
th1 = multi:newSystemThread("Test_Thread_1", function(a,b,c,d,e,f)
|
th1 = multi:newSystemThread("Test_Thread_1", function(a,b,c,d,e,f)
|
||||||
queue = THREAD.waitFor("Test_Queue"):init()
|
queue = THREAD.waitFor("Test_Queue"):init()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user