xref: /netbsd-src/external/cddl/osnet/sys/sys/time.h (revision b132f31296fbdef312449ce310df331c8ef14ff1)
1 /*	$NetBSD: time.h,v 1.15 2022/10/31 04:50:12 simonb Exp $	*/
2 
3 /*-
4  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD: head/sys/cddl/compat/opensolaris/sys/time.h 296530 2016-03-08 18:28:24Z mav $
29  */
30 
31 #ifndef _OPENSOLARIS_SYS_TIME_H_
32 #define	_OPENSOLARIS_SYS_TIME_H_
33 
34 #include_next <sys/time.h>
35 
36 #define SEC		1
37 #define MILLISEC	1000
38 #define MICROSEC	1000000
39 #define NANOSEC		1000000000
40 #define TIME_MAX	LLONG_MAX
41 
42 #define	MSEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / MILLISEC))
43 #define	NSEC2MSEC(n)	((n) / (NANOSEC / MILLISEC))
44 
45 #define	NSEC2SEC(n)	((n) / (NANOSEC / SEC))
46 #define	SEC2NSEC(m)	((hrtime_t)(m) * (NANOSEC / SEC))
47 
48 #ifndef __defined_hr_t
49 #define __defined_hr_t
50 typedef longlong_t	hrtime_t;
51 #endif
52 
53 /* FreeBSD i386 and PPC use int32 for time, all else int64 */
54 #if defined(__FreeBSD__) && (defined(__i386__) || defined(__powerpc__))
55 #define	TIMESPEC_OVERFLOW(ts)						\
56 	((ts)->tv_sec < INT32_MIN || (ts)->tv_sec > INT32_MAX)
57 #else
58 #define	TIMESPEC_OVERFLOW(ts)						\
59 	((ts)->tv_sec < INT64_MIN || (ts)->tv_sec > INT64_MAX)
60 #endif
61 
62 #define	SEC_TO_TICK(sec)	((sec) * hz)
63 #define	NSEC_TO_TICK(nsec)	((nsec) / (NANOSEC / hz))
64 
65 #ifdef _KERNEL
66 
67 static __inline hrtime_t
gethrtime(void)68 gethrtime(void) {
69 
70 	struct timespec ts;
71 	hrtime_t nsec;
72 
73 	getnanouptime(&ts);
74 	nsec = (hrtime_t)ts.tv_sec * NANOSEC + ts.tv_nsec;
75 	return (nsec);
76 }
77 
78 #define	gethrestime_sec()	(time_second)
79 #define	gethrestime(ts)		getnanotime(ts)
80 #define	gethrtime_waitfree()	gethrtime()
81 
82 static inline int64_t
ddi_get_lbolt64(void)83 ddi_get_lbolt64(void)
84 {
85 	struct timespec ts;
86 	extern int hz;
87 
88 	getnanouptime(&ts);
89 	return (int64_t)(SEC_TO_TICK(ts.tv_sec) + NSEC_TO_TICK(ts.tv_nsec));
90 }
91 
92 #define ddi_get_lbolt()		(clock_t)ddi_get_lbolt64()
93 
94 #else
95 
96 #ifdef __NetBSD__
97 int clock_gettime(clockid_t, struct timespec *)
98     __RENAME(__clock_gettime50);
99 #endif
100 #if defined(__linux__) || defined(__GNU__)
101 #include <time.h>
102 #endif
103 
gethrtime(void)104 static __inline hrtime_t gethrtime(void) {
105 	struct timespec ts;
106 	clock_gettime(CLOCK_REALTIME,&ts);
107 	return (((u_int64_t) ts.tv_sec) * NANOSEC + ts.tv_nsec);
108 }
109 
110 #define	ddi_get_lbolt()		(gethrtime() >> 23)
111 #define	ddi_get_lbolt64()	(gethrtime() >> 23)
112 
113 #endif	/* _KERNEL */
114 
115 #endif	/* !_OPENSOLARIS_SYS_TIME_H_ */
116