1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
RIOT/sys/net/sixlowpan/semaphore.c
mlenders 0bacfdbfbf [sys/net/sixlowpan/semaphore.*]
* added semaphores for sliding window
2011-07-09 20:34:20 +02:00

33 lines
607 B
C

#include <semaphore.h>
#include <mutex.h>
sem_t sem_init(int8_t value) {
sem_t sem;
sem.value = value;
mutex_init(&sem.mutex);
sem.locked = 0;
return sem;
}
int sem_wait(sem_t *sem) {
int res;
if(--(sem->value) <= 0 && !sem->locked) {
sem->locked = !(sem->locked);
res = mutex_lock(&(sem->mutex));
if (res < 0) {
return res;
}
}
return 0;
}
int sem_signal(sem_t *sem) {
if (++(sem->value) > 0 && sem->locked) {
sem->locked = !(sem->locked);
mutex_unlock(&(sem->mutex),0);
}
return 0;
}