Cygwin: Cleanup time handling

* Redefine NSPERSEC to NS100PERSEC
* Define NSPERSEC as nanosecs per second
* Define USPERSEC as microsecs per second
* Use above constants throughout where appropriate
* Rename to_us to timespec_to_us and inline
* Rename it_bad to timespec_bad and inline

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
This commit is contained in:
Corinna Vinschen 2018-02-07 13:07:44 +01:00
parent f8ce691223
commit 2af67d21b2
13 changed files with 115 additions and 97 deletions

View File

@ -1314,8 +1314,8 @@ fhandler_base::utimens_fs (const struct timespec *tvp)
tmp[1] = tmp[0] = timeofday; tmp[1] = tmp[0] = timeofday;
else else
{ {
if ((tvp[0].tv_nsec < UTIME_NOW || tvp[0].tv_nsec > 999999999L) if ((tvp[0].tv_nsec < UTIME_NOW || tvp[0].tv_nsec >= NSPERSEC)
|| (tvp[1].tv_nsec < UTIME_NOW || tvp[1].tv_nsec > 999999999L)) || (tvp[1].tv_nsec < UTIME_NOW || tvp[1].tv_nsec >= NSPERSEC))
{ {
if (closeit) if (closeit)
close_fs (); close_fs ();

View File

@ -535,9 +535,9 @@ format_proc_stat (void *, char *&destbuf)
for (unsigned long i = 0; i < wincap.cpu_count (); i++) for (unsigned long i = 0; i < wincap.cpu_count (); i++)
{ {
kernel_time += (spt[i].KernelTime.QuadPart - spt[i].IdleTime.QuadPart) kernel_time += (spt[i].KernelTime.QuadPart - spt[i].IdleTime.QuadPart)
* HZ / 10000000ULL; * HZ / NS100PERSEC;
user_time += spt[i].UserTime.QuadPart * HZ / 10000000ULL; user_time += spt[i].UserTime.QuadPart * HZ / NS100PERSEC;
idle_time += spt[i].IdleTime.QuadPart * HZ / 10000000ULL; idle_time += spt[i].IdleTime.QuadPart * HZ / NS100PERSEC;
} }
eobuf += __small_sprintf (eobuf, "cpu %U %U %U %U\n", eobuf += __small_sprintf (eobuf, "cpu %U %U %U %U\n",
@ -546,9 +546,9 @@ format_proc_stat (void *, char *&destbuf)
for (unsigned long i = 0; i < wincap.cpu_count (); i++) for (unsigned long i = 0; i < wincap.cpu_count (); i++)
{ {
interrupt_count += spt[i].InterruptCount; interrupt_count += spt[i].InterruptCount;
kernel_time = (spt[i].KernelTime.QuadPart - spt[i].IdleTime.QuadPart) * HZ / 10000000ULL; kernel_time = (spt[i].KernelTime.QuadPart - spt[i].IdleTime.QuadPart) * HZ / NS100PERSEC;
user_time = spt[i].UserTime.QuadPart * HZ / 10000000ULL; user_time = spt[i].UserTime.QuadPart * HZ / NS100PERSEC;
idle_time = spt[i].IdleTime.QuadPart * HZ / 10000000ULL; idle_time = spt[i].IdleTime.QuadPart * HZ / NS100PERSEC;
eobuf += __small_sprintf (eobuf, "cpu%d %U %U %U %U\n", i, eobuf += __small_sprintf (eobuf, "cpu%d %U %U %U %U\n", i,
user_time, 0ULL, kernel_time, idle_time); user_time, 0ULL, kernel_time, idle_time);
} }

View File

@ -1139,19 +1139,19 @@ format_process_stat (void *data, char *&destbuf)
return 0; return 0;
} }
fault_count = vmc.PageFaultCount; fault_count = vmc.PageFaultCount;
utime = put.UserTime.QuadPart * HZ / 10000000ULL; utime = put.UserTime.QuadPart * HZ / NS100PERSEC;
stime = put.KernelTime.QuadPart * HZ / 10000000ULL; stime = put.KernelTime.QuadPart * HZ / NS100PERSEC;
#if 0 #if 0
if (stodi.CurrentTime.QuadPart > put.CreateTime.QuadPart) if (stodi.CurrentTime.QuadPart > put.CreateTime.QuadPart)
start_time = (spt.KernelTime.QuadPart + spt.UserTime.QuadPart - start_time = (spt.KernelTime.QuadPart + spt.UserTime.QuadPart -
stodi.CurrentTime.QuadPart + put.CreateTime.QuadPart) * HZ / 10000000ULL; stodi.CurrentTime.QuadPart + put.CreateTime.QuadPart) * HZ / NS100PERSEC;
else else
/* /*
* sometimes stodi.CurrentTime is a bit behind * sometimes stodi.CurrentTime is a bit behind
* Note: some older versions of procps are broken and can't cope * Note: some older versions of procps are broken and can't cope
* with process start times > time(NULL). * with process start times > time(NULL).
*/ */
start_time = (spt.KernelTme.QuadPart + spt.UserTime.QuadPart) * HZ / 10000000ULL; start_time = (spt.KernelTme.QuadPart + spt.UserTime.QuadPart) * HZ / NS100PERSEC;
#endif #endif
/* The BasePriority returned to a 32 bit process under WOW64 is /* The BasePriority returned to a 32 bit process under WOW64 is
apparently broken, for 32 and 64 bit target processes. 64 bit apparently broken, for 32 and 64 bit target processes. 64 bit

View File

@ -29,8 +29,12 @@ details. */
/* 100ns difference between Windows and UNIX timebase. */ /* 100ns difference between Windows and UNIX timebase. */
#define FACTOR (0x19db1ded53e8000LL) #define FACTOR (0x19db1ded53e8000LL)
/* # of nanosecs per second. */
#define NSPERSEC (1000000000)
/* # of 100ns intervals per second. */ /* # of 100ns intervals per second. */
#define NSPERSEC 10000000LL #define NS100PERSEC (10000000)
/* # of millisecs per second. */
#define USPERSEC (1000000)
class hires_base class hires_base
{ {

View File

@ -183,7 +183,7 @@ ipc_cond_timedwait (HANDLE evt, HANDLE mtx, const struct timespec *abstime)
{ {
if (abstime->tv_sec < 0 if (abstime->tv_sec < 0
|| abstime->tv_nsec < 0 || abstime->tv_nsec < 0
|| abstime->tv_nsec > 999999999) || abstime->tv_nsec >= NSPERSEC)
return EINVAL; return EINVAL;
/* If a timeout is set, we create a waitable timer to wait for. /* If a timeout is set, we create a waitable timer to wait for.

View File

@ -29,9 +29,9 @@ add_timeval (struct timeval *tv1, struct timeval *tv2)
{ {
tv1->tv_sec += tv2->tv_sec; tv1->tv_sec += tv2->tv_sec;
tv1->tv_usec += tv2->tv_usec; tv1->tv_usec += tv2->tv_usec;
if (tv1->tv_usec >= 1000000) if (tv1->tv_usec >= USPERSEC)
{ {
tv1->tv_usec -= 1000000; tv1->tv_usec -= USPERSEC;
tv1->tv_sec++; tv1->tv_sec++;
} }
} }

View File

@ -12,8 +12,10 @@
#include "miscfuncs.h" #include "miscfuncs.h"
#include "cygerrno.h" #include "cygerrno.h"
#include "pinfo.h" #include "pinfo.h"
#include "hires.h"
/* for getpid */ /* for getpid */
#include <unistd.h> #include <unistd.h>
#include <sys/param.h>
#include "registry.h" #include "registry.h"
/* Win32 priority to UNIX priority Mapping. */ /* Win32 priority to UNIX priority Mapping. */
@ -199,9 +201,9 @@ sched_rr_get_interval (pid_t pid, struct timespec *interval)
slindex -= 1; slindex -= 1;
nsec = quantable[vfindex][slindex][qindex] / quantapertick nsec = quantable[vfindex][slindex][qindex] / quantapertick
* clocktickinterval * 1000000; * clocktickinterval * (NSPERSEC / HZ);
interval->tv_sec = nsec / 1000000000; interval->tv_sec = nsec / NSPERSEC;
interval->tv_nsec = nsec % 1000000000; interval->tv_nsec = nsec % NSPERSEC;
return 0; return 0;
} }

View File

@ -107,7 +107,10 @@ pselect (int maxfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
else else
{ {
/* Convert to microseconds or -1 if to == NULL */ /* Convert to microseconds or -1 if to == NULL */
LONGLONG us = to ? to->tv_sec * 1000000LL + (to->tv_nsec + 999) / 1000 : -1LL; LONGLONG us = to ? to->tv_sec * USPERSEC
+ (to->tv_nsec + (NSPERSEC/USPERSEC) - 1)
/ (NSPERSEC/USPERSEC)
: -1LL;
if (to) if (to)
select_printf ("to->tv_sec %ld, to->tv_nsec %ld, us %D", to->tv_sec, to->tv_nsec, us); select_printf ("to->tv_sec %ld, to->tv_nsec %ld, us %D", to->tv_sec, to->tv_nsec, us);

View File

@ -65,7 +65,7 @@ clock_nanosleep (clockid_t clk_id, int flags, const struct timespec *rqtp,
sig_dispatch_pending (); sig_dispatch_pending ();
pthread_testcancel (); pthread_testcancel ();
if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec > 999999999L) if (rqtp->tv_sec < 0 || rqtp->tv_nsec < 0 || rqtp->tv_nsec >= NSPERSEC)
return EINVAL; return EINVAL;
/* Explicitly disallowed by POSIX. Needs to be checked first to avoid /* Explicitly disallowed by POSIX. Needs to be checked first to avoid
@ -89,8 +89,9 @@ clock_nanosleep (clockid_t clk_id, int flags, const struct timespec *rqtp,
LARGE_INTEGER timeout; LARGE_INTEGER timeout;
timeout.QuadPart = (LONGLONG) rqtp->tv_sec * NSPERSEC timeout.QuadPart = (LONGLONG) rqtp->tv_sec * NS100PERSEC
+ ((LONGLONG) rqtp->tv_nsec + 99LL) / 100LL; + ((LONGLONG) rqtp->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
/ (NSPERSEC/NS100PERSEC);
if (abstime) if (abstime)
{ {
@ -107,7 +108,8 @@ clock_nanosleep (clockid_t clk_id, int flags, const struct timespec *rqtp,
else else
{ {
/* other clocks need to be handled with a relative timeout */ /* other clocks need to be handled with a relative timeout */
timeout.QuadPart -= tp.tv_sec * NSPERSEC + tp.tv_nsec / 100LL; timeout.QuadPart -= tp.tv_sec * NS100PERSEC
+ tp.tv_nsec / (NSPERSEC/NS100PERSEC);
timeout.QuadPart *= -1LL; timeout.QuadPart *= -1LL;
} }
} }
@ -123,8 +125,9 @@ clock_nanosleep (clockid_t clk_id, int flags, const struct timespec *rqtp,
/* according to POSIX, rmtp is used only if !abstime */ /* according to POSIX, rmtp is used only if !abstime */
if (rmtp && !abstime) if (rmtp && !abstime)
{ {
rmtp->tv_sec = (time_t) (timeout.QuadPart / NSPERSEC); rmtp->tv_sec = (time_t) (timeout.QuadPart / NS100PERSEC);
rmtp->tv_nsec = (long) ((timeout.QuadPart % NSPERSEC) * 100LL); rmtp->tv_nsec = (long) ((timeout.QuadPart % NS100PERSEC)
* (NSPERSEC/NS100PERSEC));
} }
syscall_printf ("%d = clock_nanosleep(%lu, %d, %ld.%09ld, %ld.%09.ld)", syscall_printf ("%d = clock_nanosleep(%lu, %d, %ld.%09ld, %ld.%09.ld)",
@ -160,8 +163,8 @@ extern "C" unsigned int
usleep (useconds_t useconds) usleep (useconds_t useconds)
{ {
struct timespec req; struct timespec req;
req.tv_sec = useconds / 1000000; req.tv_sec = useconds / USPERSEC;
req.tv_nsec = (useconds % 1000000) * 1000; req.tv_nsec = (useconds % USPERSEC) * (NSPERSEC/USPERSEC);
int res = clock_nanosleep (CLOCK_REALTIME, 0, &req, NULL); int res = clock_nanosleep (CLOCK_REALTIME, 0, &req, NULL);
if (res != 0) if (res != 0)
{ {
@ -628,14 +631,15 @@ sigtimedwait (const sigset_t *set, siginfo_t *info, const timespec *timeout)
if (timeout) if (timeout)
{ {
if (timeout->tv_sec < 0 if (timeout->tv_sec < 0
|| timeout->tv_nsec < 0 || timeout->tv_nsec > (NSPERSEC * 100LL)) || timeout->tv_nsec < 0 || timeout->tv_nsec > NSPERSEC)
{ {
set_errno (EINVAL); set_errno (EINVAL);
return -1; return -1;
} }
/* convert timespec to 100ns units */ /* convert timespec to 100ns units */
waittime.QuadPart = (LONGLONG) timeout->tv_sec * NSPERSEC waittime.QuadPart = (LONGLONG) timeout->tv_sec * NS100PERSEC
+ ((LONGLONG) timeout->tv_nsec + 99LL) / 100LL; + ((LONGLONG) timeout->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
/ (NSPERSEC/NS100PERSEC);
} }
return sigwait_common (set, info, timeout ? &waittime : cw_infinite); return sigwait_common (set, info, timeout ? &waittime : cw_infinite);

View File

@ -19,6 +19,7 @@ details. */
#include "ntdll.h" #include "ntdll.h"
#include "tls_pbuf.h" #include "tls_pbuf.h"
#include "cpuid.h" #include "cpuid.h"
#include "hires.h"
static long static long
get_open_max (int in) get_open_max (int in)
@ -793,7 +794,7 @@ sysinfo (struct sysinfo *info)
sizeof_stodi, NULL); sizeof_stodi, NULL);
if (NT_SUCCESS (status)) if (NT_SUCCESS (status))
uptime = (stodi->CurrentTime.QuadPart - stodi->BootTime.QuadPart) uptime = (stodi->CurrentTime.QuadPart - stodi->BootTime.QuadPart)
/ 10000000ULL; / NS100PERSEC;
else else
debug_printf ("NtQuerySystemInformation(SystemTimeOfDayInformation), " debug_printf ("NtQuerySystemInformation(SystemTimeOfDayInformation), "
"status %y", status); "status %y", status);

View File

@ -2546,7 +2546,7 @@ pthread_convert_abstime (clockid_t clock_id, const struct timespec *abstime,
/* According to SUSv3, the abstime value must be checked for validity. */ /* According to SUSv3, the abstime value must be checked for validity. */
if (abstime->tv_sec < 0 if (abstime->tv_sec < 0
|| abstime->tv_nsec < 0 || abstime->tv_nsec < 0
|| abstime->tv_nsec > 999999999) || abstime->tv_nsec >= NSPERSEC)
return EINVAL; return EINVAL;
/* Check for immediate timeout before converting */ /* Check for immediate timeout before converting */
@ -2556,8 +2556,9 @@ pthread_convert_abstime (clockid_t clock_id, const struct timespec *abstime,
&& tp.tv_nsec > abstime->tv_nsec)) && tp.tv_nsec > abstime->tv_nsec))
return ETIMEDOUT; return ETIMEDOUT;
timeout->QuadPart = abstime->tv_sec * NSPERSEC timeout->QuadPart = abstime->tv_sec * NS100PERSEC
+ (abstime->tv_nsec + 99LL) / 100LL; + (abstime->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
/ (NSPERSEC/NS100PERSEC);
switch (clock_id) switch (clock_id)
{ {
case CLOCK_REALTIME: case CLOCK_REALTIME:
@ -2565,7 +2566,8 @@ pthread_convert_abstime (clockid_t clock_id, const struct timespec *abstime,
break; break;
default: default:
/* other clocks must be handled as relative timeout */ /* other clocks must be handled as relative timeout */
timeout->QuadPart -= tp.tv_sec * NSPERSEC + tp.tv_nsec / 100LL; timeout->QuadPart -= tp.tv_sec * NS100PERSEC + tp.tv_nsec
/ (NSPERSEC/NS100PERSEC);
timeout->QuadPart *= -1LL; timeout->QuadPart *= -1LL;
break; break;
} }

View File

@ -14,6 +14,7 @@ details. */
#include "fhandler.h" #include "fhandler.h"
#include "dtable.h" #include "dtable.h"
#include "cygheap.h" #include "cygheap.h"
#include <sys/param.h>
#define TT_MAGIC 0x513e4a1c #define TT_MAGIC 0x513e4a1c
struct timer_tracker struct timer_tracker
@ -105,12 +106,12 @@ timer_tracker::timer_tracker (clockid_t c, const sigevent *e)
} }
} }
static long long static inline long long
to_us (const timespec& ts) timespec_to_us (const timespec& ts)
{ {
long long res = ts.tv_sec; long long res = ts.tv_sec;
res *= 1000000; res *= USPERSEC;
res += ts.tv_nsec / 1000 + ((ts.tv_nsec % 1000) ? 1 : 0); res += (ts.tv_nsec + (NSPERSEC/USPERSEC) - 1) / (NSPERSEC/USPERSEC);
return res; return res;
} }
@ -131,7 +132,7 @@ timer_thread (VOID *x)
if (sleep_us > 0) if (sleep_us > 0)
{ {
tt->sleepto_us = sleepto_us; tt->sleepto_us = sleepto_us;
sleep_ms = (sleep_us + 999) / 1000; sleep_ms = (sleep_us + (USPERSEC/HZ) - 1) / (USPERSEC/HZ);
} }
else else
{ {
@ -203,10 +204,10 @@ out:
return 0; return 0;
} }
static bool static inline bool
it_bad (const timespec& t) timespec_bad (const timespec& t)
{ {
if (t.tv_nsec < 0 || t.tv_nsec >= 1000000000 || t.tv_sec < 0) if (t.tv_nsec < 0 || t.tv_nsec >= NSPERSEC || t.tv_sec < 0)
{ {
set_errno (EINVAL); set_errno (EINVAL);
return true; return true;
@ -227,7 +228,7 @@ timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalu
__leave; __leave;
} }
if (it_bad (value->it_value) || it_bad (value->it_interval)) if (timespec_bad (value->it_value) || timespec_bad (value->it_interval))
__leave; __leave;
long long now = in_flags & TIMER_ABSTIME ? 0 : gtod.usecs (); long long now = in_flags & TIMER_ABSTIME ? 0 : gtod.usecs ();
@ -242,8 +243,8 @@ timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalu
interval_us = sleepto_us = 0; interval_us = sleepto_us = 0;
else else
{ {
sleepto_us = now + to_us (value->it_value); sleepto_us = now + timespec_to_us (value->it_value);
interval_us = to_us (value->it_interval); interval_us = timespec_to_us (value->it_interval);
it_interval = value->it_interval; it_interval = value->it_interval;
if (!hcancel) if (!hcancel)
hcancel = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL); hcancel = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
@ -274,8 +275,8 @@ timer_tracker::gettime (itimerspec *ovalue)
long long left_us = sleepto_us - now; long long left_us = sleepto_us - now;
if (left_us < 0) if (left_us < 0)
left_us = 0; left_us = 0;
ovalue->it_value.tv_sec = left_us / 1000000; ovalue->it_value.tv_sec = left_us / USPERSEC;
ovalue->it_value.tv_nsec = (left_us % 1000000) * 1000; ovalue->it_value.tv_nsec = (left_us % USPERSEC) * (NSPERSEC/USPERSEC);
} }
} }
@ -481,16 +482,18 @@ ualarm (useconds_t value, useconds_t interval)
Interpret negative arguments as zero */ Interpret negative arguments as zero */
if (value > 0) if (value > 0)
{ {
timer.it_value.tv_sec = value / 1000000; timer.it_value.tv_sec = value / USPERSEC;
timer.it_value.tv_nsec = (value % 1000000) * 1000; timer.it_value.tv_nsec = (value % USPERSEC) * (NSPERSEC/USPERSEC);
} }
if (interval > 0) if (interval > 0)
{ {
timer.it_interval.tv_sec = interval / 1000000; timer.it_interval.tv_sec = interval / USPERSEC;
timer.it_interval.tv_nsec = (interval % 1000000) * 1000; timer.it_interval.tv_nsec = (interval % USPERSEC) * (NSPERSEC/USPERSEC);
} }
timer_settime ((timer_t) &ttstart, 0, &timer, &otimer); timer_settime ((timer_t) &ttstart, 0, &timer, &otimer);
useconds_t ret = otimer.it_value.tv_sec * 1000000 + (otimer.it_value.tv_nsec + 999) / 1000; useconds_t ret = otimer.it_value.tv_sec * USPERSEC
+ (otimer.it_value.tv_nsec + (NSPERSEC/USPERSEC) - 1)
/ (NSPERSEC/USPERSEC);
syscall_printf ("%d = ualarm(%ld , %ld)", ret, value, interval); syscall_printf ("%d = ualarm(%ld , %ld)", ret, value, interval);
return ret; return ret;
} }

View File

@ -10,6 +10,7 @@ details. */
#include "winsup.h" #include "winsup.h"
#include <sys/times.h> #include <sys/times.h>
#include <sys/timeb.h> #include <sys/timeb.h>
#include <sys/param.h>
#include <utime.h> #include <utime.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
@ -45,7 +46,7 @@ __to_clock_t (PLARGE_INTEGER src, int flag)
if (flag) if (flag)
total -= FACTOR; total -= FACTOR;
total /= NSPERSEC / CLOCKS_PER_SEC; total /= NS100PERSEC / CLOCKS_PER_SEC;
return total; return total;
} }
@ -72,7 +73,7 @@ times (struct tms *buf)
/* uptime */ /* uptime */
ticks.QuadPart -= stodi.BootTime.QuadPart; ticks.QuadPart -= stodi.BootTime.QuadPart;
/* ticks is in in 100ns, convert to clock ticks. */ /* ticks is in in 100ns, convert to clock ticks. */
tc = (clock_t) (ticks.QuadPart * CLOCKS_PER_SEC / NSPERSEC); tc = (clock_t) (ticks.QuadPart * CLOCKS_PER_SEC / NS100PERSEC);
buf->tms_stime = __to_clock_t (&kut.KernelTime, 0); buf->tms_stime = __to_clock_t (&kut.KernelTime, 0);
buf->tms_utime = __to_clock_t (&kut.UserTime, 0); buf->tms_utime = __to_clock_t (&kut.UserTime, 0);
@ -102,7 +103,7 @@ settimeofday (const struct timeval *tv, const struct timezone *tz)
__try __try
{ {
if (tv->tv_usec < 0 || tv->tv_usec >= 1000000) if (tv->tv_usec < 0 || tv->tv_usec >= USPERSEC)
{ {
set_errno (EINVAL); set_errno (EINVAL);
return -1; return -1;
@ -116,7 +117,7 @@ settimeofday (const struct timeval *tv, const struct timezone *tz)
st.wHour = ptm->tm_hour; st.wHour = ptm->tm_hour;
st.wMinute = ptm->tm_min; st.wMinute = ptm->tm_min;
st.wSecond = ptm->tm_sec; st.wSecond = ptm->tm_sec;
st.wMilliseconds = tv->tv_usec / 1000; st.wMilliseconds = tv->tv_usec / (USPERSEC/HZ);
res = -!SetSystemTime (&st); res = -!SetSystemTime (&st);
gtod.reset (); gtod.reset ();
@ -158,11 +159,11 @@ totimeval (struct timeval *dst, PLARGE_INTEGER src, int sub, int flag)
{ {
int64_t x = __to_clock_t (src, flag); int64_t x = __to_clock_t (src, flag);
x *= (int64_t) 1000000 / CLOCKS_PER_SEC; /* Turn x into usecs */ x *= (int64_t) USPERSEC / CLOCKS_PER_SEC; /* Turn x into usecs */
x -= (int64_t) sub * 1000000; x -= (int64_t) sub * USPERSEC;
dst->tv_usec = x % 1000000; /* And split */ dst->tv_usec = x % USPERSEC; /* And split */
dst->tv_sec = x / 1000000; dst->tv_sec = x / USPERSEC;
} }
/* FIXME: Make thread safe */ /* FIXME: Make thread safe */
@ -176,8 +177,8 @@ gettimeofday (struct timeval *__restrict tv, void *__restrict tzvp)
if (now == (LONGLONG) -1) if (now == (LONGLONG) -1)
return -1; return -1;
tv->tv_sec = now / 1000000; tv->tv_sec = now / USPERSEC;
tv->tv_usec = now % 1000000; tv->tv_usec = now % USPERSEC;
if (tz != NULL) if (tz != NULL)
{ {
@ -202,16 +203,16 @@ timespec_to_filetime (const struct timespec *time_in, PLARGE_INTEGER out)
if (time_in->tv_nsec == UTIME_OMIT) if (time_in->tv_nsec == UTIME_OMIT)
out->QuadPart = 0; out->QuadPart = 0;
else else
out->QuadPart = time_in->tv_sec * NSPERSEC out->QuadPart = time_in->tv_sec * NS100PERSEC
+ time_in->tv_nsec / (1000000000/NSPERSEC) + FACTOR; + time_in->tv_nsec / (NSPERSEC/NS100PERSEC) + FACTOR;
} }
/* Cygwin internal */ /* Cygwin internal */
void __stdcall void __stdcall
timeval_to_filetime (const struct timeval *time_in, PLARGE_INTEGER out) timeval_to_filetime (const struct timeval *time_in, PLARGE_INTEGER out)
{ {
out->QuadPart = time_in->tv_sec * NSPERSEC out->QuadPart = time_in->tv_sec * NS100PERSEC
+ time_in->tv_usec * (NSPERSEC/1000000) + FACTOR; + time_in->tv_usec * (NS100PERSEC/USPERSEC) + FACTOR;
} }
/* Cygwin internal */ /* Cygwin internal */
@ -232,18 +233,18 @@ timeval_to_timespec (const struct timeval *tvp, struct timespec *tmp)
return NULL; return NULL;
tmp[0].tv_sec = tvp[0].tv_sec; tmp[0].tv_sec = tvp[0].tv_sec;
tmp[0].tv_nsec = tvp[0].tv_usec * 1000; tmp[0].tv_nsec = tvp[0].tv_usec * (NSPERSEC/USPERSEC);
if (tmp[0].tv_nsec < 0) if (tmp[0].tv_nsec < 0)
tmp[0].tv_nsec = 0; tmp[0].tv_nsec = 0;
else if (tmp[0].tv_nsec > 999999999) else if (tmp[0].tv_nsec >= NSPERSEC)
tmp[0].tv_nsec = 999999999; tmp[0].tv_nsec = NSPERSEC - 1;
tmp[1].tv_sec = tvp[1].tv_sec; tmp[1].tv_sec = tvp[1].tv_sec;
tmp[1].tv_nsec = tvp[1].tv_usec * 1000; tmp[1].tv_nsec = tvp[1].tv_usec * (NSPERSEC/USPERSEC);
if (tmp[1].tv_nsec < 0) if (tmp[1].tv_nsec < 0)
tmp[1].tv_nsec = 0; tmp[1].tv_nsec = 0;
else if (tmp[1].tv_nsec > 999999999) else if (tmp[1].tv_nsec >= NSPERSEC)
tmp[1].tv_nsec = 999999999; tmp[1].tv_nsec = NSPERSEC - 1;
return tmp; return tmp;
} }
@ -264,7 +265,7 @@ to_time_t (PLARGE_INTEGER ptr)
return 0; return 0;
x -= FACTOR; /* number of 100ns between 1601 and 1970 */ x -= FACTOR; /* number of 100ns between 1601 and 1970 */
x /= NSPERSEC; /* number of 100ns in a second */ x /= NS100PERSEC; /* number of 100ns in a second */
return x; return x;
} }
@ -277,8 +278,6 @@ to_timestruc_t (PLARGE_INTEGER ptr, timestruc_t *out)
stuffed into two long words. stuffed into two long words.
A timestruc_t is the number of seconds and microseconds since jan 1 1970 A timestruc_t is the number of seconds and microseconds since jan 1 1970
stuffed into a time_t and a long. */ stuffed into a time_t and a long. */
int64_t rem;
int64_t x = ptr->QuadPart; int64_t x = ptr->QuadPart;
/* pass "no time" as epoch */ /* pass "no time" as epoch */
@ -290,10 +289,8 @@ to_timestruc_t (PLARGE_INTEGER ptr, timestruc_t *out)
} }
x -= FACTOR; /* number of 100ns between 1601 and 1970 */ x -= FACTOR; /* number of 100ns between 1601 and 1970 */
rem = x % NSPERSEC; out->tv_sec = x / NS100PERSEC;
x /= NSPERSEC; /* number of 100ns in a second */ out->tv_nsec = (x % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
out->tv_nsec = rem * 100; /* as tv_nsec is in nanoseconds */
out->tv_sec = x;
} }
/* Cygwin internal */ /* Cygwin internal */
@ -470,7 +467,7 @@ hires_ns::prime ()
return; return;
} }
freq = (double) ((double) 1000000000. / (double) ifreq.QuadPart); freq = (double) ((double) NSPERSEC / (double) ifreq.QuadPart);
inited = true; inited = true;
SetThreadPriority (GetCurrentThread (), priority); SetThreadPriority (GetCurrentThread (), priority);
} }
@ -534,8 +531,8 @@ clock_gettime (clockid_t clk_id, struct timespec *tp)
&kut, sizeof kut, NULL); &kut, sizeof kut, NULL);
x = kut.KernelTime.QuadPart + kut.UserTime.QuadPart; x = kut.KernelTime.QuadPart + kut.UserTime.QuadPart;
tp->tv_sec = x / NSPERSEC; tp->tv_sec = x / NS100PERSEC;
tp->tv_nsec = (x % NSPERSEC) * 100LL; tp->tv_nsec = (x % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
CloseHandle (hProcess); CloseHandle (hProcess);
return 0; return 0;
@ -562,8 +559,8 @@ clock_gettime (clockid_t clk_id, struct timespec *tp)
&kut, sizeof kut, NULL); &kut, sizeof kut, NULL);
x = kut.KernelTime.QuadPart + kut.UserTime.QuadPart; x = kut.KernelTime.QuadPart + kut.UserTime.QuadPart;
tp->tv_sec = x / NSPERSEC; tp->tv_sec = x / NS100PERSEC;
tp->tv_nsec = (x % NSPERSEC) * 100LL; tp->tv_nsec = (x % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
CloseHandle (hThread); CloseHandle (hThread);
return 0; return 0;
@ -576,8 +573,8 @@ clock_gettime (clockid_t clk_id, struct timespec *tp)
LONGLONG now = gtod.nsecs (); LONGLONG now = gtod.nsecs ();
if (now == (LONGLONG) -1) if (now == (LONGLONG) -1)
return -1; return -1;
tp->tv_sec = now / NSPERSEC; tp->tv_sec = now / NS100PERSEC;
tp->tv_nsec = (now % NSPERSEC) * (1000000000 / NSPERSEC); tp->tv_nsec = (now % NS100PERSEC) * (NSPERSEC / NS100PERSEC);
break; break;
} }
@ -587,8 +584,8 @@ clock_gettime (clockid_t clk_id, struct timespec *tp)
if (now == (LONGLONG) -1) if (now == (LONGLONG) -1)
return -1; return -1;
tp->tv_sec = now / 1000000000; tp->tv_sec = now / NSPERSEC;
tp->tv_nsec = (now % 1000000000); tp->tv_nsec = (now % NSPERSEC);
break; break;
} }
@ -667,8 +664,8 @@ clock_getres (clockid_t clk_id, struct timespec *tp)
ULONG coarsest, finest, actual; ULONG coarsest, finest, actual;
NtQueryTimerResolution (&coarsest, &finest, &actual); NtQueryTimerResolution (&coarsest, &finest, &actual);
tp->tv_sec = coarsest / NSPERSEC; tp->tv_sec = coarsest / NS100PERSEC;
tp->tv_nsec = (coarsest % NSPERSEC) * 100; tp->tv_nsec = (coarsest % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
return 0; return 0;
} }
@ -677,16 +674,16 @@ clock_getres (clockid_t clk_id, struct timespec *tp)
case CLOCK_REALTIME: case CLOCK_REALTIME:
{ {
DWORD period = gtod.resolution (); DWORD period = gtod.resolution ();
tp->tv_sec = period / NSPERSEC; tp->tv_sec = period / NS100PERSEC;
tp->tv_nsec = (period % NSPERSEC) * 100; tp->tv_nsec = (period % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
break; break;
} }
case CLOCK_MONOTONIC: case CLOCK_MONOTONIC:
{ {
LONGLONG period = ntod.resolution (); LONGLONG period = ntod.resolution ();
tp->tv_sec = period / 1000000000; tp->tv_sec = period / NSPERSEC;
tp->tv_nsec = period % 1000000000; tp->tv_nsec = period % NSPERSEC;
break; break;
} }
@ -715,7 +712,9 @@ clock_setres (clockid_t clk_id, struct timespec *tp)
is < 430 secs. Actually the coarsest timer resolution is only slightly is < 430 secs. Actually the coarsest timer resolution is only slightly
beyond 15ms, but this might change in future OS versions, so we play nice beyond 15ms, but this might change in future OS versions, so we play nice
here. */ here. */
ULONGLONG period = (tp->tv_sec * 10000000ULL) + ((tp->tv_nsec) / 100ULL); ULONGLONG period = tp->tv_sec * NS100PERSEC
+ (tp->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
/ (NSPERSEC/NS100PERSEC);
/* clock_setres is non-POSIX/non-Linux. On QNX, the function always /* clock_setres is non-POSIX/non-Linux. On QNX, the function always
rounds the incoming value to the nearest supported value. */ rounds the incoming value to the nearest supported value. */