]> rtime.felk.cvut.cz Git - frescor/fosa.git/blob - include/fosa_time_timespec.h
4d5f2b5b26e2c636032272e567d8618fba6f273f
[frescor/fosa.git] / include / fosa_time_timespec.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_timespec.h
53 //==============================================
54 //  ********  ******    ********  **********
55 //  **///// /**    **  **//////  /**     /**
56 //  **      /**    ** /**        /**     /**
57 //  ******* /**    ** /********* /**********
58 //  **////  /**    ** ////////** /**//////**
59 //  **      /**    **        /** /**     /**
60 //  **      /**    **  ********  /**     /**
61 //  //       /******/  ////////   //      // 
62 //
63 // FOSA(Frescor Operating System Adaptation layer)
64 //================================================
65 #ifndef         FOSA_TIME_TIMESPEC_H_
66 #define         FOSA_TIME_TIMESPEC_H_
67
68
69 /**********************************/
70 /* T I M E S P E C   M A C R O S  */
71 /**********************************/
72
73 #define add_timespec(sum, t1, t2) \
74 do { \
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) \
78     { \
79         (sum).tv_sec++; \
80         (sum).tv_nsec -= 1000000000; \
81     } \
82 } while(0)
83
84
85 // -------------------------------------------------------------------
86
87 /* TODO:  Test that for past > future we obtain a correct negative
88  * interval */
89 #define substract_timespec(diff, base, interval) \
90 do { \
91     if ((base).tv_nsec < (interval).tv_nsec) \
92     { \
93         (diff).tv_sec = (base).tv_sec - (interval).tv_sec + 1; \
94         (diff).tv_nsec = (base).tv_nsec + 1000000000 - (interval).tv_nsec; \
95     } \
96     else \
97     { \
98         (diff).tv_sec = (base).tv_sec - (interval).tv_sec; \
99         (diff).tv_nsec = (base).tv_nsec - (interval).tv_nsec; \
100     } \
101 } while(0) 
102
103
104 // ---------------------------------------------------------
105
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) )
108
109 // ---------------------------------------------------------
110
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) )
113
114 // ---------------------------------------------------------
115
116 #define msec_to_timespec(tspec, msec) \
117 do { \
118     if ((msec) >= 1000) { \
119         (tspec).tv_sec = (msec)/1000; \
120         (tspec).tv_nsec = ((msec) % 1000) * 1000000; \
121     } else { \
122         (tspec).tv_sec = 0; \
123         (tspec).tv_nsec = (msec) * 1000000; \
124     } \
125 } while (0)
126
127 // ---------------------------------------------------------
128
129 #define timespec_to_msec(t1) \
130    ( ((t1).tv_sec % 2147482) * 1000 + (t1).tv_nsec/1000000 ) 
131
132
133 // ---------------------------------------------------------
134
135 #define usec_to_timespec(tspec, usec) \
136 do { \
137     if ((usec) >= 1000000) { \
138         (tspec).tv_sec = (usec)/1000000; \
139         (tspec).tv_nsec = ((usec) % 1000000) * 1000; \
140     } else { \
141         (tspec).tv_sec = 0; \
142         (tspec).tv_nsec = (usec) * 1000; \
143     } \
144 } while(0)
145
146 // ---------------------------------------------------------
147
148 #define timespec_to_usec(t1) \
149    ( ((t1).tv_sec % 2148) * 1000000 + (t1).tv_nsec/1000 )
150
151
152
153 /***************************************/
154 /* T I M E S P E C   F U N C T I O N S */
155 /***************************************/
156
157
158 static inline fosa_abs_time_t fosa_abs_time_incr(fosa_abs_time_t base, fosa_rel_time_t interval)
159 {
160     fosa_abs_time_t result;
161     
162     add_timespec(result, base, interval);
163
164     return result;
165 }
166
167
168 // ---------------------------------------------------------
169
170 static inline fosa_abs_time_t fosa_abs_time_decr(fosa_abs_time_t base, fosa_rel_time_t interval)
171 {
172     fosa_abs_time_t result;
173
174
175
176     substract_timespec(result, base, interval);
177
178     return result;
179 }
180
181
182
183 // ---------------------------------------------------------
184
185 static inline fosa_rel_time_t fosa_abs_time_extract_interval(fosa_abs_time_t past, fosa_abs_time_t future)
186 {
187     fosa_rel_time_t result;
188
189     substract_timespec(result, future, past);
190
191     return result;
192 }
193
194
195 // ---------------------------------------------------------
196
197 static inline fosa_rel_time_t fosa_rel_time_add(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
198 {
199     fosa_rel_time_t result;
200
201     add_timespec(result, relt1, relt2);
202
203     return result;
204 }
205     
206
207
208 // ---------------------------------------------------------
209
210 static inline fosa_rel_time_t fosa_rel_time_decr(fosa_rel_time_t total, fosa_rel_time_t part)
211 {
212     fosa_rel_time_t result;
213
214     substract_timespec(result, total, part);
215
216     return result;
217 }
218
219
220 /* Comparison */
221 /**************/
222 static inline bool fosa_abs_time_smaller(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
223 {
224     bool result;
225
226     result = smaller_timespec(abst1, abst2);
227
228     return result;
229 }
230
231 // -----------------------------------------------------------
232
233 static inline bool fosa_rel_time_smaller(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
234 {
235     bool result;
236
237     result = smaller_timespec(relt1, relt2);
238
239     return result;
240 }
241
242
243 // -----------------------------------------------------------
244
245 static inline bool fosa_abs_time_smaller_or_equal(fosa_abs_time_t abst1, fosa_abs_time_t abst2)
246 {
247     bool result;
248
249     result = smaller_or_equal_timespec(abst1, abst2);
250
251     return result;
252 }
253
254
255 // -----------------------------------------------------------
256
257 static inline bool fosa_rel_time_smaller_or_equal(fosa_rel_time_t relt1, fosa_rel_time_t relt2)
258 {
259     bool result;
260
261     result = smaller_or_equal_timespec(relt1, relt2);
262
263     return result;
264 }
265
266
267 /* Conversion */
268 /**************/
269 static inline fosa_rel_time_t fosa_msec_to_rel_time(long msec)
270 {
271     fosa_rel_time_t result;
272
273     msec_to_timespec(result, msec);
274
275     return result;
276
277
278 }
279
280 // --------------------------------------------------
281
282 static inline long fosa_rel_time_to_msec(fosa_rel_time_t relt)
283 {
284     long result;
285
286     result = timespec_to_msec(relt);
287
288     return result;
289 }
290
291
292 // --------------------------------------------------
293
294
295 static inline fosa_rel_time_t fosa_msec_to_abs_time(long msec)
296 {
297     fosa_abs_time_t result;
298
299     msec_to_timespec(result, msec);
300
301     return result;
302 }
303
304 // --------------------------------------------------
305
306 static inline long fosa_abs_time_to_msec(fosa_abs_time_t abst)
307 {
308     long result;
309
310     result = timespec_to_msec(abst);
311
312     return result;
313 }
314
315
316 // --------------------------------------------------
317
318 static inline fosa_rel_time_t fosa_usec_to_rel_time(long usec)
319 {
320     fosa_rel_time_t result;
321
322     usec_to_timespec(result, usec);
323
324     return result;
325 }
326
327 // --------------------------------------------------
328
329 static inline long fosa_rel_time_to_usec(fosa_rel_time_t relt)
330 {
331     long result;
332
333     result = timespec_to_usec(relt);
334
335     return result;
336 }
337
338
339 // --------------------------------------------------
340
341 static inline fosa_abs_time_t fosa_usec_to_abs_time(long usec)
342 {
343     fosa_abs_time_t result;
344
345     usec_to_timespec(result, usec);
346
347     return result;
348 }
349
350 // --------------------------------------------------
351
352 static inline long fosa_abs_time_to_usec(fosa_abs_time_t abst)
353 {
354     long result;
355
356     result = timespec_to_usec(abst);
357
358     return result;
359 }
360
361
362 // --------------------------------------------------
363
364 static inline fosa_rel_time_t fosa_timespec_to_rel_time(struct timespec time_tspec)
365 {
366     return (fosa_rel_time_t) time_tspec;
367 }
368
369 // --------------------------------------------------
370
371 static inline struct timespec fosa_rel_time_to_timespec(fosa_rel_time_t relt)
372 {
373     return (struct timespec) relt;
374 }
375
376 // --------------------------------------------------
377
378 static inline fosa_abs_time_t fosa_timespec_to_abs_time(struct timespec time_tspec)
379 {
380     return (fosa_abs_time_t) time_tspec;
381 }
382
383 // --------------------------------------------------
384
385 static inline struct timespec fosa_abs_time_to_timespec(fosa_abs_time_t abst)
386 {
387     return (struct timespec) abst;
388 }
389
390
391 #endif      /* !FOSA_TIME_TIMESPEC_H_ */