refactor: abstract out the code for prdte creation (by imposing a constrain on buffer...
[lunaix-os.git] / lunaix-os / kernel / ds / semaphore.c
1 #include <lunaix/ds/semaphore.h>
2 #include <lunaix/sched.h>
3
4 void
5 sem_init(struct sem_t* sem, unsigned int initial)
6 {
7     sem->counter = ATOMIC_VAR_INIT(initial);
8 }
9
10 void
11 sem_wait(struct sem_t* sem)
12 {
13     while (!atomic_load(&sem->counter)) {
14         // FIXME: better thing like wait queue
15     }
16     atomic_fetch_sub(&sem->counter, 1);
17 }
18
19 void
20 sem_post(struct sem_t* sem)
21 {
22     atomic_fetch_add(&sem->counter, 1);
23     // TODO: wake up a thread
24 }