Source-Changes-HG archive

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

[src/trunk]: src/sys New timedwaitclock_setup.



details:   https://anonhg.NetBSD.org/src/rev/e2a12a078e32
branches:  trunk
changeset: 1009821:e2a12a078e32
user:      riastradh <riastradh%NetBSD.org@localhost>
date:      Mon May 04 18:23:37 2020 +0000

description:
New timedwaitclock_setup.

C99 initializers would have been nice, but part of the struct is
explicit parameters and part of the struct is implicit state, and
-Wmissing-field-initializers can't discriminate between them
(although for some reason it doesn't always fire!).

Instead, just do:

        struct timedwaitclock T;

        timedwaitclock_setup(&T, timeout, clockid, flags, epsilon);
        while (...) {
                error = timedwaitclock_begin(&T, &timo);
                if (error)
                        ...
                error = waitwhatever(timo);
                timedwaitclock_end(&T);
                ...
        }

diffstat:

 sys/kern/kern_condvar.c |  20 ++++++--------------
 sys/kern/subr_time.c    |  44 ++++++++++++++++++++++++++++++++++++++++++--
 sys/sys/timevar.h       |   4 +++-
 3 files changed, 51 insertions(+), 17 deletions(-)

diffs (151 lines):

diff -r e46a2523cc06 -r e2a12a078e32 sys/kern/kern_condvar.c
--- a/sys/kern/kern_condvar.c   Mon May 04 18:19:34 2020 +0000
+++ b/sys/kern/kern_condvar.c   Mon May 04 18:23:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: kern_condvar.c,v 1.50 2020/05/03 17:36:33 thorpej Exp $        */
+/*     $NetBSD: kern_condvar.c,v 1.51 2020/05/04 18:23:37 riastradh 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.50 2020/05/03 17:36:33 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: kern_condvar.c,v 1.51 2020/05/04 18:23:37 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -261,12 +261,7 @@
 cv_timedwaitclock(kcondvar_t *cv, kmutex_t *mtx, struct timespec *timeout,
     clockid_t clockid, int flags, const struct bintime *epsilon)
 {
-       struct timedwaitclock T = {
-               .timeout = timeout,
-               .clockid = clockid,
-               .flags = flags,
-               .epsilon = epsilon,
-       };
+       struct timedwaitclock T;
        int timo;
        int error;
 
@@ -275,6 +270,7 @@
                return 0;
        }
 
+       timedwaitclock_setup(&T, timeout, clockid, flags, epsilon);
        error = timedwaitclock_begin(&T, &timo);
        if (error)
                return error;
@@ -301,18 +297,14 @@
 cv_timedwaitclock_sig(kcondvar_t *cv, kmutex_t *mtx, struct timespec *timeout,
     clockid_t clockid, int flags, const struct bintime *epsilon)
 {
-       struct timedwaitclock T = {
-               .timeout = timeout,
-               .clockid = clockid,
-               .flags = flags,
-               .epsilon = epsilon,
-       };
+       struct timedwaitclock T;
        int timo;
        int error;
 
        if (timeout == NULL)
                return cv_wait_sig(cv, mtx);
 
+       timedwaitclock_setup(&T, timeout, clockid, flags, epsilon);
        error = timedwaitclock_begin(&T, &timo);
        if (error)
                return error;
diff -r e46a2523cc06 -r e2a12a078e32 sys/kern/subr_time.c
--- a/sys/kern/subr_time.c      Mon May 04 18:19:34 2020 +0000
+++ b/sys/kern/subr_time.c      Mon May 04 18:23:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: subr_time.c,v 1.22 2020/05/03 17:36:33 thorpej Exp $   */
+/*     $NetBSD: subr_time.c,v 1.23 2020/05/04 18:23:37 riastradh Exp $ */
 
 /*
  * Copyright (c) 1982, 1986, 1989, 1993
@@ -33,7 +33,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.22 2020/05/03 17:36:33 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.23 2020/05/04 18:23:37 riastradh Exp $");
 
 #include <sys/param.h>
 #include <sys/kernel.h>
@@ -355,6 +355,38 @@
        return 0;
 }
 
+/*
+ * timedwaitclock_setup(T, timeout, clockid, flags, epsilon)
+ *
+ *     Initialize state for a timedwaitclock, to be used subsequently
+ *     with timedwaitclock_begin/end, possibly many times in a row.
+ *
+ *     No cleanup action required at the end; the caller-allocated
+ *     (typically stack-allocated) timedwaitclock just holds
+ *     parameters and a little state for timedwaitclock_begin/end.
+ */
+void
+timedwaitclock_setup(struct timedwaitclock *T, struct timespec *timeout,
+    clockid_t clockid, int flags, const struct bintime *epsilon)
+{
+
+       memset(T, 0, sizeof(*T));
+       T->timeout = timeout;
+       T->clockid = clockid;
+       T->flags = flags;
+       T->epsilon = epsilon;
+       T->starttime = (struct timespec){0,0};
+}
+
+/*
+ * timedwaitclock_begin(T, timo)
+ *
+ *     Decide how many ticks to wait for the timedwaitclock T and
+ *     store it in *timo.  Keep state for timedwaitclock_end.  May
+ *     fail with EINVAL if the specified timeout is invalid, or if the
+ *     specified clock fails.  Fails with ETIMEDOUT if there is no
+ *     time left to wait.
+ */
 int
 timedwaitclock_begin(struct timedwaitclock *T, int *timo)
 {
@@ -406,6 +438,14 @@
        return 0;
 }
 
+/*
+ * timedwaitclock_end(T)
+ *
+ *     If the timedwaitclock T was relative, update the caller's
+ *     original timeout to reflect how much time is left, or zero if
+ *     there is no time left or if the clock has gone bad, so that the
+ *     next timedwaitclock_begin will immediately time out.
+ */
 void
 timedwaitclock_end(struct timedwaitclock *T)
 {
diff -r e46a2523cc06 -r e2a12a078e32 sys/sys/timevar.h
--- a/sys/sys/timevar.h Mon May 04 18:19:34 2020 +0000
+++ b/sys/sys/timevar.h Mon May 04 18:23:37 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: timevar.h,v 1.42 2020/05/03 17:36:33 thorpej Exp $     */
+/*     $NetBSD: timevar.h,v 1.43 2020/05/04 18:23:37 riastradh Exp $   */
 
 /*
  *  Copyright (c) 2005, 2008, The NetBSD Foundation.
@@ -200,6 +200,8 @@
 void   time_init2(void);
 bool   time_wraps(struct timespec *, struct timespec *);
 
+void   timedwaitclock_setup(struct timedwaitclock *, struct timespec *,
+           clockid_t, int, const struct bintime *);
 int    timedwaitclock_begin(struct timedwaitclock *, int *);
 void   timedwaitclock_end(struct timedwaitclock *);
 



Home | Main Index | Thread Index | Old Index