diff options
-rw-r--r-- | lib/core/rlocks.nim | 10 | ||||
-rw-r--r-- | tests/stdlib/tuserlocks.nim | 11 |
2 files changed, 14 insertions, 7 deletions
diff --git a/lib/core/rlocks.nim b/lib/core/rlocks.nim index 27a9c5e60..f9224adb8 100644 --- a/lib/core/rlocks.nim +++ b/lib/core/rlocks.nim @@ -47,9 +47,9 @@ proc release*(lock: var RLock) = template withRLock*(lock: var RLock, code: untyped): untyped = ## Acquires the given lock and then executes the code. - block: - acquire(lock) - defer: - release(lock) - {.locks: [lock].}: + acquire(lock) + {.locks: [lock].}: + try: code + finally: + release(lock) diff --git a/tests/stdlib/tuserlocks.nim b/tests/stdlib/tuserlocks.nim index f6eafa05d..9251fa9e2 100644 --- a/tests/stdlib/tuserlocks.nim +++ b/tests/stdlib/tuserlocks.nim @@ -1,8 +1,8 @@ discard """ - cmd: "nim $target --threads:on $options $file" + matrix: "--threads:on" """ -import rlocks +import std/rlocks var r: RLock r.initRLock() @@ -10,4 +10,11 @@ doAssert r.tryAcquire() doAssert r.tryAcquire() r.release() r.release() + +block: + var x = 12 + withRLock r: + inc x + doAssert x == 13 + r.deinitRLock() |