Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

locks and rlocks use destructors #18640

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 47 additions & 17 deletions lib/core/locks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ when not compileOption("threads") and not defined(nimdoc):
{.error: "Locks requires --threads:on option.".}

const insideRLocksModule = false
const useOrcArc = defined(gcArc) or defined(gcOrc)

include "system/syslocks"

type
Lock* = SysLock ## Nim lock; whether this is re-entrant
## or not is unspecified!
Cond* = SysCond ## Nim condition variable
Lock* = object
## Nim lock; whether this is re-entrant or not is unspecified!
lock: SysLock
Cond* = object
## Nim condition variable.
cond: SysCond

{.push stackTrace: off.}

Expand All @@ -36,47 +41,72 @@ proc `$`*(lock: Lock): string =
proc initLock*(lock: var Lock) {.inline.} =
## Initializes the given lock.
when not defined(js):
initSysLock(lock)
initSysLock(lock.lock)

when useOrcArc:
proc `=sink`*(x: var Lock, y: Lock) {.error.}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


proc `=copy`*(x: var Lock, y: Lock) {.error.}

proc `=destroy`*(lock: var Lock) {.inline.} =
deinitSys(lock.lock)

proc deinitLock*(lock: var Lock) {.inline.} =
## Frees the resources associated with the lock.
deinitSys(lock)
proc deinitLock*(lock: var Lock) {.inline,
deprecated: "`deinitLock` is not needed anymore in ARC/ORC (it is a no-op now); `=destroy` is already defined for `Lock`".} =
discard
else:
proc deinitLock*(lock: var Lock) {.inline.} =
## Frees the resources associated with the lock.
deinitSys(lock.lock)

proc tryAcquire*(lock: var Lock): bool {.inline.} =
## Tries to acquire the given lock. Returns `true` on success.
result = tryAcquireSys(lock)
result = tryAcquireSys(lock.lock)

proc acquire*(lock: var Lock) {.inline.} =
## Acquires the given lock.
when not defined(js):
acquireSys(lock)
acquireSys(lock.lock)

proc release*(lock: var Lock) {.inline.} =
## Releases the given lock.
when not defined(js):
releaseSys(lock)
releaseSys(lock.lock)


proc initCond*(cond: var Cond) {.inline.} =
## Initializes the given condition variable.
initSysCond(cond)
initSysCond(cond.cond)


when useOrcArc:
proc `=sink`*(x: var Cond, y: Cond) {.error.}

proc `=copy`*(x: var Cond, y: Cond) {.error.}

proc `=destroy`*(cond: var Cond) {.inline.} =
deinitSysCond(cond.cond)

proc deinitCond*(cond: var Cond) {.inline.} =
## Frees the resources associated with the condition variable.
deinitSysCond(cond)
proc deinitCond*(cond: var Cond) {.inline,
deprecated: "`deinitCond` is not needed anymore in ARC/ORC (it is a no-op now); `=destroy` is already defined for `Cond`".} =
discard
else:
proc deinitCond*(cond: var Cond) {.inline.} =
## Frees the resources associated with the condition variable.
deinitSysCond(cond.cond)

proc wait*(cond: var Cond, lock: var Lock) {.inline.} =
## Waits on the condition variable `cond`.
waitSysCond(cond, lock)
waitSysCond(cond.cond, lock.lock)

proc signal*(cond: var Cond) {.inline.} =
## Sends a signal to the condition variable `cond`.
signalSysCond(cond)
signalSysCond(cond.cond)

proc broadcast*(cond: var Cond) {.inline.} =
## Unblocks all threads currently blocked on the
## specified condition variable `cond`.
broadcastSysCond(cond)
broadcastSysCond(cond.cond)

template withLock*(a: Lock, body: untyped) =
## Acquires the given lock, executes the statements in body and
Expand Down
34 changes: 25 additions & 9 deletions lib/core/rlocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,52 @@ when not compileOption("threads") and not defined(nimdoc):
{.error: "Rlocks requires --threads:on option.".}

const insideRLocksModule = true
const useOrcArc = defined(gcArc) or defined(gcOrc)

include "system/syslocks"

type
RLock* = SysLock ## Nim lock, re-entrant
RLock* = object
## Nim lock, re-entrant.
lock: SysLock

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)
initSysLock(lock.lock, a.addr)
else:
initSysLock(lock)
initSysLock(lock.lock)

when useOrcArc:
proc `=sink`*(x: var RLock, y: RLock) {.error.}

proc `=copy`*(x: var RLock, y: RLock) {.error.}

proc `=destroy`*(lock: var RLock) {.inline.} =
deinitSys(lock.lock)

proc deinitRLock*(lock: var RLock) {.inline.} =
## Frees the resources associated with the lock.
deinitSys(lock)
proc deinitRLock*(lock: var RLock) {.inline,
deprecated: "`deinitRLock` is not needed anymore in ARC/ORC (it is a no-op now); `=destroy` is already defined for `RLock`".} =
discard
else:
proc deinitRLock*(lock: var RLock) {.inline.} =
## Frees the resources associated with the lock.
deinitSys(lock.lock)

proc tryAcquire*(lock: var RLock): bool {.inline.} =
## Tries to acquire the given lock. Returns `true` on success.
result = tryAcquireSys(lock)
result = tryAcquireSys(lock.lock)

proc acquire*(lock: var RLock) {.inline.} =
## Acquires the given lock.
acquireSys(lock)
acquireSys(lock.lock)

proc release*(lock: var RLock) {.inline.} =
## Releases the given lock.
releaseSys(lock)
releaseSys(lock.lock)

template withRLock*(lock: RLock, code: untyped) =
## Acquires the given lock and then executes the code.
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/trlocks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ discard """
# Disallow joining to ensure it can compile in isolation.
# See #15584
joinable: false
cmd: "nim $target --threads:on $options $file"
matrix: "--threads:on --gc:refc; --threads:on --gc:arc"
ringabout marked this conversation as resolved.
Show resolved Hide resolved
"""

# bugfix #15584
Expand Down
2 changes: 1 addition & 1 deletion tests/stdlib/tuserlocks.nim
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
discard """
matrix: "--threads:on"
matrix: "--threads:on --gc:refc; --threads:on --gc:arc"
"""

import std/rlocks
Expand Down
1 change: 1 addition & 0 deletions tests/threads/treusetvar.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
discard """
matrix: "--gc:refc; --gc:arc"
outputsub: "65"
"""

Expand Down