Skip to content

Commit

Permalink
Create _yield function
Browse files Browse the repository at this point in the history
To improve code readability.
  • Loading branch information
YetAnotherClown committed Mar 26, 2023
1 parent cbf47cd commit 8649b62
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions lib/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,26 @@ function ThreadPool:_call<T...>(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
Expand All @@ -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
Expand All @@ -83,7 +99,7 @@ function ThreadPool:_createThread()
end)
end

coroutine.resume(newThread :: thread)
coroutine.resume(newThread :: thread, closeThread)

table.insert(self._openThreads, newThread)
end
Expand Down

0 comments on commit 8649b62

Please sign in to comment.