diff options
author | Andreas Rumpf <rumpf_a@web.de> | 2023-05-14 16:58:28 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-14 16:58:28 +0200 |
commit | f4a9b258c34a83efb74d9dea6853880e47f45b06 (patch) | |
tree | 3dc2c3fcf0e3fc3325a69614056bb7772675a6ab /tests | |
parent | 0ece98620f8d9d7b874262c75fa148970626d44d (diff) | |
download | Nim-f4a9b258c34a83efb74d9dea6853880e47f45b06.tar.gz |
isolation spec update; WIP (#21843)
* isolation spec update; WIP * wip * docs update, WIP * progress * Update doc/manual.md
Diffstat (limited to 'tests')
-rw-r--r-- | tests/isolate/tisolated_lock.nim | 67 |
1 files changed, 67 insertions, 0 deletions
diff --git a/tests/isolate/tisolated_lock.nim b/tests/isolate/tisolated_lock.nim new file mode 100644 index 000000000..312abf0f6 --- /dev/null +++ b/tests/isolate/tisolated_lock.nim @@ -0,0 +1,67 @@ +discard """ + cmd: "nim $target --threads:on $options $file" + action: "compile" +""" + +import std / [os, locks, atomics, isolation] + +type + MyList {.acyclic.} = ref object + data: string + next: Isolated[MyList] + +template withMyLock*(a: Lock, body: untyped) = + acquire(a) + {.gcsafe.}: + try: + body + finally: + release(a) + +var head: Isolated[MyList] +var headL: Lock + +var shouldStop: Atomic[bool] + +initLock headL + +proc send(x: sink string) = + withMyLock headL: + head = isolate MyList(data: x, next: move head) + +proc worker() {.thread.} = + var workItem = MyList(nil) + var echoed = 0 + while true: + withMyLock headL: + var h = extract head + if h != nil: + workItem = h + # workitem is now isolated: + head = move h.next + else: + workItem = nil + # workItem is isolated, so we can access it outside + # the lock: + if workItem.isNil: + if shouldStop.load: + break + else: + # give producer time to breath: + os.sleep 30 + else: + if echoed < 100: + echo workItem.data + inc echoed + +var thr: Thread[void] +createThread(thr, worker) + +send "abc" +send "def" +for i in 0 ..< 10_000: + send "xzy" + send "zzz" +shouldStop.store true + +joinThread(thr) |