Skip to content

Commit

Permalink
removed tryRecv with Option[T]
Browse files Browse the repository at this point in the history
  • Loading branch information
darkestpigeon committed Oct 17, 2024
1 parent 16f5c8d commit 1410787
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions threading/channels.nim
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ runnableExamples("--threads:on --gc:orc"):
when not (defined(gcArc) or defined(gcOrc) or defined(gcAtomicArc) or defined(nimdoc)):
{.error: "This module requires one of --mm:arc / --mm:atomicArc / --mm:orc compilation flags".}

import std/[locks, isolation, atomics, options]
import std/[locks, isolation, atomics]

# Channel
# ------------------------------------------------------------------------------
Expand Down Expand Up @@ -256,14 +256,15 @@ type
Chan*[T] = object ## Typed channel
d: ChannelRaw

proc tryRecv*[T](c: Chan[T]): Option[T] {.inline, raises: [].}
proc tryRecv*[T](c: Chan[T], dst: var T): bool {.inline, raises: [].}

template frees(c) =
template frees[T](c: Chan[T]) =
if c.d != nil:
# this `fetchSub` returns current val then subs
# so count == 0 means we're the last
while true:
if c.tryRecv().isNone:
var msg: T
if not c.tryRecv(msg):
break
if c.d.atomicCounter.fetchSub(1, moAcquireRelease) == 0:
freeChannel(c.d)
Expand Down Expand Up @@ -348,22 +349,6 @@ proc tryRecv*[T](c: Chan[T], dst: var T): bool {.inline.} =
## Returns `false` and does not change `dist` if no message was received.
channelReceive(c.d, dst.addr, sizeof(T), false)

proc tryRecv*[T](c: Chan[T]): Option[T] {.inline.} =
## Tries to receive a message from the channel `c`.
##
## Doesn't block waiting for messages in the channel to become available.
## Instead returns after an attempt to receive a message was made.
##
## .. warning:: In high-concurrency situations, consider using an exponential
## backoff strategy to reduce contention and improve the success rate of
## operations.
var dst: T
let dataAvailable = tryRecv(c, dst)
if dataAvailable:
result = some(dst)
else:
result = none(T)

proc send*[T](c: Chan[T], src: sink Isolated[T]) {.inline.} =
## Sends the message `src` to the channel `c`.
## This blocks the sending thread until `src` was successfully sent.
Expand Down

0 comments on commit 1410787

Please sign in to comment.