summary refs log tree commit diff stats
path: root/tests/stdlib/tchannels.nim
blob: 33108c50c48480b2f2b6a6e0b826b0c25cc605fa (plain) (blame)
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
discard """
  timeout:  5.0 # but typically < 1s
  disabled: "freebsd"
  matrix: "--gc:arc --threads:on; --gc:arc --threads:on -d:danger"
"""

when true:
  # bug #17380: this was either blocking (without -d:danger) or crashing with SIGSEGV (with -d:danger)
  import std/[channels, isolation]
  const
    N1 = 10
    N2 = 100
  var
    sender: array[N1, Thread[void]]
    receiver: array[5, Thread[void]] 

  var chan = newChannel[seq[string]](N1 * N2) # large enough to not block
  proc sendHandler() =
    chan.send(isolate(@["Hello, Nim"]))
  proc recvHandler() =
    template fn =
      let x = chan.recv()
    fn()

  template benchmark() =
    for t in mitems(sender):
      t.createThread(sendHandler)
    joinThreads(sender)
    for t in mitems(receiver):
      t.createThread(recvHandler)
    joinThreads(receiver)
  for i in 0..<N2:
    benchmark()