File: tblocking_channel.nim

package info (click to toggle)
nim 1.6.10-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,165,312 kB
  • sloc: sh: 20,999; ansic: 1,716; makefile: 958; python: 461; sql: 298; asm: 141; xml: 13
file content (39 lines) | stat: -rw-r--r-- 801 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
discard """
output: ""
disabled: "freebsd"
"""
# disabled pending bug #15725
import threadpool, os

var chan: Channel[int]

chan.open(2)
chan.send(1)
chan.send(2)
doAssert(not chan.trySend(3)) # At this point chan is at max capacity

proc receiver() =
    doAssert(chan.recv() == 1)
    doAssert(chan.recv() == 2)
    doAssert(chan.recv() == 3)
    doAssert(chan.recv() == 4)
    doAssert(chan.recv() == 5)

var msgSent = false

proc emitter() =
    chan.send(3)
    msgSent = true

spawn emitter()
# At this point emitter should be stuck in `send`
sleep(50) # Sleep a bit to ensure that it is still stuck
doAssert(not msgSent)

spawn receiver()
sleep(50) # Sleep a bit to let receicer consume the messages
doAssert(msgSent) # Sender should be unblocked

doAssert(chan.trySend(4))
chan.send(5)
sync()