Xenomai  3.0-rc7
time.h
1 /*
2  * Copyright (C) 2013 Philippe Gerum <rpm@xenomai.org>.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13 
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18 #ifndef _BOILERPLATE_TIME_H
19 #define _BOILERPLATE_TIME_H
20 
21 #include <time.h>
22 
23 typedef unsigned long long ticks_t;
24 
25 typedef long long sticks_t;
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 void timespec_sub(struct timespec *__restrict r,
32  const struct timespec *__restrict t1,
33  const struct timespec *__restrict t2);
34 
35 void timespec_subs(struct timespec *__restrict r,
36  const struct timespec *__restrict t1,
37  sticks_t t2);
38 
39 void timespec_add(struct timespec *__restrict r,
40  const struct timespec *__restrict t1,
41  const struct timespec *__restrict t2);
42 
43 void timespec_adds(struct timespec *__restrict r,
44  const struct timespec *__restrict t1,
45  sticks_t t2);
46 
47 #ifdef __cplusplus
48 }
49 #endif
50 
51 static inline sticks_t timespec_scalar(const struct timespec *__restrict t)
52 {
53  return t->tv_sec * 1000000000LL + t->tv_nsec;
54 }
55 
56 static inline int __attribute__ ((always_inline))
57 timespec_before(const struct timespec *__restrict t1,
58  const struct timespec *__restrict t2)
59 {
60  if (t1->tv_sec < t2->tv_sec)
61  return 1;
62 
63  if (t1->tv_sec == t2->tv_sec &&
64  t1->tv_nsec < t2->tv_nsec)
65  return 1;
66 
67  return 0;
68 }
69 
70 static inline int __attribute__ ((always_inline))
71 timespec_before_or_same(const struct timespec *__restrict t1,
72  const struct timespec *__restrict t2)
73 {
74  if (t1->tv_sec < t2->tv_sec)
75  return 1;
76 
77  if (t1->tv_sec == t2->tv_sec &&
78  t1->tv_nsec <= t2->tv_nsec)
79  return 1;
80 
81  return 0;
82 }
83 
84 static inline int __attribute__ ((always_inline))
85 timespec_after(const struct timespec *__restrict t1,
86  const struct timespec *__restrict t2)
87 {
88  return !timespec_before_or_same(t1, t2);
89 }
90 
91 static inline int __attribute__ ((always_inline))
92 timespec_after_or_same(const struct timespec *__restrict t1,
93  const struct timespec *__restrict t2)
94 {
95  return !timespec_before(t1, t2);
96 }
97 
98 #endif /* _BOILERPLATE_TIME_H */