gethrtime and gettimeofday mechanisms added to sp_clock

Signed-off-by: Martin Sustrik <sustrik@250bpm.com>
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 753c1a2..d85beef 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -50,6 +50,21 @@
     add_definitions(-DSP_HAVE_SOCKETPAIR)
 endif()
 
+check_symbol_exists(CLOCK_MONOTONIC time.h SP_HAVE_CLOCK_MONOTONIC)
+if(SP_HAVE_CLOCK_MONOTONIC)
+    add_definitions(-DSP_HAVE_CLOCK_MONOTONIC)
+endif()
+
+check_symbol_exists(CLOCK_MONOTONIC time.h SP_HAVE_CLOCK_MONOTONIC)
+if(SP_HAVE_CLOCK_MONOTONIC)
+    add_definitions(-DSP_HAVE_CLOCK_MONOTONIC)
+endif()
+
+check_symbol_exists(gethrtime time.h SP_HAVE_GETHRTIME)
+if(SP_HAVE_GETHRTIME)
+    add_definitions(-DSP_HAVE_GETHRTIME)
+endif()
+
 check_include_files(sys/epoll.h SP_HAVE_EPOLL)
 if(SP_HAVE_EPOLL)
     add_definitions(-DSP_HAVE_EPOLL)
diff --git a/src/utils/clock.c b/src/utils/clock.c
index d45b03e..fe62787 100644
--- a/src/utils/clock.c
+++ b/src/utils/clock.c
@@ -25,8 +25,10 @@
 #include "win.h"
 #elif defined SP_HAVE_OSX
 #include <mach/mach_time.h>
-#else
+#elif defined SP_HAVE_CLOCK_MONOTONIC || defined SP_HAVE_GETHRTIME
 #include <time.h>
+#else
+#include <sys/time.h>
 #endif
 
 #include "clock.h"
@@ -90,7 +92,7 @@
     return ticks * sp_clock_timebase_info.numer /
         sp_clock_timebase_info.denom / 1000000;
  
-#else
+#elif defined SP_HAVE_CLOCK_MONOTONIC
 
     int rc;
     struct timespec tv;
@@ -99,6 +101,20 @@
     errno_assert (rc == 0);
     return tv.tv_sec * (uint64_t) 1000 + tv.tv_nsec / 1000000;
 
+#elif defined SP_HAVE_GETHRTIME
+
+    return gethrtime () / 1000000;
+
+#else
+
+    int rc;
+    struct timeval tv;
+
+    /*  Gettimeofday is slow on some systems, so it's used as a last resort. */
+    rc = gettimeofday (&tv, NULL);
+    errno_assert (rc == 0);
+    return tv.tv_sec * (uint64_t) 1000 + tv.tv_usec / 1000;
+
 #endif
 }