summary refs log tree commit diff stats
path: root/tests/threads/tthreadvars.nim
diff options
context:
space:
mode:
authorZahary Karadjov <zahary@gmail.com>2018-04-29 13:50:21 +0300
committerAndreas Rumpf <rumpf_a@web.de>2018-05-07 09:37:49 +0200
commitae5c946a325c8833472173e63017dbed39f23abe (patch)
tree14922c780706850e7f47c50114dfbe9a7ce6eebb /tests/threads/tthreadvars.nim
parentb0d85b0adf950685e2f7d88665a3d5b03cd06cfd (diff)
downloadNim-ae5c946a325c8833472173e63017dbed39f23abe.tar.gz
Support thread-local variables declared inside procs; fixes #7565
Diffstat (limited to 'tests/threads/tthreadvars.nim')
-rw-r--r--tests/threads/tthreadvars.nim78
1 files changed, 78 insertions, 0 deletions
diff --git a/tests/threads/tthreadvars.nim b/tests/threads/tthreadvars.nim
new file mode 100644
index 000000000..81aa2e5ec
--- /dev/null
+++ b/tests/threads/tthreadvars.nim
@@ -0,0 +1,78 @@
+discard """
+output: '''
+10
+1111
+1222
+3030303
+3060606
+6060606
+6121212
+3030903
+3061206
+3031503
+3061806
+5050505
+5101010
+'''
+"""
+
+import typetraits
+
+var tls1 {.threadvar.}: int
+var g0: int
+var g1 {.global.}: int
+
+proc customInc(x: var int, delta: int) =
+  x += delta
+
+customInc(tls1, 10)
+echo tls1
+
+proc nonGenericProc: int =
+  var local: int
+  var nonGenericTls {.threadvar.}: int
+  var nonGenericGlobal {.global.}: int
+  var nonGenericMixedPragmas {.global, threadvar.}: int
+
+  customInc local, 1000
+  customInc nonGenericTls, 1
+  customInc nonGenericGlobal, 10
+  customInc nonGenericMixedPragmas, 100
+
+  return local + nonGenericTls + nonGenericGlobal + nonGenericMixedPragmas
+
+proc genericProc(T: typedesc): int =
+  var local: int
+  var genericTls {.threadvar.}: int
+  var genericGlobal {.global.}: int
+  var genericMixedPragmas {.global, threadvar.}: int
+
+  customInc local, T.name.len * 1000000
+  customInc genericTls, T.name.len * 1
+  customInc genericGlobal, T.name.len * 100
+  customInc genericMixedPragmas, T.name.len * 10000
+
+  return local + genericTls + genericGlobal + genericMixedPragmas
+
+echo nonGenericProc()
+echo nonGenericProc()
+
+echo genericProc(int)
+echo genericProc(int)
+
+echo genericProc(string)
+echo genericProc(string)
+
+proc echoInThread[T]() {.thread.} =
+  echo genericProc(T)
+  echo genericProc(T)
+
+proc newEchoThread(T: typedesc) =
+  var t: Thread[void]
+  createThread(t, echoInThread[T])
+  joinThreads(t)
+
+newEchoThread int
+newEchoThread int
+newEchoThread float
+