summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-09-22 11:32:12 +0200
committerAndreas Rumpf <rumpf_a@web.de>2016-09-24 02:27:12 +0200
commitbc53d2c9ded45e790a8424660ff6152b3876fdab (patch)
treecbcf6c09c1619404db2f9b8f978b45b8bc6ba9d0
parent5a2ff9035eb075b744d5665e1c27fe317bdf25e0 (diff)
downloadNim-bc53d2c9ded45e790a8424660ff6152b3876fdab.tar.gz
bugfix: bottom of AVL tree is now threadsafe
-rw-r--r--lib/system/alloc.nim4
-rw-r--r--lib/system/avltree.nim6
-rw-r--r--lib/system/threads.nim2
3 files changed, 7 insertions, 5 deletions
diff --git a/lib/system/alloc.nim b/lib/system/alloc.nim
index bed9fd906..745bbbf62 100644
--- a/lib/system/alloc.nim
+++ b/lib/system/alloc.nim
@@ -101,8 +101,8 @@ type
 
 # shared:
 var
-  bottomData: AvlNode
-  bottom: PAvlNode
+  bottomData {.threadvar.}: AvlNode
+  bottom {.threadvar.}: PAvlNode
 
 {.push stack_trace: off.}
 proc initAllocator() =
diff --git a/lib/system/avltree.nim b/lib/system/avltree.nim
index d5c901542..892ee571d 100644
--- a/lib/system/avltree.nim
+++ b/lib/system/avltree.nim
@@ -9,7 +9,7 @@
 
 # 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 =
   var it = n
@@ -65,14 +65,14 @@ proc add(a: var MemRegion, t: var PAvlNode, key, upperBound: int) {.benign.} =
     split(t)
 
 proc del(a: var MemRegion, t: var PAvlNode, x: int) {.benign.} =
-  if t == bottom: return
+  if isBottom(t): return
   a.last = t
   if x <% t.key:
     del(a, t.link[0], x)
   else:
     a.deleted = t
     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.upperBound = t.upperBound
     a.deleted = bottom
diff --git a/lib/system/threads.nim b/lib/system/threads.nim
index 62829f62c..6f5bb38b1 100644
--- a/lib/system/threads.nim
+++ b/lib/system/threads.nim
@@ -356,6 +356,8 @@ proc threadProcWrapStackFrame[TArg](thrd: ptr Thread[TArg]) =
 
 template threadProcWrapperBody(closure: expr) {.immediate.} =
   when declared(globalsSlot): threadVarSetValue(globalsSlot, closure)
+  when declared(initAllocator):
+    initAllocator()
   var thrd = cast[ptr Thread[TArg]](closure)
   threadProcWrapStackFrame(thrd)
   # Since an unhandled exception terminates the whole process (!), there is