]> AND Private Git Repository - loba.git/blobdiff - timer.h
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Rename process::send() -> process::send_all().
[loba.git] / timer.h
diff --git a/timer.h b/timer.h
index dbf32e12c25e4ef82626e16172882d05e26efabb..ccb115bcba15736b7012599aa9f5713e1b5ef15e 100644 (file)
--- a/timer.h
+++ b/timer.h
@@ -4,24 +4,31 @@
 #include <sys/time.h>
 #include <sys/resource.h>
 
-#if NEED_TIMERCLEAR
-inline
-void timerclear(struct timeval& a)
-{
-    tv.sec = tv.usec = 0;
-}
+#ifdef _BSD_SOURCE
+#  define HAVE_TIMERADD
+#  define HAVE_TIMERSUB
+#  define HAVE_TIMERCLEAR
+#else
+#  warning _BSD_SOURCE not defined
+#  undef HAVE_TIMERADD
+#  undef HAVE_TIMERSUB
+#  undef HAVE_TIMERCLEAR
 #endif
 
 inline
 struct timeval operator+(const struct timeval& a, const struct timeval& b)
 {
     struct timeval result;
+#ifdef HAVE_TIMERADD
+    timeradd(&a, &b, &result);
+#else
     result.tv_sec = a.tv_sec + b.tv_sec;
     result.tv_usec = a.tv_usec + b.tv_usec;
     if (result.tv_usec >= 1000000) {
         ++result.tv_sec;
         result.tv_usec -= 1000000;
     }
+#endif
     return result;
 }
 
@@ -29,63 +36,102 @@ inline
 struct timeval operator-(const struct timeval& a, const struct timeval& b)
 {
     struct timeval result;
+#ifdef HAVE_TIMERSUB
+    timersub(&a, &b, &result);
+#else
     result.tv_sec = a.tv_sec - b.tv_sec;
     result.tv_usec = a.tv_usec - b.tv_usec;
     if (result.tv_usec < 0) {
-        -- result.tv_sec;
+        --result.tv_sec;
         result.tv_usec += 1000000;
     }
+#endif
     return result;
 }
 
-double timertod(const struct timeval& a)
-{
-    return a.tv_sec + a.tv_usec / 1e6;
-}
-
 class timestamp {
+public:
+    timestamp();
+    ~timestamp();
+    void reset();
+
+    void start();
+    void stop();
+
+    struct timeval tv_duration() const;
+    double duration() const;
+
 private:
     struct rusage before;
     struct rusage after;
     struct timeval difference;
 
-public:
-    timestamp()
-    {
-        reset();
-    }
+    static void tv_clear(struct timeval& a);
+    static double timertod(const struct timeval& a);
+};
 
-    void reset()
-    {
-        timerclear(&before.ru_utime);
-        timerclear(&before.ru_stime);
-        timerclear(&after.ru_utime);
-        timerclear(&after.ru_stime);
-        timerclear(&difference);
-    }
+inline
+timestamp::timestamp()
+{
+    reset();
+}
 
-    void start()
-    {
-        getrusage(RUSAGE_SELF, &before);
-    }
+inline
+timestamp::~timestamp()
+{
+}
 
-    void stop()
-    {
-        getrusage(RUSAGE_SELF, &after);
-        difference = difference + ((after.ru_utime + after.ru_stime) -
-                                   (before.ru_utime + before.ru_stime));
-    }
+inline
+void timestamp::reset()
+{
+    tv_clear(before.ru_utime);
+    tv_clear(before.ru_stime);
+    tv_clear(after.ru_utime);
+    tv_clear(after.ru_stime);
+    tv_clear(difference);
+}
 
-    struct timeval tv_duration() const
-    {
-        return difference;
-    }
+inline
+void timestamp::start()
+{
+    getrusage(RUSAGE_SELF, &before);
+}
 
-    double duration() const
-    {
-        return timertod(difference);
-    }
-};
+inline
+void timestamp::stop()
+{
+    getrusage(RUSAGE_SELF, &after);
+    difference = difference + ((after.ru_utime + after.ru_stime) -
+                               (before.ru_utime + before.ru_stime));
+}
+
+inline
+struct timeval timestamp::tv_duration() const
+{
+    return difference;
+}
+
+inline
+double timestamp::duration() const
+{
+    return timertod(difference);
+}
+
+inline
+void timestamp::tv_clear(struct timeval& a)
+{
+#ifdef HAVE_TIMERCLEAR
+    timerclear(&a);
+#else
+    tv.sec = tv.usec = 0;
+#endif
+}
+
+inline
+double timestamp::timertod(const struct timeval& a)
+{
+    return a.tv_sec + a.tv_usec / 1e6;
+}
 
 #endif // !TIMER_H