changeset 653:255e93cd013b

synch: add CONDTIMEDWAIT_NSEC & rewrite CONDTIMEDWAIT_SPEC in terms of it Signed-off-by: Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
author Josef 'Jeff' Sipek <jeffpc@josefsipek.net>
date Sun, 06 Jan 2019 19:38:53 -0500
parents 645b65c7a097
children f15830590cbe
files include/jeffpc/synch.h mapfile-vers synch.c
diffstat 3 files changed, 24 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/include/jeffpc/synch.h	Sun Jan 06 19:10:18 2019 -0500
+++ b/include/jeffpc/synch.h	Sun Jan 06 19:38:53 2019 -0500
@@ -165,7 +165,7 @@
 				}; \
 				condwait(&cond_ctx, (c), (m)); \
 			} while (0)
-#define CONDTIMEDWAIT_SPEC(c, m, t) \
+#define CONDTIMEDWAIT_NSEC(c, m, t) \
 			do { \
 				struct lock_context cond_ctx = { \
 					.condname = #c, \
@@ -173,7 +173,14 @@
 					.file = __FILE__, \
 					.line = __LINE__, \
 				}; \
-				condtimedwait_spec(&cond_ctx, (c), (m), (t)); \
+				condtimedwait(&cond_ctx, (c), (m), (t)); \
+			} while (0)
+#define CONDTIMEDWAIT_SPEC(c, m, t) \
+			do { \
+				struct time_spec tmp = *(t); \
+				CONDTIMEDWAIT_NSEC((c), (m), \
+						   (tmp.tv_sec * 1000000000ull) + \
+						   tmp->tv_nsec); \
 			} while (0)
 #define CONDSIG(c)	do { \
 				struct lock_context cond_ctx = { \
@@ -241,8 +248,8 @@
 extern void conddestroy(const struct lock_context *where, struct cond *c);
 extern void condwait(const struct lock_context *where, struct cond *c,
 		     struct lock *m);
-extern int condtimedwait_spec(const struct lock_context *where, struct cond *c,
-			      struct lock *m, const struct timespec *reltime);
+extern int condtimedwait(const struct lock_context *where, struct cond *c,
+			 struct lock *m, const uint64_t reltime);
 extern void condsig(const struct lock_context *where, struct cond *c);
 extern void condbcast(const struct lock_context *where, struct cond *c);
 
--- a/mapfile-vers	Sun Jan 06 19:10:18 2019 -0500
+++ b/mapfile-vers	Sun Jan 06 19:38:53 2019 -0500
@@ -213,7 +213,7 @@
 		condbcast;
 		conddestroy;
 		condinit;
-		condtimedwait_spec;
+		condtimedwait;
 		condsig;
 		condwait;
 		lockdep_no_locks;
--- a/synch.c	Sun Jan 06 19:10:18 2019 -0500
+++ b/synch.c	Sun Jan 06 19:38:53 2019 -0500
@@ -642,30 +642,28 @@
 		      where->file, where->line, strerror(ret));
 }
 
-int condtimedwait_spec(const struct lock_context *where, struct cond *c,
-		       struct lock *l, const struct timespec *reltime)
+int condtimedwait(const struct lock_context *where, struct cond *c,
+		  struct lock *l, const uint64_t reltime)
 {
+	struct timespec when;
 	int ret;
 
 	verify_cond_wait(where, c, l, true);
 
 #ifdef JEFFPC_HAVE_PTHREAD_COND_RELTIMEDWAIT_NP
-	ret = pthread_cond_reltimedwait_np(&c->cond, &l->lock, reltime);
+	when.tv_sec = reltime / 1000000000ull;
+	when.tv_nsec = reltime % 1000000000ull;
+
+	ret = pthread_cond_reltimedwait_np(&c->cond, &l->lock, &when);
 #else
-	struct timespec abstime;
-	struct timespec now;
-
-	VERIFY0(clock_gettime(CLOCK_REALTIME, &now));
+	uint64_t abstime;
 
-	while ((now.tv_nsec + reltime->tv_nsec) >= 1000000000) {
-		now.tv_sec++;
-		now.tv_nsec -= 1000000000;
-	}
+	abstime = gettime() + reltime;
 
-	abstime.tv_sec  = now.tv_sec  + reltime->tv_sec;
-	abstime.tv_nsec = now.tv_nsec + reltime->tv_nsec;
+	when.tv_sec = abstime / 1000000000ull;
+	when.tv_nsec = abstime % 1000000000ull;
 
-	ret = pthread_cond_timedwait(&c->cond, &l->lock, &abstime);
+	ret = pthread_cond_timedwait(&c->cond, &l->lock, &when);
 #endif
 
 	if ((ret != 0) && (ret != ETIMEDOUT))