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