mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
sync: only release semaphore in WaitGroup when there are waiters (#10967)
This commit is contained in:
41
vlib/sync/waitgroup_test.v
Normal file
41
vlib/sync/waitgroup_test.v
Normal file
@ -0,0 +1,41 @@
|
||||
module sync
|
||||
|
||||
import time
|
||||
|
||||
fn test_waitgroup_reuse() {
|
||||
mut wg := new_waitgroup()
|
||||
|
||||
wg.add(1)
|
||||
wg.done()
|
||||
|
||||
wg.add(1)
|
||||
mut executed := false
|
||||
go fn (mut wg WaitGroup, executed voidptr) {
|
||||
defer {
|
||||
wg.done()
|
||||
}
|
||||
unsafe {
|
||||
*(&bool(executed)) = true
|
||||
}
|
||||
time.sleep(100 * time.millisecond)
|
||||
assert wg.wait_count == 1
|
||||
}(mut wg, voidptr(&executed))
|
||||
|
||||
wg.wait()
|
||||
assert executed
|
||||
assert wg.wait_count == 0
|
||||
}
|
||||
|
||||
fn test_waitgroup_no_use() {
|
||||
mut done := false
|
||||
go fn (done voidptr) {
|
||||
time.sleep(1 * time.second)
|
||||
if *(&bool(done)) == false {
|
||||
panic('test_waitgroup_no_use did not complete in time')
|
||||
}
|
||||
}(voidptr(&done))
|
||||
|
||||
mut wg := new_waitgroup()
|
||||
wg.wait()
|
||||
done = true
|
||||
}
|
Reference in New Issue
Block a user