semaphore: add px4_sem_trywait

directly mapped to the posix method sem_trywait
This commit is contained in:
Beat Küng
2017-08-04 18:08:51 +02:00
committed by Lorenz Meier
parent ed478f40fd
commit 1942641ff6
2 changed files with 23 additions and 0 deletions
+21
View File
@@ -91,6 +91,27 @@ int px4_sem_wait(px4_sem_t *s)
return (ret) ? ret : mret;
}
int px4_sem_trywait(px4_sem_t *s)
{
int ret = pthread_mutex_lock(&(s->lock));
if (ret) {
return ret;
}
if (s->value <= 0) {
errno = EAGAIN;
ret = -1;
} else {
s->value--;
}
int mret = pthread_mutex_unlock(&(s->lock));
return (ret) ? ret : mret;
}
int px4_sem_timedwait(px4_sem_t *s, const struct timespec *abstime)
{
int ret = pthread_mutex_lock(&(s->lock));