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
9struct semaphore {
10 _Atomic int32_t count;
11 bool irq_disable;
12
13 struct spinlock lock;
14 struct condvar cv;
15};
16
17void semaphore_init(struct semaphore *s, int value, bool irq_disable);
18void semaphore_wait(struct semaphore *s);
19bool semaphore_timedwait(struct semaphore *s, time_t timeout_ms);
20void semaphore_post(struct semaphore *s);
21void semaphore_postn(struct semaphore *s, int n);
22