summary refs log tree commit diff stats
path: root/tests/alloc/tmembug2.nim
blob: 01bce6f141a3cf7241a6249278e2e672d1f76c36 (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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
discard """
  disabled: "true"
"""

import std / [atomics, strutils, sequtils, isolation]

import threading / channels

type
  BackendMessage* = object
    field*: seq[int]

const MESSAGE_COUNT = 100

var
  chan1 = newChan[BackendMessage](MESSAGE_COUNT*2)
  chan2 = newChan[BackendMessage](MESSAGE_COUNT*2)

#chan1.open()
#chan2.open()

proc routeMessage*(msg: BackendMessage) =
  var m = isolate(msg)
  discard chan2.trySend(m)

var
  thr: Thread[void]
  stopToken: Atomic[bool]

proc recvMsg() =
  while not stopToken.load(moRelaxed):
    var resp: BackendMessage
    if chan1.tryRecv(resp):
      #if resp.dataAvailable:
      routeMessage(resp)
      echo "child consumes ", formatSize getOccupiedMem()

createThread[void](thr, recvMsg)

proc main() =
  let msg: BackendMessage = BackendMessage(field: (0..5).toSeq())
  for j in 0..100:
    echo "New iteration"

    for _ in 1..MESSAGE_COUNT:
      chan1.send(msg)
    echo "After sending"

    var counter = 0
    while counter < MESSAGE_COUNT:
      let resp = recv(chan2)
      counter.inc
    echo "After receiving ", formatSize getOccupiedMem()

  stopToken.store true, moRelaxed
  joinThreads(thr)

main()