Module Name: src Committed By: riastradh Date: Mon May 4 18:23:37 UTC 2020
Modified Files: src/sys/kern: kern_condvar.c subr_time.c src/sys/sys: timevar.h Log Message: 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); ... } To generate a diff of this commit: cvs rdiff -u -r1.50 -r1.51 src/sys/kern/kern_condvar.c cvs rdiff -u -r1.22 -r1.23 src/sys/kern/subr_time.c cvs rdiff -u -r1.42 -r1.43 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.50 src/sys/kern/kern_condvar.c:1.51 --- src/sys/kern/kern_condvar.c:1.50 Sun May 3 17:36:33 2020 +++ src/sys/kern/kern_condvar.c Mon May 4 18:23:37 2020 @@ -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 @@ int 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 @@ cv_timedwaitclock(kcondvar_t *cv, kmutex return 0; } + timedwaitclock_setup(&T, timeout, clockid, flags, epsilon); error = timedwaitclock_begin(&T, &timo); if (error) return error; @@ -301,18 +297,14 @@ int 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; Index: src/sys/kern/subr_time.c diff -u src/sys/kern/subr_time.c:1.22 src/sys/kern/subr_time.c:1.23 --- src/sys/kern/subr_time.c:1.22 Sun May 3 17:36:33 2020 +++ src/sys/kern/subr_time.c Mon May 4 18:23:37 2020 @@ -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 @@ ts2timo(clockid_t clock_id, int flags, s 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 @@ timedwaitclock_begin(struct timedwaitclo 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) { Index: src/sys/sys/timevar.h diff -u src/sys/sys/timevar.h:1.42 src/sys/sys/timevar.h:1.43 --- src/sys/sys/timevar.h:1.42 Sun May 3 17:36:33 2020 +++ src/sys/sys/timevar.h Mon May 4 18:23:37 2020 @@ -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_init(void); 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 *);