xref: /freebsd/sys/contrib/openzfs/lib/libspl/include/sys/timer.h (revision 8ccc0d235c226d84112561d453c49904398d085c)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License, Version 1.0 only
7  * (the "License").  You may not use this file except in compliance
8  * with the License.
9  *
10  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
11  * or https://opensource.org/licenses/CDDL-1.0.
12  * See the License for the specific language governing permissions
13  * and limitations under the License.
14  *
15  * When distributing Covered Code, include this CDDL HEADER in each
16  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
17  * If applicable, add the following below this CDDL HEADER, with the
18  * fields enclosed by brackets "[]" replaced with your own identifying
19  * information: Portions Copyright [yyyy] [name of copyright owner]
20  *
21  * CDDL HEADER END
22  */
23 /*
24  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
26  * Copyright (c) 2012, 2018 by Delphix. All rights reserved.
27  * Copyright (c) 2012, Joyent, Inc. All rights reserved.
28  */
29 
30 #ifndef _SPL_TIMER_H
31 #define	_SPL_TIMER_H
32 
33 #include <sys/time.h>
34 
35 #define	ddi_get_lbolt()		(gethrtime() >> 23)
36 #define	ddi_get_lbolt64()	(gethrtime() >> 23)
37 #define	hz	119	/* frequency when using gethrtime() >> 23 for lbolt */
38 
39 #define	ddi_time_before(a, b)		(a < b)
40 #define	ddi_time_after(a, b)		ddi_time_before(b, a)
41 #define	ddi_time_before_eq(a, b)	(!ddi_time_after(a, b))
42 #define	ddi_time_after_eq(a, b)		ddi_time_before_eq(b, a)
43 
44 #define	ddi_time_before64(a, b)		(a < b)
45 #define	ddi_time_after64(a, b)		ddi_time_before64(b, a)
46 #define	ddi_time_before_eq64(a, b)	(!ddi_time_after64(a, b))
47 #define	ddi_time_after_eq64(a, b)	ddi_time_before_eq64(b, a)
48 
49 extern void delay(clock_t ticks);
50 
51 #define	SEC_TO_TICK(sec)	((sec) * hz)
52 #define	MSEC_TO_TICK(msec)	(howmany((hrtime_t)(msec) * hz, MILLISEC))
53 #define	USEC_TO_TICK(usec)	(howmany((hrtime_t)(usec) * hz, MICROSEC))
54 #define	NSEC_TO_TICK(nsec)	(howmany((hrtime_t)(nsec) * hz, NANOSEC))
55 
56 #define	usleep_range(min, max)						\
57 	do {								\
58 		struct timespec ts;					\
59 		ts.tv_sec = min / MICROSEC;				\
60 		ts.tv_nsec = USEC2NSEC(min);				\
61 		(void) nanosleep(&ts, NULL);				\
62 	} while (0)
63 
64 #endif  /* _SPL_TIMER_H */
65