From 8649b62261bd543978b17c371bb635af84944443 Mon Sep 17 00:00:00 2001 From: CIownxz Date: Sun, 26 Mar 2023 01:02:38 -0400 Subject: [PATCH] Create _yield function To improve code readability. --- lib/init.lua | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/init.lua b/lib/init.lua index 5554013..f65db36 100644 --- a/lib/init.lua +++ b/lib/init.lua @@ -51,6 +51,26 @@ function ThreadPool:_call(callback: (T...) -> nil, ...: T...) table.insert(self._openThreads, thread) -- Store the newly opened Thread into openThreads end +--[=[ + @method _yield + @within ThreadPool + @private + @tag Internal Use Only + + An Internal Function for use when creating a new thread. + + @param closingReference boolean + + @return void +]=] +function ThreadPool:_yield(closeThread: boolean): nil + while not closeThread do + self:_call(coroutine.yield()) + end + + return +end + --[=[ @method _createThread @within ThreadPool @@ -66,11 +86,7 @@ function ThreadPool:_createThread() -- Create new thread and add it to the openThreads table local newThread: thread | nil - newThread = coroutine.create(function() - while not closeThread do - self:_call(coroutine.yield()) - end - end) + newThread = coroutine.create(self._yield) -- Implement Lifetime if #self._openThreads > self._threadCount then @@ -83,7 +99,7 @@ function ThreadPool:_createThread() end) end - coroutine.resume(newThread :: thread) + coroutine.resume(newThread :: thread, closeThread) table.insert(self._openThreads, newThread) end