cycle collector: make it threadsafe

This commit is contained in:
Araq 2020-05-12 20:12:55 +02:00
commit 82f008158c

View file

@ -89,15 +89,15 @@ proc nimTraceRefDyn(q: pointer; env: pointer) {.compilerRtl.} =
j.traceStack.add(head p[], cast[ptr PNimType](p[])[]) j.traceStack.add(head p[], cast[ptr PNimType](p[])[])
var var
roots: CellSeq roots {.threadvar.}: CellSeq
proc unregisterCycle(s: Cell) = proc unregisterCycle(s: Cell) =
# swap with the last element. O(1) # swap with the last element. O(1)
let idx = s.rootIdx let idx = s.rootIdx
if idx >= roots.len or idx < 0: when false:
cprintf("[Bug!] %ld\n", idx) if idx >= roots.len or idx < 0:
quit 1 cprintf("[Bug!] %ld\n", idx)
quit 1
roots.d[idx] = roots.d[roots.len-1] roots.d[idx] = roots.d[roots.len-1]
roots.d[idx][0].rootIdx = idx roots.d[idx][0].rootIdx = idx
dec roots.len dec roots.len