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
13struct 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
31void completion_init(struct completion *c, bool irq_disable);
32void completion_reinit(struct completion *c);
33void completion_wait(struct completion *c);
34bool completion_wait_timeout(struct completion *c, time_ms_t timeout_ms);
35bool completion_try_wait(struct completion *c);
36void complete(struct completion *c);
37void complete_all(struct completion *c);
38bool completion_done(struct completion *c);
39