Skip to content

Commit 39c75bb

Browse files
committed
scripting/lua: scheduler fixes for undefined behavior
1 parent ade4b2b commit 39c75bb

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

data/shared/citizen/scripting/lua/scheduler.lua

+12-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
local threads = {}
1+
local newThreads = {}
2+
local threads = setmetatable({}, {
3+
-- This circumvents undefined behaviour in "next" (and therefore "pairs")
4+
__newindex = newThreads,
5+
-- This is needed for CreateThreadNow to work correctly
6+
__index = newThreads
7+
})
28

39
--[[
410
@@ -97,6 +103,11 @@ SetTimeout = Citizen.SetTimeout
97103
Citizen.SetTickRoutine(function()
98104
local curTime = GetGameTimer()
99105

106+
for coro, thread in pairs(newThreads) do
107+
rawset(threads, coro, thread)
108+
newThreads[coro] = nil
109+
end
110+
100111
for coro, thread in pairs(threads) do
101112
if curTime >= thread.wakeTime then
102113
resumeThread(coro)

0 commit comments

Comments
 (0)