mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
sync: use Event for waitgroup on windows
This commit is contained in:
@ -15,13 +15,13 @@ struct WaitGroup {
|
||||
mut:
|
||||
task_count int // current task count
|
||||
task_count_mutex &Mutex = &Mutex(0) // This mutex protects the task_count count in add()
|
||||
wait_blocker &Mutex = &Mutex(0) // This mutex blocks the wait() until released by add()
|
||||
wait_blocker &Waiter = &Waiter(0) // This blocks the wait() until released by add()
|
||||
}
|
||||
|
||||
pub fn new_waitgroup() &WaitGroup {
|
||||
return &WaitGroup{
|
||||
task_count_mutex: new_mutex()
|
||||
wait_blocker: new_mutex()
|
||||
wait_blocker: new_waiter()
|
||||
}
|
||||
}
|
||||
|
||||
@ -36,14 +36,14 @@ pub fn (mut wg WaitGroup) add(delta int) {
|
||||
}
|
||||
// If task_count likely to leave zero, set wait() to block
|
||||
if wg.task_count == 0 {
|
||||
wg.wait_blocker.lock()
|
||||
wg.wait_blocker.wait()
|
||||
}
|
||||
wg.task_count += delta
|
||||
if wg.task_count < 0 {
|
||||
panic('Negative number of jobs in waitgroup')
|
||||
}
|
||||
if wg.task_count == 0 { // if no more task_count tasks
|
||||
wg.wait_blocker.unlock() // unblock wait()
|
||||
wg.wait_blocker.stop() // unblock wait()
|
||||
}
|
||||
}
|
||||
|
||||
@ -54,6 +54,6 @@ pub fn (mut wg WaitGroup) done() {
|
||||
|
||||
// wait blocks until all tasks are done (task count becomes zero)
|
||||
pub fn (mut wg WaitGroup) wait() {
|
||||
wg.wait_blocker.lock() // blocks until task_count becomes 0
|
||||
wg.wait_blocker.unlock() // allow other wait()s to unblock or reuse wait group
|
||||
wg.wait_blocker.wait() // blocks until task_count becomes 0
|
||||
wg.wait_blocker.stop() // allow other wait()s to unblock or reuse wait group
|
||||
}
|
||||
|
Reference in New Issue
Block a user