summary refs log tree commit diff stats
path: root/lib/core/rlocks.nim
diff options
context:
space:
mode:
authorAndreas Rumpf <rumpf_a@web.de>2016-02-19 01:08:00 +0100
committerAndreas Rumpf <rumpf_a@web.de>2016-02-19 01:08:00 +0100
commit608170b9d6a8b63c4c2915f9f1d98f18a4db07a4 (patch)
treeafa9029f04c5f9846fb347010508972cb88f32ab /lib/core/rlocks.nim
parent8ec5c01cae61a727159ddfd0ea7c781d10be9f83 (diff)
parent12b5c0985d7e7449d98a6f80a77369368698b0a3 (diff)
downloadNim-608170b9d6a8b63c4c2915f9f1d98f18a4db07a4.tar.gz
Merge branch 'devel' of github.com:nim-lang/Nim into devel
Diffstat (limited to 'lib/core/rlocks.nim')
-rw-r--r--lib/core/rlocks.nim50
1 files changed, 50 insertions, 0 deletions
diff --git a/lib/core/rlocks.nim b/lib/core/rlocks.nim
new file mode 100644
index 000000000..14f04592b
--- /dev/null
+++ b/lib/core/rlocks.nim
@@ -0,0 +1,50 @@
+#
+#
+#            Nim's Runtime Library
+#        (c) Copyright 2016 Anatoly Galiulin
+#
+#    See the file "copying.txt", included in this
+#    distribution, for details about the copyright.
+#
+
+## This module contains Nim's support for reentrant locks.
+
+include "system/syslocks"
+
+type
+  RLock* = SysLock ## Nim lock, re-entrant
+
+proc initRLock*(lock: var RLock) {.inline.} =
+  ## Initializes the given lock.
+  when defined(posix):
+    var a: SysLockAttr
+    initSysLockAttr(a)
+    setSysLockType(a, SysLockType_Reentrant())
+    initSysLock(lock, a.addr)
+  else:
+    initSysLock(lock)
+
+proc deinitRLock*(lock: var RLock) {.inline.} =
+  ## Frees the resources associated with the lock.
+  deinitSys(lock)
+
+proc tryAcquire*(lock: var RLock): bool =
+  ## Tries to acquire the given lock. Returns `true` on success.
+  result = tryAcquireSys(lock)
+
+proc acquire*(lock: var RLock) =
+  ## Acquires the given lock.
+  acquireSys(lock)
+
+proc release*(lock: var RLock) =
+  ## Releases the given lock.
+  releaseSys(lock)
+
+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].}:
+      code