bugfix: bottom of AVL tree is now threadsafe

This commit is contained in:
Andreas Rumpf 2016-09-22 11:32:12 +02:00
commit bc53d2c9de
3 changed files with 7 additions and 5 deletions

View file

@ -101,8 +101,8 @@ type
# shared: # shared:
var var
bottomData: AvlNode bottomData {.threadvar.}: AvlNode
bottom: PAvlNode bottom {.threadvar.}: PAvlNode
{.push stack_trace: off.} {.push stack_trace: off.}
proc initAllocator() = proc initAllocator() =

View file

@ -9,7 +9,7 @@
# not really an AVL tree anymore, but still balanced ... # not really an AVL tree anymore, but still balanced ...
template isBottom(n: PAvlNode): bool = n == bottom template isBottom(n: PAvlNode): bool = n.link[0] == n
proc lowGauge(n: PAvlNode): int = proc lowGauge(n: PAvlNode): int =
var it = n var it = n
@ -65,14 +65,14 @@ proc add(a: var MemRegion, t: var PAvlNode, key, upperBound: int) {.benign.} =
split(t) split(t)
proc del(a: var MemRegion, t: var PAvlNode, x: int) {.benign.} = proc del(a: var MemRegion, t: var PAvlNode, x: int) {.benign.} =
if t == bottom: return if isBottom(t): return
a.last = t a.last = t
if x <% t.key: if x <% t.key:
del(a, t.link[0], x) del(a, t.link[0], x)
else: else:
a.deleted = t a.deleted = t
del(a, t.link[1], x) del(a, t.link[1], x)
if t == a.last and a.deleted != bottom and x == a.deleted.key: if t == a.last and not isBottom(a.deleted) and x == a.deleted.key:
a.deleted.key = t.key a.deleted.key = t.key
a.deleted.upperBound = t.upperBound a.deleted.upperBound = t.upperBound
a.deleted = bottom a.deleted = bottom

View file

@ -356,6 +356,8 @@ proc threadProcWrapStackFrame[TArg](thrd: ptr Thread[TArg]) =
template threadProcWrapperBody(closure: expr) {.immediate.} = template threadProcWrapperBody(closure: expr) {.immediate.} =
when declared(globalsSlot): threadVarSetValue(globalsSlot, closure) when declared(globalsSlot): threadVarSetValue(globalsSlot, closure)
when declared(initAllocator):
initAllocator()
var thrd = cast[ptr Thread[TArg]](closure) var thrd = cast[ptr Thread[TArg]](closure)
threadProcWrapStackFrame(thrd) threadProcWrapStackFrame(thrd)
# Since an unhandled exception terminates the whole process (!), there is # Since an unhandled exception terminates the whole process (!), there is