2020-06-22 11:07:34 +02:00
// Copyright (c) 2019 Alexander Medvednikov. All rights reserved.
2019-07-30 15:06:16 +02:00
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module sync
2020-05-10 07:58:54 +02:00
2020-06-22 11:07:34 +02:00
// WaitGroup implementation. wait() blocks until all tasks complete.
// Do not copy an instance of WaitGroup, use a ref instead.
//
// Two mutexes are required so that wait() doesn't unblock on done()/add() before
// task_count becomes zero.
//
2019-12-21 23:41:42 +01:00
// [init_with=new_waitgroup] // TODO: implement support for init_with struct attribute, and disallow WaitGroup{} from outside the sync.new_waitgroup() function.
2020-05-10 07:58:54 +02:00
[ ref_only ]
2020-06-22 11:07:34 +02:00
struct WaitGroup {
2019-07-30 15:06:16 +02:00
mut :
2020-06-22 11:07:34 +02:00
task_count int // current task count
task_count_mutex & Mutex = & Mutex ( 0 ) // This mutex protects the task_count count in add()
2020-06-22 19:23:42 +02:00
wait_blocker & Waiter = & Waiter ( 0 ) // This blocks the wait() until released by add()
2019-07-30 15:06:16 +02:00
}
2020-01-19 20:32:22 +01:00
pub fn new_waitgroup ( ) & WaitGroup {
2020-06-22 11:07:34 +02:00
return & WaitGroup {
task_count_mutex : new_mutex ( )
2020-06-22 19:23:42 +02:00
wait_blocker : new_waiter ( )
2020-06-22 11:07:34 +02:00
}
2019-10-25 16:24:40 +02:00
}
2020-06-22 11:07:34 +02:00
// add increments (+ve delta) or decrements (-ve delta) task count by delta
// and unblocks any wait() calls if task count becomes zero.
// add panics if task count drops below zero.
2020-05-17 13:51:18 +02:00
pub fn ( mut wg WaitGroup ) add ( delta int ) {
2020-06-22 11:07:34 +02:00
// protect task_count
2020-07-04 12:44:25 +02:00
wg . task_count_mutex . m_lock ( )
2020-06-22 11:07:34 +02:00
defer {
wg . task_count_mutex . unlock ( )
}
// If task_count likely to leave zero, set wait() to block
if wg . task_count == 0 {
2020-06-22 19:23:42 +02:00
wg . wait_blocker . wait ( )
2020-06-22 11:07:34 +02:00
}
wg . task_count += delta
if wg . task_count < 0 {
2019-08-29 10:48:03 +02:00
panic ( ' N e g a t i v e n u m b e r o f j o b s i n w a i t g r o u p ' )
}
2020-06-22 11:07:34 +02:00
if wg . task_count == 0 { // if no more task_count tasks
2020-06-22 19:23:42 +02:00
wg . wait_blocker . stop ( ) // unblock wait()
2020-06-22 11:07:34 +02:00
}
2019-07-30 15:06:16 +02:00
}
2020-06-22 11:07:34 +02:00
// done is a convenience fn for add(-1)
2020-05-17 13:51:18 +02:00
pub fn ( mut wg WaitGroup ) done ( ) {
2019-08-29 10:48:03 +02:00
wg . add ( - 1 )
2019-07-30 15:06:16 +02:00
}
2020-06-22 11:07:34 +02:00
// wait blocks until all tasks are done (task count becomes zero)
pub fn ( mut wg WaitGroup ) wait ( ) {
2020-06-22 19:23:42 +02:00
wg . wait_blocker . wait ( ) // blocks until task_count becomes 0
wg . wait_blocker . stop ( ) // allow other wait()s to unblock or reuse wait group
2019-07-30 15:06:16 +02:00
}