[PATCH 2/2] Workaround for systems lacking clock_gettime()

Dave Yeo <[email protected]>
Newsgroups gmane.comp.lib.cairo
Message-ID <[email protected]>
Fall back to gettimeofday() when system is missing clock_gettime().
Also include sys/time.h for systems such as OS/2 where the timeval struct is
located there.

Signed-off-by: Dave Yeo <[email protected]>
---
 build/configure.ac.system    |    2 +-
 src/cairo-surface-observer.c |   49 +++++++++++++++++++++++++++++++++++------
 2 files changed, 42 insertions(+), 9 deletions(-)

diff --git a/build/configure.ac.system b/build/configure.ac.system
index b405740..f3d5483 100644
--- a/build/configure.ac.system
+++ b/build/configure.ac.system
@@ -112,7 +112,7 @@ AC_CHECK_HEADER(fenv.h,
 	[AC_CHECK_FUNCS(feenableexcept fedisableexcept feclearexcept)])
 
 dnl check for misc headers and functions
-AC_CHECK_HEADERS([libgen.h byteswap.h signal.h setjmp.h fenv.h])
+AC_CHECK_HEADERS([libgen.h byteswap.h signal.h setjmp.h fenv.h sys/time.h])
 AC_CHECK_FUNCS([vasnprintf link ctime_r drand48 flockfile funlockfile ffs])
 
 dnl check for win32 headers (this detects mingw as well)
diff --git a/src/cairo-surface-observer.c b/src/cairo-surface-observer.c
index 3c4521f..e0cbaa1 100644
--- a/src/cairo-surface-observer.c
+++ b/src/cairo-surface-observer.c
@@ -48,6 +48,16 @@
 #include "cairo-surface-subsurface-private.h"
 #include "cairo-reference-count-private.h"
 
+#if HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
+#if HAVE_CLOCK_TIME
+#define tspec timespec
+#else
+#define tspec timeval
+#endif
+
 static const cairo_surface_backend_t _cairo_surface_observer_backend;
 
 /* observation/stats */
@@ -623,16 +633,17 @@ add_record (cairo_observation_t *log,
     assert (status == CAIRO_INT_STATUS_SUCCESS);
 }
 
+#if HAVE_CLOCK_GETTIME
 static void
-start_timer (struct timespec *ts)
+start_timer (struct tspec *ts)
 {
     clock_gettime (CLOCK_MONOTONIC, ts);
 }
 
 static double
-stop_timer (const struct timespec *then)
+stop_timer (const struct tspec *then)
 {
-    struct timespec now;
+    struct tspec now;
     double elapsed;
 
     clock_gettime (CLOCK_MONOTONIC, &now);
@@ -642,6 +653,28 @@ stop_timer (const struct timespec *then)
     return elapsed;
 }
 
+#else
+
+static void
+start_timer (struct tspec *ts)
+{
+    gettimeofday (ts, NULL);
+}
+
+static double
+stop_timer (const struct tspec *then)
+{
+    struct tspec now;
+    double elapsed;
+
+    gettimeofday (&now, NULL);
+
+    elapsed = now.tv_usec - then->tv_usec;
+    elapsed += 1e6 * (now.tv_sec - then->tv_sec);
+    return elapsed;
+}
+#endif
+
 static void
 sync (cairo_surface_t *target, int x, int y)
 {
@@ -704,7 +737,7 @@ _cairo_surface_observer_paint (void *abstract_surface,
     cairo_composite_rectangles_t composite;
     cairo_rectangle_int_t extents;
     cairo_int_status_t status;
-    struct timespec ts;
+    struct tspec ts;
     double elapsed;
     int x, y;
 
@@ -790,7 +823,7 @@ _cairo_surface_observer_mask (void *abstract_surface,
     cairo_composite_rectangles_t composite;
     cairo_rectangle_int_t extents;
     cairo_int_status_t status;
-    struct timespec ts;
+    struct tspec ts;
     double elapsed;
     int x, y;
 
@@ -892,7 +925,7 @@ _cairo_surface_observer_fill (void			*abstract_surface,
     cairo_composite_rectangles_t composite;
     cairo_rectangle_int_t extents;
     cairo_int_status_t status;
-    struct timespec ts;
+    struct tspec ts;
     double elapsed;
     int x, y;
 
@@ -1007,7 +1040,7 @@ _cairo_surface_observer_stroke (void				*abstract_surface,
     cairo_composite_rectangles_t composite;
     cairo_rectangle_int_t extents;
     cairo_int_status_t status;
-    struct timespec ts;
+    struct tspec ts;
     double elapsed;
     int x, y;
 
@@ -1125,7 +1158,7 @@ _cairo_surface_observer_glyphs (void			*abstract_surface,
     cairo_rectangle_int_t extents;
     cairo_int_status_t status;
     cairo_glyph_t *dev_glyphs;
-    struct timespec ts;
+    struct tspec ts;
     double elapsed;
     int x, y;
 
-- 
1.7.2.3
--
cairo mailing list
[email protected]
http://lists.cairographics.org/mailman/listinfo/cairo
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.