summary refs log tree commit diff stats
path: root/lib/std
diff options
context:
space:
mode:
authorflywind <xzsflywind@gmail.com>2021-03-17 17:08:54 +0800
committerGitHub <noreply@github.com>2021-03-17 10:08:54 +0100
commite9b5543bd503ffebbd7fc4b5328d8a9c69419908 (patch)
tree3f04a474ec18028f39ef199fc4b6c04f9f90c647 /lib/std
parentbebf2ce24a43bef4cde5c90c4010631a1e4a7927 (diff)
downloadNim-e9b5543bd503ffebbd7fc4b5328d8a9c69419908.tar.gz
[std/channels]fix recv leaks(Part One) (#17394)
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/channels.nim29
1 files changed, 6 insertions, 23 deletions
diff --git a/lib/std/channels.nim b/lib/std/channels.nim
index 383a58dda..69f6a6ffb 100644
--- a/lib/std/channels.nim
+++ b/lib/std/channels.nim
@@ -49,8 +49,7 @@ runnableExamples("--threads:on --gc:orc"):
   createThread(worker1, firstWorker)
 
   # Block until the message arrives, then print it out.
-  var dest = ""
-  chan.recv(dest)
+  let dest = chan.recv()
   assert dest == "Hello World!"
 
   # Wait for the thread to exit before moving on to the next example.
@@ -453,21 +452,10 @@ proc `=`*[T](dest: var Channel[T], src: Channel[T]) =
     `=destroy`(dest)
   dest.d = src.d
 
-proc channelSend[T](chan: Channel[T], data: sink T, size: int, nonBlocking: bool): bool {.inline.} =
-  ## Send item to the channel (FIFO queue)
-  ## (Insert at last)
-  result = sendMpmc(chan.d, data.unsafeAddr, size, nonBlocking)
-  wasMoved(data)
-
-proc channelReceive[T](chan: Channel[T], data: ptr T, size: int, nonBlocking: bool): bool {.inline.} =
-  ## Receive an item from the channel
-  ## (Remove the first item)
-  recvMpmc(chan.d, data, size, nonBlocking)
-
 func trySend*[T](c: Channel[T], src: var Isolated[T]): bool {.inline.} =
   ## Sends item to the channel(non blocking).
   var data = src.extract
-  result = channelSend(c, data, sizeof(data), true)
+  result = sendMpmc(c.d, data.addr, sizeof(T), true)
   if result:
     wasMoved(data)
 
@@ -477,26 +465,21 @@ template trySend*[T](c: Channel[T], src: T): bool =
 
 func tryRecv*[T](c: Channel[T], dst: var T): bool {.inline.} =
   ## Receives item from the channel(non blocking).
-  channelReceive(c, dst.addr, sizeof(dst), true)
+  recvMpmc(c.d, dst.addr, sizeof(T), true)
 
 func send*[T](c: Channel[T], src: sink Isolated[T]) {.inline.} =
   ## Sends item to the channel(blocking).
   var data = src.extract
-  discard channelSend(c, data, sizeof(data), false)
+  discard sendMpmc(c.d, data.addr, sizeof(T), false)
   wasMoved(data)
 
 template send*[T](c: var Channel[T]; src: T) =
   ## Helper templates for `send`.
   send(c, isolate(src))
 
-func recv*[T](c: Channel[T], dst: var T) {.inline.} =
+func recv*[T](c: Channel[T]): T {.inline.} =
   ## Receives item from the channel(blocking).
-  discard channelReceive(c, dst.addr, sizeof(dst), false)
-
-func recvIso*[T](c: Channel[T]): Isolated[T] {.inline.} =
-  var dst: T
-  discard channelReceive(c, dst.addr, sizeof(dst), false)
-  result = isolate(dst)
+  discard recvMpmc(c.d, result.addr, sizeof(result), false)
 
 func open*[T](c: Channel[T]): bool {.inline.} =
   result = c.d.channelOpenMpmc()