1 //----------------------------------------------------------------------
2 // Copyright (C) 2006 - 2007 by the FRESCOR consortium:
4 // Universidad de Cantabria, SPAIN
5 // University of York, UK
6 // Scuola Superiore Sant'Anna, ITALY
7 // Kaiserslautern University, GERMANY
8 // Univ. Politecnica Valencia, SPAIN
9 // Czech Technical University in Prague, CZECH REPUBLIC
11 // Thales Communication S.A. FRANCE
12 // Visual Tools S.A. SPAIN
13 // Rapita Systems Ltd UK
16 // See http://www.frescor.org
18 // The FRESCOR project (FP6/2005/IST/5-034026) is funded
19 // in part by the European Union Sixth Framework Programme
20 // The European Union is not liable of any use that may be
24 // based on previous work (FSF) done in the FIRST project
26 // Copyright (C) 2005 Mälardalen University, SWEDEN
27 // Scuola Superiore S.Anna, ITALY
28 // Universidad de Cantabria, SPAIN
29 // University of York, UK
31 // This file is part of FOSA (Frsh Operating System Abstraction)
33 // FOSA is free software; you can redistribute it and/or modify it
34 // under terms of the GNU General Public License as published by the
35 // Free Software Foundation; either version 2, or (at your option) any
36 // later version. FOSA is distributed in the hope that it will be
37 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
38 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 // General Public License for more details. You should have received a
40 // copy of the GNU General Public License along with FOSA; see file
41 // COPYING. If not, write to the Free Software Foundation, 675 Mass Ave,
42 // Cambridge, MA 02139, USA.
44 // As a special exception, including FOSA header files in a file,
45 // instantiating FOSA generics or templates, or linking other files
46 // with FOSA objects to produce an executable application, does not
47 // by itself cause the resulting executable application to be covered
48 // by the GNU General Public License. This exception does not
49 // however invalidate any other reasons why the executable file might be
50 // covered by the GNU Public License.
51 // -----------------------------------------------------------------------
52 //fosa_time_timespec.h
53 //==============================================
54 // ******** ****** ******** **********
55 // **///// /** ** **////// /** /**
56 // ** /** ** /** /** /**
57 // ******* /** ** /********* /**********
58 // **//// /** ** ////////** /**//////**
59 // ** /** ** /** /** /**
60 // ** /** ** ******** /** /**
61 // // /******/ //////// // //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65 #ifndef FOSA_TIME_TIMESPEC_H_
66 #define FOSA_TIME_TIMESPEC_H_
69 /**********************************/
70 /* T I M E S P E C M A C R O S */
71 /**********************************/
73 #define add_timespec(sum, t1, t2) \
75 (sum).tv_sec = (t1).tv_sec + (t2).tv_sec; \
76 (sum).tv_nsec = (t1).tv_nsec + (t2).tv_nsec; \
77 if ((sum).tv_nsec >= 1000000000) \
80 (sum).tv_nsec -= 1000000000; \
85 // -------------------------------------------------------------------
87 /* TODO: Test that for past > future we obtain a correct negative
89 #define substract_timespec(diff, base, interval) \
91 if ((base).tv_nsec < (interval).tv_nsec) \
93 (diff).tv_sec = (base).tv_sec - (interval).tv_sec + 1; \
94 (diff).tv_nsec = (base).tv_nsec + 1000000000 - (interval).tv_nsec; \
98 (diff).tv_sec = (base).tv_sec - (interval).tv_sec; \
99 (diff).tv_nsec = (base).tv_nsec - (interval).tv_nsec; \
104 // ---------------------------------------------------------
106 #define smaller_timespec(t1, t2) \
107 ((t1).tv_sec < (t2).tv_sec || ((t1).tv_sec == (t2).tv_sec && (t1).tv_nsec < (t2).tv_nsec) )
109 // ---------------------------------------------------------
111 #define smaller_or_equal_timespec(t1, t2) \
112 ((t1).tv_sec < (t2).tv_sec || ((t1).tv_sec == (t2).tv_sec && (t1).tv_nsec <= (t2).tv_nsec) )
114 // ---------------------------------------------------------
116 #define msec_to_timespec(tspec, msec) \
118 if ((msec) >= 1000) { \
119 (tspec).tv_sec = (msec)/1000; \
120 (tspec).tv_nsec = ((msec) % 1000) * 1000000; \
122 (tspec).tv_sec = 0; \
123 (tspec).tv_nsec = (msec) * 1000000; \
127 // ---------------------------------------------------------
129 #define timespec_to_msec(t1) \
130 ( ((t1).tv_sec % 2147482) * 1000 + (t1).tv_nsec/1000000 )
133 // ---------------------------------------------------------
135 #define usec_to_timespec(tspec, usec) \
137 if ((usec) >= 1000000) { \
138 (tspec).tv_sec = (usec)/1000000; \
139 (tspec).tv_nsec = ((usec) % 1000000) * 1000; \
141 (tspec).tv_sec = 0; \
142 (tspec).tv_nsec = (usec) * 1000; \
146 // ---------------------------------------------------------
148 #define timespec_to_usec(t1) \
149 ( ((t1).tv_sec % 2148) * 1000000 + (t1).tv_nsec/1000 )
153 /***************************************/
154 /* T I M E S P E C F U N C T I O N S */
155 /***************************************/
158 static inline fosa_abs_time_t fosa_abs_time_incr(fosa_abs_time_t base, fosa_rel_time_t interval)
160 fosa_abs_time_t result;
162 add_timespec(result, base, interval);
168 // ---------------------------------------------------------
170 static inline fosa_abs_time_t fosa_abs_time_decr(fosa_abs_time_t base, fosa_rel_time_t interval)
172 fosa_abs_time_t result;
176 substract_timespec(result, base, interval);
183 // ---------------------------------------------------------
185 static inline fosa_rel_time_t fosa_abs_time_extract_interval(fosa_abs_time_t past, fosa_abs_time_t future)
187 fosa_rel_time_t result;
189 substract_timespec(result, future, past);
195 // ---------------------------------------------------------
197 static inline fosa_rel_time_t fosa_rel_time_add(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
199 fosa_rel_time_t result;
201 add_timespec(result, relt1, relt2);
208 // ---------------------------------------------------------
210 static inline fosa_rel_time_t fosa_rel_time_decr(fosa_rel_time_t total, fosa_rel_time_t part)
212 fosa_rel_time_t result;
214 substract_timespec(result, total, part);
222 static inline bool fosa_abs_time_smaller(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
226 result = smaller_timespec(abst1, abst2);
231 // -----------------------------------------------------------
233 static inline bool fosa_rel_time_smaller(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
237 result = smaller_timespec(relt1, relt2);
243 // -----------------------------------------------------------
245 static inline bool fosa_abs_time_smaller_or_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
249 result = smaller_or_equal_timespec(abst1, abst2);
255 // -----------------------------------------------------------
257 static inline bool fosa_rel_time_smaller_or_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
261 result = smaller_or_equal_timespec(relt1, relt2);
269 static inline fosa_rel_time_t fosa_msec_to_rel_time(long msec)
271 fosa_rel_time_t result;
273 msec_to_timespec(result, msec);
280 // --------------------------------------------------
282 static inline long fosa_rel_time_to_msec(fosa_rel_time_t relt)
286 result = timespec_to_msec(relt);
292 // --------------------------------------------------
295 static inline fosa_rel_time_t fosa_msec_to_abs_time(long msec)
297 fosa_abs_time_t result;
299 msec_to_timespec(result, msec);
304 // --------------------------------------------------
306 static inline long fosa_abs_time_to_msec(fosa_abs_time_t abst)
310 result = timespec_to_msec(abst);
316 // --------------------------------------------------
318 static inline fosa_rel_time_t fosa_usec_to_rel_time(long usec)
320 fosa_rel_time_t result;
322 usec_to_timespec(result, usec);
327 // --------------------------------------------------
329 static inline long fosa_rel_time_to_usec(fosa_rel_time_t relt)
333 result = timespec_to_usec(relt);
339 // --------------------------------------------------
341 static inline fosa_abs_time_t fosa_usec_to_abs_time(long usec)
343 fosa_abs_time_t result;
345 usec_to_timespec(result, usec);
350 // --------------------------------------------------
352 static inline long fosa_abs_time_to_usec(fosa_abs_time_t abst)
356 result = timespec_to_usec(abst);
362 // --------------------------------------------------
364 static inline fosa_rel_time_t fosa_timespec_to_rel_time(struct timespec time_tspec)
366 return (fosa_rel_time_t) time_tspec;
369 // --------------------------------------------------
371 static inline struct timespec fosa_rel_time_to_timespec(fosa_rel_time_t relt)
373 return (struct timespec) relt;
376 // --------------------------------------------------
378 static inline fosa_abs_time_t fosa_timespec_to_abs_time(struct timespec time_tspec)
380 return (fosa_abs_time_t) time_tspec;
383 // --------------------------------------------------
385 static inline struct timespec fosa_abs_time_to_timespec(fosa_abs_time_t abst)
387 return (struct timespec) abst;
391 #endif /* !FOSA_TIME_TIMESPEC_H_ */