lxrt/fosi.h
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jun 10 14:43:39 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 
45 #ifndef __FOSI_H
46 #define __FOSI_H
47 
48 #ifndef _GNU_SOURCE
49 #define _GNU_SOURCE // use all Posix features (and then some).
50 #endif
51 
52 
53 #ifndef _XOPEN_SOURCE
54 #define _XOPEN_SOURCE 600 // use all Posix98 features.
55 #endif
56 
57 #define HAVE_FOSI_API
58 
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <stdarg.h>
62 #include <sys/types.h>
63 #include <sched.h>
64 #include <assert.h>
65 #include <limits.h>
66 #include <float.h>
67 #include "../oro_limits.h"
68 
69 #ifdef __cplusplus
70 extern "C" {
71 #endif
72 
73 #include "../../rtt-config.h"
74 #if !defined(OROBLD_OS_AGNOSTIC) || defined(OROBLD_OS_LXRT_INTERNAL) // define the latter to include nevertheless the RTAI header files
75 
76  // force inlining if internal and internal to avoid an extra indirection.
77  // otherwise, the inlining is selected by the user in his rtai configuration
78 #if defined(OROBLD_OS_LXRT_INTERNAL)
79 #define CONFIG_RTAI_LXRT_INLINE 1
80 #endif
81 
82 // include custom redirect-like include
83 #include <rtai_config.h>
84 #include <rtai_lxrt.h>
85 #include <rtai_sem.h>
86 
87  // Finally, define the types we use :
88  typedef RT_TASK RTOS_RTAI_TASK;
89  typedef SEM RTOS_RTAI_SEM;
90  typedef CND RTOS_RTAI_CND;
91 
92 #else // AGNOSTIC
93 
94  // For PeriodicTask.cxx :
95  // we need to define the types without the headers,
96  // this is RTAI version dependent.
97  // v3.x :
98  typedef struct oro_lxrt_t {
99  int opaque;
100  } __LXRT_HANDLE_STRUCT;
101 
102  typedef __LXRT_HANDLE_STRUCT RTOS_RTAI_TASK;
103  typedef __LXRT_HANDLE_STRUCT RTOS_RTAI_SEM;
104  typedef __LXRT_HANDLE_STRUCT RTOS_RTAI_CND;
105 #endif // OROBLD_OS_AGNOSTIC // for RTAI header files.
106 
107  // this is required because the rtos_sem_init function takes a pointer to RTOS_SEM,
108  // which contains a pointer to the real RTAI sem.
109  typedef struct oro_rtai_sem_t {
110  RTOS_RTAI_SEM* sem;
111  } rt_sem_t;
112 
113  // this is required because the rtos_cond_init function takes a pointer to RTOS_COND,
114  // which contains a pointer to the real RTAI cond
115  typedef struct oro_rtai_cond_t {
116  RTOS_RTAI_CND* cond;
117  } rt_cond_t;
118 
119 #define __LXRT_USERSPACE__
120 
121 
124 
125  // Time Related
126 
127  typedef long long NANO_TIME;
128  typedef long long TICK_TIME;
129  typedef struct timespec TIME_SPEC;
130 
131  typedef struct {
132  pthread_t thread;
133  char * name;
134  int priority;
135 
136  RTOS_RTAI_TASK* rtaitask;
137  } RTOS_TASK;
138 
139  static const TICK_TIME InfiniteTicks = LLONG_MAX;
140  static const NANO_TIME InfiniteNSecs = LLONG_MAX;
141  static const double InfiniteSeconds = DBL_MAX;
142 
143 #define SCHED_LXRT_HARD 0
144 #define SCHED_LXRT_SOFT 1
145 #define ORO_SCHED_RT 0
146 #define ORO_SCHED_OTHER 1
148 #define ORO_WAIT_ABS 0
149 #define ORO_WAIT_REL 1
153 // rtai undef cfr boost::graph library adjacency_list.hpp:443
154 #undef DS
155 #undef OEL
156 #undef VL
157 #undef VP
158 #undef EP
159 #undef GP
160 #undef EL
161 
162 #ifndef OROBLD_OS_AGNOSTIC
163 
164  // hrt is in ticks
165 inline TIME_SPEC ticks2timespec(TICK_TIME hrt)
166 {
167  TIME_SPEC timevl;
168  timevl.tv_sec = nano2count(hrt) / 1000000000LL;
169  timevl.tv_nsec = nano2count(hrt) % 1000000000LL;
170  return timevl;
171 }
172 
173 // turn this on to have maximum detection of valid system calls.
174 #ifdef OROSEM_OS_LXRT_CHECK
175 #define CHK_LXRT_CALL() do { if(rt_buddy() == 0) { \
176  printf("LXRT NOT INITIALISED IN THIS THREAD pid=%d,\n\
177  BUT TRIES TO INVOKE LXRT FUNCTION >>%s<< ANYWAY\n", getpid(), __FUNCTION__ );\
178  assert( rt_buddy() != 0 ); }\
179  } while(0)
180 #define CHK_LXRT_PTR(ptr) do { if(ptr == 0) { \
181  printf("TRIED TO PASS NULL POINTER TO LXRT IN THREAD pid=%d,\n\
182  IN TRYING TO INVOKE LXRT FUNCTION >>%s<<\n", getpid(), __FUNCTION__ );\
183  assert( ptr != 0 ); }\
184  } while(0)
185 #else
186 #define CHK_LXRT_CALL()
187 #define CHK_LXRT_PTR( a )
188 #endif
189 
190 inline NANO_TIME rtos_get_time_ns(void) { return rt_get_time_ns(); }
191 
192 inline TICK_TIME rtos_get_time_ticks(void) { return rt_get_time(); }
193 
194 inline TICK_TIME ticksPerSec(void) { return nano2count( 1000 * 1000 * 1000 ); }
195 
196  inline TICK_TIME nano2ticks(NANO_TIME t) { return nano2count(t); }
197  inline NANO_TIME ticks2nano(TICK_TIME t) { return count2nano(t); }
198 
199 inline int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
200 {
201  CHK_LXRT_CALL();
202  nanosleep(rqtp,rmtp); // rtai 24.1.9
203  return 0;
204 }
205 
206  static inline int rtos_sem_init(rt_sem_t* m, int value )
207  {
208  CHK_LXRT_CALL();
209  // store the pointer in m->opaque...
210  m->sem = rt_sem_init( rt_get_name(0) , value);
211  return m->sem == 0 ? -1 : 0;
212  }
213 
214  static inline int rtos_sem_destroy(rt_sem_t* m )
215  {
216  CHK_LXRT_CALL();
217  return rt_sem_delete( m->sem );
218  }
219 
220  static inline int rtos_sem_signal(rt_sem_t* m )
221  {
222  CHK_LXRT_CALL();
223  return rt_sem_signal( m->sem );
224  }
225 
226  static inline int rtos_sem_wait(rt_sem_t* m )
227  {
228  CHK_LXRT_CALL();
229  return rt_sem_wait( m->sem );
230  }
231 
232  static inline int rtos_sem_trywait(rt_sem_t* m )
233  {
234  CHK_LXRT_CALL();
235  return rt_sem_wait_if(m->sem);
236  }
237 
238  static inline int rtos_sem_value(rt_sem_t* m )
239  {
240  CHK_LXRT_CALL();
241  return rt_sem_count(m->sem);
242  }
243 
244  static inline int rtos_sem_wait_timed(rt_sem_t* m, NANO_TIME delay )
245  {
246  int ret;
247  CHK_LXRT_CALL();
248  ret = rt_sem_wait_timed(m->sem, nano2count(delay) ) ;
249 #if defined(CONFIG_RTAI_VERSION_MINOR) && defined(CONFIG_RTAI_VERSION_MAJOR)
250 # if CONFIG_RTAI_VERSION_MAJOR == 3 && CONFIG_RTAI_VERSION_MINOR > 3
251  return (ret == RTE_TIMOUT) ? -1 : 0;
252 # else
253  return (ret == SEM_TIMOUT) ? -1 : 0;
254 # endif
255 #else
256  return (ret == SEM_TIMOUT) ? -1 : 0;
257 #endif
258  }
259 
260  static inline int rtos_sem_wait_until(rt_sem_t* m, NANO_TIME when )
261  {
262  int ret;
263  CHK_LXRT_CALL();
264  ret = rt_sem_wait_until(m->sem, nano2count(when) ) ;
265 #if defined(CONFIG_RTAI_VERSION_MINOR) && defined(CONFIG_RTAI_VERSION_MAJOR)
266 # if CONFIG_RTAI_VERSION_MAJOR == 3 && CONFIG_RTAI_VERSION_MINOR > 3
267  return (ret == RTE_TIMOUT) ? -1 : 0;
268 # else
269  return (ret == SEM_TIMOUT) ? -1 : 0;
270 # endif
271 #else
272  return (ret == SEM_TIMOUT) ? -1 : 0;
273 #endif
274  }
275 
276  static inline int rtos_mutex_init(rt_mutex_t* m)
277  {
278  CHK_LXRT_CALL();
279  m->sem = rt_typed_sem_init( rt_get_name(0),1, BIN_SEM | PRIO_Q);
280  return m->sem == 0 ? -1 : 0;
281  }
282 
283  static inline int rtos_mutex_destroy(rt_mutex_t* m )
284  {
285  CHK_LXRT_CALL();
286  return rt_sem_delete(m->sem);
287  }
288 
289  static inline int rtos_mutex_rec_init(rt_rec_mutex_t* m)
290  {
291  CHK_LXRT_CALL();
292  // RES_SEM is PRIO_Q anyhow.
293  m->sem = rt_typed_sem_init( rt_get_name(0), 1, RES_SEM);
294  return m->sem == 0 ? -1 : 0;
295  }
296 
297  static inline int rtos_mutex_rec_destroy(rt_rec_mutex_t* m )
298  {
299  CHK_LXRT_CALL();
300  return rt_sem_delete(m->sem);
301  }
302 
303  static inline int rtos_mutex_lock( rt_mutex_t* m)
304  {
305  CHK_LXRT_CALL();
306  return rt_sem_wait(m->sem);
307  }
308 
309  static inline int rtos_mutex_rec_lock( rt_rec_mutex_t* m)
310  {
311  CHK_LXRT_CALL();
312  return rt_sem_wait(m->sem);
313  }
314 
315  static inline int rtos_mutex_trylock( rt_mutex_t* m)
316  {
317  CHK_LXRT_CALL();
318  return rt_sem_wait_if(m->sem) > 0 ? 0 : -EAGAIN;
319  }
320 
321  static inline int rtos_mutex_rec_trylock( rt_rec_mutex_t* m)
322  {
323  CHK_LXRT_CALL();
324  return rt_sem_wait_if(m->sem) > 0 ? 0 : -EAGAIN;
325  }
326 
327  static inline int rtos_mutex_lock_until( rt_mutex_t* m, NANO_TIME abs_time)
328  {
329  CHK_LXRT_CALL();
330  return rt_sem_wait_until(m->sem, nano2count(abs_time)) < SEM_TIMOUT ? 0 : -EAGAIN;
331  }
332 
333  static inline int rtos_mutex_rec_lock_until( rt_rec_mutex_t* m, NANO_TIME abs_time)
334  {
335  CHK_LXRT_CALL();
336  return rt_sem_wait_until(m->sem, nano2count(abs_time)) < SEM_TIMOUT ? 0 : -EAGAIN;
337  }
338 
339 
340  static inline int rtos_mutex_unlock( rt_mutex_t* m)
341  {
342  CHK_LXRT_CALL();
343  return rt_sem_signal(m->sem);
344  }
345 
346  static inline int rtos_mutex_rec_unlock( rt_rec_mutex_t* m)
347  {
348  CHK_LXRT_CALL();
349  return rt_sem_signal(m->sem);
350  }
351 
352  static inline int rtos_cond_init(rt_cond_t *cond)
353  {
354  CHK_LXRT_CALL();
355  cond->cond = rt_cond_init(0);
356  return cond->cond == 0 ? -1 : 0;
357  }
358 
359  static inline int rtos_cond_destroy(rt_cond_t *cond)
360  {
361  CHK_LXRT_CALL();
362  return rt_cond_delete(cond->cond);
363  }
364 
365  static inline int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex)
366  {
367  CHK_LXRT_CALL();
368  int ret = rt_cond_wait(cond->cond, mutex->sem );
369  if (ret == 0)
370  return 0;
371  return -1;
372  }
373 
374  static inline int rtos_cond_timedwait(rt_cond_t *cond, rt_mutex_t *mutex, NANO_TIME abstime)
375  {
376  CHK_LXRT_CALL();
377  int ret = rt_cond_wait_until(cond->cond, mutex->sem, nano2count(abs_time) );
378  if (ret == 0)
379  return 0;
380  if ( ret == SEM_TIMOUT )
381  return ETIMEOUT;
382  return -1;
383  }
384 
385  static inline int rtos_cond_broadcast(rt_cond_t *cond)
386  {
387  CHK_LXRT_CALL();
388  return rt_cond_broadcast(cond->cond);
389  }
390 inline
391 int rtos_printf(const char *fmt, ...)
392 {
393  va_list list;
394  char printkbuf [2000];
395  printkbuf[0] = '\0';
396  va_start (list, fmt);
397  vsprintf(printkbuf, fmt, list);
398  va_end (list);
399  // XXX revert to print to screen when debugging is over
400  return rtai_print_to_screen(printkbuf);
401  //return printf(printkbuf);
402 }
403 
404 #else // OSBLD_OS_AGNOSTIC
405 
410 TIME_SPEC ticks2timespec(TICK_TIME hrt);
411 
412 NANO_TIME rtos_get_time_ns(void);
413 
414 TICK_TIME rtos_get_time_ticks(void);
415 
416 TICK_TIME ticksPerSec(void);
417 
418 TICK_TIME nano2ticks(NANO_TIME t);
419 
420 NANO_TIME ticks2nano(TICK_TIME t);
421 
422 int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp) ;
423 
424 int rtos_mutex_init(rt_mutex_t* m);
425 
426 int rtos_mutex_destroy(rt_mutex_t* m );
427 
428 int rtos_mutex_rec_init(rt_mutex_t* m);
429 
430 int rtos_mutex_rec_destroy(rt_mutex_t* m );
431 
432 int rtos_mutex_lock( rt_mutex_t* m);
433 
434 int rtos_mutex_trylock( rt_mutex_t* m);
435 
436 int rtos_mutex_lock_until( rt_mutex_t* m, NANO_TIME abs_time);
437 
438 int rtos_mutex_rec_lock_until( rt_mutex_t* m, NANO_TIME abs_time);
439 
440 int rtos_mutex_unlock( rt_mutex_t* m);
441 
442 int rtos_mutex_rec_lock( rt_rec_mutex_t* m);
443 
444 int rtos_mutex_rec_trylock( rt_rec_mutex_t* m);
445 
446 int rtos_mutex_rec_unlock( rt_rec_mutex_t* m);
447 
448 int rtos_printf(const char *fmt, ...);
449 
450 int rtos_sem_init(rt_sem_t* m, int value );
451 int rtos_sem_destroy(rt_sem_t* m );
452 int rtos_sem_signal(rt_sem_t* m );
453 int rtos_sem_wait(rt_sem_t* m );
454 int rtos_sem_trywait(rt_sem_t* m );
455 int rtos_sem_value(rt_sem_t* m );
456 int rtos_sem_wait_timed(rt_sem_t* m, NANO_TIME delay );
457 int rtos_sem_wait_until(rt_sem_t* m, NANO_TIME when );
458 
459 int rtos_cond_init(rt_cond_t *cond);
460 int rtos_cond_destroy(rt_cond_t *cond);
461 int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex);
462 int rtos_cond_timedwait(rt_cond_t *cond, rt_mutex_t *mutex, NANO_TIME abs_time);
463 int rtos_cond_broadcast(rt_cond_t *cond);
464 
465 #endif // OSBLD_OS_AGNOSTIC
466 
467 static inline void rtos_enable_rt_warning()
468 {
469 }
470 
471 static inline void rtos_disable_rt_warning()
472 {
473 }
474 
475 #ifdef __cplusplus
476 }
477 #endif
478 
479 #endif
static int rtos_sem_value(rt_sem_t *m)
Definition: lxrt/fosi.h:238
static int rtos_mutex_rec_lock(rt_rec_mutex_t *m)
Definition: lxrt/fosi.h:309
long long NANO_TIME
Definition: lxrt/fosi.h:127
static int rtos_cond_timedwait(rt_cond_t *cond, rt_mutex_t *mutex, NANO_TIME abstime)
Definition: lxrt/fosi.h:374
int rtos_printf(const char *fmt,...)
Definition: lxrt/fosi.h:391
static int rtos_mutex_unlock(rt_mutex_t *m)
Definition: lxrt/fosi.h:340
static int rtos_sem_init(rt_sem_t *m, int value)
Definition: lxrt/fosi.h:206
static int rtos_mutex_trylock(rt_mutex_t *m)
Definition: lxrt/fosi.h:315
int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
Definition: lxrt/fosi.h:199
RTOS_RTAI_CND * cond
Definition: lxrt/fosi.h:116
static int rtos_cond_destroy(rt_cond_t *cond)
Definition: lxrt/fosi.h:359
TICK_TIME rtos_get_time_ticks(void)
Definition: lxrt/fosi.h:192
#define LLONG_MAX
Definition: oro_limits.h:47
static void rtos_enable_rt_warning()
Definition: lxrt/fosi.h:467
static const TICK_TIME InfiniteTicks
Definition: lxrt/fosi.h:139
SEM RTOS_RTAI_SEM
Definition: lxrt/fosi.h:89
static int rtos_mutex_destroy(rt_mutex_t *m)
Definition: lxrt/fosi.h:283
NANO_TIME ticks2nano(TICK_TIME t)
Definition: lxrt/fosi.h:197
static int rtos_sem_wait_until(rt_sem_t *m, NANO_TIME when)
Definition: lxrt/fosi.h:260
struct oro_rtai_cond_t rt_cond_t
TIME_SPEC ticks2timespec(TICK_TIME hrt)
Definition: lxrt/fosi.h:165
TICK_TIME nano2ticks(NANO_TIME t)
Definition: lxrt/fosi.h:196
static const double InfiniteSeconds
Definition: lxrt/fosi.h:141
struct oro_rtai_sem_t rt_sem_t
static int rtos_sem_trywait(rt_sem_t *m)
Definition: lxrt/fosi.h:232
static int rtos_sem_destroy(rt_sem_t *m)
Definition: lxrt/fosi.h:214
static int rtos_mutex_rec_lock_until(rt_rec_mutex_t *m, NANO_TIME abs_time)
Definition: lxrt/fosi.h:333
#define CHK_LXRT_CALL()
Definition: lxrt/fosi.h:186
RT_TASK RTOS_RTAI_TASK
Definition: lxrt/fosi.h:88
long long TICK_TIME
Definition: lxrt/fosi.h:128
TICK_TIME ticksPerSec(void)
Definition: lxrt/fosi.h:194
static int rtos_mutex_rec_unlock(rt_rec_mutex_t *m)
Definition: lxrt/fosi.h:346
RTOS_RTAI_SEM * sem
Definition: lxrt/fosi.h:110
static int rtos_mutex_rec_destroy(rt_rec_mutex_t *m)
Definition: lxrt/fosi.h:297
static int rtos_cond_broadcast(rt_cond_t *cond)
Definition: lxrt/fosi.h:385
rt_sem_t rt_rec_mutex_t
Definition: lxrt/fosi.h:123
NANO_TIME rtos_get_time_ns(void)
Definition: lxrt/fosi.h:190
static int rtos_cond_init(rt_cond_t *cond)
Definition: lxrt/fosi.h:352
static int rtos_sem_signal(rt_sem_t *m)
Definition: lxrt/fosi.h:220
static int rtos_mutex_rec_trylock(rt_rec_mutex_t *m)
Definition: lxrt/fosi.h:321
static int rtos_sem_wait_timed(rt_sem_t *m, NANO_TIME delay)
Definition: lxrt/fosi.h:244
static int rtos_sem_wait(rt_sem_t *m)
Definition: lxrt/fosi.h:226
rt_sem_t rt_mutex_t
Definition: lxrt/fosi.h:122
static int rtos_mutex_rec_init(rt_rec_mutex_t *m)
Definition: lxrt/fosi.h:289
struct MyTask RTOS_TASK
static int rtos_mutex_lock(rt_mutex_t *m)
Definition: lxrt/fosi.h:303
static const NANO_TIME InfiniteNSecs
Definition: lxrt/fosi.h:140
struct timespec TIME_SPEC
Definition: ecos/fosi.h:109
CND RTOS_RTAI_CND
Definition: lxrt/fosi.h:90
static void rtos_disable_rt_warning()
Definition: lxrt/fosi.h:471
static int rtos_mutex_init(rt_mutex_t *m)
Definition: lxrt/fosi.h:276
static int rtos_mutex_lock_until(rt_mutex_t *m, NANO_TIME abs_time)
Definition: lxrt/fosi.h:327
RTOS_RTAI_TASK * rtaitask
Definition: lxrt/fosi.h:136
cyg_sem_t rt_sem_t
Definition: ecos/fosi.h:143
static int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex)
Definition: lxrt/fosi.h:365


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