diff options
| author | gingerBill <bill@gingerbill.org> | 2024-02-21 13:08:12 +0000 |
|---|---|---|
| committer | gingerBill <bill@gingerbill.org> | 2024-02-21 13:08:12 +0000 |
| commit | f989f4df3e1e4f2ffa313dc116a04a6c761d6d80 (patch) | |
| tree | 5d12b88aefd9df5a069cbb6825d145128583b199 /core/sync | |
| parent | 21d1c0e5a41627cfc0bc65e00a13d40e7380c50d (diff) | |
Add `sync.One_Shot_Event`
Diffstat (limited to 'core/sync')
| -rw-r--r-- | core/sync/extended.odin | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/core/sync/extended.odin b/core/sync/extended.odin index c76ab504b..76b7686fe 100644 --- a/core/sync/extended.odin +++ b/core/sync/extended.odin @@ -417,4 +417,28 @@ unpark :: proc "contextless" (p: ^Parker) { if atomic_exchange_explicit(&p.state, NOTIFIED, .Release) == PARKED { futex_signal(&p.state) } +} + + + +// A One_Shot_Event is an associated token which is initially not present: +// * The `one_shot_event_wait` blocks the current thread until the event +// is made available +// * The `one_shot_event_signal` procedure automatically makes the token +// available if its was not already. +One_Shot_Event :: struct #no_copy { + state: Futex, +} + +// Blocks the current thread until the event is made available with `one_shot_event_signal`. +one_shot_event_wait :: proc "contextless" (e: ^One_Shot_Event) { + for atomic_load_explicit(&e.state, .Acquire) == 0 { + futex_wait(&e.state, 1) + } +} + +// Releases any threads that are currently blocked by this event with `one_shot_event_wait`. +one_shot_event_signal :: proc "contextless" (e: ^One_Shot_Event) { + atomic_store_explicit(&e.state, 1, .Release) + futex_broadcast(&e.state) }
\ No newline at end of file |