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 // -----------------------------------------------------------------------
53 //==============================================
54 // ******** ****** ******** **********
55 // **///// /** ** **////// /** /**
56 // ** /** ** /** /** /**
57 // ******* /** ** /********* /**********
58 // **//// /** ** ////////** /**//////**
59 // ** /** ** /** /** /**
60 // ** /** ** ******** /** /**
61 // // /******/ //////// // //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65 #ifndef FOSA_TIME_NUMERIC_H_
66 #define FOSA_TIME_NUMERIC_H_
68 #include "fosa_types.h"
70 /***************************************/
71 /* C O N V E R S I O N M A C R O S */
72 /***************************************/
74 #if FOSA_TIME_BASE == 9 /* nanoseconds */
76 #define fosa_time_to_msec(time) (time) / 1000000
77 #define msec_to_fosa_time(msec) (msec) * 1000000
78 #define fosa_time_to_usec(time) (time) / 1000
79 #define usec_to_fosa_time(usec) (usec) * 1000
81 #define timespec_to_fosa_time(time_type, tspec) ((time_type) (tspec).tv_sec) * 1000000000 + (time_type) (tspec).tv_nsec
83 #define fosa_time_to_timespec(tspec, time) \
85 (tspec).tv_sec = (time) / 1000000000; \
86 (tspec).tv_nsec = (time) % 1000000000; \
93 #if FOSA_TIME_BASE == 6 /* microseconds */
95 #define fosa_time_to_msec(time) (time) / 1000
96 #define msec_to_fosa_time(msec) (msec) * 1000
97 #define fosa_time_to_usec(time) (time)
98 #define usec_to_fosa_time(usec) (usec)
100 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000000 + (time_type) (tspec).tv_nsec/1000
102 #define fosa_time_to_timespec(tspec,time) \
104 (tspec).tv_sec = (time) / 1000000; \
105 (tspec).tv_nsec = ( (time) % 1000000) * 1000; \
111 #if FOSA_TIME_BASE == 3 /* miliseconds */
113 #define fosa_time_to_msec(time) (time)
114 #define msec_to_fosa_time(msec) (msec)
115 #define fosa_time_to_usec(time) (time) * 1000
116 #define usec_to_fosa_time(usec) (usec) / 1000
118 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000 + (time_type) (tspec).tv_nsec/1000000
120 #define fosa_time_to_timespec(tspec, time) \
122 (tspec).tv_sec = (time) / 1000; \
123 (tspec).tv_nsec = ( (time) % 1000) * 1000000; \
131 /* Arithmetic and comparison */
132 /*****************************/
133 static inline fosa_abs_time_t fosa_abs_time_incr(fosa_abs_time_t base, fosa_rel_time_t interval)
135 fosa_abs_time_t result;
137 result = base + interval;
143 // ---------------------------------------------------------
145 static inline fosa_abs_time_t fosa_abs_time_decr(fosa_abs_time_t base, fosa_rel_time_t interval)
147 fosa_abs_time_t result;
149 result = base - interval;
156 // ---------------------------------------------------------
158 static inline fosa_rel_time_t fosa_abs_time_extract_interval(fosa_abs_time_t past, fosa_abs_time_t future)
160 fosa_rel_time_t result;
162 result = future - past;
168 // ---------------------------------------------------------
170 static inline fosa_rel_time_t fosa_rel_time_add(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
172 fosa_rel_time_t result;
174 result = relt1 + relt2;
181 // ---------------------------------------------------------
183 static inline fosa_rel_time_t fosa_rel_time_decr(fosa_rel_time_t total, fosa_rel_time_t part)
185 fosa_rel_time_t result;
187 result = total - part;
195 static inline bool fosa_abs_time_smaller(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
199 result = abst1 < abst2;
204 // -----------------------------------------------------------
206 static inline bool fosa_rel_time_smaller(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
210 result = relt1 < relt2;
216 // -----------------------------------------------------------
218 static inline bool fosa_abs_time_smaller_or_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
222 result = abst1 <= abst2;
228 // -----------------------------------------------------------
230 static inline bool fosa_rel_time_smaller_or_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
234 result = relt1 <= relt2;
242 static inline fosa_rel_time_t fosa_msec_to_rel_time(long msec)
244 fosa_rel_time_t result;
246 result = msec_to_fosa_time( (fosa_rel_time_t) msec );
251 // --------------------------------------------------
253 static inline long fosa_rel_time_to_msec(fosa_rel_time_t relt)
257 result = fosa_time_to_msec(relt);
263 // --------------------------------------------------
266 static inline fosa_rel_time_t fosa_msec_to_abs_time(long msec)
268 fosa_abs_time_t result;
270 result = msec_to_fosa_time( (fosa_abs_time_t) msec);
275 // --------------------------------------------------
277 static inline long fosa_abs_time_to_msec(fosa_abs_time_t abst)
281 result = fosa_time_to_msec(abst);
287 // --------------------------------------------------
289 static inline fosa_rel_time_t fosa_usec_to_rel_time(long usec)
291 fosa_rel_time_t result;
293 result = usec_to_fosa_time((fosa_rel_time_t ) usec);
298 // --------------------------------------------------
300 static inline long fosa_rel_time_to_usec(fosa_rel_time_t relt)
304 result = fosa_time_to_usec(relt);
310 // --------------------------------------------------
312 static inline fosa_abs_time_t fosa_usec_to_abs_time(long usec)
314 fosa_abs_time_t result;
316 result = usec_to_fosa_time( (fosa_abs_time_t) usec);
321 // --------------------------------------------------
323 static inline long fosa_abs_time_to_usec(fosa_abs_time_t abst)
327 result = fosa_time_to_usec(abst);
333 // --------------------------------------------------
335 static inline fosa_rel_time_t fosa_timespec_to_rel_time(struct timespec time_tspec)
337 fosa_rel_time_t result;
339 result = timespec_to_fosa_time(fosa_rel_time_t, time_tspec);
344 // --------------------------------------------------
346 static inline struct timespec fosa_rel_time_to_timespec(fosa_rel_time_t relt)
348 struct timespec result;
350 fosa_time_to_timespec(result, relt);
355 // --------------------------------------------------
357 static inline fosa_abs_time_t fosa_timespec_to_abs_time(struct timespec time_tspec)
359 fosa_abs_time_t result;
361 result = timespec_to_fosa_time(fosa_abs_time_t, time_tspec);
366 // --------------------------------------------------
368 static inline struct timespec fosa_abs_time_to_timespec(fosa_abs_time_t abst)
370 struct timespec result;
372 fosa_time_to_timespec(result, abst);
378 #endif /* !FOSA_TIME_H_ */