Module Name:    src
Committed By:   thorpej
Date:           Sun May  3 17:36:33 UTC 2020

Modified Files:
        src/sys/kern: kern_condvar.c subr_time.c
        src/sys/sys: timevar.h

Log Message:
Move timedwaitclock_begin() and timedwaitclock_end() to subr_time.c
so they can be used by other things.


To generate a diff of this commit:
cvs rdiff -u -r1.49 -r1.50 src/sys/kern/kern_condvar.c
cvs rdiff -u -r1.21 -r1.22 src/sys/kern/subr_time.c
cvs rdiff -u -r1.41 -r1.42 src/sys/sys/timevar.h

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/kern/kern_condvar.c
diff -u src/sys/kern/kern_condvar.c:1.49 src/sys/kern/kern_condvar.c:1.50
--- src/sys/kern/kern_condvar.c:1.49	Sun May  3 01:24:37 2020
+++ src/sys/kern/kern_condvar.c	Sun May  3 17:36:33 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: kern_condvar.c,v 1.49 2020/05/03 01:24:37 riastradh Exp $	*/
+/*	$NetBSD: kern_condvar.c,v 1.50 2020/05/03 17:36:33 thorpej Exp $	*/
 
 /*-
  * Copyright (c) 2006, 2007, 2008, 2019, 2020 The NetBSD Foundation, Inc.
@@ -34,7 +34,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.49 2020/05/03 01:24:37 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.50 2020/05/03 17:36:33 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -244,112 +244,6 @@ cv_timedwait_sig(kcondvar_t *cv, kmutex_
 	return error;
 }
 
-struct timedwaitclock {
-	struct timespec		*timeout;
-	clockid_t		clockid;
-	int			flags;
-	const struct bintime	*epsilon;
-	struct timespec		starttime;
-};
-
-static int
-cv_timedwaitclock_begin(struct timedwaitclock *T, int *timo)
-{
-	struct timespec delta;
-	const struct timespec *deltap;
-	int error;
-
-	/* Sanity-check timeout -- may have come from userland.  */
-	if (T->timeout->tv_nsec < 0 || T->timeout->tv_nsec >= 1000000000L)
-		return EINVAL;
-
-	/*
-	 * Compute the time delta.
-	 */
-	if ((T->flags & TIMER_ABSTIME) == TIMER_ABSTIME) {
-		/* Check our watch.  */
-		error = clock_gettime1(T->clockid, &T->starttime);
-		if (error)
-			return error;
-
-		/* If the deadline has passed, we're done.  */
-		if (timespeccmp(T->timeout, &T->starttime, <=))
-			return ETIMEDOUT;
-
-		/* Count how much time is left.  */
-		timespecsub(T->timeout, &T->starttime, &delta);
-		deltap = &delta;
-	} else {
-		/* The user specified how much time is left.  */
-		deltap = T->timeout;
-
-		/* If there's none left, we've timed out.  */
-		if (deltap->tv_sec == 0 && deltap->tv_nsec == 0)
-			return ETIMEDOUT;
-	}
-
-	/*
-	 * Convert to ticks, but clamp to be >=1.
-	 *
-	 * XXX In the tickless future, use a high-resolution timer if
-	 * timo would round to zero.
-	 */
-	*timo = tstohz(deltap);
-	KASSERTMSG(*timo >= 0, "negative ticks: %d", *timo);
-	if (*timo == 0)
-		*timo = 1;
-
-	/* Success!  */
-	return 0;
-}
-
-static void
-cv_timedwaitclock_end(struct timedwaitclock *T)
-{
-	struct timespec endtime, delta;
-
-	/* If the timeout is absolute, nothing to do.  */
-	if ((T->flags & TIMER_ABSTIME) == TIMER_ABSTIME)
-		return;
-
-	/*
-	 * Check our watch.  If anything goes wrong with it, make sure
-	 * that the next time we immediately time out rather than fail
-	 * to deduct the time elapsed.
-	 */
-	if (clock_gettime1(T->clockid, &endtime)) {
-		T->timeout->tv_sec = 0;
-		T->timeout->tv_nsec = 0;
-		return;
-	}
-
-	/* Find how much time elapsed while we waited.  */
-	timespecsub(&endtime, &T->starttime, &delta);
-
-	/*
-	 * Paranoia: If the clock went backwards, treat it as if no
-	 * time elapsed at all rather than adding anything.
-	 */
-	if (delta.tv_sec < 0 ||
-	    (delta.tv_sec == 0 && delta.tv_nsec < 0)) {
-		delta.tv_sec = 0;
-		delta.tv_nsec = 0;
-	}
-
-	/*
-	 * Set it to the time left, or zero, whichever is larger.  We
-	 * do not fail with EWOULDBLOCK here because this may have been
-	 * an explicit wakeup, so the caller needs to check before they
-	 * give up or else cv_signal would be lost.
-	 */
-	if (timespeccmp(T->timeout, &delta, <=)) {
-		T->timeout->tv_sec = 0;
-		T->timeout->tv_nsec = 0;
-	} else {
-		timespecsub(T->timeout, &delta, T->timeout);
-	}
-}
-
 /*
  * cv_timedwaitclock:
  *
@@ -381,11 +275,11 @@ cv_timedwaitclock(kcondvar_t *cv, kmutex
 		return 0;
 	}
 
-	error = cv_timedwaitclock_begin(&T, &timo);
+	error = timedwaitclock_begin(&T, &timo);
 	if (error)
 		return error;
 	error = cv_timedwait(cv, mtx, timo);
-	cv_timedwaitclock_end(&T);
+	timedwaitclock_end(&T);
 	return error;
 }
 
@@ -419,11 +313,11 @@ cv_timedwaitclock_sig(kcondvar_t *cv, km
 	if (timeout == NULL)
 		return cv_wait_sig(cv, mtx);
 
-	error = cv_timedwaitclock_begin(&T, &timo);
+	error = timedwaitclock_begin(&T, &timo);
 	if (error)
 		return error;
 	error = cv_timedwait_sig(cv, mtx, timo);
-	cv_timedwaitclock_end(&T);
+	timedwaitclock_end(&T);
 	return error;
 }
 

Index: src/sys/kern/subr_time.c
diff -u src/sys/kern/subr_time.c:1.21 src/sys/kern/subr_time.c:1.22
--- src/sys/kern/subr_time.c:1.21	Fri Oct  4 14:17:07 2019
+++ src/sys/kern/subr_time.c	Sun May  3 17:36:33 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_time.c,v 1.21 2019/10/04 14:17:07 kamil Exp $	*/
+/*	$NetBSD: subr_time.c,v 1.22 2020/05/03 17:36:33 thorpej Exp $	*/
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.21 2019/10/04 14:17:07 kamil Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.22 2020/05/03 17:36:33 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -354,3 +354,101 @@ ts2timo(clockid_t clock_id, int flags, s
 
 	return 0;
 }
+
+int
+timedwaitclock_begin(struct timedwaitclock *T, int *timo)
+{
+	struct timespec delta;
+	const struct timespec *deltap;
+	int error;
+
+	/* Sanity-check timeout -- may have come from userland.  */
+	if (T->timeout->tv_nsec < 0 || T->timeout->tv_nsec >= 1000000000L)
+		return EINVAL;
+
+	/*
+	 * Compute the time delta.
+	 */
+	if ((T->flags & TIMER_ABSTIME) == TIMER_ABSTIME) {
+		/* Check our watch.  */
+		error = clock_gettime1(T->clockid, &T->starttime);
+		if (error)
+			return error;
+
+		/* If the deadline has passed, we're done.  */
+		if (timespeccmp(T->timeout, &T->starttime, <=))
+			return ETIMEDOUT;
+
+		/* Count how much time is left.  */
+		timespecsub(T->timeout, &T->starttime, &delta);
+		deltap = &delta;
+	} else {
+		/* The user specified how much time is left.  */
+		deltap = T->timeout;
+
+		/* If there's none left, we've timed out.  */
+		if (deltap->tv_sec == 0 && deltap->tv_nsec == 0)
+			return ETIMEDOUT;
+	}
+
+	/*
+	 * Convert to ticks, but clamp to be >=1.
+	 *
+	 * XXX In the tickless future, use a high-resolution timer if
+	 * timo would round to zero.
+	 */
+	*timo = tstohz(deltap);
+	KASSERTMSG(*timo >= 0, "negative ticks: %d", *timo);
+	if (*timo == 0)
+		*timo = 1;
+
+	/* Success!  */
+	return 0;
+}
+
+void
+timedwaitclock_end(struct timedwaitclock *T)
+{
+	struct timespec endtime, delta;
+
+	/* If the timeout is absolute, nothing to do.  */
+	if ((T->flags & TIMER_ABSTIME) == TIMER_ABSTIME)
+		return;
+
+	/*
+	 * Check our watch.  If anything goes wrong with it, make sure
+	 * that the next time we immediately time out rather than fail
+	 * to deduct the time elapsed.
+	 */
+	if (clock_gettime1(T->clockid, &endtime)) {
+		T->timeout->tv_sec = 0;
+		T->timeout->tv_nsec = 0;
+		return;
+	}
+
+	/* Find how much time elapsed while we waited.  */
+	timespecsub(&endtime, &T->starttime, &delta);
+
+	/*
+	 * Paranoia: If the clock went backwards, treat it as if no
+	 * time elapsed at all rather than adding anything.
+	 */
+	if (delta.tv_sec < 0 ||
+	    (delta.tv_sec == 0 && delta.tv_nsec < 0)) {
+		delta.tv_sec = 0;
+		delta.tv_nsec = 0;
+	}
+
+	/*
+	 * Set it to the time left, or zero, whichever is larger.  We
+	 * do not fail with EWOULDBLOCK here because this may have been
+	 * an explicit wakeup, so the caller needs to check before they
+	 * give up or else cv_signal would be lost.
+	 */
+	if (timespeccmp(T->timeout, &delta, <=)) {
+		T->timeout->tv_sec = 0;
+		T->timeout->tv_nsec = 0;
+	} else {
+		timespecsub(T->timeout, &delta, T->timeout);
+	}
+}

Index: src/sys/sys/timevar.h
diff -u src/sys/sys/timevar.h:1.41 src/sys/sys/timevar.h:1.42
--- src/sys/sys/timevar.h:1.41	Sun May  3 01:20:37 2020
+++ src/sys/sys/timevar.h	Sun May  3 17:36:33 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: timevar.h,v 1.41 2020/05/03 01:20:37 riastradh Exp $	*/
+/*	$NetBSD: timevar.h,v 1.42 2020/05/03 17:36:33 thorpej Exp $	*/
 
 /*
- *  Copyright (c) 2005, 2008 The NetBSD Foundation.
+ *  Copyright (c) 2005, 2008, The NetBSD Foundation.
  *  All rights reserved.
  *
  *  Redistribution and use in source and binary forms, with or without
@@ -107,6 +107,14 @@ struct	ptimers {
 	struct ptimer *pts_timers[TIMER_MAX];
 };
 
+struct timedwaitclock {
+	struct timespec		*timeout;
+	clockid_t		clockid;
+	int			flags;
+	const struct bintime	*epsilon;
+	struct timespec		starttime;
+};
+
 /*
  * Functions for looking at our clock: [get]{bin,nano,micro}[up]time()
  *
@@ -192,6 +200,9 @@ void	time_init(void);
 void	time_init2(void);
 bool	time_wraps(struct timespec *, struct timespec *);
 
+int	timedwaitclock_begin(struct timedwaitclock *, int *);
+void	timedwaitclock_end(struct timedwaitclock *);
+
 extern volatile time_t time_second;	/* current second in the epoch */
 extern volatile time_t time_uptime;	/* system uptime in seconds */
 

Reply via email to