summary refs log tree commit diff stats
path: root/tests/generics/tthread_generic.nim
diff options
context:
space:
mode:
Diffstat (limited to 'tests/generics/tthread_generic.nim')
-rw-r--r--tests/generics/tthread_generic.nim39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/generics/tthread_generic.nim b/tests/generics/tthread_generic.nim
new file mode 100644
index 000000000..300da56a6
--- /dev/null
+++ b/tests/generics/tthread_generic.nim
@@ -0,0 +1,39 @@
+discard """
+  matrix: "--mm:refc; --mm:orc"
+  action: compile
+"""
+
+type
+  ThreadFuncArgs[T] = object of RootObj
+    a: proc(): T {.thread.}
+    b: proc(val: T) {.thread.}
+
+proc handleThreadFunc(arg: ThreadFuncArgs[int]){.thread.} =
+  var fn = arg.a
+  var callback = arg.b
+  var output = fn()
+  callback(output)
+
+proc `@||->`*[T](fn: proc(): T {.thread.},
+                 callback: proc(val: T){.thread.}): Thread[ThreadFuncArgs[T]] =
+  var thr: Thread[ThreadFuncArgs[T]]
+  var args: ThreadFuncArgs[T]
+  args.a = fn
+  args.b = callback
+  createThread(thr, handleThreadFunc, args)
+  return thr
+
+proc `||->`*[T](fn: proc(): T{.thread.}, callback: proc(val: T){.thread.}) =
+  discard fn @||-> callback
+
+when true:
+  import os
+  proc testFunc(): int {.thread.} =
+    return 1
+  proc callbackFunc(val: int) {.thread.} =
+    echo($(val))
+
+  var thr = (testFunc @||-> callbackFunc)
+  echo("test")
+  joinThread(thr)
+  os.sleep(3000)