Xenomai  3.0.5
clock.h
1 /*
2  * Written by Gilles Chanteperdrix <gilles.chanteperdrix@xenomai.org>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program 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
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17  */
18 #ifndef _COBALT_POSIX_CLOCK_H
19 #define _COBALT_POSIX_CLOCK_H
20 
21 #include <linux/types.h>
22 #include <linux/time.h>
23 #include <linux/cpumask.h>
24 #include <cobalt/uapi/time.h>
25 #include <xenomai/posix/syscall.h>
26 
27 #define ONE_BILLION 1000000000
28 
29 struct xnclock;
30 
31 static inline void ns2ts(struct timespec *ts, xnticks_t nsecs)
32 {
33  ts->tv_sec = xnclock_divrem_billion(nsecs, &ts->tv_nsec);
34 }
35 
36 static inline xnticks_t ts2ns(const struct timespec *ts)
37 {
38  xnticks_t nsecs = ts->tv_nsec;
39 
40  if (ts->tv_sec)
41  nsecs += (xnticks_t)ts->tv_sec * ONE_BILLION;
42 
43  return nsecs;
44 }
45 
46 static inline xnticks_t tv2ns(const struct timeval *tv)
47 {
48  xnticks_t nsecs = tv->tv_usec * 1000;
49 
50  if (tv->tv_sec)
51  nsecs += (xnticks_t)tv->tv_sec * ONE_BILLION;
52 
53  return nsecs;
54 }
55 
56 static inline void ticks2tv(struct timeval *tv, xnticks_t ticks)
57 {
58  unsigned long nsecs;
59 
60  tv->tv_sec = xnclock_divrem_billion(ticks, &nsecs);
61  tv->tv_usec = nsecs / 1000;
62 }
63 
64 static inline xnticks_t clock_get_ticks(clockid_t clock_id)
65 {
66  return clock_id == CLOCK_REALTIME ?
67  xnclock_read_realtime(&nkclock) :
68  xnclock_read_monotonic(&nkclock);
69 }
70 
71 static inline int clock_flag(int flag, clockid_t clock_id)
72 {
73  if ((flag & TIMER_ABSTIME) == 0)
74  return XN_RELATIVE;
75 
76  if (clock_id == CLOCK_REALTIME)
77  return XN_REALTIME;
78 
79  return XN_ABSOLUTE;
80 }
81 
82 int __cobalt_clock_getres(clockid_t clock_id,
83  struct timespec *ts);
84 
85 int __cobalt_clock_gettime(clockid_t clock_id,
86  struct timespec *ts);
87 
88 int __cobalt_clock_settime(clockid_t clock_id,
89  const struct timespec *ts);
90 
91 int __cobalt_clock_nanosleep(clockid_t clock_id, int flags,
92  const struct timespec *rqt,
93  struct timespec *rmt);
94 
95 COBALT_SYSCALL_DECL(clock_getres,
96  (clockid_t clock_id, struct timespec __user *u_ts));
97 
98 COBALT_SYSCALL_DECL(clock_gettime,
99  (clockid_t clock_id, struct timespec __user *u_ts));
100 
101 COBALT_SYSCALL_DECL(clock_settime,
102  (clockid_t clock_id, const struct timespec __user *u_ts));
103 
104 COBALT_SYSCALL_DECL(clock_nanosleep,
105  (clockid_t clock_id, int flags,
106  const struct timespec __user *u_rqt,
107  struct timespec __user *u_rmt));
108 
109 int cobalt_clock_register(struct xnclock *clock,
110  const cpumask_t *affinity,
111  clockid_t *clk_id);
112 
113 void cobalt_clock_deregister(struct xnclock *clock);
114 
115 struct xnclock *cobalt_clock_find(clockid_t clock_id);
116 
117 extern DECLARE_BITMAP(cobalt_clock_extids, COBALT_MAX_EXTCLOCKS);
118 
119 #endif /* !_COBALT_POSIX_CLOCK_H */
int clock_getres(clockid_t clock_id, struct timespec *tp)
Get the resolution of the specified clock.
Definition: clock.c:100
int clock_settime(clockid_t clock_id, const struct timespec *tp)
Set the specified clock.
Definition: clock.c:236
int clock_gettime(clockid_t clock_id, struct timespec *tp)
Read the specified clock.
Definition: clock.c:181
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp)
Sleep some amount of time.
Definition: clock.c:291