2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License, Version 1.0 only
2N/A * (the "License"). You may not use this file except in compliance
2N/A * with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <sys/types.h>
2N/A#include <time.h>
2N/A#include <errno.h>
2N/A
2N/A/*
2N/A * This function is blatently stolen from the kernel.
2N/A * See the dissertation in the comments preceding the
2N/A * hrt2ts() function in:
2N/A * uts/common/os/timers.c
2N/A */
2N/Avoid
2N/Ahrt2ts(hrtime_t hrt, timespec_t *tsp)
2N/A{
2N/A uint32_t sec, nsec, tmp;
2N/A
2N/A tmp = (uint32_t)(hrt >> 30);
2N/A sec = tmp - (tmp >> 2);
2N/A sec = tmp - (sec >> 5);
2N/A sec = tmp + (sec >> 1);
2N/A sec = tmp - (sec >> 6) + 7;
2N/A sec = tmp - (sec >> 3);
2N/A sec = tmp + (sec >> 1);
2N/A sec = tmp + (sec >> 3);
2N/A sec = tmp + (sec >> 4);
2N/A tmp = (sec << 7) - sec - sec - sec;
2N/A tmp = (tmp << 7) - tmp - tmp - tmp;
2N/A tmp = (tmp << 7) - tmp - tmp - tmp;
2N/A nsec = (uint32_t)hrt - (tmp << 9);
2N/A while (nsec >= NANOSEC) {
2N/A nsec -= NANOSEC;
2N/A sec++;
2N/A }
2N/A tsp->tv_sec = (time_t)sec;
2N/A tsp->tv_nsec = nsec;
2N/A}
2N/A
2N/A/*
2N/A * Convert absolute time to relative time.
2N/A * All *timedwait() system call traps expect relative time.
2N/A */
2N/Avoid
2N/Aabstime_to_reltime(clockid_t clock_id,
2N/A const timespec_t *abstime, timespec_t *reltime)
2N/A{
2N/A extern int __clock_gettime(clockid_t, timespec_t *);
2N/A timespec_t now;
2N/A
2N/A if (clock_id == CLOCK_HIGHRES)
2N/A hrt2ts(gethrtime(), &now);
2N/A else
2N/A (void) __clock_gettime(clock_id, &now);
2N/A if (abstime->tv_nsec >= now.tv_nsec) {
2N/A reltime->tv_sec = abstime->tv_sec - now.tv_sec;
2N/A reltime->tv_nsec = abstime->tv_nsec - now.tv_nsec;
2N/A } else {
2N/A reltime->tv_sec = abstime->tv_sec - now.tv_sec - 1;
2N/A reltime->tv_nsec = abstime->tv_nsec - now.tv_nsec + NANOSEC;
2N/A }
2N/A /*
2N/A * If the absolute time has already passed,
2N/A * just set the relative time to zero.
2N/A */
2N/A if (reltime->tv_sec < 0) {
2N/A reltime->tv_sec = 0;
2N/A reltime->tv_nsec = 0;
2N/A }
2N/A /*
2N/A * If the specified absolute time has a bad nanoseconds value,
2N/A * assign it to the relative time value. If the interface
2N/A * attempts to sleep, the bad value will be detected then.
2N/A * The SUSV3 Posix spec is very clear that such detection
2N/A * should not happen until an attempt to sleep is made.
2N/A */
2N/A if ((ulong_t)abstime->tv_nsec >= NANOSEC)
2N/A reltime->tv_nsec = abstime->tv_nsec;
2N/A}