Skip to content
Open
Changes from all 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
16 changes: 9 additions & 7 deletions lib/system/channels_builtin.nim
Original file line number Diff line number Diff line change
Expand Up @@ -349,9 +349,11 @@ template lockChannel(q, action): untyped =
releaseSys(q.lock)

proc sendImpl(q: PRawChannel, typ: PNimType, msg: pointer, noBlock: bool): bool =
acquireSys(q.lock)
# Check mask inside the lock to avoid data race
if q.mask == ChannelDeadMask:
releaseSys(q.lock)
sysFatal(DeadThreadDefect, "cannot send message; thread died")
acquireSys(q.lock)
if q.maxItems > 0:
# Wait until count is less than maxItems
if noBlock and q.count >= q.maxItems:
Expand Down Expand Up @@ -430,12 +432,12 @@ proc tryRecv*[TMsg](c: var Channel[TMsg]): tuple[dataAvailable: bool,
## returns `(true, msg)`.
result = default(tuple[dataAvailable: bool, msg: TMsg])
var q = cast[PRawChannel](addr(c))
if q.mask != ChannelDeadMask:
if tryAcquireSys(q.lock):
if q.count > 0:
llRecv(q, addr(result.msg), cast[PNimType](getTypeInfo(result.msg)))
result.dataAvailable = true
releaseSys(q.lock)
if tryAcquireSys(q.lock):
# Check mask inside the lock to avoid data race
if q.mask != ChannelDeadMask and q.count > 0:
llRecv(q, addr(result.msg), cast[PNimType](getTypeInfo(result.msg)))
result.dataAvailable = true
releaseSys(q.lock)

proc peek*[TMsg](c: var Channel[TMsg]): int =
## Returns the current number of messages in the channel `c`.
Expand Down