lxrt/fosi.c
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jan 10 15:59:15 CET 2005 fosi.c
3 
4  fosi.c - description
5  -------------------
6  begin : Mon January 10 2005
7  copyright : (C) 2005 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  * Lesser 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 
39 #define OROBLD_OS_LXRT_INTERNAL
40 #include "os/fosi.h"
41 
42 #ifdef OROBLD_OS_AGNOSTIC
43 // Encapsulate all RTAI/LXRT specific functions
44 
45 // hrt is in nanoseconds
46 TIME_SPEC ticks2timespec(RTIME hrt)
47 {
48  TIME_SPEC timevl;
49  timevl.tv_sec = nano2count(hrt) / 1000000000LL;
50  timevl.tv_nsec = nano2count(hrt) % 1000000000LL;
51  return timevl;
52 }
53 
54 // turn this on to have maximum detection of valid system calls.
55 #ifdef OROSEM_OS_LXRT_CHECK
56 #define CHK_LXRT_CALL() do { if(rt_buddy() == 0) { \
57  printf("LXRT NOT INITIALISED IN THIS THREAD pid=%d,\n\
58  BUT TRIES TO INVOKE LXRT FUNCTION >>%s<< ANYWAY\n", getpid(), __FUNCTION__ );\
59  assert( rt_buddy() != 0 ); }\
60  } while(0)
61 #define CHK_LXRT_PTR(ptr) do { if(ptr == 0) { \
62  printf("TRIED TO PASS NULL POINTER TO LXRT IN THREAD pid=%d,\n\
63  IN TRYING TO INVOKE LXRT FUNCTION >>%s<<\n", getpid(), __FUNCTION__ );\
64  assert( ptr != 0 ); }\
65  } while(0)
66 #else
67 #define CHK_LXRT_CALL()
68 #define CHK_LXRT_PTR( a )
69 #endif
70 
71 
72 NANO_TIME rtos_get_time_ns(void) { return rt_get_time_ns(); }
73 
74 TICK_TIME rtos_get_time_ticks(void) { return rt_get_time(); }
75 
76 TICK_TIME ticksPerSec(void) { return nano2count( 1000 * 1000 * 1000 ); }
77 
78 TICK_TIME nano2ticks(NANO_TIME t) { return nano2count(t); }
79 
80 NANO_TIME ticks2nano(TICK_TIME t) { return count2nano(t); }
81 
82 
83 int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
84 {
85  CHK_LXRT_CALL();
86  nanosleep(rqtp,rmtp); // rtai 24.1.9
87  return 0;
88 }
89 
90  int rtos_sem_init(rt_sem_t* m, int value )
91  {
92  CHK_LXRT_CALL();
93  // store the pointer in m->opaque...
94  m->sem = rt_sem_init( rt_get_name(0) , value);
95  return m->sem == 0 ? -1 : 0;
96  }
97 
98  int rtos_sem_destroy(rt_sem_t* m )
99  {
100  CHK_LXRT_CALL();
101  return rt_sem_delete(m->sem);
102  }
103 
104  int rtos_sem_signal(rt_sem_t* m )
105  {
106  CHK_LXRT_CALL();
107  return rt_sem_signal(m->sem);
108  }
109 
110  int rtos_sem_wait(rt_sem_t* m )
111  {
112  CHK_LXRT_CALL();
113  return rt_sem_wait(m->sem);
114  }
115 
116  int rtos_sem_trywait(rt_sem_t* m )
117  {
118  CHK_LXRT_CALL();
119  return rt_sem_wait_if(m->sem);
120  }
121 
122  int rtos_sem_value(rt_sem_t* m )
123  {
124  CHK_LXRT_CALL();
125  return rt_sem_count(m->sem);
126  }
127 
128  int rtos_sem_wait_timed(rt_sem_t* m, NANO_TIME delay )
129  {
130  int ret;
131  CHK_LXRT_CALL();
132  ret = rt_sem_wait_timed(m->sem, nano2count(delay) );
133 #if defined(CONFIG_RTAI_VERSION_MINOR) && defined(CONFIG_RTAI_VERSION_MAJOR)
134 # if CONFIG_RTAI_VERSION_MAJOR == 3 && CONFIG_RTAI_VERSION_MINOR > 3
135  return (ret == RTE_TIMOUT) ? -1 : 0;
136 # else
137  return (ret == SEM_TIMOUT) ? -1 : 0;
138 # endif
139 #else
140  return (ret == SEM_TIMOUT) ? -1 : 0;
141 #endif
142  }
143 
144  int rtos_sem_wait_until(rt_sem_t* m, NANO_TIME when )
145  {
146  int ret;
147  CHK_LXRT_CALL();
148  ret = rt_sem_wait_until(m->sem, nano2count(when) ) ;
149 #if defined(CONFIG_RTAI_VERSION_MINOR) && defined(CONFIG_RTAI_VERSION_MAJOR)
150 # if CONFIG_RTAI_VERSION_MAJOR == 3 && CONFIG_RTAI_VERSION_MINOR > 3
151  return (ret == RTE_TIMOUT) ? -1 : 0;
152 # else
153  return (ret == SEM_TIMOUT) ? -1 : 0;
154 # endif
155 #else
156  return (ret == SEM_TIMOUT) ? -1 : 0;
157 #endif
158  }
159 
161  {
162  CHK_LXRT_CALL();
163  m->sem = rt_typed_sem_init( rt_get_name(0),1, BIN_SEM | PRIO_Q);
164  return m->sem == 0 ? -1 : 0;
165  }
166 
168  {
169  CHK_LXRT_CALL();
170  CHK_LXRT_PTR(m);
171  return rt_sem_delete(m->sem);
172  }
173 
175  {
176  CHK_LXRT_CALL();
177  // RES_SEM is PRIO_Q anyhow.
178  m->sem = rt_typed_sem_init( rt_get_name(0), 1, RES_SEM);
179  return m->sem == 0 ? -1 : 0;
180  }
181 
183  {
184  CHK_LXRT_CALL();
185  CHK_LXRT_PTR(m->sem);
186  return rt_sem_delete(m->sem);
187  }
188 
190  {
191  CHK_LXRT_CALL();
192  return rt_sem_wait(m->sem);
193  }
194 
196  {
197  CHK_LXRT_CALL();
198  return rt_sem_wait_if(m->sem) > 0 ? 0 : -EAGAIN;
199  }
200 
201  int rtos_mutex_lock_until( rt_mutex_t* m, NANO_TIME abs_time)
202  {
203  CHK_LXRT_CALL();
204  return rt_sem_wait_until(m->sem, nano2count(abs_time)) < SEM_TIMOUT ? 0 : -EAGAIN;
205  }
206 
208  {
209  CHK_LXRT_CALL();
210  return rt_sem_wait_until(m->sem, nano2count(abs_time)) < SEM_TIMOUT ? 0 : -EAGAIN;
211  }
212 
214  {
215  CHK_LXRT_CALL();
216  return rt_sem_signal(m->sem);
217  }
218 
219  int rtos_mutex_lock( rt_mutex_t* m)
220  {
221  CHK_LXRT_CALL();
222  CHK_LXRT_PTR(m->sem);
223  return rt_sem_wait(m->sem);
224  }
225 
227  {
228  CHK_LXRT_CALL();
229  CHK_LXRT_PTR(m->sem);
230  return rt_sem_wait_if(m->sem) > 0 ? 0 : -EAGAIN;
231  }
232 
234  {
235  CHK_LXRT_CALL();
236  CHK_LXRT_PTR(m->sem);
237  return rt_sem_signal(m->sem);
238  }
239 
240  int rtos_cond_init(rt_cond_t *cond)
241  {
242  CHK_LXRT_CALL();
243  cond->cond = rt_cond_init(0);
244  return cond->cond == 0 ? -1 : 0;
245  }
246 
247  int rtos_cond_destroy(rt_cond_t *cond)
248  {
249  CHK_LXRT_CALL();
250  return rt_cond_delete(cond->cond);
251  }
252 
253  int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex)
254  {
255  CHK_LXRT_CALL();
256  int ret = rt_cond_wait(cond->cond, mutex->sem );
257  if (ret == 0)
258  return 0;
259  return -1;
260  }
261 
262  int rtos_cond_timedwait(rt_cond_t *cond, rt_mutex_t *mutex, NANO_TIME abs_time)
263  {
264  CHK_LXRT_CALL();
265  int ret = rt_cond_wait_until(cond->cond, mutex->sem, nano2count(abs_time) );
266  if (ret == 0)
267  return 0;
268  if ( ret == SEM_TIMOUT )
269  return ETIMEDOUT;
270  return -1;
271  }
272 
273  int rtos_cond_broadcast(rt_cond_t *cond)
274  {
275  CHK_LXRT_CALL();
276  return rt_cond_broadcast(cond->cond);
277  }
278 
279 int rtos_printf(const char *fmt, ...)
280 {
281  va_list list;
282  char printkbuf [2000];
283  printkbuf[0] = '\0';
284  va_start (list, fmt);
285  vsprintf(printkbuf, fmt, list);
286  va_end (list);
287  // XXX revert to print to screen when debugging is over
288  return rtai_print_to_screen(printkbuf);
289  //return printf(printkbuf);
290 }
291 
292 #endif // OROBLD_OS_AGNOSTIC
293 
long long NANO_TIME
Definition: ecos/fosi.h:67
int rtos_mutex_rec_lock(rt_rec_mutex_t *m)
static int rtos_mutex_destroy(rt_mutex_t *m)
Definition: ecos/fosi.h:207
int rtos_cond_destroy(rt_cond_t *cond)
NANO_TIME rtos_get_time_ns(void)
Definition: ecos/fosi.h:125
static int rtos_mutex_unlock(rt_mutex_t *m)
Definition: ecos/fosi.h:252
int rtos_mutex_rec_init(rt_rec_mutex_t *m)
int rtos_sem_trywait(rt_sem_t *m)
int rtos_mutex_rec_trylock(rt_rec_mutex_t *m)
int rtos_sem_signal(rt_sem_t *m)
static int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
int rtos_cond_timedwait(rt_cond_t *cond, rt_mutex_t *mutex, NANO_TIME abs_time)
#define CHK_LXRT_PTR(a)
Definition: lxrt/fosi.h:187
static int rtos_sem_wait_until(rt_sem_t *m, NANO_TIME when)
Definition: lxrt/fosi.h:260
int rtos_cond_wait(rt_cond_t *cond, rt_mutex_t *mutex)
cyg_tick_count_t TICK_TIME
Definition: ecos/fosi.h:68
int rtos_mutex_rec_unlock(rt_rec_mutex_t *m)
#define CHK_LXRT_CALL()
Definition: lxrt/fosi.h:186
int rtos_sem_destroy(rt_sem_t *m)
int rtos_mutex_lock_until(rt_mutex_t *m, NANO_TIME abs_time)
static TIME_SPEC ticks2timespec(TICK_TIME hrt)
int rtos_mutex_rec_lock_until(rt_rec_mutex_t *m, NANO_TIME abs_time)
TICK_TIME rtos_get_time_ticks(void)
Definition: ecos/fosi.h:130
TICK_TIME ticksPerSec(void)
Definition: lxrt/fosi.h:194
int rtos_mutex_trylock(rt_mutex_t *m)
NANO_TIME ticks2nano(TICK_TIME count)
Definition: ecos/fosi.h:119
int rtos_sem_init(rt_sem_t *m, int value)
TICK_TIME nano2ticks(NANO_TIME nano)
Definition: ecos/fosi.h:112
#define rtos_printf
Definition: ecos/fosi.h:272
int rtos_sem_value(rt_sem_t *m)
static int rtos_sem_wait_timed(rt_sem_t *m, NANO_TIME delay)
Definition: ecos/fosi.h:179
struct timespec TIME_SPEC
Definition: ecos/fosi.h:109
static int rtos_mutex_init(rt_mutex_t *m)
Definition: ecos/fosi.h:201
int rtos_cond_init(rt_cond_t *cond)
int rtos_sem_wait(rt_sem_t *m)
static int rtos_mutex_lock(rt_mutex_t *m)
Definition: ecos/fosi.h:226
int rtos_cond_broadcast(rt_cond_t *cond)
cyg_sem_t rt_sem_t
Definition: ecos/fosi.h:143
int rtos_mutex_rec_destroy(rt_rec_mutex_t *m)
cyg_mutex_t rt_mutex_t
Definition: ecos/fosi.h:197


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:33