Xenomai  3.0-rc7
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 <cobalt/uapi/time.h>
24 #include <xenomai/posix/syscall.h>
25 
26 #define ONE_BILLION 1000000000
27 
28 static inline void ns2ts(struct timespec *ts, xnticks_t nsecs)
29 {
30  ts->tv_sec = xnclock_divrem_billion(nsecs, &ts->tv_nsec);
31 }
32 
33 static inline xnticks_t ts2ns(const struct timespec *ts)
34 {
35  xnticks_t nsecs = ts->tv_nsec;
36 
37  if (ts->tv_sec)
38  nsecs += (xnticks_t)ts->tv_sec * ONE_BILLION;
39 
40  return nsecs;
41 }
42 
43 static inline xnticks_t tv2ns(const struct timeval *tv)
44 {
45  xnticks_t nsecs = tv->tv_usec * 1000;
46 
47  if (tv->tv_sec)
48  nsecs += (xnticks_t)tv->tv_sec * ONE_BILLION;
49 
50  return nsecs;
51 }
52 
53 static inline void ticks2tv(struct timeval *tv, xnticks_t ticks)
54 {
55  unsigned long nsecs;
56 
57  tv->tv_sec = xnclock_divrem_billion(ticks, &nsecs);
58  tv->tv_usec = nsecs / 1000;
59 }
60 
61 static inline xnticks_t clock_get_ticks(clockid_t clock_id)
62 {
63  return clock_id == CLOCK_REALTIME ?
64  xnclock_read_realtime(&nkclock) :
65  xnclock_read_monotonic(&nkclock);
66 }
67 
68 static inline int clock_flag(int flag, clockid_t clock_id)
69 {
70  switch(flag & TIMER_ABSTIME) {
71  case 0:
72  return XN_RELATIVE;
73 
74  case TIMER_ABSTIME:
75  switch(clock_id) {
76  case CLOCK_MONOTONIC:
77  case CLOCK_MONOTONIC_RAW:
78  return XN_ABSOLUTE;
79 
80  case CLOCK_REALTIME:
81  return XN_REALTIME;
82  }
83  }
84  return -EINVAL;
85 }
86 
87 int __cobalt_clock_getres(clockid_t clock_id,
88  struct timespec *ts);
89 
90 int __cobalt_clock_gettime(clockid_t clock_id,
91  struct timespec *ts);
92 
93 int __cobalt_clock_settime(clockid_t clock_id,
94  const struct timespec *ts);
95 
96 int __cobalt_clock_nanosleep(clockid_t clock_id, int flags,
97  const struct timespec *rqt,
98  struct timespec *rmt);
99 
100 COBALT_SYSCALL_DECL(clock_getres,
101  (clockid_t clock_id, struct timespec __user *u_ts));
102 
103 COBALT_SYSCALL_DECL(clock_gettime,
104  (clockid_t clock_id, struct timespec __user *u_ts));
105 
106 COBALT_SYSCALL_DECL(clock_settime,
107  (clockid_t clock_id, const struct timespec __user *u_ts));
108 
109 COBALT_SYSCALL_DECL(clock_nanosleep,
110  (clockid_t clock_id, int flags,
111  const struct timespec __user *u_rqt,
112  struct timespec __user *u_rmt));
113 
114 int cobalt_clock_register(struct xnclock *clock,
115  clockid_t *clk_id);
116 
117 void cobalt_clock_deregister(struct xnclock *clock);
118 
119 extern DECLARE_BITMAP(cobalt_clock_extids, COBALT_MAX_EXTCLOCKS);
120 
121 #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:99
int clock_settime(clockid_t clock_id, const struct timespec *tp)
Set the specified clock.
Definition: clock.c:233
int clock_gettime(clockid_t clock_id, struct timespec *tp)
Read the specified clock.
Definition: clock.c:179
int clock_nanosleep(clockid_t clock_id, int flags, const struct timespec *rqtp, struct timespec *rmtp)
Sleep some amount of time.
Definition: clock.c:287