mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Improve atomic logic for sync.Wait_Group
This commit is contained in:
+6
-12
@@ -47,12 +47,12 @@ wait_group_add :: proc "contextless" (wg: ^Wait_Group, delta: int) {
|
|||||||
guard(&wg.mutex)
|
guard(&wg.mutex)
|
||||||
|
|
||||||
atomic_add(&wg.counter, delta)
|
atomic_add(&wg.counter, delta)
|
||||||
if wg.counter < 0 {
|
switch counter := atomic_load(&wg.counter); counter {
|
||||||
|
case counter < 0:
|
||||||
panic_contextless("sync.Wait_Group negative counter")
|
panic_contextless("sync.Wait_Group negative counter")
|
||||||
}
|
case if wg.counter == 0:
|
||||||
if wg.counter == 0 {
|
|
||||||
cond_broadcast(&wg.cond)
|
cond_broadcast(&wg.cond)
|
||||||
if wg.counter != 0 {
|
if atomic_load(&wg.counter) != 0 {
|
||||||
panic_contextless("sync.Wait_Group misuse: sync.wait_group_add called concurrently with sync.wait_group_wait")
|
panic_contextless("sync.Wait_Group misuse: sync.wait_group_add called concurrently with sync.wait_group_wait")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -78,11 +78,8 @@ wait group's internal counter reaches zero.
|
|||||||
wait_group_wait :: proc "contextless" (wg: ^Wait_Group) {
|
wait_group_wait :: proc "contextless" (wg: ^Wait_Group) {
|
||||||
guard(&wg.mutex)
|
guard(&wg.mutex)
|
||||||
|
|
||||||
if wg.counter != 0 {
|
for atomic_load(&wg.counter) != 0 {
|
||||||
cond_wait(&wg.cond, &wg.mutex)
|
cond_wait(&wg.cond, &wg.mutex)
|
||||||
if wg.counter != 0 {
|
|
||||||
panic_contextless("sync.Wait_Group misuse: sync.wait_group_add called concurrently with sync.wait_group_wait")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,13 +97,10 @@ wait_group_wait_with_timeout :: proc "contextless" (wg: ^Wait_Group, duration: t
|
|||||||
}
|
}
|
||||||
guard(&wg.mutex)
|
guard(&wg.mutex)
|
||||||
|
|
||||||
if wg.counter != 0 {
|
for atomic_load(&wg.counter) != 0 {
|
||||||
if !cond_wait_with_timeout(&wg.cond, &wg.mutex, duration) {
|
if !cond_wait_with_timeout(&wg.cond, &wg.mutex, duration) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
if wg.counter != 0 {
|
|
||||||
panic_contextless("sync.Wait_Group misuse: sync.wait_group_add called concurrently with sync.wait_group_wait")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user