Files
Nim/lib/core/rlocks.nim
shirleyquirk 4ef255b69d fix rlock compilation failure (#15584)
* change SysLockType_Reentrant

fix edge case where using SysLockType_Reentrant doesn't trigger an #include pthread.h

* syslocktype_reentrant now a var
* remove nodecl to remove empty system_syslocks.c
* let is better than var.

in reality SysLockType = enum, maybe that would be a better fix
2020-10-15 12:54:01 +02:00

56 lines
1.3 KiB
Nim

#
#
# 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.
when not compileOption("threads") and not defined(nimdoc):
{.error: "Rlocks requires --threads:on option.".}
const insideRLocksModule = true
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