]> sourceware.org Git - newlib-cygwin.git/commitdiff
Cygwin: Cleanup time handling
authorCorinna Vinschen <corinna@vinschen.de>
Wed, 7 Feb 2018 12:07:44 +0000 (13:07 +0100)
committerCorinna Vinschen <corinna@vinschen.de>
Wed, 7 Feb 2018 12:07:44 +0000 (13:07 +0100)
* 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>
13 files changed:
winsup/cygwin/fhandler_disk_file.cc
winsup/cygwin/fhandler_proc.cc
winsup/cygwin/fhandler_process.cc
winsup/cygwin/hires.h
winsup/cygwin/posix_ipc.cc
winsup/cygwin/resource.cc
winsup/cygwin/sched.cc
winsup/cygwin/select.cc
winsup/cygwin/signal.cc
winsup/cygwin/sysconf.cc
winsup/cygwin/thread.cc
winsup/cygwin/timer.cc
winsup/cygwin/times.cc

index 5dfcae4d9eb7119bc5e224debf9b8b38f400f7cf..f0799657c77f8f570e126ad4c3ebce9b6ca5e7c1 100644 (file)
@@ -1314,8 +1314,8 @@ fhandler_base::utimens_fs (const struct timespec *tvp)
     tmp[1] = tmp[0] = timeofday;
   else
     {
-      if ((tvp[0].tv_nsec < UTIME_NOW || tvp[0].tv_nsec > 999999999L)
-         || (tvp[1].tv_nsec < UTIME_NOW || tvp[1].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 >= NSPERSEC))
        {
          if (closeit)
            close_fs ();
index ad367e45793f5c146c4ba1c9dddc50917f37bb5f..771235f0a121cff46a421535114ee39c088c2294 100644 (file)
@@ -535,9 +535,9 @@ format_proc_stat (void *, char *&destbuf)
       for (unsigned long i = 0; i < wincap.cpu_count (); i++)
        {
          kernel_time += (spt[i].KernelTime.QuadPart - spt[i].IdleTime.QuadPart)
-                        * HZ / 10000000ULL;
-         user_time += spt[i].UserTime.QuadPart * HZ / 10000000ULL;
-         idle_time += spt[i].IdleTime.QuadPart * HZ / 10000000ULL;
+                        * HZ / NS100PERSEC;
+         user_time += spt[i].UserTime.QuadPart * HZ / NS100PERSEC;
+         idle_time += spt[i].IdleTime.QuadPart * HZ / NS100PERSEC;
        }
 
       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++)
        {
          interrupt_count += spt[i].InterruptCount;
-         kernel_time = (spt[i].KernelTime.QuadPart - spt[i].IdleTime.QuadPart) * HZ / 10000000ULL;
-         user_time = spt[i].UserTime.QuadPart * HZ / 10000000ULL;
-         idle_time = 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 / NS100PERSEC;
+         idle_time = spt[i].IdleTime.QuadPart * HZ / NS100PERSEC;
          eobuf += __small_sprintf (eobuf, "cpu%d %U %U %U %U\n", i,
                                    user_time, 0ULL, kernel_time, idle_time);
        }
index eb5a523ff78fc21cc48fc5b687855d0101e286a1..c74844784cf44622bcbb714e47911aa46ea65390 100644 (file)
@@ -1139,19 +1139,19 @@ format_process_stat (void *data, char *&destbuf)
       return 0;
     }
   fault_count = vmc.PageFaultCount;
-  utime = put.UserTime.QuadPart * HZ / 10000000ULL;
-  stime = put.KernelTime.QuadPart * HZ / 10000000ULL;
+  utime = put.UserTime.QuadPart * HZ / NS100PERSEC;
+  stime = put.KernelTime.QuadPart * HZ / NS100PERSEC;
 #if 0
    if (stodi.CurrentTime.QuadPart > put.CreateTime.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
      /*
       * sometimes stodi.CurrentTime is a bit behind
       * Note: some older versions of procps are broken and can't cope
       * 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
   /* The BasePriority returned to a 32 bit process under WOW64 is
      apparently broken, for 32 and 64 bit target processes.  64 bit
index c6a286cff1559b57aea1c390dd176ad780df00a8..96010a6059dcbbd804fc4a1e8a8fd9588dbb72ed 100644 (file)
@@ -29,8 +29,12 @@ details. */
 
 /* 100ns difference between Windows and UNIX timebase. */
 #define FACTOR (0x19db1ded53e8000LL)
+/* # of nanosecs per second. */
+#define NSPERSEC (1000000000)
 /* # of 100ns intervals per second. */
-#define NSPERSEC 10000000LL
+#define NS100PERSEC (10000000)
+/* # of millisecs per second. */
+#define USPERSEC (1000000)
 
 class hires_base
 {
index f5c4009ffc8bc943fb39773ed5124674ecad8d33..36e9ed9cf354d3824bb8f0048339df0c91915f37 100644 (file)
@@ -183,7 +183,7 @@ ipc_cond_timedwait (HANDLE evt, HANDLE mtx, const struct timespec *abstime)
     {
       if (abstime->tv_sec < 0
               || abstime->tv_nsec < 0
-              || abstime->tv_nsec > 999999999)
+              || abstime->tv_nsec >= NSPERSEC)
        return EINVAL;
 
       /* If a timeout is set, we create a waitable timer to wait for.
index 11a04f13dbe11311773fd78bae3147d5de66d452..9e39d3a047e9574e8da2ec096215e15f1401d1a2 100644 (file)
@@ -29,9 +29,9 @@ add_timeval (struct timeval *tv1, struct timeval *tv2)
 {
   tv1->tv_sec += tv2->tv_sec;
   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++;
     }
 }
index 3fd058ff0467acbe84f1fd49b0fda93b3909d034..b8e0a2b5d000739ced4f8c9ec98f4fc056d5bca5 100644 (file)
 #include "miscfuncs.h"
 #include "cygerrno.h"
 #include "pinfo.h"
+#include "hires.h"
 /* for getpid */
 #include <unistd.h>
+#include <sys/param.h>
 #include "registry.h"
 
 /* Win32 priority to UNIX priority Mapping. */
@@ -199,9 +201,9 @@ sched_rr_get_interval (pid_t pid, struct timespec *interval)
     slindex -= 1;
 
   nsec = quantable[vfindex][slindex][qindex] / quantapertick
-    * clocktickinterval * 1000000;
-  interval->tv_sec = nsec / 1000000000;
-  interval->tv_nsec = nsec % 1000000000;
+        * clocktickinterval * (NSPERSEC / HZ);
+  interval->tv_sec = nsec / NSPERSEC;
+  interval->tv_nsec = nsec % NSPERSEC;
 
   return 0;
 }
index 0cd2dc929d63058c1f509f7ca11f5a6277dd5067..ca2cd5ab264c7f51ad1422daddebc3058456a75f 100644 (file)
@@ -107,7 +107,10 @@ pselect (int maxfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
       else
        {
          /* 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)
            select_printf ("to->tv_sec %ld, to->tv_nsec %ld, us %D", to->tv_sec, to->tv_nsec, us);
index f7c56c591954ff47c009b8d60b2bb25bd5c66503..e581d28da06a2b9f26e603f1b4fd43495902fec7 100644 (file)
@@ -65,7 +65,7 @@ clock_nanosleep (clockid_t clk_id, int flags, const struct timespec *rqtp,
   sig_dispatch_pending ();
   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;
 
   /* 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;
 
-  timeout.QuadPart = (LONGLONG) rqtp->tv_sec * NSPERSEC
-                    + ((LONGLONG) rqtp->tv_nsec + 99LL) / 100LL;
+  timeout.QuadPart = (LONGLONG) rqtp->tv_sec * NS100PERSEC
+                    + ((LONGLONG) rqtp->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
+                      / (NSPERSEC/NS100PERSEC);
 
   if (abstime)
     {
@@ -107,7 +108,8 @@ clock_nanosleep (clockid_t clk_id, int flags, const struct timespec *rqtp,
       else
        {
          /* 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;
        }
     }
@@ -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 */
   if (rmtp && !abstime)
     {
-      rmtp->tv_sec = (time_t) (timeout.QuadPart / NSPERSEC);
-      rmtp->tv_nsec = (long) ((timeout.QuadPart % NSPERSEC) * 100LL);
+      rmtp->tv_sec = (time_t) (timeout.QuadPart / NS100PERSEC);
+      rmtp->tv_nsec = (long) ((timeout.QuadPart % NS100PERSEC)
+                             * (NSPERSEC/NS100PERSEC));
     }
 
   syscall_printf ("%d = clock_nanosleep(%lu, %d, %ld.%09ld, %ld.%09.ld)",
@@ -160,8 +163,8 @@ extern "C" unsigned int
 usleep (useconds_t useconds)
 {
   struct timespec req;
-  req.tv_sec = useconds / 1000000;
-  req.tv_nsec = (useconds % 1000000) * 1000;
+  req.tv_sec = useconds / USPERSEC;
+  req.tv_nsec = (useconds % USPERSEC) * (NSPERSEC/USPERSEC);
   int res = clock_nanosleep (CLOCK_REALTIME, 0, &req, NULL);
   if (res != 0)
     {
@@ -628,14 +631,15 @@ sigtimedwait (const sigset_t *set, siginfo_t *info, const timespec *timeout)
   if (timeout)
     {
       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);
          return -1;
        }
       /* convert timespec to 100ns units */
-      waittime.QuadPart = (LONGLONG) timeout->tv_sec * NSPERSEC
-                          + ((LONGLONG) timeout->tv_nsec + 99LL) / 100LL;
+      waittime.QuadPart = (LONGLONG) timeout->tv_sec * NS100PERSEC
+                          + ((LONGLONG) timeout->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
+                           / (NSPERSEC/NS100PERSEC);
     }
 
   return sigwait_common (set, info, timeout ? &waittime : cw_infinite);
index 9563b889a264d18732e57a8891519afa882b1eb9..7680cfc90a34ccabff058ebc7b2c9084bf2f0303 100644 (file)
@@ -19,6 +19,7 @@ details. */
 #include "ntdll.h"
 #include "tls_pbuf.h"
 #include "cpuid.h"
+#include "hires.h"
 
 static long
 get_open_max (int in)
@@ -793,7 +794,7 @@ sysinfo (struct sysinfo *info)
                                     sizeof_stodi, NULL);
   if (NT_SUCCESS (status))
     uptime = (stodi->CurrentTime.QuadPart - stodi->BootTime.QuadPart)
-            / 10000000ULL;
+            / NS100PERSEC;
   else
     debug_printf ("NtQuerySystemInformation(SystemTimeOfDayInformation), "
                  "status %y", status);
index 913f7b655b95bde958a7ff89e9ea22451d2df864..9f2e1835790116014456205d713b071e02c7f665 100644 (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. */
   if (abstime->tv_sec < 0
       || abstime->tv_nsec < 0
-      || abstime->tv_nsec > 999999999)
+      || abstime->tv_nsec >= NSPERSEC)
     return EINVAL;
 
   /* 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))
     return ETIMEDOUT;
 
-  timeout->QuadPart = abstime->tv_sec * NSPERSEC
-                    + (abstime->tv_nsec + 99LL) / 100LL;
+  timeout->QuadPart = abstime->tv_sec * NS100PERSEC
+                    + (abstime->tv_nsec + (NSPERSEC/NS100PERSEC) - 1)
+                      / (NSPERSEC/NS100PERSEC);
   switch (clock_id)
     {
     case CLOCK_REALTIME:
@@ -2565,7 +2566,8 @@ pthread_convert_abstime (clockid_t clock_id, const struct timespec *abstime,
       break;
     default:
       /* 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;
       break;
     }
index 044cdda402e6ce5b6e4373ed98100e73a2cc434c..94679c323d681690fe4a1ad3fc6e48733a7dca6f 100644 (file)
@@ -14,6 +14,7 @@ details. */
 #include "fhandler.h"
 #include "dtable.h"
 #include "cygheap.h"
+#include <sys/param.h>
 
 #define TT_MAGIC 0x513e4a1c
 struct timer_tracker
@@ -105,12 +106,12 @@ timer_tracker::timer_tracker (clockid_t c, const sigevent *e)
     }
 }
 
-static long long
-to_us (const timespec& ts)
+static inline long long
+timespec_to_us (const timespec& ts)
 {
   long long res = ts.tv_sec;
-  res *= 1000000;
-  res += ts.tv_nsec / 1000 + ((ts.tv_nsec % 1000) ? 1 : 0);
+  res *= USPERSEC;
+  res += (ts.tv_nsec + (NSPERSEC/USPERSEC) - 1) / (NSPERSEC/USPERSEC);
   return res;
 }
 
@@ -131,7 +132,7 @@ timer_thread (VOID *x)
       if (sleep_us > 0)
        {
          tt->sleepto_us = sleepto_us;
-         sleep_ms = (sleep_us + 999) / 1000;
+         sleep_ms = (sleep_us + (USPERSEC/HZ) - 1) / (USPERSEC/HZ);
        }
       else
        {
@@ -203,10 +204,10 @@ out:
   return 0;
 }
 
-static bool
-it_bad (const timespec& t)
+static inline bool
+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);
       return true;
@@ -227,7 +228,7 @@ timer_tracker::settime (int in_flags, const itimerspec *value, itimerspec *ovalu
          __leave;
        }
 
-      if (it_bad (value->it_value) || it_bad (value->it_interval))
+      if (timespec_bad (value->it_value) || timespec_bad (value->it_interval))
        __leave;
 
       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;
       else
        {
-         sleepto_us = now + to_us (value->it_value);
-         interval_us = to_us (value->it_interval);
+         sleepto_us = now + timespec_to_us (value->it_value);
+         interval_us = timespec_to_us (value->it_interval);
          it_interval = value->it_interval;
          if (!hcancel)
            hcancel = CreateEvent (&sec_none_nih, TRUE, FALSE, NULL);
@@ -274,8 +275,8 @@ timer_tracker::gettime (itimerspec *ovalue)
       long long left_us = sleepto_us - now;
       if (left_us < 0)
        left_us = 0;
-      ovalue->it_value.tv_sec = left_us / 1000000;
-      ovalue->it_value.tv_nsec = (left_us % 1000000) * 1000;
+      ovalue->it_value.tv_sec = left_us / USPERSEC;
+      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 */
  if (value > 0)
    {
-     timer.it_value.tv_sec = value / 1000000;
-     timer.it_value.tv_nsec = (value % 1000000) * 1000;
+     timer.it_value.tv_sec = value / USPERSEC;
+     timer.it_value.tv_nsec = (value % USPERSEC) * (NSPERSEC/USPERSEC);
    }
  if (interval > 0)
    {
-     timer.it_interval.tv_sec = interval / 1000000;
-     timer.it_interval.tv_nsec = (interval % 1000000) * 1000;
+     timer.it_interval.tv_sec = interval / USPERSEC;
+     timer.it_interval.tv_nsec = (interval % USPERSEC) * (NSPERSEC/USPERSEC);
    }
  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);
  return ret;
 }
index 5da0bbc7a2d421e7fa46ffa1980bd9ab49a220b1..86e32b8bf77b0833769b0ae0d2b6453828e6d146 100644 (file)
@@ -10,6 +10,7 @@ details. */
 #include "winsup.h"
 #include <sys/times.h>
 #include <sys/timeb.h>
+#include <sys/param.h>
 #include <utime.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -45,7 +46,7 @@ __to_clock_t (PLARGE_INTEGER src, int flag)
   if (flag)
     total -= FACTOR;
 
-  total /= NSPERSEC / CLOCKS_PER_SEC;
+  total /= NS100PERSEC / CLOCKS_PER_SEC;
   return total;
 }
 
@@ -72,7 +73,7 @@ times (struct tms *buf)
       /* uptime */
       ticks.QuadPart -= stodi.BootTime.QuadPart;
       /* 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_utime = __to_clock_t (&kut.UserTime, 0);
@@ -102,7 +103,7 @@ settimeofday (const struct timeval *tv, const struct timezone *tz)
 
   __try
     {
-      if (tv->tv_usec < 0 || tv->tv_usec >= 1000000)
+      if (tv->tv_usec < 0 || tv->tv_usec >= USPERSEC)
        {
          set_errno (EINVAL);
          return -1;
@@ -116,7 +117,7 @@ settimeofday (const struct timeval *tv, const struct timezone *tz)
       st.wHour    = ptm->tm_hour;
       st.wMinute       = ptm->tm_min;
       st.wSecond       = ptm->tm_sec;
-      st.wMilliseconds = tv->tv_usec / 1000;
+      st.wMilliseconds = tv->tv_usec / (USPERSEC/HZ);
 
       res = -!SetSystemTime (&st);
       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);
 
-  x *= (int64_t) 1000000 / CLOCKS_PER_SEC; /* Turn x into usecs */
-  x -= (int64_t) sub * 1000000;
+  x *= (int64_t) USPERSEC / CLOCKS_PER_SEC; /* Turn x into usecs */
+  x -= (int64_t) sub * USPERSEC;
 
-  dst->tv_usec = x % 1000000; /* And split */
-  dst->tv_sec = x / 1000000;
+  dst->tv_usec = x % USPERSEC; /* And split */
+  dst->tv_sec = x / USPERSEC;
 }
 
 /* FIXME: Make thread safe */
@@ -176,8 +177,8 @@ gettimeofday (struct timeval *__restrict tv, void *__restrict tzvp)
   if (now == (LONGLONG) -1)
     return -1;
 
-  tv->tv_sec = now / 1000000;
-  tv->tv_usec = now % 1000000;
+  tv->tv_sec = now / USPERSEC;
+  tv->tv_usec = now % USPERSEC;
 
   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)
     out->QuadPart = 0;
   else
-    out->QuadPart = time_in->tv_sec * NSPERSEC
-                   + time_in->tv_nsec / (1000000000/NSPERSEC) + FACTOR;
+    out->QuadPart = time_in->tv_sec * NS100PERSEC
+                   + time_in->tv_nsec / (NSPERSEC/NS100PERSEC) + FACTOR;
 }
 
 /* Cygwin internal */
 void __stdcall
 timeval_to_filetime (const struct timeval *time_in, PLARGE_INTEGER out)
 {
-  out->QuadPart = time_in->tv_sec * NSPERSEC
-                 + time_in->tv_usec * (NSPERSEC/1000000) + FACTOR;
+  out->QuadPart = time_in->tv_sec * NS100PERSEC
+                 + time_in->tv_usec * (NS100PERSEC/USPERSEC) + FACTOR;
 }
 
 /* Cygwin internal */
@@ -232,18 +233,18 @@ timeval_to_timespec (const struct timeval *tvp, struct timespec *tmp)
     return NULL;
 
   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)
     tmp[0].tv_nsec = 0;
-  else if (tmp[0].tv_nsec > 999999999)
-    tmp[0].tv_nsec = 999999999;
+  else if (tmp[0].tv_nsec >= NSPERSEC)
+    tmp[0].tv_nsec = NSPERSEC - 1;
 
   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)
     tmp[1].tv_nsec = 0;
-  else if (tmp[1].tv_nsec > 999999999)
-    tmp[1].tv_nsec = 999999999;
+  else if (tmp[1].tv_nsec >= NSPERSEC)
+    tmp[1].tv_nsec = NSPERSEC - 1;
 
   return tmp;
 }
@@ -264,7 +265,7 @@ to_time_t (PLARGE_INTEGER ptr)
     return 0;
 
   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;
 }
 
@@ -277,8 +278,6 @@ to_timestruc_t (PLARGE_INTEGER ptr, timestruc_t *out)
      stuffed into two long words.
      A timestruc_t is the number of seconds and microseconds since jan 1 1970
      stuffed into a time_t and a long.  */
-
-  int64_t rem;
   int64_t x = ptr->QuadPart;
 
   /* 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 */
-  rem = x % NSPERSEC;
-  x /= NSPERSEC;               /* number of 100ns in a second */
-  out->tv_nsec = rem * 100;    /* as tv_nsec is in nanoseconds */
-  out->tv_sec = x;
+  out->tv_sec = x / NS100PERSEC;
+  out->tv_nsec = (x % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
 }
 
 /* Cygwin internal */
@@ -470,7 +467,7 @@ hires_ns::prime ()
       return;
     }
 
-  freq = (double) ((double) 1000000000. / (double) ifreq.QuadPart);
+  freq = (double) ((double) NSPERSEC / (double) ifreq.QuadPart);
   inited = true;
   SetThreadPriority (GetCurrentThread (), priority);
 }
@@ -534,8 +531,8 @@ clock_gettime (clockid_t clk_id, struct timespec *tp)
                                 &kut, sizeof kut, NULL);
 
       x = kut.KernelTime.QuadPart + kut.UserTime.QuadPart;
-      tp->tv_sec = x / NSPERSEC;
-      tp->tv_nsec = (x % NSPERSEC) * 100LL;
+      tp->tv_sec = x / NS100PERSEC;
+      tp->tv_nsec = (x % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
 
       CloseHandle (hProcess);
       return 0;
@@ -562,8 +559,8 @@ clock_gettime (clockid_t clk_id, struct timespec *tp)
                                &kut, sizeof kut, NULL);
 
       x = kut.KernelTime.QuadPart + kut.UserTime.QuadPart;
-      tp->tv_sec = x / NSPERSEC;
-      tp->tv_nsec = (x % NSPERSEC) * 100LL;
+      tp->tv_sec = x / NS100PERSEC;
+      tp->tv_nsec = (x % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
 
       CloseHandle (hThread);
       return 0;
@@ -576,8 +573,8 @@ clock_gettime (clockid_t clk_id, struct timespec *tp)
          LONGLONG now = gtod.nsecs ();
          if (now == (LONGLONG) -1)
            return -1;
-         tp->tv_sec = now / NSPERSEC;
-         tp->tv_nsec = (now % NSPERSEC) * (1000000000 / NSPERSEC);
+         tp->tv_sec = now / NS100PERSEC;
+         tp->tv_nsec = (now % NS100PERSEC) * (NSPERSEC / NS100PERSEC);
          break;
        }
 
@@ -587,8 +584,8 @@ clock_gettime (clockid_t clk_id, struct timespec *tp)
          if (now == (LONGLONG) -1)
            return -1;
 
-         tp->tv_sec = now / 1000000000;
-         tp->tv_nsec = (now % 1000000000);
+         tp->tv_sec = now / NSPERSEC;
+         tp->tv_nsec = (now % NSPERSEC);
          break;
        }
 
@@ -667,8 +664,8 @@ clock_getres (clockid_t clk_id, struct timespec *tp)
       ULONG coarsest, finest, actual;
 
       NtQueryTimerResolution (&coarsest, &finest, &actual);
-      tp->tv_sec = coarsest / NSPERSEC;
-      tp->tv_nsec = (coarsest % NSPERSEC) * 100;
+      tp->tv_sec = coarsest / NS100PERSEC;
+      tp->tv_nsec = (coarsest % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
       return 0;
     }
 
@@ -677,16 +674,16 @@ clock_getres (clockid_t clk_id, struct timespec *tp)
       case CLOCK_REALTIME:
        {
          DWORD period = gtod.resolution ();
-         tp->tv_sec = period / NSPERSEC;
-         tp->tv_nsec = (period % NSPERSEC) * 100;
+         tp->tv_sec = period / NS100PERSEC;
+         tp->tv_nsec = (period % NS100PERSEC) * (NSPERSEC/NS100PERSEC);
          break;
        }
 
       case CLOCK_MONOTONIC:
        {
          LONGLONG period = ntod.resolution ();
-         tp->tv_sec = period / 1000000000;
-         tp->tv_nsec = period % 1000000000;
+         tp->tv_sec = period / NSPERSEC;
+         tp->tv_nsec = period % NSPERSEC;
          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
      beyond 15ms, but this might change in future OS versions, so we play nice
      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
      rounds the incoming value to the nearest supported value. */
This page took 0.064808 seconds and 5 git commands to generate.