diff options
Diffstat (limited to 'lib/core')
-rw-r--r-- | lib/core/macros.nim | 2 | ||||
-rw-r--r-- | lib/core/rlocks.nim | 50 |
2 files changed, 51 insertions, 1 deletions
diff --git a/lib/core/macros.nim b/lib/core/macros.nim index 872d4848d..eda793620 100644 --- a/lib/core/macros.nim +++ b/lib/core/macros.nim @@ -601,7 +601,7 @@ proc last*(node: NimNode): NimNode {.compileTime.} = node[<node.len] const - RoutineNodes* = {nnkProcDef, nnkMethodDef, nnkDo, nnkLambda, nnkIteratorDef} + RoutineNodes* = {nnkProcDef, nnkMethodDef, nnkDo, nnkLambda, nnkIteratorDef, nnkTemplateDef, nnkConverterDef} AtomicNodes* = {nnkNone..nnkNilLit} CallNodes* = {nnkCall, nnkInfix, nnkPrefix, nnkPostfix, nnkCommand, nnkCallStrLit, nnkHiddenCallConv} 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 |