gnulinux/fosi.h
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jun 10 14:42:55 CEST 2002 fosi.h
3 
4  fosi.h - description
5  -------------------
6  begin : Mon June 10 2002
7  copyright : (C) 2002 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
46 #ifndef __FOSI_H
47 #define __FOSI_H
48 
49 #define HAVE_FOSI_API
50 
51 #ifdef __cplusplus
52 extern "C"
53 {
54 #endif
55 
56 #ifndef _XOPEN_SOURCE
57 #define _XOPEN_SOURCE 600 // use all Posix features.
58 #endif
59 
60 #include <stdio.h>
61 #include <semaphore.h>
62 #include <pthread.h>
63 #include <errno.h>
64 #include <string.h>
65 #include <limits.h>
66 #include <float.h>
67 #include <assert.h>
68 #include "../oro_limits.h"
69 
70  // Time Related
71 #include <sys/time.h>
72 #include <time.h>
73 #include <unistd.h>
74 
75 
76  typedef long long NANO_TIME;
77  typedef long long TICK_TIME;
78  typedef struct timespec TIME_SPEC;
79 
80 
81  static const TICK_TIME InfiniteTicks = LLONG_MAX;
82  static const NANO_TIME InfiniteNSecs = LLONG_MAX;
83  static const double InfiniteSeconds = DBL_MAX;
84 
85 #define ORO_WAIT_ABS 0
87 #define ORO_WAIT_REL 1
90  typedef struct {
91  pthread_t thread;
92  pthread_attr_t attr;
93 
95  NANO_TIME period;
96 
97  char* name;
98 
99  int priority;
101  pid_t pid;
102  } RTOS_TASK;
103 
104 
105 #define ORO_SCHED_RT SCHED_FIFO
106 #define ORO_SCHED_OTHER SCHED_OTHER
109  // high-resolution time to timespec
110  // hrt is in ticks
111  static inline TIME_SPEC ticks2timespec(TICK_TIME hrt)
112  {
113  TIME_SPEC timevl;
114  timevl.tv_sec = hrt / 1000000000LL;
115  timevl.tv_nsec = hrt % 1000000000LL;
116  return timevl;
117  }
118 
119  static inline NANO_TIME rtos_get_time_ns( void )
120  {
121  TIME_SPEC tv;
122  clock_gettime(CLOCK_MONOTONIC, &tv);
123  return ( NANO_TIME ) ( tv.tv_sec * 1000000000LL ) + ( NANO_TIME ) ( tv.tv_nsec );
124  }
125 
126  // internal use only, for rtos_mutex_trylock_for/rtos_mutex_rec_trylock_for!
127  static inline NANO_TIME rtos_get_realtime_ns( void )
128  {
129  TIME_SPEC tv;
131  return ( NANO_TIME ) ( tv.tv_sec * 1000000000LL ) + ( NANO_TIME ) ( tv.tv_nsec );
132  }
133 
138  static inline NANO_TIME rtos_get_time_ticks(void)
139  {
140  return rtos_get_time_ns();
141  }
142 
143  static inline int rtos_nanosleep( const TIME_SPEC * rqtp, TIME_SPEC * rmtp )
144  {
145  // return usleep(rqtp->tv_nsec/1000L);
146  return nanosleep( rqtp, rmtp );
147  }
148 
154  static inline
155  long long nano2ticks( long long nano )
156  {
157  return nano;
158  }
159 
160  static inline
161  long long ticks2nano( long long count )
162  {
163  return count;
164  }
165 
166  typedef sem_t rt_sem_t;
167 
168  static inline int rtos_sem_init(rt_sem_t* m, int value )
169  {
170  return sem_init(m, 0, value);
171  }
172 
173  static inline int rtos_sem_destroy(rt_sem_t* m )
174  {
175  return sem_destroy(m);
176  }
177 
178  static inline int rtos_sem_signal(rt_sem_t* m )
179  {
180  return sem_post(m);
181  }
182 
183  static inline int rtos_sem_wait(rt_sem_t* m )
184  {
185  return sem_wait(m);
186  }
187 
188  static inline int rtos_sem_trywait(rt_sem_t* m )
189  {
190  return sem_trywait(m);
191  }
192 
193  static inline int rtos_sem_value(rt_sem_t* m )
194  {
195  int val = 0;
196  if ( sem_getvalue(m, &val) == 0)
197  return val;
198  return -1;
199  }
200 
201  // Mutex functions
202 
203  typedef pthread_mutex_t rt_mutex_t;
204  typedef pthread_mutex_t rt_rec_mutex_t;
205 
206  static inline int rtos_mutex_init(rt_mutex_t* m)
207  {
208  return pthread_mutex_init(m, 0 );
209  }
210 
211  static inline int rtos_mutex_destroy(rt_mutex_t* m )
212  {
213  return pthread_mutex_destroy(m);
214  }
215 
216  static inline int rtos_mutex_rec_init(rt_rec_mutex_t* m)
217  {
218  pthread_mutexattr_t attr;
219  int ret = pthread_mutexattr_init(&attr);
220  if (ret != 0) return ret;
221 
222  // make mutex recursive
223  ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
224  if (ret != 0) return ret;
225 
226  ret = pthread_mutex_init(m, &attr);
227 
228  pthread_mutexattr_destroy(&attr);
229  return ret;
230  }
231 
232  static inline int rtos_mutex_rec_destroy(rt_rec_mutex_t* m )
233  {
234  return pthread_mutex_destroy(m);
235  }
236 
237  static inline int rtos_mutex_lock( rt_mutex_t* m)
238  {
239  return pthread_mutex_lock(m);
240  }
241 
242  static inline int rtos_mutex_rec_lock( rt_rec_mutex_t* m)
243  {
244  return pthread_mutex_lock(m);
245  }
246 
247  static inline int rtos_mutex_trylock_for( rt_mutex_t* m, NANO_TIME relative_time)
248  {
249  // pthread_mutex_timedlock() does not support relative timeouts, nor CLOCK_MONOTONIC.
250  // Workaround: add the relative time period to an absolute time retrieved
251  // by rtos_get_realtime_ns() using CLOCK_REALTIME (may be affected by time
252  // adjustments while waiting)
253  TIME_SPEC arg_time = ticks2timespec( rtos_get_realtime_ns() + relative_time );
254  return pthread_mutex_timedlock(m, &arg_time);
255  }
256 
257  static inline int rtos_mutex_rec_trylock_for( rt_rec_mutex_t* m, NANO_TIME relative_time)
258  {
259  // pthread_mutex_timedlock() does not support relative timeouts, nor CLOCK_MONOTONIC.
260  // Workaround: add the relative time period to an absolute time retrieved
261  // by rtos_get_realtime_ns() using CLOCK_REALTIME (may be affected by time
262  // adjustments while waiting)
263  TIME_SPEC arg_time = ticks2timespec( rtos_get_realtime_ns() + relative_time );
264  return pthread_mutex_timedlock(m, &arg_time);
265  }
266 
267  static inline int rtos_mutex_lock_until( rt_mutex_t* m, NANO_TIME abs_time)
268  {
269  // pthread_mutex_timedlock() does not accept an absolute CLOCK_MONOTONIC timestamp.
270  // Workaround: convert abs_time to a relative time using rtos_get_time_ns()
271  // and fall back to rtos_mutex_trylock_for().
272  NANO_TIME relative_time = abs_time - rtos_get_time_ns();
273  if (relative_time < 0) return ETIMEDOUT;
274  return rtos_mutex_trylock_for( m, relative_time );
275  }
276 
277  static inline int rtos_mutex_rec_lock_until( rt_rec_mutex_t* m, NANO_TIME abs_time)
278  {
279  // pthread_mutex_timedlock() does not accept an absolute CLOCK_MONOTONIC timestamp.
280  // Workaround: convert abs_time to a relative time using rtos_get_time_ns()
281  // and fall back to rtos_mutex_rec_trylock_for().
282  NANO_TIME relative_time = abs_time - rtos_get_time_ns();
283  if (relative_time < 0) return ETIMEDOUT;
284  return rtos_mutex_rec_trylock_for( m, relative_time );
285  }
286 
287  static inline int rtos_mutex_trylock( rt_mutex_t* m)
288  {
289  return pthread_mutex_trylock(m);
290  }
291 
292  static inline int rtos_mutex_rec_trylock( rt_rec_mutex_t* m)
293  {
294  return pthread_mutex_trylock(m);
295  }
296 
297  static inline int rtos_mutex_unlock( rt_mutex_t* m)
298  {
299  return pthread_mutex_unlock(m);
300  }
301 
302  static inline int rtos_mutex_rec_unlock( rt_rec_mutex_t* m)
303  {
304  return pthread_mutex_unlock(m);
305  }
306 
307  static inline void rtos_enable_rt_warning(void)
308  {
309  }
310 
311  static inline void rtos_disable_rt_warning(void)
312  {
313  }
314 
315  typedef pthread_cond_t rt_cond_t;
316 
317  static inline int rtos_cond_init(rt_cond_t *cond)
318  {
319  pthread_condattr_t attr;
320  int ret = pthread_condattr_init(&attr);
321  if (ret != 0) return ret;
322 
323  // set the clock selection condition variable attribute to CLOCK_MONOTONIC
324  ret = pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
325  if (ret != 0) {
326  pthread_condattr_destroy(&attr);
327  return ret;
328  }
329 
330  ret = pthread_cond_init(cond, &attr);
331  pthread_condattr_destroy(&attr);
332 
333  return ret;
334  }
335 
336  static inline int rtos_cond_destroy(rt_cond_t *cond)
337  {
338  return pthread_cond_destroy(cond);
339  }
340 
341  static inline int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex)
342  {
343  return pthread_cond_wait(cond, mutex);
344  }
345 
346  static inline int rtos_cond_timedwait(rt_cond_t *cond, rt_mutex_t *mutex, NANO_TIME abs_time)
347  {
348  TIME_SPEC arg_time = ticks2timespec( abs_time );
349  return pthread_cond_timedwait(cond, mutex, &arg_time);
350  }
351 
352  static inline int rtos_cond_broadcast(rt_cond_t *cond)
353  {
354  return pthread_cond_broadcast(cond);
355  }
356 
357 #define rtos_printf printf
358 
359 #ifdef __cplusplus
360 }
361 
362 #endif
363 #endif
long long NANO_TIME
Definition: ecos/fosi.h:67
static int rtos_cond_timedwait(rt_cond_t *cond, rt_mutex_t *mutex, NANO_TIME abs_time)
static int rtos_sem_wait(rt_sem_t *m)
static int rtos_sem_destroy(rt_sem_t *m)
static int rtos_mutex_rec_unlock(rt_rec_mutex_t *m)
static int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex)
static int rtos_cond_destroy(rt_cond_t *cond)
#define LLONG_MAX
Definition: oro_limits.h:47
static void rtos_enable_rt_warning(void)
static long long nano2ticks(long long nano)
static int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
static int rtos_mutex_unlock(rt_mutex_t *m)
static const double InfiniteSeconds
Definition: gnulinux/fosi.h:83
pthread_t thread
Definition: gnulinux/fosi.h:91
static int rtos_mutex_rec_lock(rt_rec_mutex_t *m)
static NANO_TIME rtos_get_time_ns(void)
static int rtos_mutex_lock_until(rt_mutex_t *m, NANO_TIME abs_time)
static long long ticks2nano(long long count)
cyg_tick_count_t TICK_TIME
Definition: ecos/fosi.h:68
static int rtos_mutex_rec_trylock_for(rt_rec_mutex_t *m, NANO_TIME relative_time)
long long NANO_TIME
Definition: gnulinux/fosi.h:76
long long TICK_TIME
Definition: gnulinux/fosi.h:77
static int rtos_cond_init(rt_cond_t *cond)
static int rtos_mutex_destroy(rt_mutex_t *m)
static int rtos_mutex_trylock_for(rt_mutex_t *m, NANO_TIME relative_time)
static NANO_TIME rtos_get_realtime_ns(void)
TIME_SPEC periodMark
Definition: gnulinux/fosi.h:94
static int rtos_mutex_rec_lock_until(rt_rec_mutex_t *m, NANO_TIME abs_time)
pthread_cond_t rt_cond_t
#define CLOCK_REALTIME
Definition: macosx/fosi.h:101
static const NANO_TIME InfiniteNSecs
Definition: gnulinux/fosi.h:82
static TIME_SPEC ticks2timespec(TICK_TIME hrt)
static void rtos_disable_rt_warning(void)
static int rtos_mutex_rec_destroy(rt_rec_mutex_t *m)
static int rtos_cond_broadcast(rt_cond_t *cond)
static const TICK_TIME InfiniteTicks
Definition: gnulinux/fosi.h:81
static int clock_gettime(int clk_id, struct timespec *tp)
Definition: macosx/fosi.h:102
static int rtos_sem_value(rt_sem_t *m)
pthread_mutex_t rt_rec_mutex_t
static int rtos_sem_init(rt_sem_t *m, int value)
static int rtos_mutex_init(rt_mutex_t *m)
static int rtos_mutex_trylock(rt_mutex_t *m)
struct MyTask RTOS_TASK
static NANO_TIME rtos_get_time_ticks(void)
struct timespec TIME_SPEC
Definition: ecos/fosi.h:109
static int rtos_mutex_lock(rt_mutex_t *m)
static int rtos_mutex_rec_init(rt_rec_mutex_t *m)
def attr(e, n)
Definition: svn2log.py:70
static int rtos_mutex_rec_trylock(rt_rec_mutex_t *m)
static int rtos_sem_signal(rt_sem_t *m)
pthread_mutex_t rt_mutex_t
cyg_sem_t rt_sem_t
Definition: ecos/fosi.h:143
sem_t rt_sem_t
static int rtos_sem_trywait(rt_sem_t *m)
cyg_mutex_t rt_mutex_t
Definition: ecos/fosi.h:197
pthread_attr_t attr
Definition: gnulinux/fosi.h:92


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:24