Skip to content

Commit

Permalink
fix potentially overlapping request ids between threads
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauumm committed Sep 21, 2024
1 parent 5861f88 commit 1e6a5dc
Showing 1 changed file with 20 additions and 21 deletions.
41 changes: 20 additions & 21 deletions threadify.lua
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ else
local thread_table = {
resolvers = {},
rejecters = {},
free_indices = {},
}
local global_threads = threads_channel:peek()
if global_threads and global_threads[require_string] then
Expand All @@ -88,18 +87,21 @@ else
return function(...)
local msg = { -1, key, ... }
return async.promise:new(function(resolve, reject)
local index = 0
if #thread.free_indices == 0 then
index = #thread.resolvers + 1
else
local last_index = #thread.free_indices
index = thread.free_indices[last_index]
thread.free_indices[last_index] = nil
end
msg[1] = index
love.thread.getChannel(require_string .. "_cmd"):push(msg)
thread.resolvers[index] = resolve
thread.rejecters[index] = reject
local cmd_channel = love.thread.getChannel(require_string .. "_cmd")
local request_id = 1
cmd_channel:performAtomic(function(channel)
local last_cmd = cmd_channel:peek()
if last_cmd then
request_id = last_cmd[1] + 1
end
while thread.resolvers[request_id] ~= nil do
request_id = request_id + 1
end
msg[1] = request_id
thread.resolvers[request_id] = resolve
thread.rejecters[request_id] = reject
channel:push(msg)
end)
end)
end
end,
Expand All @@ -112,14 +114,12 @@ else
local thread = threads[require_string]
local channel = love.thread.getChannel(require_string .. "_out")
local result
channel:performAtomic(function()
local check_result = channel:peek()
if check_result then
if thread.resolvers[check_result[1]] and thread.rejecters[check_result[1]] then
result = channel:pop()
end
local check_result = channel:peek()
if check_result then
if thread.resolvers[check_result[1]] and thread.rejecters[check_result[1]] then
result = channel:pop()
end
end)
end
if result then
if result[2] then
thread.resolvers[result[1]](unpack(result, 3))
Expand All @@ -129,7 +129,6 @@ else
end
thread.resolvers[result[1]] = nil
thread.rejecters[result[1]] = nil
thread.free_indices[#thread.free_indices + 1] = result[1]
end
end
end
Expand Down

0 comments on commit 1e6a5dc

Please sign in to comment.