diff options
Diffstat (limited to 'tests/parallel/tguard1.nim')
-rw-r--r-- | tests/parallel/tguard1.nim | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/tests/parallel/tguard1.nim b/tests/parallel/tguard1.nim new file mode 100644 index 000000000..f4c92319b --- /dev/null +++ b/tests/parallel/tguard1.nim @@ -0,0 +1,41 @@ +discard """ +output: "90" +""" + + +when false: + template lock(a, b: ptr Lock; body: stmt) = + if cast[int](a) < cast[int](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): untyped = + {.locks: [L].}: + x + +proc main = + {.locks: [c.L].}: + inc c.i + discard + echo(atomicRead(c.L, c.i)) + +main() |