summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorDominik Picheta <dominikpicheta@googlemail.com>2016-03-23 12:48:38 +0000
committerDominik Picheta <dominikpicheta@googlemail.com>2016-03-23 12:48:38 +0000
commit418985d3f06d74452eff7144dede5fb280b0e1a1 (patch)
tree5572dbdbe8ec5ea58e03eb91ce4734ba0c3da302 /lib
parent2ffbd1d81edb70d1b75c1a44a52dc0c3df09fab3 (diff)
parent34401a3639d016de22050ac3703ce0daa78e18e7 (diff)
downloadNim-418985d3f06d74452eff7144dede5fb280b0e1a1.tar.gz
Merge pull request #3981 from nim-lang/locks-lock-template
Implement a `lock` template in `locks` module.
Diffstat (limited to 'lib')
-rw-r--r--lib/core/locks.nim10
1 files changed, 10 insertions, 0 deletions
diff --git a/lib/core/locks.nim b/lib/core/locks.nim
index f1a74876e..66e0ab520 100644
--- a/lib/core/locks.nim
+++ b/lib/core/locks.nim
@@ -54,3 +54,13 @@ proc wait*(cond: var Cond, lock: var Lock) {.inline.} =
 proc signal*(cond: var Cond) {.inline.} =
   ## sends a signal to the condition variable `cond`.
   signalSysCond(cond)
+
+template withLock*(a: Lock, body: untyped) =
+  ## Acquires the given lock, executes the statements in body and
+  ## releases the lock after the statements finish executing.
+  a.acquire()
+  {.locks: [a].}:
+    try:
+      body
+    finally:
+      a.release()
\ No newline at end of file