rlocks.nim 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #
  2. #
  3. # Nim's Runtime Library
  4. # (c) Copyright 2016 Anatoly Galiulin
  5. #
  6. # See the file "copying.txt", included in this
  7. # distribution, for details about the copyright.
  8. #
  9. ## This module contains Nim's support for reentrant locks.
  10. const insideRLocksModule = true
  11. include "system/syslocks"
  12. type
  13. RLock* = SysLock ## Nim lock, re-entrant
  14. proc initRLock*(lock: var RLock) {.inline.} =
  15. ## Initializes the given lock.
  16. when defined(posix):
  17. var a: SysLockAttr
  18. initSysLockAttr(a)
  19. setSysLockType(a, SysLockType_Reentrant())
  20. initSysLock(lock, a.addr)
  21. else:
  22. initSysLock(lock)
  23. proc deinitRLock*(lock: var RLock) {.inline.} =
  24. ## Frees the resources associated with the lock.
  25. deinitSys(lock)
  26. proc tryAcquire*(lock: var RLock): bool =
  27. ## Tries to acquire the given lock. Returns `true` on success.
  28. result = tryAcquireSys(lock)
  29. proc acquire*(lock: var RLock) =
  30. ## Acquires the given lock.
  31. acquireSys(lock)
  32. proc release*(lock: var RLock) =
  33. ## Releases the given lock.
  34. releaseSys(lock)
  35. template withRLock*(lock: var RLock, code: untyped): untyped =
  36. ## Acquires the given lock and then executes the code.
  37. block:
  38. acquire(lock)
  39. defer:
  40. release(lock)
  41. {.locks: [lock].}:
  42. code