1 /* $NetBSD: subr_time.c,v 1.4 2008/07/15 16:18:08 christos Exp $ */
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)kern_clock.c 8.5 (Berkeley) 1/21/94
32 * @(#)kern_time.c 8.4 (Berkeley) 5/26/95
35 #include <sys/cdefs.h>
36 __KERNEL_RCSID(0, "$NetBSD: subr_time.c,v 1.4 2008/07/15 16:18:08 christos Exp $");
38 #include <sys/param.h>
39 #include <sys/kernel.h>
40 #include <sys/timex.h>
42 #include <sys/timetc.h>
46 * Compute number of hz until specified time. Used to compute second
47 * argument to callout_reset() from an absolute time.
50 tvhzto(const struct timeval
*tvp
)
52 struct timeval now
, tv
;
54 tv
= *tvp
; /* Don't modify original tvp. */
56 timersub(&tv
, &now
, &tv
);
61 * Compute number of ticks in the specified amount of time.
64 tvtohz(const struct timeval
*tv
)
70 * If the number of usecs in the whole seconds part of the time
71 * difference fits in a long, then the total number of usecs will
72 * fit in an unsigned long. Compute the total and convert it to
73 * ticks, rounding up and adding 1 to allow for the current tick
74 * to expire. Rounding also depends on unsigned long arithmetic
77 * Otherwise, if the number of ticks in the whole seconds part of
78 * the time difference fits in a long, then convert the parts to
79 * ticks separately and add, using similar rounding methods and
80 * overflow avoidance. This method would work in the previous
81 * case, but it is slightly slower and assumes that hz is integral.
83 * Otherwise, round the time difference down to the maximum
84 * representable value.
86 * If ints are 32-bit, then the maximum value for any timeout in
87 * 10ms ticks is 248 days.
97 if (sec
< 0 || (sec
== 0 && usec
<= 0)) {
99 * Would expire now or in the past. Return 0 ticks.
100 * This is different from the legacy tvhzto() interface,
101 * and callers need to check for it.
104 } else if (sec
<= (LONG_MAX
/ 1000000))
105 ticks
= (((sec
* 1000000) + (unsigned long)usec
+ (tick
- 1))
107 else if (sec
<= (LONG_MAX
/ hz
))
109 (((unsigned long)usec
+ (tick
- 1)) / tick
) + 1;
120 tshzto(const struct timespec
*tsp
)
122 struct timespec now
, ts
;
124 ts
= *tsp
; /* Don't modify original tsp. */
126 timespecsub(&ts
, &now
, &ts
);
130 * Compute number of ticks in the specified amount of time.
133 tstohz(const struct timespec
*ts
)
138 * usec has great enough resolution for hz, so convert to a
139 * timeval and use tvtohz() above.
141 TIMESPEC_TO_TIMEVAL(&tv
, ts
);
146 * Check that a proposed value to load into the .it_value or
147 * .it_interval part of an interval timer is acceptable, and
148 * fix it to have at least minimal value (i.e. if it is less
149 * than the resolution of the clock, round it up.)
152 itimerfix(struct timeval
*tv
)
155 if (tv
->tv_sec
< 0 || tv
->tv_usec
< 0 || tv
->tv_usec
>= 1000000)
157 if (tv
->tv_sec
== 0 && tv
->tv_usec
!= 0 && tv
->tv_usec
< tick
)
163 itimespecfix(struct timespec
*ts
)
166 if (ts
->tv_sec
< 0 || ts
->tv_nsec
< 0 || ts
->tv_nsec
>= 1000000000)
168 if (ts
->tv_sec
== 0 && ts
->tv_nsec
!= 0 && ts
->tv_nsec
< tick
* 1000)
169 ts
->tv_nsec
= tick
* 1000;
174 inittimeleft(struct timespec
*ts
, struct timespec
*sleepts
)
177 if (itimespecfix(ts
)) {
180 getnanouptime(sleepts
);
185 gettimeleft(struct timespec
*ts
, struct timespec
*sleepts
)
187 struct timespec sleptts
;
190 * Reduce ts by elapsed time based on monotonic time scale.
192 getnanouptime(&sleptts
);
193 timespecadd(ts
, sleepts
, ts
);
194 timespecsub(ts
, &sleptts
, ts
);
201 * Calculate delta and convert from struct timespec to the ticks.
204 abstimeout2timo(struct timespec
*ts
, int *timo
)
210 timespecsub(ts
, &tsd
, &tsd
);
211 if (tsd
.tv_sec
< 0 || (tsd
.tv_sec
== 0 && tsd
.tv_nsec
<= 0)) {
214 error
= itimespecfix(&tsd
);
218 *timo
= tstohz(&tsd
);