| 1 | module sync |
| 2 | |
| 3 | import time |
| 4 | import sync.stdatomic |
| 5 | |
| 6 | fn issue_6870_worker(mut wg WaitGroup, ready chan bool, release chan bool) { |
| 7 | ready <- true |
| 8 | _ = <-release |
| 9 | wg.add(1) |
| 10 | wg.done() |
| 11 | wg.done() |
| 12 | } |
| 13 | |
| 14 | fn test_waitgroup_reuse() { |
| 15 | mut wg := new_waitgroup() |
| 16 | |
| 17 | wg.add(1) |
| 18 | wg.done() |
| 19 | |
| 20 | wg.add(1) |
| 21 | mut executed := false |
| 22 | spawn fn (mut wg WaitGroup, executed voidptr) { |
| 23 | defer { |
| 24 | wg.done() |
| 25 | } |
| 26 | unsafe { |
| 27 | *(&bool(executed)) = true |
| 28 | } |
| 29 | time.sleep(100 * time.millisecond) |
| 30 | }(mut wg, voidptr(&executed)) |
| 31 | |
| 32 | wg.wait() |
| 33 | assert executed |
| 34 | } |
| 35 | |
| 36 | fn test_waitgroup_no_use() { |
| 37 | done := chan bool{cap: 1} |
| 38 | watchdog := spawn fn (done chan bool) { |
| 39 | select { |
| 40 | _ := <-done {} |
| 41 | 10 * time.second { |
| 42 | panic('test_waitgroup_no_use did not complete in time') |
| 43 | } |
| 44 | } |
| 45 | }(done) |
| 46 | |
| 47 | mut wg := new_waitgroup() |
| 48 | wg.wait() |
| 49 | done <- true |
| 50 | watchdog.wait() |
| 51 | } |
| 52 | |
| 53 | fn test_waitgroup_go() { |
| 54 | mut counter := stdatomic.new_atomic(0) |
| 55 | mut wg := new_waitgroup() |
| 56 | for i in 0 .. 10 { |
| 57 | wg.go(fn [mut counter] () { |
| 58 | counter.add(1) |
| 59 | }) |
| 60 | } |
| 61 | wg.wait() |
| 62 | assert counter.load() == 10 |
| 63 | } |
| 64 | |
| 65 | fn test_waitgroup_add_while_waiting() { |
| 66 | for _ in 0 .. 50 { |
| 67 | mut wg := new_waitgroup() |
| 68 | ready := chan bool{cap: 1} |
| 69 | release := chan bool{cap: 1} |
| 70 | wait_done := chan bool{cap: 8} |
| 71 | wg.add(1) |
| 72 | |
| 73 | spawn issue_6870_worker(mut wg, ready, release) |
| 74 | _ = <-ready |
| 75 | |
| 76 | for _ in 0 .. 8 { |
| 77 | spawn fn [mut wg, wait_done] () { |
| 78 | wg.wait() |
| 79 | wait_done <- true |
| 80 | }() |
| 81 | } |
| 82 | release <- true |
| 83 | |
| 84 | for _ in 0 .. 8 { |
| 85 | select { |
| 86 | _ := <-wait_done {} |
| 87 | 2 * time.second { |
| 88 | assert false, 'wait() missed a wakeup while work added more tasks' |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |