2828# # the underlying resources and synchronization. It has to be initialized using
2929# # the `newChan` proc. Sending and receiving operations are provided by the
3030# # blocking `send` and `recv` procs, and non-blocking `trySend` and `tryRecv`
31- # # procs. Send operations add messages to the channel, receiving operations
32- # # remove them.
31+ # # procs. For ring buffer behavior, use the `push` proc rather than `send`.
32+ # # Send operations add messages to the channel, receiving operations remove them,
33+ # # while `push` overwrites the oldest message if the channel is full.
3334# #
34- # # Normally, the `send` proc will block if the channel is full. If the `overwrite`
35- # # parameter is set to `true`, the oldest message will be overwritten instead of blocking.
3635# #
3736# # See also:
3837# # * [std/isolation](https://nim-lang.org/docs/isolation.html)
@@ -102,8 +101,8 @@ runnableExamples("--threads:on --gc:orc"):
102101
103102 block example_non_blocking_overwrite:
104103 var chanRingBuffer = newChan [string ](elements = 1 )
105- chanRingBuffer.send (" Hello" )
106- chanRingBuffer.send (" World" , overwrite = true )
104+ chanRingBuffer.push (" Hello" )
105+ chanRingBuffer.push (" World" )
107106 var msg = " "
108107 assert chanRingBuffer.tryRecv (msg)
109108 assert msg == " World"
@@ -367,7 +366,7 @@ proc tryRecv*[T](c: Chan[T], dst: var T): bool {.inline.} =
367366 # # Returns `false` and does not change `dist` if no message was received.
368367 channelReceive (c.d, dst.addr , sizeof (T), false )
369368
370- proc send * [T](c: Chan [T], src: sink Isolated [T], overwrite = false ) {.inline .} =
369+ proc send * [T](c: Chan [T], src: sink Isolated [T]) {.inline .} =
371370 # # Sends the message `src` to the channel `c`.
372371 # # This blocks the sending thread until `src` was successfully sent.
373372 # #
@@ -377,13 +376,31 @@ proc send*[T](c: Chan[T], src: sink Isolated[T], overwrite = false) {.inline.} =
377376 # # messages from the channel are removed.
378377 when defined (gcOrc) and defined (nimSafeOrcSend):
379378 GC_runOrc ()
380- discard channelSend (c.d, src.addr , sizeof (T), true , overwrite )
379+ discard channelSend (c.d, src.addr , sizeof (T), true , false )
381380 wasMoved (src)
382381
383- template send * [T](c: Chan [T]; src: T, overwrite = false ) =
382+ template send * [T](c: Chan [T]; src: T) =
384383 # # Helper template for `send`.
385384 mixin isolate
386- send (c, isolate (src), overwrite)
385+ send (c, isolate (src))
386+
387+ proc push * [T](c: Chan [T], src: sink Isolated [T]) {.inline .} =
388+ # # Sends the message `src` to the channel `c`.
389+ # # This blocks the sending thread until `src` was successfully sent.
390+ # #
391+ # # The memory of `src` is moved, not copied.
392+ # #
393+ # # If the channel is already full with messages this will block the thread until
394+ # # messages from the channel are removed.
395+ when defined (gcOrc) and defined (nimSafeOrcSend):
396+ GC_runOrc ()
397+ discard channelSend (c.d, src.addr , sizeof (T), true , overwrite= true )
398+ wasMoved (src)
399+
400+ template push * [T](c: Chan [T]; src: T) =
401+ # # Helper template for `push`.
402+ mixin isolate
403+ push (c, isolate (src))
387404
388405proc recv * [T](c: Chan [T], dst: var T) {.inline .} =
389406 # # Receives a message from the channel `c` and fill `dst` with its value.
0 commit comments