2019-07-30 15:06:16 +02:00
|
|
|
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
|
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
module sync
|
|
|
|
|
2019-10-24 12:19:27 +02:00
|
|
|
pub struct WaitGroup {
|
2019-07-30 15:06:16 +02:00
|
|
|
mut:
|
2019-08-29 10:48:03 +02:00
|
|
|
mu Mutex
|
|
|
|
active int
|
2019-07-30 15:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (wg mut WaitGroup) add(delta int) {
|
2019-08-29 10:48:03 +02:00
|
|
|
wg.mu.lock()
|
|
|
|
wg.active += delta
|
|
|
|
wg.mu.unlock()
|
|
|
|
if wg.active < 0 {
|
|
|
|
panic('Negative number of jobs in waitgroup')
|
|
|
|
}
|
2019-07-30 15:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (wg mut WaitGroup) done() {
|
2019-08-29 10:48:03 +02:00
|
|
|
wg.add(-1)
|
2019-07-30 15:06:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn (wg mut WaitGroup) wait() {
|
2019-08-29 10:48:03 +02:00
|
|
|
for wg.active > 0 {
|
|
|
|
// waiting
|
|
|
|
}
|
2019-07-30 15:06:16 +02:00
|
|
|
}
|
|
|
|
|