summary refs log tree commit diff stats
path: root/lib/core/rlocks.nim
blob: 4710d6cf1307fd530d07a061b3df86a7d71e76a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#
#
#            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.

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
ptyException() def bottom(self): try: return self.history[0] except IndexError: raise HistoryEmptyException() def back(self): if len(self.history) > 1: self.history_forward.appendleft( self.history.pop() ) return self.current() def move(self, n): if n > 0: return self.forward() if n < 0: return self.back() def __iter__(self): return self.history.__iter__() def next(self): return self.history.next() def forward(self): if len(self.history_forward) > 0: self.history.append( self.history_forward.popleft() ) return self.current() def fast_forward(self): if self.history_forward: self.history.extend(self.history_forward) self.history_forward.clear()