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