From 484a1b61049ca9927979216503d4a08abf9600b3 Mon Sep 17 00:00:00 2001 From: Freddie Chopin Date: Wed, 9 Nov 2016 07:01:49 -0600 Subject: [PATCH] sem_wait() and sem_trywait() no longer modify the errno value UNLESS an error occurs. This allows these functions to be used internallly without clobbering the errno value. --- sched/semaphore/sem_trywait.c | 24 ++++++++++++------------ sched/semaphore/sem_wait.c | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/sched/semaphore/sem_trywait.c b/sched/semaphore/sem_trywait.c index 37ba0d66e2..c55116a2b7 100644 --- a/sched/semaphore/sem_trywait.c +++ b/sched/semaphore/sem_trywait.c @@ -84,13 +84,9 @@ int sem_trywait(FAR sem_t *sem) /* This API should not be called from interrupt handlers */ - DEBUGASSERT(up_interrupt_context() == false); + DEBUGASSERT(sem != NULL && up_interrupt_context() == false); - /* Assume any errors reported are due to invalid arguments. */ - - set_errno(EINVAL); - - if (sem) + if (sem != NULL) { /* The following operations must be performed with interrupts disabled * because sem_post() may be called from an interrupt handler. @@ -98,12 +94,6 @@ int sem_trywait(FAR sem_t *sem) flags = enter_critical_section(); - /* Any further errors could only occurr because the semaphore is not - * available. - */ - - set_errno(EAGAIN); - /* If the semaphore is available, give it to the requesting task */ if (sem->semcount > 0) @@ -114,11 +104,21 @@ int sem_trywait(FAR sem_t *sem) rtcb->waitsem = NULL; ret = OK; } + else + { + /* Semaphore is not available */ + + set_errno(EAGAIN); + } /* Interrupts may now be enabled. */ leave_critical_section(flags); } + else + { + set_errno(EINVAL); + } return ret; } diff --git a/sched/semaphore/sem_wait.c b/sched/semaphore/sem_wait.c index 9c19d406d7..da855bc70c 100644 --- a/sched/semaphore/sem_wait.c +++ b/sched/semaphore/sem_wait.c @@ -84,11 +84,11 @@ int sem_wait(FAR sem_t *sem) /* This API should not be called from interrupt handlers */ - DEBUGASSERT(up_interrupt_context() == false); + DEBUGASSERT(sem != NULL && up_interrupt_context() == false); /* Make sure we were supplied with a valid semaphore. */ - if (sem) + if (sem != NULL) { /* The following operations must be performed with interrupts * disabled because sem_post() may be called from an interrupt