| 1 | #pragma once |
| 2 | #include <stdint.h> |
| 3 | #include <sync/condvar.h> |
| 4 | #include <sync/spinlock.h> |
| 5 | |
| 6 | #define SEMAPHORE_INIT_IRQ_DISABLE true |
| 7 | #define SEMAPHORE_INIT_NORMAL false |
| 8 | |
| 9 | struct semaphore { |
| 10 | _Atomic int32_t count; |
| 11 | bool irq_disable; |
| 12 | |
| 13 | struct spinlock lock; |
| 14 | struct condvar cv; |
| 15 | }; |
| 16 | |
| 17 | void semaphore_init(struct semaphore *s, int value, bool irq_disable); |
| 18 | void semaphore_wait(struct semaphore *s); |
| 19 | bool semaphore_timedwait(struct semaphore *s, time_t timeout_ms); |
| 20 | void semaphore_post(struct semaphore *s); |
| 21 | void semaphore_postn(struct semaphore *s, int n); |
| 22 | |