Ecore: ecore_time : Add a fast high-resolution monotonic clock
authorcaro <caro@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 28 Apr 2011 19:38:41 +0000 (19:38 +0000)
committercaro <caro@7cbeb6ba-43b4-40fd-8cce-4c39aea84d33>
Thu, 28 Apr 2011 19:38:41 +0000 (19:38 +0000)
git-svn-id: svn+ssh://svn.enlightenment.org/var/svn/e/trunk/ecore@59021 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33

src/lib/ecore/ecore_time.c

index 5926ca1..a5b9d3b 100644 (file)
 # include <Evil.h>
 #endif
 
+#if defined(__APPLE__) && defined(__MACH__)
+# include <mach/mach_time.h>
+#endif
+
 #include "Ecore.h"
 #include "ecore_private.h"
 
@@ -19,6 +23,8 @@
 
 #ifdef HAVE_CLOCK_GETTIME
 static clockid_t _ecore_time_clock_id = -1;
+#elif defined(__APPLE__) && defined(__MACH__)
+static double _ecore_time_clock_conversion = 1e-9;
 #endif
 double _ecore_time_loop_time = -1.0;
 
@@ -66,12 +72,12 @@ ecore_time_get(void)
      }
 
    return (double)t.tv_sec + (((double)t.tv_nsec) / 1000000000.0);
-#else
-# ifdef HAVE_EVIL
+#elif HAVE_EVIL
    return evil_time_get();
-# else
+#elif defined(__APPLE__) && defined(__MACH__)
+   return _ecore_time_clock_conversion * (double) mach_absolute_time();
+#else
    return ecore_time_unix_get();
-# endif
 #endif
 }
 
@@ -165,9 +171,22 @@ _ecore_time_init(void)
      }
 #else
 # ifndef HAVE_EVIL
+#  if defined(__APPLE__) && defined(__MACH__)
+   mach_timebase_info_data_t info;
+   kern_return_t err = mach_timebase_info(&info);
+   if (err == 0)
+     {
+        _ecore_time_clock_conversion = 1e-9 * (double) info.numer / (double) info.denom;
+     }
+   else
+     {
+        WRN("Unable to get timebase info. Fallback to nanoseconds.");
+     }
+#  else
 #  warning "Your platform isn't supported yet"
    CRIT("Platform does not support clock_gettime. "
         "Fallback to unix time.");
+#  endif
 # endif
 #endif