summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorSergey Avseyev <sergey.avseyev@gmail.com>2015-06-26 00:15:08 +0300
committerSergey Avseyev <sergey.avseyev@gmail.com>2015-06-26 07:42:58 +0300
commit70ce8695e703f9bc92868226421f8b9d69c005d9 (patch)
tree10e5d3593f89840c6c0a3704210e2c2d6b545b08
parentf9d8d6ce09ac345e0c1194b12031944a3949c7dd (diff)
downloadNim-70ce8695e703f9bc92868226421f8b9d69c005d9.tar.gz
Fix #2672. Do not define globalsSlot for native TLS
Motivation
----------
globalsSlot is always defined so threading code works incorrectly when
native TLS supported.

Modification
------------
Defined globalsSlot only in TLS emulation mode. Remove myThreadId, which
based on broken behavior. It might be reimplemented later

Result
------
No segfaults.
-rw-r--r--lib/system/threads.nim27
1 files changed, 11 insertions, 16 deletions
diff --git a/lib/system/threads.nim b/lib/system/threads.nim
index 1ec9aadf1..180e0410b 100644
--- a/lib/system/threads.nim
+++ b/lib/system/threads.nim
@@ -196,28 +196,28 @@ type
       nil
 {.deprecated: [TThreadLocalStorage: ThreadLocalStorage, TGcThread: GcThread].}
 
-# XXX it'd be more efficient to not use a global variable for the
-# thread storage slot, but to rely on the implementation to assign slot X
-# for us... ;-)
-var globalsSlot: ThreadVarSlot
-
 when not defined(useNimRtl):
   when not useStackMaskHack:
     var mainThread: GcThread
 
-proc initThreadVarsEmulation() {.compilerProc, inline.} =
-  when not defined(useNimRtl):
-    globalsSlot = threadVarAlloc()
-    when declared(mainThread):
-      threadVarSetValue(globalsSlot, addr(mainThread))
-
 #const globalsSlot = ThreadVarSlot(0)
 #sysAssert checkSlot.int == globalsSlot.int
 
 when emulatedThreadVars:
+  # XXX it'd be more efficient to not use a global variable for the
+  # thread storage slot, but to rely on the implementation to assign slot X
+  # for us... ;-)
+  var globalsSlot: ThreadVarSlot
+
   proc GetThreadLocalVars(): pointer {.compilerRtl, inl.} =
     result = addr(cast[PGcThread](threadVarGetValue(globalsSlot)).tls)
 
+  proc initThreadVarsEmulation() {.compilerProc, inline.} =
+    when not defined(useNimRtl):
+      globalsSlot = threadVarAlloc()
+      when declared(mainThread):
+        threadVarSetValue(globalsSlot, addr(mainThread))
+
 when useStackMaskHack:
   proc maskStackPointer(offset: int): pointer {.compilerRtl, inl.} =
     var x {.volatile.}: pointer
@@ -398,11 +398,6 @@ proc threadId*[TArg](t: var Thread[TArg]): ThreadId[TArg] {.inline.} =
   ## returns the thread ID of `t`.
   result = addr(t)
 
-proc myThreadId*[TArg](): ThreadId[TArg] =
-  ## returns the thread ID of the thread that calls this proc. This is unsafe
-  ## because the type ``TArg`` is not checked for consistency!
-  result = cast[ThreadId[TArg]](threadVarGetValue(globalsSlot))
-
 when false:
   proc mainThreadId*[TArg](): ThreadId[TArg] =
     ## returns the thread ID of the main thread.