changeset 1134:e16b0e3a137c

irccd: fix EINVAL in pthread_cond_timedwait
author David Demelier <markand@malikania.fr>
date Thu, 09 Dec 2021 14:13:46 +0100
parents 8ea7366ef45a
children 2daceccf7392
files irccd/jsapi-timer.c
diffstat 1 files changed, 7 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/irccd/jsapi-timer.c	Thu Dec 09 14:13:14 2021 +0100
+++ b/irccd/jsapi-timer.c	Thu Dec 09 14:13:46 2021 +0100
@@ -64,9 +64,9 @@
 {
 	tm->status = TIMER_INACTIVE;
 
+	pthread_join(tm->thr, NULL);
 	pthread_cond_destroy(&tm->cv);
 	pthread_mutex_destroy(&tm->mtx);
-	pthread_join(tm->thr, NULL);
 }
 
 static void
@@ -128,8 +128,12 @@
 	/* Prepare maximum time to wait. */
 	timespec_get(&ts, TIME_UTC);
 
-	ts.tv_sec += tm->duration / 1000;
-	ts.tv_nsec += (tm->duration % 1000) * 1000;
+	ts.tv_sec  += tm->duration / 1000;
+	ts.tv_nsec += (tm->duration % 1000) * 1000000;
+
+	/* Readjust to avoid EINVAL. */
+	ts.tv_sec  += ts.tv_nsec / 1000000000;
+	ts.tv_nsec %= 1000000000;
 
 	if (pthread_mutex_lock(&tm->mtx) != 0)
 		tm->status = TIMER_MUST_STOP;