diff options
Diffstat (limited to 'tests/parallel')
-rw-r--r-- | tests/parallel/nim.cfg (renamed from tests/parallel/nimrod.cfg) | 0 | ||||
-rw-r--r-- | tests/parallel/tdeepcopy2.nim | 35 | ||||
-rw-r--r-- | tests/parallel/tflowvar.nim | 25 | ||||
-rw-r--r-- | tests/parallel/tguard1.nim | 37 | ||||
-rw-r--r-- | tests/parallel/tguard2.nim | 27 | ||||
-rw-r--r-- | tests/parallel/tlet_spawn.nim | 14 | ||||
-rw-r--r-- | tests/parallel/tsysspawn.nim | 2 | ||||
-rw-r--r-- | tests/parallel/tsysspawnbadarg.nim | 2 |
8 files changed, 137 insertions, 5 deletions
diff --git a/tests/parallel/nimrod.cfg b/tests/parallel/nim.cfg index b81c89721..b81c89721 100644 --- a/tests/parallel/nimrod.cfg +++ b/tests/parallel/nim.cfg diff --git a/tests/parallel/tdeepcopy2.nim b/tests/parallel/tdeepcopy2.nim new file mode 100644 index 000000000..748ef4d9b --- /dev/null +++ b/tests/parallel/tdeepcopy2.nim @@ -0,0 +1,35 @@ +discard """ + output: '''called deepCopy for int +called deepCopy for int +done999 999 +""" + +import threadpool + + +type + Bar[T] = object + x: T + +proc deepCopy[T](b: ref Bar[T]): ref Bar[T] {.override.} = + result.new + result.x = b.x + when T is int: + echo "called deepCopy for int" + else: + echo "called deepCopy for something else" + +proc foo(b: ref Bar[int]): int = 999 + +# test that the disjoint checker deals with 'a = spawn f(); g = spawn f()': + +proc main = + var dummy: ref Bar[int] + new(dummy) + dummy.x = 44 + parallel: + let f = spawn foo(dummy) + let b = spawn foo(dummy) + echo "done", f, " ", b + +main() diff --git a/tests/parallel/tflowvar.nim b/tests/parallel/tflowvar.nim index 77fab14b5..fd3aa326e 100644 --- a/tests/parallel/tflowvar.nim +++ b/tests/parallel/tflowvar.nim @@ -1,11 +1,14 @@ discard """ - output: '''foobarfoobarbazbearbazbear''' - cmd: "nimrod $target --threads:on $options $file" + output: '''foobarfoobar +bazbearbazbear + +1''' + cmd: "nim $target --threads:on $options $file" """ import threadpool -proc computeSomething(a, b: string): string = a & b & a & b +proc computeSomething(a, b: string): string = a & b & a & b & "\n" proc main = let fvA = spawn computeSomething("foo", "bar") @@ -15,3 +18,19 @@ proc main = main() sync() + + +type + TIntSeq = seq[int] + +proc t(): TIntSeq = + result = @[1] + +proc p(): int = + var a: FlowVar[TIntSeq] + parallel: + var aa = spawn t() + a = aa + result = (^a)[0] + +echo p() diff --git a/tests/parallel/tguard1.nim b/tests/parallel/tguard1.nim new file mode 100644 index 000000000..d96e17589 --- /dev/null +++ b/tests/parallel/tguard1.nim @@ -0,0 +1,37 @@ + +when false: + template lock(a, b: ptr TLock; body: stmt) = + if cast[ByteAddress](a) < cast[ByteAddress](b): + pthread_mutex_lock(a) + pthread_mutex_lock(b) + else: + pthread_mutex_lock(b) + pthread_mutex_lock(a) + {.locks: [a, b].}: + try: + body + finally: + pthread_mutex_unlock(a) + pthread_mutex_unlock(b) + +type + ProtectedCounter[T] = object + i {.guard: L.}: T + L: int + +var + c: ProtectedCounter[int] + +c.i = 89 + +template atomicRead(L, x): expr = + {.locks: [L].}: + x + +proc main = + {.locks: [c.L].}: + inc c.i + discard + echo(atomicRead(c.L, c.i)) + +main() diff --git a/tests/parallel/tguard2.nim b/tests/parallel/tguard2.nim new file mode 100644 index 000000000..b69ea3371 --- /dev/null +++ b/tests/parallel/tguard2.nim @@ -0,0 +1,27 @@ +discard """ + errormsg: "unguarded access: c.i" + line: 25 +""" + +type + ProtectedCounter[T] = object + i {.guard: L.}: T + L: int + +var + c: ProtectedCounter[int] + +c.i = 89 + +template atomicRead(L, x): expr = + {.locks: [L].}: + x + +proc main = + {.locks: [c.L].}: + inc c.i + discard + echo(atomicRead(c.L, c.i)) + echo c.i + +main() diff --git a/tests/parallel/tlet_spawn.nim b/tests/parallel/tlet_spawn.nim new file mode 100644 index 000000000..463ee1a47 --- /dev/null +++ b/tests/parallel/tlet_spawn.nim @@ -0,0 +1,14 @@ + +import threadpool + +proc foo(): int = 999 + +# test that the disjoint checker deals with 'a = spawn f(); g = spawn f()': + +proc main = + parallel: + let f = spawn foo() + let b = spawn foo() + echo "done", f, " ", b + +main() diff --git a/tests/parallel/tsysspawn.nim b/tests/parallel/tsysspawn.nim index fc7921b0e..7244a5ee6 100644 --- a/tests/parallel/tsysspawn.nim +++ b/tests/parallel/tsysspawn.nim @@ -1,7 +1,7 @@ discard """ output: '''4 8''' - cmd: "nimrod $target --threads:on $options $file" + cmd: "nim $target --threads:on $options $file" """ import threadpool diff --git a/tests/parallel/tsysspawnbadarg.nim b/tests/parallel/tsysspawnbadarg.nim index ad798a7d3..2d3ffd241 100644 --- a/tests/parallel/tsysspawnbadarg.nim +++ b/tests/parallel/tsysspawnbadarg.nim @@ -1,7 +1,7 @@ discard """ line: 9 errormsg: "'spawn' takes a call expression" - cmd: "nimrod $target --threads:on $options $file" + cmd: "nim $target --threads:on $options $file" """ import threadpool |