|
| 1 | +discard """ |
| 2 | +output:''' |
| 3 | +Thread 1: iteration 0 |
| 4 | +Thread 2: iteration 0 |
| 5 | +Thread 1: iteration 1 |
| 6 | +Thread 2: iteration 1 |
| 7 | +Thread 1: iteration 2 |
| 8 | +Thread 2: iteration 2 |
| 9 | +Thread 1: iteration 3 |
| 10 | +Thread 2: iteration 3 |
| 11 | +Thread 1: iteration 4 |
| 12 | +Thread 2: iteration 4 |
| 13 | +Thread 1: iteration 5 |
| 14 | +Thread 2: iteration 5 |
| 15 | +Thread 1: iteration 6 |
| 16 | +Thread 2: iteration 6 |
| 17 | +Thread 1: iteration 7 |
| 18 | +Thread 2: iteration 7 |
| 19 | +Thread 1: iteration 8 |
| 20 | +Thread 2: iteration 8 |
| 21 | +Thread 1: iteration 9 |
| 22 | +Thread 2: iteration 9 |
| 23 | +Thread 1: iteration 10 |
| 24 | +Thread 2: iteration 10 |
| 25 | +Thread 1: iteration 11 |
| 26 | +Thread 2: iteration 11 |
| 27 | +Thread 1: iteration 12 |
| 28 | +Thread 2: iteration 12 |
| 29 | +Thread 1: iteration 13 |
| 30 | +Thread 2: iteration 13 |
| 31 | +Thread 1: iteration 14 |
| 32 | +Thread 2: iteration 14 |
| 33 | +Thread 1: iteration 15 |
| 34 | +Thread 2: iteration 15 |
| 35 | +Thread 1: iteration 16 |
| 36 | +Thread 2: iteration 16 |
| 37 | +Thread 1: iteration 17 |
| 38 | +Thread 2: iteration 17 |
| 39 | +Thread 1: iteration 18 |
| 40 | +Thread 2: iteration 18 |
| 41 | +Thread 1: iteration 19 |
| 42 | +Thread 2: iteration 19 |
| 43 | +Thread 1: iteration 20 |
| 44 | +Thread 2: iteration 20 |
| 45 | +Thread 1: iteration 21 |
| 46 | +Thread 2: iteration 21 |
| 47 | +Thread 1: iteration 22 |
| 48 | +Thread 2: iteration 22 |
| 49 | +Thread 1: iteration 23 |
| 50 | +Thread 2: iteration 23 |
| 51 | +Thread 1: iteration 24 |
| 52 | +Thread 2: iteration 24 |
| 53 | +Thread 1: iteration 25 |
| 54 | +Thread 2: iteration 25 |
| 55 | +Thread 1: iteration 26 |
| 56 | +Thread 2: iteration 26 |
| 57 | +Thread 1: iteration 27 |
| 58 | +Thread 2: iteration 27 |
| 59 | +Thread 1: iteration 28 |
| 60 | +Thread 2: iteration 28 |
| 61 | +Thread 1: iteration 29 |
| 62 | +Thread 2: iteration 29 |
| 63 | +Thread 1: iteration 30 |
| 64 | +Thread 2: iteration 30 |
| 65 | +Thread 1: iteration 31 |
| 66 | +Thread 2: iteration 31 |
| 67 | +Thread 1: iteration 32 |
| 68 | +Thread 2: iteration 32 |
| 69 | +Thread 1: iteration 33 |
| 70 | +Thread 2: iteration 33 |
| 71 | +Thread 1: iteration 34 |
| 72 | +Thread 2: iteration 34 |
| 73 | +Thread 1: iteration 35 |
| 74 | +Thread 2: iteration 35 |
| 75 | +Thread 1: iteration 36 |
| 76 | +Thread 2: iteration 36 |
| 77 | +Thread 1: iteration 37 |
| 78 | +Thread 2: iteration 37 |
| 79 | +Thread 1: iteration 38 |
| 80 | +Thread 2: iteration 38 |
| 81 | +Thread 1: iteration 39 |
| 82 | +Thread 2: iteration 39 |
| 83 | +Thread 1: iteration 40 |
| 84 | +Thread 2: iteration 40 |
| 85 | +Thread 1: iteration 41 |
| 86 | +Thread 2: iteration 41 |
| 87 | +Thread 1: iteration 42 |
| 88 | +Thread 2: iteration 42 |
| 89 | +Thread 1: iteration 43 |
| 90 | +Thread 2: iteration 43 |
| 91 | +Thread 1: iteration 44 |
| 92 | +Thread 2: iteration 44 |
| 93 | +Thread 1: iteration 45 |
| 94 | +Thread 2: iteration 45 |
| 95 | +Thread 1: iteration 46 |
| 96 | +Thread 2: iteration 46 |
| 97 | +Thread 1: iteration 47 |
| 98 | +Thread 2: iteration 47 |
| 99 | +Thread 1: iteration 48 |
| 100 | +Thread 2: iteration 48 |
| 101 | +Thread 1: iteration 49 |
| 102 | +Thread 2: iteration 49 |
| 103 | +Thread 1: iteration 50 |
| 104 | +Thread 2: iteration 50 |
| 105 | +''' |
| 106 | +""" |
| 107 | + |
| 108 | +import os, asyncdispatch |
| 109 | + |
| 110 | +type |
| 111 | + ThreadArg = object |
| 112 | + event1: AsyncEvent |
| 113 | + event2: AsyncEvent |
| 114 | + |
| 115 | +when not(compileOption("threads")): |
| 116 | + {.fatal: "Please, compile this program with the --threads:on option!".} |
| 117 | + |
| 118 | +proc wait(event: AsyncEvent): Future[void] = |
| 119 | + var retFuture = newFuture[void]("AsyncEvent.wait") |
| 120 | + proc continuation(fd: AsyncFD): bool {.gcsafe.} = |
| 121 | + if not retFuture.finished: |
| 122 | + retFuture.complete() |
| 123 | + result = true |
| 124 | + addEvent(event, continuation) |
| 125 | + return retFuture |
| 126 | + |
| 127 | +proc asyncProc1(args: ThreadArg) {.async.} = |
| 128 | + for i in 0 .. 50: |
| 129 | + # why 50 iterations? It's arbitrary. We can't run forever, but we want to run long enough |
| 130 | + # to sanity check against a race condition or deadlock. |
| 131 | + await args.event1.wait() |
| 132 | + args.event2.trigger() |
| 133 | + # Why echoes and not just a count? Because this test is about coordination of threads. |
| 134 | + # We need to make sure the threads get properly synchronized on each iteration. |
| 135 | + echo "Thread 1: iteration ", i |
| 136 | + |
| 137 | +proc asyncProc2(args: ThreadArg) {.async.} = |
| 138 | + for i in 0 .. 50: |
| 139 | + args.event1.trigger() |
| 140 | + await args.event2.wait() |
| 141 | + echo "Thread 2: iteration ", i |
| 142 | + |
| 143 | +proc threadProc1(args: ThreadArg) {.thread.} = |
| 144 | + ## We create new dispatcher explicitly to avoid bugs. |
| 145 | + let loop = getGlobalDispatcher() |
| 146 | + waitFor asyncProc1(args) |
| 147 | + |
| 148 | +proc threadProc2(args: ThreadArg) {.thread.} = |
| 149 | + ## We create new dispatcher explicitly avoid bugs. |
| 150 | + let loop = getGlobalDispatcher() |
| 151 | + waitFor asyncProc2(args) |
| 152 | + |
| 153 | +proc main() = |
| 154 | + var |
| 155 | + args: ThreadArg |
| 156 | + thread1: Thread[ThreadArg] |
| 157 | + thread2: Thread[ThreadArg] |
| 158 | + |
| 159 | + args.event1 = newAsyncEvent() |
| 160 | + args.event2 = newAsyncEvent() |
| 161 | + thread1.createThread(threadProc1, args) |
| 162 | + # make sure the threads startup in order, or we will either deadlock or error. |
| 163 | + sleep(100) |
| 164 | + thread2.createThread(threadProc2, args) |
| 165 | + joinThreads(thread1, thread2) |
| 166 | + |
| 167 | +when isMainModule: |
| 168 | + main() |
0 commit comments