| 1 | /* @title: Completion */ |
| 2 | #pragma once |
| 3 | #include <stdbool.h> |
| 4 | #include <stdint.h> |
| 5 | #include <sync/condvar.h> |
| 6 | #include <sync/spinlock.h> |
| 7 | #include <time/time.h> |
| 8 | |
| 9 | /* Basically just struct semaphore with completion semantics :^) */ |
| 10 | #define COMPLETION_INIT_IRQ_DISABLE true |
| 11 | #define COMPLETION_INIT_NORMAL false |
| 12 | |
| 13 | struct completion { |
| 14 | _Atomic uint32_t done; |
| 15 | bool irq_disable; |
| 16 | |
| 17 | struct spinlock lock; |
| 18 | struct condvar cv; |
| 19 | }; |
| 20 | |
| 21 | #define COMPLETION_INIT(irq_dis) \ |
| 22 | (struct completion) { \ |
| 23 | .done = ATOMIC_VAR_INIT(0), .irq_disable = (irq_dis), \ |
| 24 | .lock = SPINLOCK_INIT, \ |
| 25 | .cv = { \ |
| 26 | .waiters = THREAD_QUEUE_INIT, \ |
| 27 | .irq_disable = (irq_dis), \ |
| 28 | }, \ |
| 29 | } |
| 30 | |
| 31 | void completion_init(struct completion *c, bool irq_disable); |
| 32 | void completion_reinit(struct completion *c); |
| 33 | void completion_wait(struct completion *c); |
| 34 | bool completion_wait_timeout(struct completion *c, time_ms_t timeout_ms); |
| 35 | bool completion_try_wait(struct completion *c); |
| 36 | void complete(struct completion *c); |
| 37 | void complete_all(struct completion *c); |
| 38 | bool completion_done(struct completion *c); |
| 39 | |