28
28
# # the underlying resources and synchronization. It has to be initialized using
29
29
# # the `newChan` proc. Sending and receiving operations are provided by the
30
30
# # 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` adds a message or overwrites the oldest message if the channel is full.
33
34
# #
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.
36
35
# #
37
36
# # See also:
38
37
# # * [std/isolation](https://nim-lang.org/docs/isolation.html)
@@ -102,8 +101,8 @@ runnableExamples("--threads:on --gc:orc"):
102
101
103
102
block example_non_blocking_overwrite:
104
103
var chanRingBuffer = newChan[string ](elements = 1 )
105
- chanRingBuffer.send (" Hello" )
106
- chanRingBuffer.send (" World" , overwrite = true )
104
+ chanRingBuffer.push (" Hello" )
105
+ chanRingBuffer.push (" World" )
107
106
var msg = " "
108
107
assert chanRingBuffer.tryRecv(msg)
109
108
assert msg == " World"
@@ -367,7 +366,7 @@ proc tryRecv*[T](c: Chan[T], dst: var T): bool {.inline.} =
367
366
## Returns `false` and does not change `dist` if no message was received.
368
367
channelReceive(c.d, dst.addr, sizeof(T), false)
369
368
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.} =
371
370
## Sends the message `src` to the channel `c`.
372
371
## This blocks the sending thread until `src` was successfully sent.
373
372
##
@@ -377,13 +376,31 @@ proc send*[T](c: Chan[T], src: sink Isolated[T], overwrite = false) {.inline.} =
377
376
## messages from the channel are removed.
378
377
when defined(gcOrc) and defined(nimSafeOrcSend):
379
378
GC_runOrc()
380
- discard channelSend(c.d, src.addr, sizeof(T), true, overwrite )
379
+ discard channelSend(c.d, src.addr, sizeof(T), true, false )
381
380
wasMoved(src)
382
381
383
- template send*[T](c: Chan[T]; src: T, overwrite = false ) =
382
+ template send*[T](c: Chan[T]; src: T) =
384
383
## Helper template for `send`.
385
384
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))
387
404
388
405
proc recv*[T](c: Chan[T], dst: var T) {.inline.} =
389
406
## Receives a message from the channel `c` and fill `dst` with its value.
0 commit comments