diff --git a/control.lua b/control.lua index 7875f4e..84b6068 100644 --- a/control.lua +++ b/control.lua @@ -2,7 +2,7 @@ module 'aux' local T = require 'T' -local event_frame = CreateFrame'Frame' +local event_frame = CreateFrame('Frame', 'AuxThreadingFrame') local listeners, threads = T.acquire(), T.acquire() @@ -10,7 +10,6 @@ local thread_id function M.thread_id() return thread_id end function handle.LOAD() - event_frame:SetScript('OnUpdate', UPDATE) event_frame:SetScript('OnEvent', EVENT) end @@ -39,6 +38,11 @@ do for id, thread in threads do if thread.killed or not thread.k then threads[id] = nil + local hasLiveThread = false + for _ in pairs(threads) do hasLiveThread = true break end + if not hasLiveThread then -- Disable threading task so it doesn't consume resources doing nothing + event_frame:SetScript('OnUpdate', nil) + end else local k = thread.k thread.k = nil @@ -100,6 +104,9 @@ do arg.f = tremove(arg, 1) local thread_id = unique_id() threads[thread_id] = T.map('k', setmetatable(arg, mt)) + if event_frame:GetScript("OnUpdate") == nil then -- Spin up threading if it was turned off + event_frame:SetScript('OnUpdate', UPDATE) + end return thread_id end diff --git a/libs/T.lua b/libs/T.lua index 738b613..fddbba7 100644 --- a/libs/T.lua +++ b/libs/T.lua @@ -15,13 +15,6 @@ function wipe(t) end M.wipe = wipe -CreateFrame'Frame':SetScript('OnUpdate', function() - for t in auto_release do - release(t) - end - wipe(auto_release) -end) - function acquire() if pool_size > 0 then pool_size = pool_size - 1 @@ -36,6 +29,19 @@ function acquire() end M.acquire = acquire +local f = CreateFrame('Frame', 'AuxGCFrame') +local function AuxGCFrame_OnUpdate() + for t in auto_release do + release(t) + end + wipe(auto_release) + this:SetScript("OnUpdate", nil) -- Remove the script, it will be set back if anything is added to auto_release +end +local function autoRelease(t) + auto_release[t] = true + f:SetScript("OnUpdate", AuxGCFrame_OnUpdate) +end + function release(t) wipe(t) auto_release[t] = nil @@ -51,7 +57,7 @@ M.release = release do local function f(_, v) if v then - auto_release[v] = true + autoRelease(v) return v end end @@ -85,7 +91,7 @@ do local MAXPARAMS = 100 local code = [[ - local f, setn, acquire, auto_release = f, setn, acquire, auto_release + local f, setn, acquire, autoRelease = f, setn, acquire, autoRelease return function( ]] for i = 1, MAXPARAMS - 1 do @@ -103,7 +109,7 @@ do code = code .. [[ until true local t = acquire() - auto_release[t] = true + autoRelease(t) setn(t, n) repeat ]] @@ -116,9 +122,11 @@ do end ]] + local chunk = loadstring(code) + local chunkEnv = {setn=setn, acquire=acquire, auto_release=auto_release, autoRelease=autoRelease} + setfenv(chunk, chunkEnv) function vararg(f) - local chunk = loadstring(code) - setfenv(chunk, {f=f, setn=setn, acquire=acquire, auto_release=auto_release}) + chunkEnv.f = f return chunk() end M.vararg = setmetatable({}, {