From 360714ceda0f2eac22b82193b3199b3432b5ad01 Mon Sep 17 00:00:00 2001
From: Sascha Wildner <saw@online.de>
Date: Sat, 2 Nov 2019 20:07:49 +0100
Subject: [PATCH] libthread_xu: Remove local versions of 3 parameter
 timespec{add,sub}().

Adjust the parameter order. Comparison of preprocessed code looks good.
---
 lib/libthread_xu/thread/thr_cond.c    |  2 +-
 lib/libthread_xu/thread/thr_join.c    |  2 +-
 lib/libthread_xu/thread/thr_mutex.c   |  6 +++---
 lib/libthread_xu/thread/thr_private.h | 20 --------------------
 lib/libthread_xu/thread/thr_sem.c     |  2 +-
 lib/libthread_xu/thread/thr_umtx.c    |  8 ++++----
 6 files changed, 10 insertions(+), 30 deletions(-)

diff --git a/lib/libthread_xu/thread/thr_cond.c b/lib/libthread_xu/thread/thr_cond.c
index f72a9f6744..ea3567f7ca 100644
--- a/lib/libthread_xu/thread/thr_cond.c
+++ b/lib/libthread_xu/thread/thr_cond.c
@@ -274,7 +274,7 @@ cond_wait_common(pthread_cond_t *cond, pthread_mutex_t *mutex,
 
 		if (abstime != NULL) {
 			clock_gettime(cv->c_clockid, &ts);
-			TIMESPEC_SUB(&ts2, abstime, &ts);
+			timespecsub(abstime, &ts, &ts2);
 			tsp = &ts2;
 		} else {
 			tsp = NULL;
diff --git a/lib/libthread_xu/thread/thr_join.c b/lib/libthread_xu/thread/thr_join.c
index fbbd2b2d20..fa92f6ac79 100644
--- a/lib/libthread_xu/thread/thr_join.c
+++ b/lib/libthread_xu/thread/thr_join.c
@@ -107,7 +107,7 @@ join_common(pthread_t pthread, void **thread_return,
 		cpu_ccfence();
 		if (abstime != NULL) {
 			clock_gettime(CLOCK_REALTIME, &ts);
-			TIMESPEC_SUB(&ts2, abstime, &ts);
+			timespecsub(abstime, &ts, &ts2);
 			if (ts2.tv_sec < 0) {
 				ret = ETIMEDOUT;
 				break;
diff --git a/lib/libthread_xu/thread/thr_mutex.c b/lib/libthread_xu/thread/thr_mutex.c
index 0659b25db9..dce4e69596 100644
--- a/lib/libthread_xu/thread/thr_mutex.c
+++ b/lib/libthread_xu/thread/thr_mutex.c
@@ -431,7 +431,7 @@ mutex_lock_common(struct pthread *curthread, pthread_mutex_t *mutex,
 				ret = EINVAL;
 		} else {
 			clock_gettime(CLOCK_REALTIME, &ts);
-			TIMESPEC_SUB(&ts2, abstime, &ts);
+			timespecsub(abstime, &ts, &ts2);
 			ret = THR_UMTX_TIMEDLOCK(curthread, &m->m_lock, &ts2);
 		}
 		if (ret == 0) {
@@ -586,7 +586,7 @@ mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime)
 	case PTHREAD_MUTEX_ERRORCHECK:
 		if (abstime) {
 			clock_gettime(CLOCK_REALTIME, &ts1);
-			TIMESPEC_SUB(&ts2, abstime, &ts1);
+			timespecsub(abstime, &ts1, &ts2);
 			__sys_nanosleep(&ts2, NULL);
 			ret = ETIMEDOUT;
 		} else {
@@ -606,7 +606,7 @@ mutex_self_lock(pthread_mutex_t m, const struct timespec *abstime)
 		ret = 0;
 		if (abstime) {
 			clock_gettime(CLOCK_REALTIME, &ts1);
-			TIMESPEC_SUB(&ts2, abstime, &ts1);
+			timespecsub(abstime, &ts1, &ts2);
 			__sys_nanosleep(&ts2, NULL);
 			ret = ETIMEDOUT;
 		} else {
diff --git a/lib/libthread_xu/thread/thr_private.h b/lib/libthread_xu/thread/thr_private.h
index cf3607c879..72e5afa9dc 100644
--- a/lib/libthread_xu/thread/thr_private.h
+++ b/lib/libthread_xu/thread/thr_private.h
@@ -93,26 +93,6 @@ void	_thr_log(const char *buf, size_t bytes);
 TAILQ_HEAD(thread_head, pthread)	thread_head;
 TAILQ_HEAD(atfork_head, pthread_atfork)	atfork_head;
 
-#define	TIMESPEC_ADD(dst, src, val)				\
-	do {							\
-		(dst)->tv_sec = (src)->tv_sec + (val)->tv_sec;	\
-		(dst)->tv_nsec = (src)->tv_nsec + (val)->tv_nsec; \
-		if ((dst)->tv_nsec >= 1000000000) {		\
-			(dst)->tv_sec++;			\
-			(dst)->tv_nsec -= 1000000000;		\
-		}						\
-	} while (0)
-
-#define	TIMESPEC_SUB(dst, src, val)				\
-	do {							\
-		(dst)->tv_sec = (src)->tv_sec - (val)->tv_sec;	\
-		(dst)->tv_nsec = (src)->tv_nsec - (val)->tv_nsec; \
-		if ((dst)->tv_nsec < 0) {			\
-			(dst)->tv_sec--;			\
-			(dst)->tv_nsec += 1000000000;		\
-		}						\
-	} while (0)
-
 struct pthread_mutex {
 	/*
 	 * Lock for accesses to this structure.
diff --git a/lib/libthread_xu/thread/thr_sem.c b/lib/libthread_xu/thread/thr_sem.c
index 147f17014c..30b564716f 100644
--- a/lib/libthread_xu/thread/thr_sem.c
+++ b/lib/libthread_xu/thread/thr_sem.c
@@ -369,7 +369,7 @@ _sem_timedwait(sem_t * __restrict sem, const struct timespec * __restrict abstim
 			return (-1);
 		}
 		clock_gettime(CLOCK_REALTIME, &ts);
-		TIMESPEC_SUB(&ts2, abstime, &ts);
+		timespecsub(abstime, &ts, &ts2);
 		oldcancel = _thr_cancel_enter(curthread);
 		sem_log("sem_wait %p %d (wait)\n", *sem, val);
 		retval = _thr_umtx_wait(&(*sem)->count, 0, &ts2,
diff --git a/lib/libthread_xu/thread/thr_umtx.c b/lib/libthread_xu/thread/thr_umtx.c
index 0ef4935d88..9b2134ff15 100644
--- a/lib/libthread_xu/thread/thr_umtx.c
+++ b/lib/libthread_xu/thread/thr_umtx.c
@@ -123,7 +123,7 @@ __thr_umtx_timedlock(volatile umtx_t *mtx, int id,
 
 	/* XXX there should have MONO timer! */
 	clock_gettime(CLOCK_REALTIME, &ts);
-	TIMESPEC_ADD(&ts, &ts, timeout);
+	timespecadd(&ts, timeout, &ts);
 	ts2 = *timeout;
 
 	id &= 0x3FFFFFFF;
@@ -140,7 +140,7 @@ __thr_umtx_timedlock(volatile umtx_t *mtx, int id,
 		if (ret != EINTR && ret != ETIMEDOUT)
 			break;
 		clock_gettime(CLOCK_REALTIME, &ts3);
-		TIMESPEC_SUB(&ts2, &ts, &ts3);
+		timespecsub(&ts, &ts3, &ts2);
 		if (ts2.tv_sec < 0 ||
 		    (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
 			ret = ETIMEDOUT;
@@ -196,7 +196,7 @@ _thr_umtx_wait(volatile umtx_t *mtx, int exp, const struct timespec *timeout,
 	return (ETIMEDOUT);
 
 	clock_gettime(clockid, &ts);
-	TIMESPEC_ADD(&ts, &ts, timeout);
+	timespecadd(&ts, timeout, &ts);
 	ts2 = *timeout;
 
 	for (;;) {
@@ -220,7 +220,7 @@ _thr_umtx_wait(volatile umtx_t *mtx, int exp, const struct timespec *timeout,
 		}
 
 		clock_gettime(clockid, &ts3);
-		TIMESPEC_SUB(&ts2, &ts, &ts3);
+		timespecsub(&ts, &ts3, &ts2);
 		if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
 			ret = ETIMEDOUT;
 			break;
-- 
2.23.0

