core: Remove use of gettimeofday()
authorChris Dickens <christopher.a.dickens@gmail.com>
Thu, 12 Jan 2017 19:01:59 +0000 (11:01 -0800)
committerChris Dickens <christopher.a.dickens@gmail.com>
Thu, 12 Jan 2017 23:03:16 +0000 (15:03 -0800)
Prior to this commit, gettimeofday() was being used for timestamps
in debug messages. This function has been marked as deprecated in
the latest POSIX specification and furthermore is not supported on
Windows, thus requiring our own implementation as usbi_gettimeofday().

This commit changes the logging code to obtain timestamps using the
clock_gettime() function provided by the backend. The implementation of
usbi_gettimeofday() for Windows was actually equivalent to that of the
USBI_CLOCK_REALTIME implementation for clock_gettime(), so this
eliminates code duplication. In addition, the USBI_CLOCK_REALTIME
implementation has been updated for Windows to leverage the
timespec_get() function available in VS 2015 and later.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
Xcode/config.h
android/config.h
configure.ac
libusb/core.c
libusb/libusbi.h
libusb/os/wince_usb.c
libusb/os/windows_common.h
libusb/os/windows_nt_common.c
libusb/version_nano.h

index 492d43a..14d1527 100644 (file)
@@ -6,9 +6,6 @@
 /* Message logging */
 #define ENABLE_LOGGING 1
 
-/* Define to 1 if you have the `gettimeofday' function. */
-#define HAVE_GETTIMEOFDAY 1
-
 /* Define to 1 if you have the <poll.h> header file. */
 #define HAVE_POLL_H 1
 
index 78387d3..0dd35e2 100644 (file)
@@ -26,9 +26,6 @@
 /* Define to 1 if you have the <dlfcn.h> header file. */
 #define HAVE_DLFCN_H 1
 
-/* Define to 1 if you have the `gettimeofday' function. */
-#define HAVE_GETTIMEOFDAY 1
-
 /* Define to 1 if you have the <inttypes.h> header file. */
 #define HAVE_INTTYPES_H 1
 
index 7b79517..49811a2 100644 (file)
@@ -127,15 +127,10 @@ linux)
                fi
                AC_SUBST(USE_UDEV)
 
-case $is_backend_android in
-yes)
-       dnl there are gettimeofday function but configure doesn't seem to be able to find it.
-       AC_DEFINE([HAVE_GETTIMEOFDAY], [1], [Define if you have gettimeofday])
-       ;;
-*)
-       THREAD_CFLAGS="-pthread"
-       LIBS="${LIBS} -pthread"
-esac
+       if test "x$is_backend_android" != xyes; then
+               THREAD_CFLAGS="-pthread"
+               LIBS="${LIBS} -pthread"
+       fi
 
        AC_CHECK_HEADERS([poll.h])
        AC_DEFINE([POLL_NFDS_TYPE],[nfds_t],[type of second poll() argument])
@@ -317,7 +312,6 @@ AM_CONDITIONAL([HAVE_SIGACTION], [test "x$have_sigaction" = "xyes"])
 
 # headers not available on all platforms but required on others
 AC_CHECK_HEADERS([sys/time.h])
-AC_CHECK_FUNCS(gettimeofday)
 
 # check for -std=gnu99 compiler support
 saved_cflags="$CFLAGS"
index 99aab7b..05de453 100644 (file)
@@ -76,7 +76,7 @@ static const struct libusb_version libusb_version_internal =
          LIBUSB_RC, "http://libusb.info" };
 static int default_context_refcnt = 0;
 static usbi_mutex_static_t default_context_lock = USBI_MUTEX_INITIALIZER;
-static struct timeval timestamp_origin = { 0, 0 };
+static struct timespec timestamp_origin = { 0, 0 };
 
 usbi_mutex_static_t active_contexts_lock = USBI_MUTEX_INITIALIZER;
 struct list_head active_contexts_list;
@@ -2070,7 +2070,7 @@ int API_EXPORTED libusb_init(libusb_context **context)
        usbi_mutex_static_lock(&default_context_lock);
 
        if (!timestamp_origin.tv_sec) {
-               usbi_gettimeofday(&timestamp_origin, NULL);
+               usbi_backend->clock_gettime(USBI_CLOCK_REALTIME, &timestamp_origin);
        }
 
        if (!context && usbi_default_context) {
@@ -2262,58 +2262,6 @@ int API_EXPORTED libusb_has_capability(uint32_t capability)
        return 0;
 }
 
-/* this is defined in libusbi.h if needed */
-#ifdef LIBUSB_GETTIMEOFDAY_WIN32
-/*
- * gettimeofday
- * Implementation according to:
- * The Open Group Base Specifications Issue 6
- * IEEE Std 1003.1, 2004 Edition
- */
-
-/*
- *  THIS SOFTWARE IS NOT COPYRIGHTED
- *
- *  This source code is offered for use in the public domain. You may
- *  use, modify or distribute it freely.
- *
- *  This code is distributed in the hope that it will be useful but
- *  WITHOUT ANY WARRANTY. ALL WARRANTIES, EXPRESS OR IMPLIED ARE HEREBY
- *  DISCLAIMED. This includes but is not limited to warranties of
- *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- *
- *  Contributed by:
- *  Danny Smith <dannysmith@users.sourceforge.net>
- */
-
-/* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */
-#define _W32_FT_OFFSET (116444736000000000)
-
-int usbi_gettimeofday(struct timeval *tp, void *tzp)
-{
-       union {
-               unsigned __int64 ns100; /* Time since 1 Jan 1601, in 100ns units */
-               FILETIME ft;
-       } _now;
-       UNUSED(tzp);
-
-       if(tp) {
-#if defined(OS_WINCE)
-               SYSTEMTIME st;
-               GetSystemTime(&st);
-               SystemTimeToFileTime(&st, &_now.ft);
-#else
-               GetSystemTimeAsFileTime (&_now.ft);
-#endif
-               tp->tv_usec=(long)((_now.ns100 / 10) % 1000000 );
-               tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000);
-       }
-       /* Always return 0 as per Open Group Base Specifications Issue 6.
-          Do not set errno on error.  */
-       return 0;
-}
-#endif
-
 static void usbi_log_str(struct libusb_context *ctx,
        enum libusb_log_level level, const char * str)
 {
@@ -2357,7 +2305,7 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
 {
        const char *prefix = "";
        char buf[USBI_MAX_LOG_LEN];
-       struct timeval now;
+       struct timespec now;
        int global_debug, header_len, text_len;
        static int has_debug_header_been_displayed = 0;
 
@@ -2386,18 +2334,18 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
                return;
 #endif
 
-       usbi_gettimeofday(&now, NULL);
+       usbi_backend->clock_gettime(USBI_CLOCK_REALTIME, &now);
        if ((global_debug) && (!has_debug_header_been_displayed)) {
                has_debug_header_been_displayed = 1;
                usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "[timestamp] [threadID] facility level [function call] <message>" USBI_LOG_LINE_END);
                usbi_log_str(ctx, LIBUSB_LOG_LEVEL_DEBUG, "--------------------------------------------------------------------------------" USBI_LOG_LINE_END);
        }
-       if (now.tv_usec < timestamp_origin.tv_usec) {
+       if (now.tv_nsec < timestamp_origin.tv_nsec) {
                now.tv_sec--;
-               now.tv_usec += 1000000;
+               now.tv_nsec += 1000000000L;
        }
        now.tv_sec -= timestamp_origin.tv_sec;
-       now.tv_usec -= timestamp_origin.tv_usec;
+       now.tv_nsec -= timestamp_origin.tv_nsec;
 
        switch (level) {
        case LIBUSB_LOG_LEVEL_INFO:
@@ -2422,7 +2370,7 @@ void usbi_log_v(struct libusb_context *ctx, enum libusb_log_level level,
        if (global_debug) {
                header_len = snprintf(buf, sizeof(buf),
                        "[%2d.%06d] [%08x] libusb: %s [%s] ",
-                       (int)now.tv_sec, (int)now.tv_usec, usbi_get_tid(), prefix, function);
+                       (int)now.tv_sec, (int)(now.tv_nsec / 1000L), usbi_get_tid(), prefix, function);
        } else {
                header_len = snprintf(buf, sizeof(buf),
                        "libusb: %s [%s] ", prefix, function);
index cc0906c..9fd634f 100644 (file)
@@ -543,14 +543,6 @@ int usbi_clear_event(struct libusb_context *ctx);
 #if (defined(OS_WINDOWS) || defined(OS_WINCE)) && !defined(__GNUC__)
 #define snprintf _snprintf
 #define vsnprintf _vsnprintf
-int usbi_gettimeofday(struct timeval *tp, void *tzp);
-#define LIBUSB_GETTIMEOFDAY_WIN32
-#define HAVE_USBI_GETTIMEOFDAY
-#else
-#ifdef HAVE_GETTIMEOFDAY
-#define usbi_gettimeofday(tv, tz) gettimeofday((tv), (tz))
-#define HAVE_USBI_GETTIMEOFDAY
-#endif
 #endif
 
 struct usbi_pollfd {
index 89b5e65..bb933a7 100644 (file)
@@ -31,7 +31,6 @@
 #include "wince_usb.h"
 
 // Global variables
-const uint64_t epoch_time = UINT64_C(116444736000000000); // 1970.01.01 00:00:000 in MS Filetime
 int windows_version = WINDOWS_CE;
 static uint64_t hires_frequency, hires_ticks_to_ps;
 static HANDLE driver_handle = INVALID_HANDLE_VALUE;
@@ -832,14 +831,14 @@ static int wince_clock_gettime(int clk_id, struct timespec *tp)
                // Fall through and return real-time if monotonic read failed or was not detected @ init
        case USBI_CLOCK_REALTIME:
                // We follow http://msdn.microsoft.com/en-us/library/ms724928%28VS.85%29.aspx
-               // with a predef epoch_time to have an epoch that starts at 1970.01.01 00:00
+               // with a predef epoch time to have an epoch that starts at 1970.01.01 00:00
                // Note however that our resolution is bounded by the Windows system time
                // functions and is at best of the order of 1 ms (or, usually, worse)
                GetSystemTime(&st);
                SystemTimeToFileTime(&st, &filetime);
                rtime.LowPart = filetime.dwLowDateTime;
                rtime.HighPart = filetime.dwHighDateTime;
-               rtime.QuadPart -= epoch_time;
+               rtime.QuadPart -= EPOCH_TIME;
                tp->tv_sec = (long)(rtime.QuadPart / 10000000);
                tp->tv_nsec = (long)((rtime.QuadPart % 10000000)*100);
                return LIBUSB_SUCCESS;
index 17a1923..77ed4a2 100644 (file)
@@ -37,6 +37,8 @@
 #define false FALSE
 #endif
 
+#define EPOCH_TIME     UINT64_C(116444736000000000)    // 1970.01.01 00:00:000 in MS Filetime
+
 #if defined(__CYGWIN__ )
 #define _stricmp strcasecmp
 #define _snprintf snprintf
index 68eb4a7..d28cb53 100644 (file)
@@ -32,9 +32,6 @@
 #include "windows_common.h"
 #include "windows_nt_common.h"
 
-// Global variables
-const uint64_t epoch_time = UINT64_C(116444736000000000); // 1970.01.01 00:00:000 in MS Filetime
-
 // Global variables for clock_gettime mechanism
 static uint64_t hires_ticks_to_ps;
 static uint64_t hires_frequency;
@@ -429,8 +426,10 @@ static unsigned __stdcall windows_clock_gettime_threaded(void *param)
 int windows_clock_gettime(int clk_id, struct timespec *tp)
 {
        struct timer_request request;
+#if !defined(_MSC_VER) || (_MSC_VER < 1900)
        FILETIME filetime;
        ULARGE_INTEGER rtime;
+#endif
        DWORD r;
 
        switch (clk_id) {
@@ -463,16 +462,20 @@ int windows_clock_gettime(int clk_id, struct timespec *tp)
                }
                // Fall through and return real-time if monotonic was not detected @ timer init
        case USBI_CLOCK_REALTIME:
+#if defined(_MSC_VER) && (_MSC_VER >= 1900)
+               timespec_get(tp, TIME_UTC);
+#else
                // We follow http://msdn.microsoft.com/en-us/library/ms724928%28VS.85%29.aspx
-               // with a predef epoch_time to have an epoch that starts at 1970.01.01 00:00
+               // with a predef epoch time to have an epoch that starts at 1970.01.01 00:00
                // Note however that our resolution is bounded by the Windows system time
                // functions and is at best of the order of 1 ms (or, usually, worse)
                GetSystemTimeAsFileTime(&filetime);
                rtime.LowPart = filetime.dwLowDateTime;
                rtime.HighPart = filetime.dwHighDateTime;
-               rtime.QuadPart -= epoch_time;
+               rtime.QuadPart -= EPOCH_TIME;
                tp->tv_sec = (long)(rtime.QuadPart / 10000000);
                tp->tv_nsec = (long)((rtime.QuadPart % 10000000) * 100);
+#endif
                return LIBUSB_SUCCESS;
        default:
                return LIBUSB_ERROR_INVALID_PARAM;
index d525e6d..d270165 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11169
+#define LIBUSB_NANO 11170