]> rtime.felk.cvut.cz Git - frescor/frsh-include.git/blobdiff - timespec_operations.h
Added the hash table for contracts
[frescor/frsh-include.git] / timespec_operations.h
index 5c225bcc479a78a98b17a4d08edfd22f2f26af74..9d6f2fe76ec64d3499af75402a8ecdfbe9898bd7 100644 (file)
@@ -95,7 +95,8 @@
  *
  *---------------------------------------------------------------------------*/
 // 16-Jul-2007 SANGORRIN corrections to msec_addto_timespec and msec2timespec
-// (TODO: test the rest of functions)
+// 23-Jul-2007 SANGORRIN moved t2d and d2t from frsh_time_operations
+// (TODO: test for __ALL__ the functions)
 // -----------------------------------------------------------------------
 
 #ifndef        _MARTE_MISC_TIMESPEC_OPERATIONS_H_
  * @file timespec_operations.h
  **/
 
-#include <time.h>
+#include <time.h> // for timespec
+#include <string.h> // for memset
 
 #define smaller_timespec(t1, t2) \
  ( \
   (t1)->tv_sec < (t2)->tv_sec || ((t1)->tv_sec == (t2)->tv_sec &&   \
-                                    (t1)->tv_nsec < (t2)->tv_nsec) \
+  (t1)->tv_nsec < (t2)->tv_nsec) \
  )
 
 #define smaller_or_equal_timespec(t1, t2) \
  ( \
   (t1)->tv_sec < (t2)->tv_sec || ((t1)->tv_sec == (t2)->tv_sec &&    \
-                                    (t1)->tv_nsec <= (t2)->tv_nsec) \
+  (t1)->tv_nsec <= (t2)->tv_nsec) \
  )
 
 #define incr_timespec(t1, t2) \
@@ -140,7 +142,6 @@ do { \
   } \
 } while (0)
 
-
 #define  add_timespec( sum , t1 , t2 ) \
 do { \
   (sum)->tv_sec  = (t1)->tv_sec  + (t2)->tv_sec; \
@@ -165,9 +166,11 @@ do { \
   (t1) \
 )
 
-#include <string.h> /* for memset */
+//---------------//
+// msec2timespec //
+//---------------//
 
-static void inline msec2timespec(long msec, struct timespec *timespec)
+static inline void msec2timespec(long msec, struct timespec *timespec)
 {
     memset(timespec, 0, sizeof(struct timespec));
 
@@ -180,27 +183,43 @@ static void inline msec2timespec(long msec, struct timespec *timespec)
     }
 }
 
-/* ------------------------------------------------------------ */
-
-static int inline timespec_lessthan_msec(struct timespec timespec, long msec)
+static inline void timespec2msec(const struct timespec *timespec, long *msec)
 {
+    *msec = 0;
+
+    /* We convert first the seconds checking for a possible overflow */
+    if (timespec->tv_sec < 2147482)
+    {
+        *msec = timespec->tv_sec*1000;
+    }
+    else
+    {
+        *msec = -1;
+        return;
+    }
 
-    long long total_timespec = 0;
-    long long total_msec = 0;
-    int result = 0;
+    /* Now we add the contribution from the msecs with a truncation */
+    *msec += (timespec->tv_nsec/1000000);
 
-    /* We pass to to ns in a long long */
-    total_timespec = timespec.tv_sec*1000000000 + timespec.tv_nsec;
-    total_msec = msec*1000000;
+}
 
-    result = total_timespec < total_msec;
+//------------------------//
+// timespec_lessthan_msec //
+//------------------------//
 
-    return result;
+static inline int timespec_lessthan_msec(struct timespec *timespec, long msec)
+{
+    struct timespec msec_timespec = {0, 0};
+
+    msec2timespec(msec, &msec_timespec);
+    return smaller_timespec(timespec, &msec_timespec);
 }
 
-/* ------------------------------------------------------------ */
+//---------------------//
+// msec_addto_timespec //
+//---------------------//
 
-static void inline msec_addto_timespec(long msec, struct timespec *timespec)
+static inline void msec_addto_timespec(long msec, struct timespec *timespec)
 {
     struct timespec msec_timespec = {0, 0};
 
@@ -208,4 +227,27 @@ static void inline msec_addto_timespec(long msec, struct timespec *timespec)
     incr_timespec(timespec, &msec_timespec);
 }
 
+//--------------------------//
+// t2d (timespec to double) //
+//--------------------------//
+
+static inline double t2d(struct timespec time)
+{
+    return time.tv_nsec*0.000000001 + (double)time.tv_sec;
+}
+
+//--------------------------//
+// d2t (double to timespec) //
+//--------------------------//
+
+static inline struct timespec d2t(double time)
+{
+    struct timespec tmp;
+
+    tmp.tv_sec = (long) time;
+    tmp.tv_nsec = (long)((time - (double)tmp.tv_sec) * 1000000000);
+
+    return tmp;
+}
+
 #endif /* _MARTE_MISC_TIMESPEC_OPERATIONS_H_ */