1 | /*- |
---|
2 | * Copyright (c) 1982, 1986, 1993 |
---|
3 | * The Regents of the University of California. All rights reserved. |
---|
4 | * |
---|
5 | * Redistribution and use in source and binary forms, with or without |
---|
6 | * modification, are permitted provided that the following conditions |
---|
7 | * are met: |
---|
8 | * 1. Redistributions of source code must retain the above copyright |
---|
9 | * notice, this list of conditions and the following disclaimer. |
---|
10 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
11 | * notice, this list of conditions and the following disclaimer in the |
---|
12 | * documentation and/or other materials provided with the distribution. |
---|
13 | * 4. Neither the name of the University nor the names of its contributors |
---|
14 | * may be used to endorse or promote products derived from this software |
---|
15 | * without specific prior written permission. |
---|
16 | * |
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
27 | * SUCH DAMAGE. |
---|
28 | * |
---|
29 | * @(#)time.h 8.5 (Berkeley) 5/4/95 |
---|
30 | * $FreeBSD$ |
---|
31 | */ |
---|
32 | |
---|
33 | #ifndef _RTEMS_BSD_SYS_TIME_H_ |
---|
34 | #define _RTEMS_BSD_SYS_TIME_H_ |
---|
35 | |
---|
36 | #include <sys/_timeval.h> |
---|
37 | #include <rtems/bsd/sys/types.h> |
---|
38 | #include <rtems/bsd/sys/timespec.h> |
---|
39 | #ifdef __rtems__ |
---|
40 | #include <sys/time.h> |
---|
41 | #endif /* __rtems__ */ |
---|
42 | |
---|
43 | #ifndef __rtems__ |
---|
44 | struct timezone { |
---|
45 | int tz_minuteswest; /* minutes west of Greenwich */ |
---|
46 | int tz_dsttime; /* type of dst correction */ |
---|
47 | }; |
---|
48 | #endif |
---|
49 | |
---|
50 | #define DST_NONE 0 /* not on dst */ |
---|
51 | #define DST_USA 1 /* USA style dst */ |
---|
52 | #define DST_AUST 2 /* Australian style dst */ |
---|
53 | #define DST_WET 3 /* Western European dst */ |
---|
54 | #define DST_MET 4 /* Middle European dst */ |
---|
55 | #define DST_EET 5 /* Eastern European dst */ |
---|
56 | #define DST_CAN 6 /* Canada */ |
---|
57 | |
---|
58 | #if __BSD_VISIBLE |
---|
59 | struct bintime { |
---|
60 | time_t sec; |
---|
61 | uint64_t frac; |
---|
62 | }; |
---|
63 | |
---|
64 | static __inline void |
---|
65 | bintime_addx(struct bintime *bt, uint64_t x) |
---|
66 | { |
---|
67 | uint64_t u; |
---|
68 | |
---|
69 | u = bt->frac; |
---|
70 | bt->frac += x; |
---|
71 | if (u > bt->frac) |
---|
72 | bt->sec++; |
---|
73 | } |
---|
74 | |
---|
75 | static __inline void |
---|
76 | bintime_add(struct bintime *bt, const struct bintime *bt2) |
---|
77 | { |
---|
78 | uint64_t u; |
---|
79 | |
---|
80 | u = bt->frac; |
---|
81 | bt->frac += bt2->frac; |
---|
82 | if (u > bt->frac) |
---|
83 | bt->sec++; |
---|
84 | bt->sec += bt2->sec; |
---|
85 | } |
---|
86 | |
---|
87 | static __inline void |
---|
88 | bintime_sub(struct bintime *bt, const struct bintime *bt2) |
---|
89 | { |
---|
90 | uint64_t u; |
---|
91 | |
---|
92 | u = bt->frac; |
---|
93 | bt->frac -= bt2->frac; |
---|
94 | if (u < bt->frac) |
---|
95 | bt->sec--; |
---|
96 | bt->sec -= bt2->sec; |
---|
97 | } |
---|
98 | |
---|
99 | /*- |
---|
100 | * Background information: |
---|
101 | * |
---|
102 | * When converting between timestamps on parallel timescales of differing |
---|
103 | * resolutions it is historical and scientific practice to round down rather |
---|
104 | * than doing 4/5 rounding. |
---|
105 | * |
---|
106 | * The date changes at midnight, not at noon. |
---|
107 | * |
---|
108 | * Even at 15:59:59.999999999 it's not four'o'clock. |
---|
109 | * |
---|
110 | * time_second ticks after N.999999999 not after N.4999999999 |
---|
111 | */ |
---|
112 | |
---|
113 | static __inline void |
---|
114 | bintime2timespec(const struct bintime *bt, struct timespec *ts) |
---|
115 | { |
---|
116 | |
---|
117 | ts->tv_sec = bt->sec; |
---|
118 | ts->tv_nsec = ((uint64_t)1000000000 * (uint32_t)(bt->frac >> 32)) >> 32; |
---|
119 | } |
---|
120 | |
---|
121 | static __inline void |
---|
122 | timespec2bintime(const struct timespec *ts, struct bintime *bt) |
---|
123 | { |
---|
124 | |
---|
125 | bt->sec = ts->tv_sec; |
---|
126 | /* 18446744073 = int(2^64 / 1000000000) */ |
---|
127 | bt->frac = ts->tv_nsec * (uint64_t)18446744073LL; |
---|
128 | } |
---|
129 | |
---|
130 | static __inline void |
---|
131 | bintime2timeval(const struct bintime *bt, struct timeval *tv) |
---|
132 | { |
---|
133 | |
---|
134 | tv->tv_sec = bt->sec; |
---|
135 | tv->tv_usec = ((uint64_t)1000000 * (uint32_t)(bt->frac >> 32)) >> 32; |
---|
136 | } |
---|
137 | |
---|
138 | static __inline void |
---|
139 | timeval2bintime(const struct timeval *tv, struct bintime *bt) |
---|
140 | { |
---|
141 | |
---|
142 | bt->sec = tv->tv_sec; |
---|
143 | /* 18446744073709 = int(2^64 / 1000000) */ |
---|
144 | bt->frac = tv->tv_usec * (uint64_t)18446744073709LL; |
---|
145 | } |
---|
146 | #endif /* __BSD_VISIBLE */ |
---|
147 | |
---|
148 | #ifdef _KERNEL |
---|
149 | |
---|
150 | /* Operations on timespecs */ |
---|
151 | #define timespecclear(tvp) ((tvp)->tv_sec = (tvp)->tv_nsec = 0) |
---|
152 | #define timespecisset(tvp) ((tvp)->tv_sec || (tvp)->tv_nsec) |
---|
153 | #define timespeccmp(tvp, uvp, cmp) \ |
---|
154 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \ |
---|
155 | ((tvp)->tv_nsec cmp (uvp)->tv_nsec) : \ |
---|
156 | ((tvp)->tv_sec cmp (uvp)->tv_sec)) |
---|
157 | #define timespecadd(vvp, uvp) \ |
---|
158 | do { \ |
---|
159 | (vvp)->tv_sec += (uvp)->tv_sec; \ |
---|
160 | (vvp)->tv_nsec += (uvp)->tv_nsec; \ |
---|
161 | if ((vvp)->tv_nsec >= 1000000000) { \ |
---|
162 | (vvp)->tv_sec++; \ |
---|
163 | (vvp)->tv_nsec -= 1000000000; \ |
---|
164 | } \ |
---|
165 | } while (0) |
---|
166 | #define timespecsub(vvp, uvp) \ |
---|
167 | do { \ |
---|
168 | (vvp)->tv_sec -= (uvp)->tv_sec; \ |
---|
169 | (vvp)->tv_nsec -= (uvp)->tv_nsec; \ |
---|
170 | if ((vvp)->tv_nsec < 0) { \ |
---|
171 | (vvp)->tv_sec--; \ |
---|
172 | (vvp)->tv_nsec += 1000000000; \ |
---|
173 | } \ |
---|
174 | } while (0) |
---|
175 | |
---|
176 | /* Operations on timevals. */ |
---|
177 | |
---|
178 | #define timevalclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) |
---|
179 | #define timevalisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) |
---|
180 | #define timevalcmp(tvp, uvp, cmp) \ |
---|
181 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \ |
---|
182 | ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ |
---|
183 | ((tvp)->tv_sec cmp (uvp)->tv_sec)) |
---|
184 | |
---|
185 | /* timevaladd and timevalsub are not inlined */ |
---|
186 | |
---|
187 | #endif /* _KERNEL */ |
---|
188 | |
---|
189 | #ifndef _KERNEL /* NetBSD/OpenBSD compatible interfaces */ |
---|
190 | |
---|
191 | #define timerclear(tvp) ((tvp)->tv_sec = (tvp)->tv_usec = 0) |
---|
192 | #define timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) |
---|
193 | #ifndef __rtems__ |
---|
194 | #define timercmp(tvp, uvp, cmp) \ |
---|
195 | (((tvp)->tv_sec == (uvp)->tv_sec) ? \ |
---|
196 | ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ |
---|
197 | ((tvp)->tv_sec cmp (uvp)->tv_sec)) |
---|
198 | #define timeradd(tvp, uvp, vvp) \ |
---|
199 | do { \ |
---|
200 | (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ |
---|
201 | (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ |
---|
202 | if ((vvp)->tv_usec >= 1000000) { \ |
---|
203 | (vvp)->tv_sec++; \ |
---|
204 | (vvp)->tv_usec -= 1000000; \ |
---|
205 | } \ |
---|
206 | } while (0) |
---|
207 | #define timersub(tvp, uvp, vvp) \ |
---|
208 | do { \ |
---|
209 | (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ |
---|
210 | (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ |
---|
211 | if ((vvp)->tv_usec < 0) { \ |
---|
212 | (vvp)->tv_sec--; \ |
---|
213 | (vvp)->tv_usec += 1000000; \ |
---|
214 | } \ |
---|
215 | } while (0) |
---|
216 | #endif |
---|
217 | #endif |
---|
218 | |
---|
219 | /* |
---|
220 | * Names of the interval timers, and structure |
---|
221 | * defining a timer setting. |
---|
222 | */ |
---|
223 | #define ITIMER_REAL 0 |
---|
224 | #define ITIMER_VIRTUAL 1 |
---|
225 | #define ITIMER_PROF 2 |
---|
226 | |
---|
227 | #ifndef __rtems__ |
---|
228 | struct itimerval { |
---|
229 | struct timeval it_interval; /* timer interval */ |
---|
230 | struct timeval it_value; /* current value */ |
---|
231 | }; |
---|
232 | #endif |
---|
233 | |
---|
234 | /* |
---|
235 | * Getkerninfo clock information structure |
---|
236 | */ |
---|
237 | struct clockinfo { |
---|
238 | int hz; /* clock frequency */ |
---|
239 | int tick; /* micro-seconds per hz tick */ |
---|
240 | int spare; |
---|
241 | int stathz; /* statistics clock frequency */ |
---|
242 | int profhz; /* profiling clock frequency */ |
---|
243 | }; |
---|
244 | |
---|
245 | /* These macros are also in time.h. */ |
---|
246 | #ifndef CLOCK_REALTIME |
---|
247 | #define CLOCK_REALTIME 0 |
---|
248 | #define CLOCK_VIRTUAL 1 |
---|
249 | #define CLOCK_PROF 2 |
---|
250 | #define CLOCK_MONOTONIC 4 |
---|
251 | #define CLOCK_UPTIME 5 /* FreeBSD-specific. */ |
---|
252 | #define CLOCK_UPTIME_PRECISE 7 /* FreeBSD-specific. */ |
---|
253 | #define CLOCK_UPTIME_FAST 8 /* FreeBSD-specific. */ |
---|
254 | #define CLOCK_REALTIME_PRECISE 9 /* FreeBSD-specific. */ |
---|
255 | #define CLOCK_REALTIME_FAST 10 /* FreeBSD-specific. */ |
---|
256 | #define CLOCK_MONOTONIC_PRECISE 11 /* FreeBSD-specific. */ |
---|
257 | #define CLOCK_MONOTONIC_FAST 12 /* FreeBSD-specific. */ |
---|
258 | #define CLOCK_SECOND 13 /* FreeBSD-specific. */ |
---|
259 | #define CLOCK_THREAD_CPUTIME_ID 14 |
---|
260 | #endif |
---|
261 | |
---|
262 | #ifndef TIMER_ABSTIME |
---|
263 | #define TIMER_RELTIME 0x0 /* relative timer */ |
---|
264 | #define TIMER_ABSTIME 0x1 /* absolute timer */ |
---|
265 | #endif |
---|
266 | |
---|
267 | #ifdef _KERNEL |
---|
268 | |
---|
269 | /* |
---|
270 | * Kernel to clock driver interface. |
---|
271 | */ |
---|
272 | void inittodr(time_t base); |
---|
273 | void resettodr(void); |
---|
274 | |
---|
275 | #ifndef __rtems__ |
---|
276 | extern time_t time_second; |
---|
277 | extern time_t time_uptime; |
---|
278 | #else /* __rtems__ */ |
---|
279 | #include <rtems.h> |
---|
280 | |
---|
281 | static inline time_t |
---|
282 | rtems_bsd_time_second(void) |
---|
283 | { |
---|
284 | return time(NULL); |
---|
285 | } |
---|
286 | |
---|
287 | static inline time_t |
---|
288 | rtems_bsd_time_uptime(void) |
---|
289 | { |
---|
290 | return rtems_clock_get_uptime_seconds(); |
---|
291 | } |
---|
292 | |
---|
293 | #define time_second rtems_bsd_time_second() |
---|
294 | #define time_uptime rtems_bsd_time_uptime() |
---|
295 | #endif /* __rtems__ */ |
---|
296 | extern struct timeval boottime; |
---|
297 | |
---|
298 | /* |
---|
299 | * Functions for looking at our clock: [get]{bin,nano,micro}[up]time() |
---|
300 | * |
---|
301 | * Functions without the "get" prefix returns the best timestamp |
---|
302 | * we can produce in the given format. |
---|
303 | * |
---|
304 | * "bin" == struct bintime == seconds + 64 bit fraction of seconds. |
---|
305 | * "nano" == struct timespec == seconds + nanoseconds. |
---|
306 | * "micro" == struct timeval == seconds + microseconds. |
---|
307 | * |
---|
308 | * Functions containing "up" returns time relative to boot and |
---|
309 | * should be used for calculating time intervals. |
---|
310 | * |
---|
311 | * Functions without "up" returns GMT time. |
---|
312 | * |
---|
313 | * Functions with the "get" prefix returns a less precise result |
---|
314 | * much faster than the functions without "get" prefix and should |
---|
315 | * be used where a precision of 10 msec is acceptable or where |
---|
316 | * performance is priority. (NB: "precision", _not_ "resolution" !) |
---|
317 | * |
---|
318 | */ |
---|
319 | |
---|
320 | #ifndef __rtems__ |
---|
321 | void binuptime(struct bintime *bt); |
---|
322 | #else /* __rtems__ */ |
---|
323 | static inline void |
---|
324 | binuptime(struct bintime *bt) |
---|
325 | { |
---|
326 | struct timeval tv; |
---|
327 | |
---|
328 | rtems_clock_get_uptime_timeval(&tv); |
---|
329 | timeval2bintime(&tv, bt); |
---|
330 | } |
---|
331 | #endif /* __rtems__ */ |
---|
332 | void nanouptime(struct timespec *tsp); |
---|
333 | void microuptime(struct timeval *tvp); |
---|
334 | |
---|
335 | #ifndef __rtems__ |
---|
336 | void bintime(struct bintime *bt); |
---|
337 | #else /* __rtems__ */ |
---|
338 | static inline void |
---|
339 | bintime(struct bintime *bt) |
---|
340 | { |
---|
341 | struct timeval tv; |
---|
342 | |
---|
343 | gettimeofday(&tv, NULL); |
---|
344 | timeval2bintime(&tv, bt); |
---|
345 | } |
---|
346 | #endif /* __rtems__ */ |
---|
347 | void nanotime(struct timespec *tsp); |
---|
348 | #ifndef __rtems__ |
---|
349 | void microtime(struct timeval *tvp); |
---|
350 | #else /* __rtems__ */ |
---|
351 | static inline void |
---|
352 | microtime(struct timeval *tvp) |
---|
353 | { |
---|
354 | gettimeofday(tvp, NULL); |
---|
355 | } |
---|
356 | #endif /* __rtems__ */ |
---|
357 | |
---|
358 | void getbinuptime(struct bintime *bt); |
---|
359 | void getnanouptime(struct timespec *tsp); |
---|
360 | #ifndef __rtems__ |
---|
361 | void getmicrouptime(struct timeval *tvp); |
---|
362 | #else /* __rtems__ */ |
---|
363 | static inline void |
---|
364 | getmicrouptime(struct timeval *tvp) |
---|
365 | { |
---|
366 | rtems_clock_get_uptime_timeval(tvp); |
---|
367 | } |
---|
368 | #endif /* __rtems__ */ |
---|
369 | |
---|
370 | void getbintime(struct bintime *bt); |
---|
371 | void getnanotime(struct timespec *tsp); |
---|
372 | #ifndef __rtems__ |
---|
373 | void getmicrotime(struct timeval *tvp); |
---|
374 | #else /* __rtems__ */ |
---|
375 | static inline void |
---|
376 | getmicrotime(struct timeval *tvp) |
---|
377 | { |
---|
378 | microtime(tvp); |
---|
379 | } |
---|
380 | #endif /* __rtems__ */ |
---|
381 | |
---|
382 | /* Other functions */ |
---|
383 | int itimerdecr(struct itimerval *itp, int usec); |
---|
384 | int itimerfix(struct timeval *tv); |
---|
385 | int ppsratecheck(struct timeval *, int *, int); |
---|
386 | int ratecheck(struct timeval *, const struct timeval *); |
---|
387 | void timevaladd(struct timeval *t1, const struct timeval *t2); |
---|
388 | void timevalsub(struct timeval *t1, const struct timeval *t2); |
---|
389 | int tvtohz(struct timeval *tv); |
---|
390 | #else /* !_KERNEL */ |
---|
391 | #include <time.h> |
---|
392 | |
---|
393 | #include <sys/cdefs.h> |
---|
394 | #include <sys/select.h> |
---|
395 | |
---|
396 | __BEGIN_DECLS |
---|
397 | int setitimer(int, const struct itimerval *, struct itimerval *); |
---|
398 | int utimes(const char *, const struct timeval *); |
---|
399 | |
---|
400 | #if __BSD_VISIBLE |
---|
401 | int adjtime(const struct timeval *, struct timeval *); |
---|
402 | int futimes(int, const struct timeval *); |
---|
403 | int futimesat(int, const char *, const struct timeval [2]); |
---|
404 | int lutimes(const char *, const struct timeval *); |
---|
405 | int settimeofday(const struct timeval *, const struct timezone *); |
---|
406 | #endif |
---|
407 | |
---|
408 | #ifndef __rtems__ |
---|
409 | #if __XSI_VISIBLE |
---|
410 | int getitimer(int, struct itimerval *); |
---|
411 | int gettimeofday(struct timeval *, struct timezone *); |
---|
412 | #endif |
---|
413 | #endif |
---|
414 | |
---|
415 | __END_DECLS |
---|
416 | |
---|
417 | #endif /* !_KERNEL */ |
---|
418 | |
---|
419 | #endif /* !_RTEMS_BSD_SYS_TIME_H_ */ |
---|