]> sjero.net Git - wget/blobdiff - src/ptimer.c
[svn] Update FSF's address and copyright years.
[wget] / src / ptimer.c
index d63e69e27821b11432aa1285fd00ebfbb3a390fc..e082575804e2d406305ecb503b5b32a53054d577 100644 (file)
@@ -14,8 +14,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with Wget; if not, write to the Free Software
-Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+along with Wget; if not, write to the Free Software Foundation, Inc.,
+51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 
 In addition, as a special exception, the Free Software Foundation
 gives permission to link the code of its release of Wget with the
@@ -39,9 +39,10 @@ so, delete this exception statement from your version.  */
      ptimer_destroy -- destroy the timer.
      ptimer_granularity -- returns the approximate granularity of the timers.
 
-   Timers operate in milliseconds, but return floating point values
-   that can be more precise.  For example, to measure the time it
-   takes to run a loop, you can use something like:
+   Timers measure time in milliseconds, but the timings they return
+   are floating point numbers, so they can carry as much precision as
+   the underlying system timer supports.  For example, to measure the
+   time it takes to run a loop, you can use something like:
 
      ptimer *tmr = ptimer_new ();
      while (...)
@@ -53,46 +54,39 @@ so, delete this exception statement from your version.  */
 
 #include <stdio.h>
 #include <stdlib.h>
-#ifdef HAVE_STRING_H
-# include <string.h>
-#else  /* not HAVE_STRING_H */
-# include <strings.h>
-#endif /* not HAVE_STRING_H */
-#include <sys/types.h>
+#include <string.h>
 #include <errno.h>
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif
-#include <assert.h>
+#include <time.h>
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
+
+/* Cygwin currently (as of 2005-04-08, Cygwin 1.5.14) lacks clock_getres,
+   but still defines _POSIX_TIMERS!  Because of that we simply use the
+   Windows timers under Cygwin.  */
+#ifdef __CYGWIN__
+# include <windows.h>
+#endif
 
 #include "wget.h"
 #include "ptimer.h"
 
-#ifndef errno
-extern int errno;
-#endif
-
-/* Depending on the OS and availability of gettimeofday(), one and
-   only one of PTIMER_POSIX, PTIMER_GETTIMEOFDAY, PTIMER_WINDOWS, or
-   PTIMER_TIME will be defined.  */
+/* Depending on the OS, one and only one of PTIMER_POSIX,
+   PTIMER_GETTIMEOFDAY, or PTIMER_WINDOWS will be defined.  */
 
 #undef PTIMER_POSIX
 #undef PTIMER_GETTIMEOFDAY
-#undef PTIMER_TIME
 #undef PTIMER_WINDOWS
 
-#ifdef WINDOWS
+#if defined(WINDOWS) || defined(__CYGWIN__)
 # define PTIMER_WINDOWS                /* use Windows timers */
+#elif _POSIX_TIMERS - 0 > 0
+# define PTIMER_POSIX          /* use POSIX timers (clock_gettime) */
 #else
-# if _POSIX_TIMERS > 0
-#  define PTIMER_POSIX         /* use POSIX timers (clock_gettime) */
-# else
-#  ifdef HAVE_GETTIMEOFDAY
-#   define PTIMER_GETTIMEOFDAY /* use gettimeofday */
-#  else
-#   define PTIMER_TIME
-#  endif
-# endif
+# define PTIMER_GETTIMEOFDAY   /* use gettimeofday */
 #endif
 
 #ifdef PTIMER_POSIX
@@ -131,7 +125,7 @@ posix_init (void)
     int id;
     int sysconf_name;
   } clocks[] = {
-#if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK >= 0
+#if defined(_POSIX_MONOTONIC_CLOCK) && _POSIX_MONOTONIC_CLOCK - 0 >= 0
     { CLOCK_MONOTONIC, _SC_MONOTONIC_CLOCK },
 #endif
 #ifdef CLOCK_HIGHRES
@@ -229,39 +223,6 @@ gettimeofday_resolution (void)
 }
 #endif /* PTIMER_GETTIMEOFDAY */
 
-#ifdef PTIMER_TIME
-/* Elapsed time measurement using the time(2) call: system time is
-   held in time_t, retrieved using time, and resolution is 1 second.
-
-   This method is a catch-all for non-Windows systems without
-   gettimeofday -- e.g. DOS or really old or non-standard Unix
-   systems.  */
-
-typedef time_t ptimer_system_time;
-
-#define IMPL_measure time_measure
-#define IMPL_diff time_diff
-#define IMPL_resolution time_resolution
-
-static inline void
-time_measure (ptimer_system_time *pst)
-{
-  time (pst);
-}
-
-static inline double
-time_diff (ptimer_system_time *pst1, ptimer_system_time *pst2)
-{
-  return 1000.0 * (*pst1 - *pst2);
-}
-
-static inline double
-time_resolution (void)
-{
-  return 1;
-}
-#endif /* PTIMER_TIME */
-
 #ifdef PTIMER_WINDOWS
 /* Elapsed time measurement on Windows: where high-resolution timers
    are available, time is stored in a LARGE_INTEGER and retrieved
@@ -282,7 +243,7 @@ typedef union {
 
 /* Whether high-resolution timers are used.  Set by ptimer_initialize_once
    the first time ptimer_new is called. */
-static int windows_hires_timers;
+static bool windows_hires_timers;
 
 /* Frequency of high-resolution timers -- number of updates per
    millisecond.  Calculated the first time ptimer_new is called
@@ -297,7 +258,7 @@ windows_init (void)
   QueryPerformanceFrequency (&freq);
   if (freq.QuadPart != 0)
     {
-      windows_hires_timers = 1;
+      windows_hires_timers = true;
       windows_hires_msfreq = (double) freq.QuadPart / 1000.0;
     }
 }
@@ -338,9 +299,6 @@ windows_resolution (void)
 /* The code below this point is independent of timer implementation. */
 
 struct ptimer {
-  /* Whether the start time has been set. */
-  int initialized;
-
   /* The starting point in time which, subtracted from the current
      time, yields elapsed time. */
   ptimer_system_time start;
@@ -360,40 +318,39 @@ struct ptimer {
 struct ptimer *
 ptimer_new (void)
 {
-  struct ptimer *wt = xnew0 (struct ptimer);
+  struct ptimer *pt = xnew0 (struct ptimer);
 #ifdef IMPL_init
-  static int init_done;
+  static bool init_done;
   if (!init_done)
     {
-      init_done = 1;
+      init_done = true;
       IMPL_init ();
     }
 #endif
-  ptimer_reset (wt);
-  return wt;
+  ptimer_reset (pt);
+  return pt;
 }
 
 /* Free the resources associated with the timer.  Its further use is
    prohibited.  */
 
 void
-ptimer_destroy (struct ptimer *wt)
+ptimer_destroy (struct ptimer *pt)
 {
-  xfree (wt);
+  xfree (pt);
 }
 
-/* Reset timer WT.  This establishes the starting point from which
+/* Reset timer PT.  This establishes the starting point from which
    ptimer_read() will return the number of elapsed milliseconds.
    It is allowed to reset a previously used timer.  */
 
 void
-ptimer_reset (struct ptimer *wt)
+ptimer_reset (struct ptimer *pt)
 {
   /* Set the start time to the current time. */
-  IMPL_measure (&wt->start);
-  wt->elapsed_last = 0;
-  wt->elapsed_pre_start = 0;
-  wt->initialized = 1;
+  IMPL_measure (&pt->start);
+  pt->elapsed_last = 0;
+  pt->elapsed_pre_start = 0;
 }
 
 /* Measure the elapsed time since timer creation/reset and return it
@@ -408,24 +365,22 @@ ptimer_reset (struct ptimer *wt)
    ignored.  */
 
 double
-ptimer_measure (struct ptimer *wt)
+ptimer_measure (struct ptimer *pt)
 {
   ptimer_system_time now;
   double elapsed;
 
-  assert (wt->initialized != 0);
-
   IMPL_measure (&now);
-  elapsed = wt->elapsed_pre_start + IMPL_diff (&now, &wt->start);
+  elapsed = pt->elapsed_pre_start + IMPL_diff (&now, &pt->start);
 
   /* Ideally we'd just return the difference between NOW and
-     wt->start.  However, the system timer can be set back, and we
+     pt->start.  However, the system timer can be set back, and we
      could return a value smaller than when we were last called, even
      a negative value.  Both of these would confuse the callers, which
      expect us to return monotonically nondecreasing values.
 
      Therefore: if ELAPSED is smaller than its previous known value,
-     we reset wt->start to the current time and effectively start
+     we reset pt->start to the current time and effectively start
      measuring from this point.  But since we don't want the elapsed
      value to start from zero, we set elapsed_pre_start to the last
      elapsed time and increment all future calculations by that
@@ -434,14 +389,14 @@ ptimer_measure (struct ptimer *wt)
      This cannot happen with Windows and POSIX monotonic/highres
      timers, but the check is not expensive.  */
 
-  if (elapsed < wt->elapsed_last)
+  if (elapsed < pt->elapsed_last)
     {
-      wt->start = now;
-      wt->elapsed_pre_start = wt->elapsed_last;
-      elapsed = wt->elapsed_last;
+      pt->start = now;
+      pt->elapsed_pre_start = pt->elapsed_last;
+      elapsed = pt->elapsed_last;
     }
 
-  wt->elapsed_last = elapsed;
+  pt->elapsed_last = elapsed;
   return elapsed;
 }
 
@@ -449,9 +404,9 @@ ptimer_measure (struct ptimer *wt)
    ptimer_reset and the last call to ptimer_update.  */
 
 double
-ptimer_read (const struct ptimer *wt)
+ptimer_read (const struct ptimer *pt)
 {
-  return wt->elapsed_last;
+  return pt->elapsed_last;
 }
 
 /* Return the assessed resolution of the timer implementation, in