summaryrefslogtreecommitdiff
path: root/usr/src/lib/libc/port/sys/time_util.c
blob: b6ea0291e2d23896dbba2bbc260296f052752431 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 * Copyright 2016 Joyent, Inc.
 */

#include <sys/types.h>
#include <time.h>
#include <errno.h>

/*
 * This function is blatently stolen from the kernel.
 * See the dissertation in the comments preceding the
 * hrt2ts() function in:
 *	uts/common/os/timers.c
 */
void
hrt2ts(hrtime_t hrt, timespec_t *tsp)
{
#if defined(__amd64)
	tsp->tv_sec = hrt / NANOSEC;
	tsp->tv_nsec = hrt % NANOSEC;
#else
	uint32_t sec, nsec, tmp;

	tmp = (uint32_t)(hrt >> 30);
	sec = tmp - (tmp >> 2);
	sec = tmp - (sec >> 5);
	sec = tmp + (sec >> 1);
	sec = tmp - (sec >> 6) + 7;
	sec = tmp - (sec >> 3);
	sec = tmp + (sec >> 1);
	sec = tmp + (sec >> 3);
	sec = tmp + (sec >> 4);
	tmp = (sec << 7) - sec - sec - sec;
	tmp = (tmp << 7) - tmp - tmp - tmp;
	tmp = (tmp << 7) - tmp - tmp - tmp;
	nsec = (uint32_t)hrt - (tmp << 9);
	while (nsec >= NANOSEC) {
		nsec -= NANOSEC;
		sec++;
	}
	tsp->tv_sec = (time_t)sec;
	tsp->tv_nsec = nsec;
#endif /* defined(__amd64) */
}

/*
 * Convert absolute time to relative time.
 * All *timedwait() system call traps expect relative time.
 */
void
abstime_to_reltime(clockid_t clock_id, const timespec_t *abstime,
    timespec_t *reltime)
{
	extern int __clock_gettime(clockid_t, timespec_t *);
	timespec_t now;

	if (clock_id == CLOCK_HIGHRES)
		hrt2ts(gethrtime(), &now);
	else
		(void) __clock_gettime(clock_id, &now);
	if (abstime->tv_nsec >= now.tv_nsec) {
		reltime->tv_sec = abstime->tv_sec - now.tv_sec;
		reltime->tv_nsec = abstime->tv_nsec - now.tv_nsec;
	} else {
		reltime->tv_sec = abstime->tv_sec - now.tv_sec - 1;
		reltime->tv_nsec = abstime->tv_nsec - now.tv_nsec + NANOSEC;
	}
	/*
	 * If the absolute time has already passed,
	 * just set the relative time to zero.
	 */
	if (reltime->tv_sec < 0) {
		reltime->tv_sec = 0;
		reltime->tv_nsec = 0;
	}
	/*
	 * If the specified absolute time has a bad nanoseconds value,
	 * assign it to the relative time value.  If the interface
	 * attempts to sleep, the bad value will be detected then.
	 * The SUSV3 Posix spec is very clear that such detection
	 * should not happen until an attempt to sleep is made.
	 */
	if ((ulong_t)abstime->tv_nsec >= NANOSEC)
		reltime->tv_nsec = abstime->tv_nsec;
}