summary refs log tree commit diff stats
path: root/tests
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2014-09-21 18:39:00 +0200
committerAraq <rumpf_a@web.de>2014-09-21 18:39:00 +0200
commit7916b1f9aa93fca368afda8b18396230ac7f338c (patch)
tree04072aeee4ae211b91c088a7267455b82daf9787 /tests
parent4800acf6ab92799316b270c991b6e8c01454608f (diff)
downloadNim-7916b1f9aa93fca368afda8b18396230ac7f338c.tar.gz
implemented 'guard' annotation
Diffstat (limited to 'tests')
-rw-r--r--tests/parallel/tguard1.nim37
-rw-r--r--tests/parallel/tguard2.nim27
2 files changed, 64 insertions, 0 deletions
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()