From 032e64aaf23359724b9c1457e46879be25395dd4 Mon Sep 17 00:00:00 2001
From: Julian Oes <julian@oes.ch>
Date: Tue, 1 Jan 2019 20:01:57 +0100
Subject: [PATCH] px4_sem: set errno, return -1 on any error

- Don't duplicate the ret variable to err.
- Return -1 on any error, 0 otherwise.
- Set errno to return value of pthread_cond_timedwait.
---
 platforms/posix/src/px4_layer/px4_sem.cpp | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

diff --git a/platforms/posix/src/px4_layer/px4_sem.cpp b/platforms/posix/src/px4_layer/px4_sem.cpp
index c5d9e241ed..d60d1cb813 100644
--- a/platforms/posix/src/px4_layer/px4_sem.cpp
+++ b/platforms/posix/src/px4_layer/px4_sem.cpp
@@ -140,20 +140,24 @@ int px4_sem_timedwait(px4_sem_t *s, const struct timespec *abstime)
 		ret = 0;
 	}
 
-	int err = ret;
+	errno = ret;
 
-	if (err != 0 && err != ETIMEDOUT) {
+	if (ret != 0 && ret != ETIMEDOUT) {
 		setbuf(stdout, nullptr);
 		setbuf(stderr, nullptr);
 		const unsigned NAMELEN = 32;
 		char thread_name[NAMELEN] = {};
 		(void)pthread_getname_np(pthread_self(), thread_name, NAMELEN);
-		PX4_WARN("%s: px4_sem_timedwait failure: ret: %d, %s", thread_name, ret, strerror(err));
+		PX4_WARN("%s: px4_sem_timedwait failure: ret: %d, %s", thread_name, ret, strerror(ret));
 	}
 
 	int mret = pthread_mutex_unlock(&(s->lock));
 
-	return (err) ? err : mret;
+	if (ret || mret) {
+		return -1;
+	}
+
+	return 0;
 }
 
 int px4_sem_post(px4_sem_t *s)
-- 
GitLab