mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-02 12:48:14 +00:00
Add wait_group_wait_with_timeout; Allow Sema to be implemented as a Wait_Group
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package sync2
|
||||
|
||||
import "core:time"
|
||||
|
||||
// A Wait_Group waits for a collection of threads to finish
|
||||
//
|
||||
// A Wait_Group must not be copied after first use
|
||||
@@ -45,6 +47,24 @@ wait_group_wait :: proc(wg: ^Wait_Group) {
|
||||
}
|
||||
}
|
||||
|
||||
wait_group_wait_with_timeout :: proc(wg: ^Wait_Group, duration: time.Duration) -> bool {
|
||||
if duration <= 0 {
|
||||
return false
|
||||
}
|
||||
mutex_lock(&wg.mutex)
|
||||
defer mutex_unlock(&wg.mutex)
|
||||
|
||||
if wg.counter != 0 {
|
||||
if !cond_wait_with_timeout(&wg.cond, &wg.mutex, duration) {
|
||||
return false
|
||||
}
|
||||
if wg.counter != 0 {
|
||||
panic("sync.Wait_Group misuse: sync.wait_group_add called concurrently with sync.wait_group_wait")
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
|
||||
|
||||
// A barrier enabling multiple threads to synchronize the beginning of some computation
|
||||
|
||||
Reference in New Issue
Block a user