implemented 'guard' annotation
This commit is contained in:
parent
4800acf6ab
commit
7916b1f9aa
21 changed files with 267 additions and 123 deletions
37
tests/parallel/tguard1.nim
Normal file
37
tests/parallel/tguard1.nim
Normal file
|
|
@ -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()
|
||||
27
tests/parallel/tguard2.nim
Normal file
27
tests/parallel/tguard2.nim
Normal file
|
|
@ -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()
|
||||
Loading…
Add table
Add a link
Reference in a new issue