replace defer with try ... finally (#17753)
This commit is contained in:
parent
7e94420847
commit
5c5f356347
2 changed files with 14 additions and 7 deletions
|
|
@ -47,9 +47,9 @@ proc release*(lock: var RLock) =
|
||||||
|
|
||||||
template withRLock*(lock: var RLock, code: untyped): untyped =
|
template withRLock*(lock: var RLock, code: untyped): untyped =
|
||||||
## Acquires the given lock and then executes the code.
|
## Acquires the given lock and then executes the code.
|
||||||
block:
|
|
||||||
acquire(lock)
|
acquire(lock)
|
||||||
defer:
|
|
||||||
release(lock)
|
|
||||||
{.locks: [lock].}:
|
{.locks: [lock].}:
|
||||||
|
try:
|
||||||
code
|
code
|
||||||
|
finally:
|
||||||
|
release(lock)
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
discard """
|
discard """
|
||||||
cmd: "nim $target --threads:on $options $file"
|
matrix: "--threads:on"
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import rlocks
|
import std/rlocks
|
||||||
|
|
||||||
var r: RLock
|
var r: RLock
|
||||||
r.initRLock()
|
r.initRLock()
|
||||||
|
|
@ -10,4 +10,11 @@ doAssert r.tryAcquire()
|
||||||
doAssert r.tryAcquire()
|
doAssert r.tryAcquire()
|
||||||
r.release()
|
r.release()
|
||||||
r.release()
|
r.release()
|
||||||
|
|
||||||
|
block:
|
||||||
|
var x = 12
|
||||||
|
withRLock r:
|
||||||
|
inc x
|
||||||
|
doAssert x == 13
|
||||||
|
|
||||||
r.deinitRLock()
|
r.deinitRLock()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue