// Visual Tools S.A. SPAIN
// Rapita Systems Ltd UK
// Evidence ITALY
-//
+//
// See http://www.frescor.org for a link to partners' websites
//
// FRESCOR project (FP6/2005/IST/5-034026) is funded
//
//
// based on previous work (FSF) done in the FIRST project
-//
+//
// Copyright (C) 2005 Mälardalen University, SWEDEN
// Scuola Superiore S.Anna, ITALY
// Universidad de Cantabria, SPAIN
* executable file might be covered by the GNU Public License.
*
*---------------------------------------------------------------------------*/
+// 16-Jul-2007 SANGORRIN corrections to msec_addto_timespec and msec2timespec
+// 23-Jul-2007 SANGORRIN moved t2d and d2t from frsh_time_operations
+// (TODO: test for __ALL__ the functions)
+// -----------------------------------------------------------------------
#ifndef _MARTE_MISC_TIMESPEC_OPERATIONS_H_
#define _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) \
-{ \
+do { \
(t1)->tv_sec += (t2)->tv_sec; \
(t1)->tv_nsec += (t2)->tv_nsec; \
if ((t1)->tv_nsec >= 1000000000) { \
(t1)->tv_sec ++; \
(t1)->tv_nsec -= 1000000000; \
} \
-}
+} while (0)
#define decr_timespec(t1, t2) \
-{ \
+do { \
if ((t1)->tv_nsec < (t2)->tv_nsec) { \
(t1)->tv_sec -= (t2)->tv_sec + 1; \
(t1)->tv_nsec = (t1)->tv_nsec + 1000000000 - (t2)->tv_nsec; \
(t1)->tv_sec -= (t2)->tv_sec; \
(t1)->tv_nsec -= (t2)->tv_nsec; \
} \
-}
-
+} while (0)
#define add_timespec( sum , t1 , t2 ) \
-{ \
+do { \
(sum)->tv_sec = (t1)->tv_sec + (t2)->tv_sec; \
(sum)->tv_nsec = (t1)->tv_nsec + (t2)->tv_nsec; \
if ((sum)->tv_nsec >= 1000000000) { \
(sum)->tv_sec ++; \
(sum)->tv_nsec -= 1000000000; \
} \
-}
+} while (0)
#define float_to_timespec( f1 , t1 ) \
( \
(t1) \
)
+//---------------//
+// msec2timespec //
+//---------------//
+
+static inline void msec2timespec(long msec, struct timespec *timespec)
+{
+ memset(timespec, 0, sizeof(struct timespec));
+
+ if (msec >= 1000) {
+ timespec->tv_sec = msec/1000;
+ timespec->tv_nsec = (msec % 1000) * 1000000;
+ } else {
+ timespec->tv_sec = 0;
+ timespec->tv_nsec = msec * 1000000;
+ }
+}
+
+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;
+ }
+
+ /* Now we add the contribution from the msecs with a truncation */
+ *msec += (timespec->tv_nsec/1000000);
+
+}
+
+//------------------------//
+// timespec_lessthan_msec //
+//------------------------//
+
+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 inline void msec_addto_timespec(long msec, struct timespec *timespec)
+{
+ struct timespec msec_timespec = {0, 0};
+
+ msec2timespec(msec, &msec_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_ */