21d0e3399d761f3aaf2d3528a83dd55187144143
[lunaix-os.git] / lunaix-os / kernel / ds / semaphore.c
1 #include <lunaix/ds/semaphore.h>
2
3 void sem_init(struct sem_t *sem, unsigned int initial) {
4     sem->counter = ATOMIC_VAR_INIT(initial);
5 }
6
7 void sem_wait(struct sem_t *sem) {
8     while (!atomic_load(&sem->counter)) {
9         // TODO: yield the cpu
10     }
11     atomic_fetch_sub(&sem->counter, 1);
12 }
13
14 void sem_post(struct sem_t *sem) {
15     atomic_fetch_add(&sem->counter, 1);
16     // TODO: wake up a thread
17 }