summary refs log tree commit diff stats
path: root/tests/threads
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-07-10 15:48:13 +0200
committerAraq <rumpf_a@web.de>2011-07-10 15:48:13 +0200
commit5b96eaa9533e877b5b7f2c6bf1e291ccdfdfecef (patch)
treef58b139b00b6af984f716164c7a3d72761df514d /tests/threads
parent2565ff8ddec9fcf43fbda2fae6f04806c1bc6e8a (diff)
downloadNim-5b96eaa9533e877b5b7f2c6bf1e291ccdfdfecef.tar.gz
preparations for 0.8.12
Diffstat (limited to 'tests/threads')
-rwxr-xr-xtests/threads/threadex.nim44
-rwxr-xr-xtests/threads/tthreadanalysis.nim54
-rwxr-xr-xtests/threads/tthreadanalysis2.nim55
-rwxr-xr-xtests/threads/tthreadheapviolation1.nim14
4 files changed, 167 insertions, 0 deletions
diff --git a/tests/threads/threadex.nim b/tests/threads/threadex.nim
new file mode 100755
index 000000000..65056a954
--- /dev/null
+++ b/tests/threads/threadex.nim
@@ -0,0 +1,44 @@
+
+type
+  TMsgKind = enum
+    mLine, mEof
+  TMsg = object {.pure, final.}
+    case k: TMsgKind
+    of mEof: backTo: TThreadId[int]
+    of mLine: data: string
+
+var
+  consumer: TThread[TMsg]
+  producer: TThread[int]
+  printedLines = 0
+
+proc consume() {.thread.} =
+  while true:
+    var x = recv[TMsg]()
+    if x.k == mEof: 
+      x.backTo.send(printedLines)
+      break
+    echo x.data
+    discard atomicInc(printedLines)
+
+proc produce() {.thread.} =
+  var m: TMsg
+  var input = open("readme.txt")
+  while not endOfFile(input):
+    if consumer.ready:
+      m.data = input.readLine()
+      consumer.send(m)
+  close(input)
+  m.k = mEof
+  m.backTo = myThreadId[int]()
+  consumer.send(m)
+  var result = recv[int]()
+  echo result
+  
+createThread(consumer, consume)
+createThread(producer, produce)
+joinThread(consumer)
+joinThread(producer)
+
+echo printedLines
+
diff --git a/tests/threads/tthreadanalysis.nim b/tests/threads/tthreadanalysis.nim
new file mode 100755
index 000000000..84545ddf7
--- /dev/null
+++ b/tests/threads/tthreadanalysis.nim
@@ -0,0 +1,54 @@
+discard """
+  outputsub: "101"
+  cmd: "nimrod cc --hints:on --threads:on $# $#"
+"""
+
+import os
+
+const
+  noDeadlocks = defined(system.deadlocksPrevented)
+
+var
+  thr: array [0..5, TThread[tuple[a, b: int]]]
+
+proc doNothing() = nil
+
+type
+  PNode = ref TNode
+  TNode = object {.pure.}
+    le, ri: PNode
+    data: string
+
+var
+  root: PNode
+
+proc buildTree(depth: int): PNode =
+  if depth == 3: return nil
+  new(result)
+  result.le = buildTree(depth-1)
+  result.ri = buildTree(depth-1)
+  result.data = $depth
+
+proc echoLeTree(n: PNode) =
+  var it: PNode
+  it = nil
+  it = n
+  while it != nil:
+    echo it.data
+    it = it.le
+
+proc threadFunc(interval: tuple[a, b: int]) {.thread.} = 
+  doNothing()
+  for i in interval.a..interval.b: 
+    var r = buildTree(i)
+    echoLeTree(r) # for local data
+  echoLeTree(root) # and the same for foreign data :-)
+
+proc main =
+  root = buildTree(5)
+  for i in 0..high(thr):
+    createThread(thr[i], threadFunc, (i*3, i*3+2))
+  joinThreads(thr)
+
+main()
+
diff --git a/tests/threads/tthreadanalysis2.nim b/tests/threads/tthreadanalysis2.nim
new file mode 100755
index 000000000..67e916c13
--- /dev/null
+++ b/tests/threads/tthreadanalysis2.nim
@@ -0,0 +1,55 @@
+discard """
+  file: "tthreadanalysis2.nim"
+  line: 45
+  errormsg: "write to foreign heap"
+  cmd: "nimrod cc --hints:on --threads:on $# $#"
+"""
+
+import os
+
+const
+  noDeadlocks = defined(system.deadlocksPrevented)
+
+var
+  thr: array [0..5, TThread[tuple[a, b: int]]]
+
+proc doNothing() = nil
+
+type
+  PNode = ref TNode
+  TNode = object {.pure.}
+    le, ri: PNode
+    data: string
+
+var
+  root: PNode
+
+proc buildTree(depth: int): PNode =
+  if depth == 3: return nil
+  new(result)
+  result.le = buildTree(depth-1)
+  result.ri = buildTree(depth-1)
+  result.data = $depth
+
+proc echoLeTree(n: PNode) =
+  var it = n
+  while it != nil:
+    echo it.data
+    it = it.le
+
+proc threadFunc(interval: tuple[a, b: int]) {.thread.} = 
+  doNothing()
+  for i in interval.a..interval.b: 
+    var r = buildTree(i)
+    echoLeTree(r) # for local data
+  root = buildTree(2) # BAD!
+  echoLeTree(root) # and the same for foreign data :-)
+
+proc main =
+  root = buildTree(5)
+  for i in 0..high(thr):
+    createThread(thr[i], threadFunc, (i*100, i*100+50))
+  joinThreads(thr)
+
+main()
+
diff --git a/tests/threads/tthreadheapviolation1.nim b/tests/threads/tthreadheapviolation1.nim
new file mode 100755
index 000000000..3cf7df7b5
--- /dev/null
+++ b/tests/threads/tthreadheapviolation1.nim
@@ -0,0 +1,14 @@
+var 
+  global: string = "test string"
+  t: TThread[string]
+
+proc horrible() {.thread.} =
+  global = "string in thread local heap!"
+  var x = global
+  var mydata = (x, "my string too")
+  echo global
+
+createThread(t, horrible)
+joinThread(t)
+
+