]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - include/fosa_time_numeric.h
Time abstraction added to FOSA to replace timespec_operations
[frescor/fosa.git] / include / fosa_time_numeric.h
1 //----------------------------------------------------------------------
2 //  Copyright (C) 2006 - 2007 by the FRESCOR consortium:
3 //
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
10 //    ENEA                                   SWEDEN
11 //    Thales Communication S.A.              FRANCE
12 //    Visual Tools S.A.                      SPAIN
13 //    Rapita Systems Ltd                     UK
14 //    Evidence                               ITALY
15 //
16 //    See http://www.frescor.org
17 //
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
21 //        made of this code.
22 //
23 //
24 //  based on previous work (FSF) done in the FIRST project
25 //
26 //   Copyright (C) 2005  Mälardalen University, SWEDEN
27 //                       Scuola Superiore S.Anna, ITALY
28 //                       Universidad de Cantabria, SPAIN
29 //                       University of York, UK
30 //
31 // This file is part of FOSA (Frsh Operating System Abstraction)
32 //
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.
43 //
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_numeric.h
53 //==============================================
54 //  ********  ******    ********  **********
55 //  **///// /**    **  **//////  /**     /**
56 //  **      /**    ** /**        /**     /**
57 //  ******* /**    ** /********* /**********
58 //  **////  /**    ** ////////** /**//////**
59 //  **      /**    **        /** /**     /**
60 //  **      /**    **  ********  /**     /**
61 //  //       /******/  ////////   //      // 
62 //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65 #ifndef         FOSA_TIME_NUMERIC_H_
66 #define         FOSA_TIME_NUMERIC_H_
67
68 #include "fosa_types.h"
69
70 /***************************************/
71 /* C O N V E R S I O N    M A C R O S  */
72 /***************************************/
73
74 #if FOSA_TIME_BASE == 9  /* nanoseconds */
75
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
80
81 #define timespec_to_fosa_time(time_type, tspec)  ((time_type) (tspec).tv_sec) * 1000000000 + (time_type) (tspec).tv_nsec
82
83 #define fosa_time_to_timespec(tspec, time) \
84 do { \
85     (tspec).tv_sec = (time) / 1000000000; \
86     (tspec).tv_nsec = (time) % 1000000000; \
87 } while(0)
88
89
90
91 #endif
92
93 #if FOSA_TIME_BASE == 6  /* microseconds */
94
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)
99
100 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000000 + (time_type) (tspec).tv_nsec/1000
101
102 #define fosa_time_to_timespec(tspec,time) \
103 do { \
104     (tspec).tv_sec = (time) / 1000000; \
105     (tspec).tv_nsec = ( (time) % 1000000) * 1000; \
106 } while(0)
107
108
109 #endif
110
111 #if FOSA_TIME_BASE == 3  /* miliseconds */
112
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
117
118 #define timespec_to_fosa_time(time_type, tspec) ( (time_type) (tspec).tv_sec) * 1000 + (time_type) (tspec).tv_nsec/1000000
119
120 #define fosa_time_to_timespec(tspec, time) \
121 do { \
122     (tspec).tv_sec = (time) / 1000; \
123     (tspec).tv_nsec = ( (time) % 1000) * 1000000; \
124 } while(0)
125
126
127 #endif
128
129
130
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)
134 {
135     fosa_abs_time_t result;
136     
137     result = base + interval;
138
139     return result;
140 }
141
142
143 // ---------------------------------------------------------
144
145 static inline fosa_abs_time_t fosa_abs_time_decr(fosa_abs_time_t base, fosa_rel_time_t interval)
146 {
147     fosa_abs_time_t result;
148
149     result =  base - interval;
150
151     return result;
152 }
153
154
155
156 // ---------------------------------------------------------
157
158 static inline fosa_rel_time_t fosa_abs_time_extract_interval(fosa_abs_time_t past, fosa_abs_time_t future)
159 {
160     fosa_rel_time_t result;
161
162     result = future - past;
163
164     return result;
165 }
166
167
168 // ---------------------------------------------------------
169
170 static inline fosa_rel_time_t fosa_rel_time_add(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
171 {
172     fosa_rel_time_t result;
173
174     result = relt1 + relt2;
175
176     return result;
177 }
178     
179
180
181 // ---------------------------------------------------------
182
183 static inline fosa_rel_time_t fosa_rel_time_decr(fosa_rel_time_t total, fosa_rel_time_t part)
184 {
185     fosa_rel_time_t result;
186
187     result = total - part;
188
189     return result;
190 }
191
192
193 /* Comparison */
194 /**************/
195 static inline bool fosa_abs_time_smaller(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
196 {
197     bool result;
198
199     result = abst1 < abst2;
200
201     return result;
202 }
203
204 // -----------------------------------------------------------
205
206 static inline bool fosa_rel_time_smaller(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
207 {
208     bool result;
209
210     result = relt1 < relt2;
211
212     return result;
213 }
214
215
216 // -----------------------------------------------------------
217
218 static inline bool fosa_abs_time_smaller_or_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
219 {
220     bool result;
221
222     result = abst1 <= abst2;
223
224     return result;
225 }
226
227
228 // -----------------------------------------------------------
229
230 static inline bool fosa_rel_time_smaller_or_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
231 {
232     bool result;
233
234     result = relt1 <= relt2;
235
236     return result;
237 }
238
239
240 /* Conversion */
241 /**************/
242 static inline fosa_rel_time_t fosa_msec_to_rel_time(long msec)
243 {
244     fosa_rel_time_t result;
245
246     result = msec_to_fosa_time( (fosa_rel_time_t) msec );
247
248     return result;
249 }
250
251 // --------------------------------------------------
252
253 static inline long fosa_rel_time_to_msec(fosa_rel_time_t relt)
254 {
255     long result;
256
257     result = fosa_time_to_msec(relt);
258
259     return result;
260 }
261
262
263 // --------------------------------------------------
264
265
266 static inline fosa_rel_time_t fosa_msec_to_abs_time(long msec)
267 {
268     fosa_abs_time_t result;
269
270     result = msec_to_fosa_time( (fosa_abs_time_t) msec);
271
272     return result;
273 }
274
275 // --------------------------------------------------
276
277 static inline long fosa_abs_time_to_msec(fosa_abs_time_t abst)
278 {
279     long result;
280
281     result = fosa_time_to_msec(abst);
282
283     return result;
284 }
285
286
287 // --------------------------------------------------
288
289 static inline fosa_rel_time_t fosa_usec_to_rel_time(long usec)
290 {
291     fosa_rel_time_t result;
292
293     result = usec_to_fosa_time((fosa_rel_time_t ) usec);
294
295     return result;
296 }
297
298 // --------------------------------------------------
299
300 static inline long fosa_rel_time_to_usec(fosa_rel_time_t relt)
301 {
302     long result;
303
304     result = fosa_time_to_usec(relt);
305
306     return result;
307 }
308
309
310 // --------------------------------------------------
311
312 static inline fosa_abs_time_t fosa_usec_to_abs_time(long usec)
313 {
314     fosa_abs_time_t result;
315
316     result = usec_to_fosa_time( (fosa_abs_time_t) usec);
317
318     return result;
319 }
320
321 // --------------------------------------------------
322
323 static inline long fosa_abs_time_to_usec(fosa_abs_time_t abst)
324 {
325     long result;
326
327     result = fosa_time_to_usec(abst);
328
329     return result;
330 }
331
332
333 // --------------------------------------------------
334
335 static inline fosa_rel_time_t fosa_timespec_to_rel_time(struct timespec time_tspec)
336 {
337     fosa_rel_time_t result;
338
339     result = timespec_to_fosa_time(fosa_rel_time_t, time_tspec);
340
341     return result;
342 }
343
344 // --------------------------------------------------
345
346 static inline struct timespec fosa_rel_time_to_timespec(fosa_rel_time_t relt)
347 {
348     struct timespec result;
349
350     fosa_time_to_timespec(result, relt);
351
352     return result;
353 }
354
355 // --------------------------------------------------
356
357 static inline fosa_abs_time_t fosa_timespec_to_abs_time(struct timespec time_tspec)
358 {
359     fosa_abs_time_t result;
360
361     result = timespec_to_fosa_time(fosa_abs_time_t, time_tspec);
362
363     return result;
364 }
365
366 // --------------------------------------------------
367
368 static inline struct timespec fosa_abs_time_to_timespec(fosa_abs_time_t abst)
369 {
370     struct timespec result;
371
372     fosa_time_to_timespec(result, abst);
373
374     return result;
375 }
376
377
378 #endif      /* !FOSA_TIME_H_ */