Skip to content

Commit 3b4e858

Browse files
committed
make into new proc
1 parent da8d503 commit 3b4e858

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

tests/tchannels_ring.nim

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ suite "Ring Buffer Channel Tests":
1111
var chan = newChan[int](BufferSize)
1212
# Fill the buffer
1313
for i in 0..<BufferSize+n:
14-
chan.send(i, overwrite = true)
14+
chan.push(i)
1515

1616
# Receive values - should get BufferSize as first value
1717
var values: seq[int]
@@ -36,8 +36,8 @@ suite "Ring Buffer Channel Tests":
3636

3737
test "Non-blocking ring buffer behavior with size 1":
3838
var chan = newChan[int](1)
39-
chan.send(1, overwrite = true)
40-
chan.send(2, overwrite = true)
39+
chan.push(1)
40+
chan.push(2)
4141
var x: int
4242
check chan.tryRecv(x)
4343
check x == 2

threading/channels.nim

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
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` adds a message or 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
388405
proc 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

Comments
 (0)