Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/sys Move timedwaitclock_begin() and timedwaitclock_end() to ...



details:   https://anonhg.NetBSD.org/src/rev/bc7ee37f5153
branches:  trunk
changeset: 932267:bc7ee37f5153
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sun May 03 17:36:33 2020 +0000

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

diffstat:

 sys/kern/kern_condvar.c |  118 ++---------------------------------------------
 sys/kern/subr_time.c    |  102 ++++++++++++++++++++++++++++++++++++++++-
 sys/sys/timevar.h       |   15 +++++-
 3 files changed, 119 insertions(+), 116 deletions(-)

diffs (truncated from 317 to 300 lines):

diff -r eccd106ca08d -r bc7ee37f5153 sys/kern/kern_condvar.c
--- a/sys/kern/kern_condvar.c   Sun May 03 17:24:11 2020 +0000
+++ b/sys/kern/kern_condvar.c   Sun May 03 17:36:33 2020 +0000
@@ -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 @@
        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 @@
                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 @@
        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;
 }
 
diff -r eccd106ca08d -r bc7ee37f5153 sys/kern/subr_time.c
--- a/sys/kern/subr_time.c      Sun May 03 17:24:11 2020 +0000
+++ b/sys/kern/subr_time.c      Sun May 03 17:36:33 2020 +0000
@@ -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 @@
 
        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);
+       }
+}
diff -r eccd106ca08d -r bc7ee37f5153 sys/sys/timevar.h
--- a/sys/sys/timevar.h Sun May 03 17:24:11 2020 +0000
+++ b/sys/sys/timevar.h Sun May 03 17:36:33 2020 +0000
@@ -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 ptimer *pts_timers[TIMER_MAX];
 };
 
+struct timedwaitclock {
+       struct timespec         *timeout;
+       clockid_t               clockid;
+       int                     flags;



Home | Main Index | Thread Index | Old Index