]> git.ipfire.org Git - thirdparty/curl.git/commitdiff
curltime: use libcurl time functions in src and tests/server
authorViktor Szakats <commit@vsz.me>
Sun, 9 Mar 2025 12:14:31 +0000 (13:14 +0100)
committerViktor Szakats <commit@vsz.me>
Wed, 12 Mar 2025 10:33:26 +0000 (11:33 +0100)
The curl tool and tests/server used 2 parallel implementations
of libcurl's `Curl_now()` and `Curl_timediff()` functions.

Make them use the libcurl one.

Closes #16653

20 files changed:
lib/timeval.c
lib/timeval.h
projects/generate.bat
src/Makefile.inc
src/tool_cb_prg.c
src/tool_cb_prg.h
src/tool_cb_rea.c
src/tool_cfgable.h
src/tool_doswin.c
src/tool_operate.c
src/tool_operate.h
src/tool_progress.c
src/tool_progress.h
src/tool_setup.h
src/tool_util.c
src/tool_util.h
tests/server/Makefile.inc
tests/server/util.c
tests/server/util.h
winbuild/MakefileBuild.vc

index 890992efcec6749fb5418b5af736f9595d0bd24c..c93ecd9d33e0ff7df45197d3ea0c06061445ce81 100644 (file)
 #ifdef _WIN32
 
 #include <curl/curl.h>
+#ifdef BUILDING_LIBCURL
 #include "system_win32.h"
+#else
+#include "version_win32.h"
+
+static LARGE_INTEGER s_freq;
+static bool s_isVistaOrGreater;
+
+/* For tool or tests, we must initialize before calling Curl_now() */
+void curlx_now_init(void)
+{
+  if(curlx_verify_windows_version(6, 0, 0, PLATFORM_WINNT,
+                                  VERSION_GREATER_THAN_EQUAL))
+    s_isVistaOrGreater = true;
+  else
+    s_isVistaOrGreater = false;
+
+  QueryPerformanceFrequency(&s_freq);
+}
+#endif
 
 /* In case of bug fix this function has a counterpart in tool_util.c */
 struct curltime Curl_now(void)
 {
   struct curltime now;
-  if(Curl_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
+  bool isVistaOrGreater;
+#ifdef BUILDING_LIBCURL
+  isVistaOrGreater = Curl_isVistaOrGreater;
+#else
+  isVistaOrGreater = s_isVistaOrGreater;
+#endif
+  if(isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
     LARGE_INTEGER count;
+    LARGE_INTEGER freq;
+#ifdef BUILDING_LIBCURL
+    freq = Curl_freq;
+#else
+    freq = s_freq;
+#endif
+    DEBUGASSERT(freq.QuadPart);
     QueryPerformanceCounter(&count);
-    now.tv_sec = (time_t)(count.QuadPart / Curl_freq.QuadPart);
-    now.tv_usec = (int)((count.QuadPart % Curl_freq.QuadPart) * 1000000 /
-                        Curl_freq.QuadPart);
+    now.tv_sec = (time_t)(count.QuadPart / freq.QuadPart);
+    now.tv_usec = (int)((count.QuadPart % freq.QuadPart) * 1000000 /
+                        freq.QuadPart);
   }
   else {
     /* Disable /analyze warning that GetTickCount64 is preferred  */
index 33dfb5b10dd26b94c8348f03a170423bf5f0da4a..5f3d79c36d0ffc09c41674259df0e1d2374384a2 100644 (file)
 
 #include "timediff.h"
 
+#ifndef BUILDING_LIBCURL
+/* this renames the functions so that the tool code can use the same code
+   without getting symbol collisions */
+#define Curl_now curlx_now
+#define Curl_timediff(a,b) curlx_timediff(a,b)
+#define Curl_timediff_ceil(a,b) curlx_timediff_ceil(a,b)
+#define Curl_timediff_us(a,b) curlx_timediff_us(a,b)
+
+/* For tool or tests, we must initialize before calling Curl_now() */
+void curlx_now_init(void);
+#endif
+
 struct curltime {
   time_t tv_sec; /* seconds */
   int tv_usec;   /* microseconds */
index 73c081707cb18aa43c279b59dea89da52baebf25..d8efd4db7bb49950c3754b5e142487cb6de13bb7 100644 (file)
@@ -153,6 +153,7 @@ rem
     ) else if "!var!" == "CURL_SRC_X_C_FILES" (
       call :element %1 lib "strparse.c" %3
       call :element %1 lib "strcase.c" %3
+      call :element %1 lib "timeval.c" %3
       call :element %1 lib "nonblock.c" %3
       call :element %1 lib "warnless.c" %3
       call :element %1 lib "curl_multibyte.c" %3
@@ -164,6 +165,7 @@ rem
       call :element %1 lib "curl_setup.h" %3
       call :element %1 lib "strparse.h" %3
       call :element %1 lib "strcase.h" %3
+      call :element %1 lib "timeval.h" %3
       call :element %1 lib "nonblock.h" %3
       call :element %1 lib "warnless.h" %3
       call :element %1 lib "curl_ctype.h" %3
index 06a3fd30aaf255ba6b5e7c913b62798c30f1d37a..dccb8c0d1df4d95236de823272c6874830086ff2 100644 (file)
@@ -32,7 +32,8 @@
 # libcurl sources to include in curltool lib we use for test binaries
 CURLTOOL_LIBCURL_CFILES = \
   ../lib/base64.c \
-  ../lib/dynbuf.c
+  ../lib/dynbuf.c \
+  ../lib/timeval.c
 
 # libcurl has sources that provide functions named curlx_* that are not part of
 # the official API, but we reuse the code here to avoid duplication.
@@ -44,6 +45,7 @@ CURLX_CFILES = \
   ../lib/strparse.c \
   ../lib/strcase.c \
   ../lib/timediff.c \
+  ../lib/timeval.c \
   ../lib/version_win32.c \
   ../lib/warnless.c
 
@@ -56,6 +58,7 @@ CURLX_HFILES = \
   ../lib/strparse.h \
   ../lib/strcase.h \
   ../lib/timediff.h \
+  ../lib/timeval.h \
   ../lib/version_win32.h \
   ../lib/warnless.h
 
index 0e58474ac851f68fe37fb9dd87264cb96a6bad5c..1f90cda7f25fbeda6f8fb093a38a2fa948f3f616 100644 (file)
@@ -133,7 +133,7 @@ int tool_progress_cb(void *clientp,
                      curl_off_t dltotal, curl_off_t dlnow,
                      curl_off_t ultotal, curl_off_t ulnow)
 {
-  struct timeval now = tvnow();
+  struct curltime now = curlx_now();
   struct per_transfer *per = clientp;
   struct OperationConfig *config = per->config;
   struct ProgressData *bar = &per->progressbar;
@@ -173,13 +173,13 @@ int tool_progress_cb(void *clientp,
       if(bar->prev == point)
         /* progress did not change since last invoke */
         return 0;
-      else if((tvdiff(now, bar->prevtime) < 100L) && point < total)
+      else if((curlx_timediff(now, bar->prevtime) < 100L) && point < total)
         /* limit progress-bar updating to 10 Hz except when we are at 100% */
         return 0;
     }
     else {
       /* total is unknown */
-      if(tvdiff(now, bar->prevtime) < 100L)
+      if(curlx_timediff(now, bar->prevtime) < 100L)
         /* limit progress-bar updating to 10 Hz */
         return 0;
       update_width(bar);
index dc10f2a5ccc813394c5e54d674867b2a82c8f3d3..1f005dbfc4e92a0bb710596323faade8fcd68e60 100644 (file)
@@ -31,7 +31,7 @@
 struct ProgressData {
   int         calls;
   curl_off_t  prev;
-  struct timeval prevtime;
+  struct curltime prevtime;
   int         width;
   FILE       *out;  /* where to write everything to */
   curl_off_t  initial_size;
index c3f4c5916c0a6272a86a4df6713cf9fbae4585b4..60c1def0cf37133532653a374abf65aea8a61555 100644 (file)
@@ -55,8 +55,8 @@ size_t tool_read_cb(char *buffer, size_t sz, size_t nmemb, void *userdata)
   }
 
   if(config->timeout_ms) {
-    struct timeval now = tvnow();
-    long msdelta = tvdiff(now, per->start);
+    struct curltime now = curlx_now();
+    long msdelta = (long)curlx_timediff(now, per->start);
 
     if(msdelta > config->timeout_ms)
       /* timeout */
@@ -132,17 +132,16 @@ int tool_readbusy_cb(void *clientp,
   if(config->readbusy) {
     /* lame code to keep the rate down because the input might not deliver
        anything, get paused again and come back here immediately */
-    static long rate = 500;
-    static struct timeval prev;
+    static timediff_t rate = 500;
+    static struct curltime prev;
     static curl_off_t ulprev;
 
     if(ulprev == ulnow) {
       /* it did not upload anything since last call */
-      struct timeval now = tvnow();
+      struct curltime now = curlx_now();
       if(prev.tv_sec)
         /* get a rolling average rate */
-        /* rate = rate - rate/4 + tvdiff(now, prev)/4; */
-        rate -= rate/4 - tvdiff(now, prev)/4;
+        rate -= rate/4 - curlx_timediff(now, prev)/4;
       prev = now;
     }
     else {
index 6a60a1622f77ae5a2cda650bc55f0b6d9fbe1580..75fef60c80a15b6a7e2c2a119b01bc2b55064149 100644 (file)
@@ -331,7 +331,7 @@ struct GlobalConfig {
   struct OperationConfig *first;
   struct OperationConfig *current;
   struct OperationConfig *last;
-  long ms_per_transfer;           /* start next transfer after (at least) this
+  timediff_t ms_per_transfer;     /* start next transfer after (at least) this
                                      many milliseconds */
   trace tracetype;
   int progressmode;               /* CURL_PROGRESS_BAR / CURL_PROGRESS_STATS */
index 23c828e3b59157a7f39d2d02a8c72c51cbd40525..d30a204e804ea446021295f9a4b8119df5fc6019 100644 (file)
@@ -732,21 +732,9 @@ static void init_terminal(void)
 }
 #endif
 
-LARGE_INTEGER tool_freq;
-bool tool_isVistaOrGreater;
-
 CURLcode win32_init(void)
 {
-  /* curlx_verify_windows_version must be called during init at least once
-     because it has its own initialization routine. */
-  if(curlx_verify_windows_version(6, 0, 0, PLATFORM_WINNT,
-                                  VERSION_GREATER_THAN_EQUAL))
-    tool_isVistaOrGreater = true;
-  else
-    tool_isVistaOrGreater = false;
-
-  QueryPerformanceFrequency(&tool_freq);
-
+  curlx_now_init();
 #if !defined(CURL_WINDOWS_UWP) && !defined(UNDER_CE)
   init_terminal();
 #endif
index 74d53404f139b114f38ffac685c931eaebf5a863..d8eebd117398868675f8d97769c1107e9a397765 100644 (file)
@@ -408,7 +408,7 @@ static CURLcode pre_transfer(struct GlobalConfig *global,
       my_setopt_offt(per->curl, CURLOPT_INFILESIZE_LARGE, uploadfilesize);
   }
   per->uploadfilesize = uploadfilesize;
-  per->start = tvnow();
+  per->start = curlx_now();
   return result;
 }
 
@@ -522,7 +522,7 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
      time */
   if(per->retry_remaining &&
      (!config->retry_maxtime ||
-      (tvdiff(tvnow(), per->retrystart) <
+      (curlx_timediff(curlx_now(), per->retrystart) <
        config->retry_maxtime*1000L)) ) {
     enum {
       RETRY_NO,
@@ -627,7 +627,8 @@ static CURLcode post_per_transfer(struct GlobalConfig *global,
              maximum time allowed for retrying, then exit the retries right
              away */
           if(config->retry_maxtime) {
-            curl_off_t seconds = tvdiff(tvnow(), per->retrystart)/1000;
+            curl_off_t seconds = curlx_timediff(curlx_now(),
+                                                per->retrystart)/1000;
 
             if((CURL_OFF_T_MAX - retry_after < seconds) ||
                (seconds + retry_after > config->retry_maxtime)) {
@@ -2305,7 +2306,7 @@ static CURLcode single_transfer(struct GlobalConfig *global,
         config->retry_delay*1000L : RETRY_SLEEP_DEFAULT; /* ms */
       per->retry_remaining = config->req_retry;
       per->retry_sleep = per->retry_sleep_default; /* ms */
-      per->retrystart = tvnow();
+      per->retrystart = curlx_now();
 
       state->li++;
       /* Here's looping around each globbed URL */
@@ -2440,7 +2441,7 @@ struct parastate {
   CURLMcode mcode;
   CURLcode result;
   int still_running;
-  struct timeval start;
+  struct curltime start;
   bool more_transfers;
   bool added_transfers;
   /* wrapitup is set TRUE after a critical error occurs to end all transfers */
@@ -2771,7 +2772,7 @@ static CURLcode parallel_transfers(struct GlobalConfig *global,
   s->mcode = CURLM_OK;
   s->result = CURLE_OK;
   s->still_running = 1;
-  s->start = tvnow();
+  s->start = curlx_now();
   s->wrapitup = FALSE;
   s->wrapitup_processed = FALSE;
   s->tick = time(NULL);
@@ -2858,9 +2859,9 @@ static CURLcode serial_transfers(struct GlobalConfig *global,
     bool retry;
     long delay_ms;
     bool bailout = FALSE;
-    struct timeval start;
+    struct curltime start;
 
-    start = tvnow();
+    start = curlx_now();
     if(!per->skip) {
       result = pre_transfer(global, per);
       if(result)
@@ -2923,12 +2924,13 @@ static CURLcode serial_transfers(struct GlobalConfig *global,
     if(per && global->ms_per_transfer) {
       /* how long time did the most recent transfer take in number of
          milliseconds */
-      long milli = tvdiff(tvnow(), start);
+      timediff_t milli = curlx_timediff(curlx_now(), start);
       if(milli < global->ms_per_transfer) {
-        notef(global, "Transfer took %ld ms, waits %ldms as set by --rate",
-              milli, global->ms_per_transfer - milli);
+        notef(global, "Transfer took %" CURL_FORMAT_CURL_OFF_T " ms, "
+                      "waits %ldms as set by --rate",
+              milli, (long)(global->ms_per_transfer - milli));
         /* The transfer took less time than wanted. Wait a little. */
-        tool_go_sleep(global->ms_per_transfer - milli);
+        tool_go_sleep((long)(global->ms_per_transfer - milli));
       }
     }
   }
index d57afedcaf4b1e30142e30b05170a958b880f2a8..88fb5f989615e9b018239aa02aedc4d92928e9fe 100644 (file)
@@ -39,8 +39,8 @@ struct per_transfer {
   long retry_sleep_default;
   long retry_sleep;
   long num_retries; /* counts the performed retries */
-  struct timeval start; /* start of this transfer */
-  struct timeval retrystart;
+  struct curltime start; /* start of this transfer */
+  struct curltime retrystart;
   char *url;
   unsigned int urlnum; /* the index of the given URL */
   char *outfile;
index c4df2111ae7a7084df6b2b51eb6a977816679095..90b31651267424ef2e7dd0c9f02c44f3b3d47e75 100644 (file)
@@ -141,7 +141,7 @@ curl_off_t all_xfers = 0;   /* current total */
 struct speedcount {
   curl_off_t dl;
   curl_off_t ul;
-  struct timeval stamp;
+  struct curltime stamp;
 };
 #define SPEEDCNT 10
 static unsigned int speedindex;
@@ -153,19 +153,19 @@ static struct speedcount speedstore[SPEEDCNT];
   |  6 --   9.9G     0     2     2   0:00:40  0:00:02  0:00:37 4087M
 */
 bool progress_meter(struct GlobalConfig *global,
-                    struct timeval *start,
+                    struct curltime *start,
                     bool final)
 {
-  static struct timeval stamp;
+  static struct curltime stamp;
   static bool header = FALSE;
-  struct timeval now;
-  long diff;
+  struct curltime now;
+  timediff_t diff;
 
   if(global->noprogress || global->silent)
     return FALSE;
 
-  now = tvnow();
-  diff = tvdiff(now, stamp);
+  now = curlx_now();
+  diff = curlx_timediff(now, stamp);
 
   if(!header) {
     header = TRUE;
@@ -178,7 +178,7 @@ bool progress_meter(struct GlobalConfig *global,
     char time_total[10];
     char time_spent[10];
     char buffer[3][6];
-    curl_off_t spent = tvdiff(now, *start)/1000;
+    curl_off_t spent = curlx_timediff(now, *start)/1000;
     char dlpercen[4]="--";
     char ulpercen[4]="--";
     struct per_transfer *per;
@@ -239,20 +239,20 @@ bool progress_meter(struct GlobalConfig *global,
     }
 
     {
-      long deltams;
+      timediff_t deltams;
       curl_off_t dl;
       curl_off_t ul;
       curl_off_t dls;
       curl_off_t uls;
       if(indexwrapped) {
         /* 'speedindex' is the oldest stored data */
-        deltams = tvdiff(now, speedstore[speedindex].stamp);
+        deltams = curlx_timediff(now, speedstore[speedindex].stamp);
         dl = all_dlnow - speedstore[speedindex].dl;
         ul = all_ulnow - speedstore[speedindex].ul;
       }
       else {
         /* since the beginning */
-        deltams = tvdiff(now, *start);
+        deltams = curlx_timediff(now, *start);
         dl = all_dlnow;
         ul = all_ulnow;
       }
index 4c869a0ac367601ee6de908f2c778de308793861..02c34d42eee81a34380b4831ef4f5030b0f1652a 100644 (file)
@@ -32,7 +32,7 @@ int xferinfo_cb(void *clientp,
                 curl_off_t ulnow);
 
 bool progress_meter(struct GlobalConfig *global,
-                    struct timeval *start,
+                    struct curltime *start,
                     bool final);
 void progress_finalize(struct per_transfer *per);
 
index 57cd9a57fcbea95787c91c05bbc6475a3380b6fd..2acc6be180a07ece24efba270cfee9d455c001e3 100644 (file)
@@ -45,6 +45,8 @@ extern FILE *tool_stderr;
 
 #include <curl/curl.h> /* external interface */
 
+#include "timeval.h"
+
 /*
  * Platform specific stuff.
  */
@@ -82,9 +84,6 @@ extern FILE *tool_stderr;
 #endif
 
 #ifdef _WIN32
-/* set in win32_init() */
-extern LARGE_INTEGER tool_freq;
-extern bool tool_isVistaOrGreater;
 /* set in init_terminal() */
 extern bool tool_term_has_bold;
 
index 6bb7c7aefe4e77b34def3e09fa6c45859d1c43a6..5afec91675365ae6ffe60e3da43af10ba58baa22 100644 (file)
 
 #ifdef _WIN32
 
-/* In case of bug fix this function has a counterpart in timeval.c */
-struct timeval tvnow(void)
-{
-  struct timeval now;
-  if(tool_isVistaOrGreater) { /* QPC timer might have issues pre-Vista */
-    LARGE_INTEGER count;
-    QueryPerformanceCounter(&count);
-    now.tv_sec = (long)(count.QuadPart / tool_freq.QuadPart);
-    now.tv_usec = (long)((count.QuadPart % tool_freq.QuadPart) * 1000000 /
-                         tool_freq.QuadPart);
-  }
-  else {
-    /* Disable /analyze warning that GetTickCount64 is preferred  */
-#ifdef _MSC_VER
-#pragma warning(push)
-#pragma warning(disable:28159)
-#endif
-    DWORD milliseconds = GetTickCount();
-#ifdef _MSC_VER
-#pragma warning(pop)
-#endif
-
-    now.tv_sec = (long)(milliseconds / 1000);
-    now.tv_usec = (long)((milliseconds % 1000) * 1000);
-  }
-  return now;
-}
-
-#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
-
-struct timeval tvnow(void)
-{
-  /*
-  ** clock_gettime() is granted to be increased monotonically when the
-  ** monotonic clock is queried. Time starting point is unspecified, it
-  ** could be the system start-up time, the Epoch, or something else,
-  ** in any case the time starting point does not change once that the
-  ** system has started up.
-  */
-  struct timeval now;
-  struct timespec tsnow;
-  if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
-    now.tv_sec = tsnow.tv_sec;
-    now.tv_usec = (int)(tsnow.tv_nsec / 1000);
-  }
-  /*
-  ** Even when the configure process has truly detected monotonic clock
-  ** availability, it might happen that it is not actually available at
-  ** runtime. When this occurs simply fallback to other time source.
-  */
-#ifdef HAVE_GETTIMEOFDAY
-  else
-    (void)gettimeofday(&now, NULL);
-#else
-  else {
-    now.tv_sec = time(NULL);
-    now.tv_usec = 0;
-  }
-#endif
-  return now;
-}
-
-#elif defined(HAVE_GETTIMEOFDAY)
-
-struct timeval tvnow(void)
-{
-  /*
-  ** gettimeofday() is not granted to be increased monotonically, due to
-  ** clock drifting and external source time synchronization it can jump
-  ** forward or backward in time.
-  */
-  struct timeval now;
-  (void)gettimeofday(&now, NULL);
-  return now;
-}
-
-#else
-
-struct timeval tvnow(void)
-{
-  /*
-  ** time() returns the value of time in seconds since the Epoch.
-  */
-  struct timeval now;
-  now.tv_sec = time(NULL);
-  now.tv_usec = 0;
-  return now;
-}
-
-#endif
-
-#ifdef _WIN32
-
 struct timeval tvrealnow(void)
 {
   /* UNIX EPOCH (1970-01-01) in FILETIME (1601-01-01) as 64-bit value */
@@ -158,18 +65,6 @@ struct timeval tvrealnow(void)
 
 #endif
 
-/*
- * Make sure that the first argument is the more recent time, as otherwise
- * we will get a weird negative time-diff back...
- *
- * Returns: the time difference in number of milliseconds.
- */
-long tvdiff(struct timeval newer, struct timeval older)
-{
-  return (long)(newer.tv_sec-older.tv_sec)*1000+
-    (long)(newer.tv_usec-older.tv_usec)/1000;
-}
-
 /* Case insensitive compare. Accept NULL pointers. */
 int struplocompare(const char *p1, const char *p2)
 {
index cf37f1a9c1ca4b66ecd9e7542de8082b3becc826..c97c1c03c2058abeba31427ce6a7c866169856ac 100644 (file)
  ***************************************************************************/
 #include "tool_setup.h"
 
-/**
- * Return timeval of the MONOTONIC timer, depending on platform
- * this may be completely unrelated to the REALTIME.
- */
-struct timeval tvnow(void);
-
 /**
  * Return timeval of the REALTIME clock.
  */
 struct timeval tvrealnow(void);
 
-/*
- * Make sure that the first argument (t1) is the more recent time and t2 is
- * the older time, as otherwise you get a weird negative time-diff back...
- *
- * Returns: the time difference in number of milliseconds.
- */
-long tvdiff(struct timeval t1, struct timeval t2);
-
 /* Case insensitive comparison support. */
 int struplocompare(const char *p1, const char *p2);
 int struplocompare4sort(const void *p1, const void *p2);
index 8122e404f5609dcd0f1f75af18681d6cd339d8ac..532984913caf1463769c4d9a3cfaeeae32e81d83 100644 (file)
@@ -31,24 +31,28 @@ CURLX_SRCS = \
   ../../lib/strequal.c \
   ../../lib/warnless.c \
   ../../lib/timediff.c \
+  ../../lib/timeval.c \
   ../../lib/dynbuf.c \
-  ../../lib/strdup.c \
   ../../lib/strcase.c \
+  ../../lib/strdup.c \
   ../../lib/curl_get_line.c \
-  ../../lib/curl_multibyte.c
+  ../../lib/curl_multibyte.c \
+  ../../lib/version_win32.c
 
 CURLX_HDRS = \
   ../../lib/curlx.h \
+  ../../lib/curl_ctype.h \
   ../../lib/nonblock.h \
   ../../lib/strcase.h \
   ../../lib/warnless.h \
   ../../lib/timediff.h \
-  ../../lib/curl_ctype.h \
+  ../../lib/timeval.h \
   ../../lib/dynbuf.h \
   ../../lib/strcase.h \
   ../../lib/strdup.h \
   ../../lib/curl_get_line.h \
-  ../../lib/curl_multibyte.h
+  ../../lib/curl_multibyte.h \
+  ../../lib/version_win32.h
 
 UTIL = \
   getpart.c \
index fcde5d4c184b24b85b9f0467da8dfc07e9d520da..26ecafd4483345f6abf1593250485481b6ea8f47 100644 (file)
@@ -65,8 +65,6 @@ unsigned short server_port = 0;
 const char *socket_type = "IPv4";
 int socket_domain = AF_INET;
 
-static struct timeval tvnow(void);
-
 /* This function returns a pointer to STATIC memory. It converts the given
  * binary lump to a hex formatted string usable for output in logs or
  * whatever.
@@ -99,7 +97,7 @@ void logmsg(const char *msg, ...)
   va_list ap;
   char buffer[2048 + 1];
   FILE *logfp;
-  struct timeval tv;
+  struct curltime tv;
   time_t sec;
   struct tm *now;
   char timebuf[20];
@@ -111,7 +109,7 @@ void logmsg(const char *msg, ...)
     return;
   }
 
-  tv = tvnow();
+  tv = curlx_now();
   if(!known_offset) {
     epoch_offset = time(NULL) - tv.tv_sec;
     known_offset = 1;
@@ -193,26 +191,29 @@ static void win32_cleanup(void)
 
 int win32_init(void)
 {
+  curlx_now_init();
 #ifdef USE_WINSOCK
-  WORD wVersionRequested;
-  WSADATA wsaData;
-  int err;
+  {
+    WORD wVersionRequested;
+    WSADATA wsaData;
+    int err;
 
-  wVersionRequested = MAKEWORD(2, 2);
-  err = WSAStartup(wVersionRequested, &wsaData);
+    wVersionRequested = MAKEWORD(2, 2);
+    err = WSAStartup(wVersionRequested, &wsaData);
 
-  if(err) {
-    win32_perror("Winsock init failed");
-    logmsg("Error initialising Winsock -- aborting");
-    return 1;
-  }
+    if(err) {
+      win32_perror("Winsock init failed");
+      logmsg("Error initialising Winsock -- aborting");
+      return 1;
+    }
 
-  if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
-     HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
-    WSACleanup();
-    win32_perror("Winsock init failed");
-    logmsg("No suitable winsock.dll found -- aborting");
-    return 1;
+    if(LOBYTE(wsaData.wVersion) != LOBYTE(wVersionRequested) ||
+       HIBYTE(wsaData.wVersion) != HIBYTE(wVersionRequested) ) {
+      WSACleanup();
+      win32_perror("Winsock init failed");
+      logmsg("No suitable winsock.dll found -- aborting");
+      return 1;
+    }
   }
 #endif  /* USE_WINSOCK */
   atexit(win32_cleanup);
@@ -384,95 +385,6 @@ void clear_advisor_read_lock(const char *filename)
            filename, error, strerror(error));
 }
 
-
-#ifdef _WIN32
-
-static struct timeval tvnow(void)
-{
-  /*
-  ** GetTickCount() is available on _all_ Windows versions from W95 up
-  ** to nowadays. Returns milliseconds elapsed since last system boot,
-  ** increases monotonically and wraps once 49.7 days have elapsed.
-  **
-  ** GetTickCount64() is available on Windows version from Windows Vista
-  ** and Windows Server 2008 up to nowadays. The resolution of the
-  ** function is limited to the resolution of the system timer, which
-  ** is typically in the range of 10 milliseconds to 16 milliseconds.
-  */
-  struct timeval now;
-#if defined(_WIN32_WINNT) && (_WIN32_WINNT >= 0x0600)
-  ULONGLONG milliseconds = GetTickCount64();
-#else
-  DWORD milliseconds = GetTickCount();
-#endif
-  now.tv_sec = (long)(milliseconds / 1000);
-  now.tv_usec = (long)((milliseconds % 1000) * 1000);
-  return now;
-}
-
-#elif defined(HAVE_CLOCK_GETTIME_MONOTONIC)
-
-static struct timeval tvnow(void)
-{
-  /*
-  ** clock_gettime() is granted to be increased monotonically when the
-  ** monotonic clock is queried. Time starting point is unspecified, it
-  ** could be the system start-up time, the Epoch, or something else,
-  ** in any case the time starting point does not change once that the
-  ** system has started up.
-  */
-  struct timeval now;
-  struct timespec tsnow;
-  if(0 == clock_gettime(CLOCK_MONOTONIC, &tsnow)) {
-    now.tv_sec = tsnow.tv_sec;
-    now.tv_usec = (int)(tsnow.tv_nsec / 1000);
-  }
-  /*
-  ** Even when the configure process has truly detected monotonic clock
-  ** availability, it might happen that it is not actually available at
-  ** run-time. When this occurs simply fallback to other time source.
-  */
-#ifdef HAVE_GETTIMEOFDAY
-  else
-    (void)gettimeofday(&now, NULL);
-#else
-  else {
-    now.tv_sec = time(NULL);
-    now.tv_usec = 0;
-  }
-#endif
-  return now;
-}
-
-#elif defined(HAVE_GETTIMEOFDAY)
-
-static struct timeval tvnow(void)
-{
-  /*
-  ** gettimeofday() is not granted to be increased monotonically, due to
-  ** clock drifting and external source time synchronization it can jump
-  ** forward or backward in time.
-  */
-  struct timeval now;
-  (void)gettimeofday(&now, NULL);
-  return now;
-}
-
-#else
-
-static struct timeval tvnow(void)
-{
-  /*
-  ** time() returns the value of time in seconds since the Epoch.
-  */
-  struct timeval now;
-  now.tv_sec = time(NULL);
-  now.tv_usec = 0;
-  return now;
-}
-
-#endif
-
 /* vars used to keep around previous signal handlers */
 
 typedef void (*SIGHANDLER_T)(int);
index 5c7be488cc8a726cc784c9b89cb51b39773f2143..dd9c13039f558c11a1d6eaec5117f41a065069ef 100644 (file)
@@ -87,7 +87,7 @@ const char *sstrerror(int err);
 /* fopens the test case file */
 FILE *test2fopen(long testno, const char *logdir);
 
-#include "timediff.h"
+#include "timeval.h"
 
 int wait_ms(timediff_t timeout_ms);
 curl_off_t our_getpid(void);
index 63440d6859bd58de96b7132baba313546096774a..db6139efc83da6f56c2f1c2a78f46b56a852911b 100644 (file)
@@ -691,6 +691,7 @@ CURL_FROM_LIBCURL=\
  $(CURL_DIROBJ)\nonblock.obj \
  $(CURL_DIROBJ)\strparse.obj \
  $(CURL_DIROBJ)\strcase.obj \
+ $(CURL_DIROBJ)\timeval.obj \
  $(CURL_DIROBJ)\warnless.obj \
  $(CURL_DIROBJ)\curl_get_line.obj \
  $(CURL_DIROBJ)\curl_multibyte.obj \
@@ -719,6 +720,8 @@ $(CURL_DIROBJ)\strparse.obj: ../lib/strparse.c
        $(CURL_CC) $(CURL_CFLAGS) /Fo"$@" ../lib/strparse.c
 $(CURL_DIROBJ)\strcase.obj: ../lib/strcase.c
        $(CURL_CC) $(CURL_CFLAGS) /Fo"$@" ../lib/strcase.c
+$(CURL_DIROBJ)\timeval.obj: ../lib/timeval.c
+       $(CURL_CC) $(CURL_CFLAGS) /Fo"$@" ../lib/timeval.c
 $(CURL_DIROBJ)\warnless.obj: ../lib/warnless.c
        $(CURL_CC) $(CURL_CFLAGS) /Fo"$@" ../lib/warnless.c
 $(CURL_DIROBJ)\curl_get_line.obj: ../lib/curl_get_line.c