From 0290bc224042ae8fb943dd55f76c046163f5062e Mon Sep 17 00:00:00 2001 From: Anatoly Galiulin Date: Wed, 17 Feb 2016 17:05:20 +0600 Subject: [PATCH 1/3] Added reentrant locks module to stdlib --- lib/core/rlocks.nim | 50 +++++++++++++++++++++++++++++++++++++++++ lib/system/syslocks.nim | 14 +++++++++++- 2 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 lib/core/rlocks.nim diff --git a/lib/core/rlocks.nim b/lib/core/rlocks.nim new file mode 100644 index 0000000000..3316ffcd49 --- /dev/null +++ b/lib/core/rlocks.nim @@ -0,0 +1,50 @@ +# +# +# Nim's Runtime Library +# (c) Copyright 2015 Andreas Rumpf +# +# 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: stmt): stmt = + ## Acquires the given lock and then executes the code + block: + acquire(lock) + defer: + release(lock) + {.locks: [lock].}: + code diff --git a/lib/system/syslocks.nim b/lib/system/syslocks.nim index 7a113b9d40..907a6c609b 100644 --- a/lib/system/syslocks.nim +++ b/lib/system/syslocks.nim @@ -75,12 +75,24 @@ else: type SysLock {.importc: "pthread_mutex_t", pure, final, header: "".} = object + SysLockAttr {.importc: "pthread_mutexattr_t", pure, final + header: "".} = object SysCond {.importc: "pthread_cond_t", pure, final, header: "".} = object + SysLockType = distinct cint - proc initSysLock(L: var SysLock, attr: pointer = nil) {. + proc SysLockType_Reentrant: SysLockType = + {.emit: "`result` = PTHREAD_MUTEX_RECURSIVE;".} + + proc initSysLock(L: var SysLock, attr: ptr SysLockAttr = nil) {. importc: "pthread_mutex_init", header: "", noSideEffect.} + proc initSysLockAttr(a: var SysLockAttr) {. + importc: "pthread_mutexattr_init", header: "", noSideEffect.} + + proc setSysLockType(a: var SysLockAttr, t: SysLockType) {. + importc: "pthread_mutexattr_settype", header: "", noSideEffect.} + proc acquireSys(L: var SysLock) {.noSideEffect, importc: "pthread_mutex_lock", header: "".} proc tryAcquireSysAux(L: var SysLock): cint {.noSideEffect, From c1028b336322ad09cda83ba2cf6e1d9ea2a84bc5 Mon Sep 17 00:00:00 2001 From: Anatoly Galiulin Date: Wed, 17 Feb 2016 20:44:48 +0600 Subject: [PATCH 2/3] Added fixes from Araq's comments --- lib/core/rlocks.nim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/core/rlocks.nim b/lib/core/rlocks.nim index 3316ffcd49..2327153c00 100644 --- a/lib/core/rlocks.nim +++ b/lib/core/rlocks.nim @@ -1,7 +1,7 @@ # # # Nim's Runtime Library -# (c) Copyright 2015 Andreas Rumpf +# (c) Copyright 2015 Anatoly Galiulin # # See the file "copying.txt", included in this # distribution, for details about the copyright. @@ -40,7 +40,7 @@ proc release*(lock: var RLock) = ## Releases the given lock. releaseSys(lock) -template withRLock*(lock: var RLock, code: stmt): stmt = +template withRLock*(lock: var RLock, code: untyped): untyped = ## Acquires the given lock and then executes the code block: acquire(lock) From b0eeacf71e33a0084e2e30636155efa43517def2 Mon Sep 17 00:00:00 2001 From: Anatoly Galiulin Date: Thu, 18 Feb 2016 09:46:34 +0600 Subject: [PATCH 3/3] Added rlocks module to documentation and news.txt --- doc/lib.txt | 3 +++ lib/core/rlocks.nim | 6 +++--- lib/system/syslocks.nim | 2 +- web/news.txt | 6 ++++++ web/website.ini | 2 +- 5 files changed, 14 insertions(+), 5 deletions(-) diff --git a/doc/lib.txt b/doc/lib.txt index 90cf36240f..5ff6de7fd0 100644 --- a/doc/lib.txt +++ b/doc/lib.txt @@ -46,6 +46,9 @@ Core * `locks `_ Locks and condition variables for Nim. +* `rlocks `_ + Reentrant locks for Nim. + * `macros `_ Contains the AST API and documentation of Nim for writing macros. diff --git a/lib/core/rlocks.nim b/lib/core/rlocks.nim index 2327153c00..14f04592be 100644 --- a/lib/core/rlocks.nim +++ b/lib/core/rlocks.nim @@ -1,13 +1,13 @@ # # # Nim's Runtime Library -# (c) Copyright 2015 Anatoly Galiulin +# (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 +## This module contains Nim's support for reentrant locks. include "system/syslocks" @@ -41,7 +41,7 @@ proc release*(lock: var RLock) = releaseSys(lock) template withRLock*(lock: var RLock, code: untyped): untyped = - ## Acquires the given lock and then executes the code + ## Acquires the given lock and then executes the code. block: acquire(lock) defer: diff --git a/lib/system/syslocks.nim b/lib/system/syslocks.nim index 907a6c609b..a91a5e7d48 100644 --- a/lib/system/syslocks.nim +++ b/lib/system/syslocks.nim @@ -7,7 +7,7 @@ # distribution, for details about the copyright. # -## Low level system locks and condition vars. +# Low level system locks and condition vars. when defined(Windows): type diff --git a/web/news.txt b/web/news.txt index d8c591d534..bf665c85c3 100644 --- a/web/news.txt +++ b/web/news.txt @@ -11,6 +11,12 @@ Changes affecting backwards compatibility - ``--out`` and ``--nimcache`` command line arguments are now relative to current directory. Previously they were relative to project directory. +Library Additions +----------------- + +- The rlocks module has been added providing reentrant lock synchronization + primitive + 2016-01-27 Nim in Action is now available! ========================================== diff --git a/web/website.ini b/web/website.ini index 46564d19fd..d1f8a04bf3 100644 --- a/web/website.ini +++ b/web/website.ini @@ -54,7 +54,7 @@ srcdoc2: "pure/collections/tables;pure/collections/sets;pure/collections/lists" srcdoc2: "pure/collections/intsets;pure/collections/queues;pure/encodings" srcdoc2: "pure/events;pure/collections/sequtils;pure/cookies" srcdoc2: "pure/memfiles;pure/subexes;pure/collections/critbits" -srcdoc2: "deprecated/pure/asyncio;deprecated/pure/actors;core/locks;pure/oids;pure/endians;pure/uri" +srcdoc2: "deprecated/pure/asyncio;deprecated/pure/actors;core/locks;core/rlocks;pure/oids;pure/endians;pure/uri" srcdoc2: "pure/nimprof;pure/unittest;packages/docutils/highlite" srcdoc2: "packages/docutils/rst;packages/docutils/rstast" srcdoc2: "packages/docutils/rstgen;pure/logging;pure/options;pure/asyncdispatch;pure/asyncnet"