changeset 83:ba392bf7439c

common: provide a way to do a relative timed condition wait on Linux This code will run on any platform that doesn't provide pthread_cond_reltimedwait_np(), not just Linux. Closes bug #12. Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sat, 17 Oct 2015 08:05:35 -0400
parents 871edff2615a
children 141fbaedf8f9
files src/common/mutex.c
diffstat 1 files changed, 14 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/common/mutex.c	Sat Oct 17 07:57:09 2015 -0400
+++ b/src/common/mutex.c	Sat Oct 17 08:05:35 2015 -0400
@@ -93,7 +93,20 @@
 #ifdef HAVE_PTHREAD_COND_RELTIMEDWAIT_NP
 	ret = pthread_cond_reltimedwait_np(c, m, reltime);
 #else
-#error need a relative timed condition wait
+	struct timespec abstime;
+	struct timespec now;
+
+	clock_gettime(CLOCK_REALTIME, &now);
+
+	if ((now.tv_nsec + reltime->tv_nsec) >= 1000000000) {
+		now.tv_sec++;
+		now.tv_nsec -= 1000000000;
+	}
+
+	abstime.tv_sec  = now.tv_sec  + reltime->tv_sec;
+	abstime.tv_nsec = now.tv_nsec + reltime->tv_nsec;
+
+	ret = pthread_cond_timedwait(c, m, &abstime);
 #endif
 
 	if ((ret != 0) || (ret != ETIMEDOUT))