macosx/fosi.h
Go to the documentation of this file.
1 /***************************************************************************
2  fosi.h - description
3  -------------------
4  begin : Mon June 10 2002
5  copyright : (C) 2006 Peter Soetens
6  copyright : (C) 2008 Klaas Gadeyne
7  email : peter.soetens@fmtc.be
8 
9  ***************************************************************************
10  * This library is free software; you can redistribute it and/or *
11  * modify it under the terms of the GNU General Public *
12  * License as published by the Free Software Foundation; *
13  * version 2 of the License. *
14  * *
15  * As a special exception, you may use this file as part of a free *
16  * software library without restriction. Specifically, if other files *
17  * instantiate templates or use macros or inline functions from this *
18  * file, or you compile this file and link it with other files to *
19  * produce an executable, this file does not by itself cause the *
20  * resulting executable to be covered by the GNU General Public *
21  * License. This exception does not however invalidate any other *
22  * reasons why the executable file might be covered by the GNU General *
23  * Public License. *
24  * *
25  * This library is distributed in the hope that it will be useful, *
26  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
27  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
28  * General Public License for more details. *
29  * *
30  * You should have received a copy of the GNU General Public *
31  * License along with this library; if not, write to the Free Software *
32  * Foundation, Inc., 59 Temple Place, *
33  * Suite 330, Boston, MA 02111-1307 USA *
34  * *
35  ***************************************************************************/
36 
37 #ifndef __FOSI_H
38 #define __FOSI_H
39 
40 #define _DARWIN_C_SOURCE
41 
42 #define HAVE_FOSI_API
43 
44 #ifdef __cplusplus
45 extern "C"
46 {
47 #endif
48 
49 
50 #include <stdio.h>
51 #include <pthread.h>
52 #include <mach/mach_init.h>
53 #include <mach/task.h>
54 
55 #include <errno.h>
56 
57 #include <limits.h>
58 #include <float.h>
59 #include <assert.h>
60 
61  typedef long long NANO_TIME;
62  typedef long long TICK_TIME;
63 
64  static const TICK_TIME InfiniteTicks = LLONG_MAX;
65  static const NANO_TIME InfiniteNSecs = LLONG_MAX;
66  static const double InfiniteSeconds = DBL_MAX;
67 
68 #define ORO_WAIT_ABS 0
70 #define ORO_WAIT_REL 1
73  typedef struct {
74  pthread_t thread;
75  pthread_attr_t attr;
76 
77  NANO_TIME periodMark;
78  NANO_TIME period;
79 
80  char* name;
81 
82  int priority;
83  int wait_policy;
84  } RTOS_TASK;
85 
86 
87 #define ORO_SCHED_RT SCHED_FIFO
88 #define ORO_SCHED_OTHER SCHED_OTHER
90  /*
91  * Time Related stuff
92  */
93 #include <sys/time.h>
94 #include <time.h>
95 #include <unistd.h>
96 
97  typedef struct timespec TIME_SPEC;
98 
99 #ifndef CLOCK_REALTIME
100  /* fake clock_gettime for systems like darwin */
101  #define CLOCK_REALTIME 0
102  static inline int clock_gettime(int clk_id /*ignored*/, struct timespec *tp)
103  {
104  struct timeval now;
105  int rv = gettimeofday(&now, NULL);
106  if (rv != 0){
107  tp->tv_sec = 0;
108  tp->tv_nsec = 0;
109  return rv;
110  }
111  tp->tv_sec = now.tv_sec;
112  tp->tv_nsec = now.tv_usec * 1000;
113  return 0;
114  }
115 #endif
116 
117  // high-resolution time to timespec
118  static inline TIME_SPEC ticks2timespec(TICK_TIME hrt)
119  {
120  TIME_SPEC timevl;
121  timevl.tv_sec = hrt / 1000000000LL;
122  timevl.tv_nsec = hrt % 1000000000LL;
123  return timevl;
124  }
125 
126  static inline NANO_TIME rtos_get_time_ns( void )
127  {
128  TIME_SPEC tv;
130  // we can not include the C++ Time.hpp header !
131 #ifdef __cplusplus
132  return NANO_TIME( tv.tv_sec ) * 1000000000LL + NANO_TIME( tv.tv_nsec );
133 #else
134  return ( NANO_TIME ) ( tv.tv_sec * 1000000000LL ) + ( NANO_TIME ) ( tv.tv_nsec );
135 #endif
136  }
137 
143  {
144  return rtos_get_time_ns();
145  }
146 
147  static inline int rtos_nanosleep( const TIME_SPEC * rqtp, TIME_SPEC * rmtp )
148  {
149  // return usleep(rqtp->tv_nsec/1000L);
150  return nanosleep( rqtp, rmtp );
151  }
152 
153  static inline long long nano2ticks( long long nano )
154  {
155  return nano;
156  }
157 
158  static inline long long ticks2nano( long long count )
159  {
160  return count;
161  }
162 
163  /*
164  * Semaphore functions
165  * See
166  * http://developer.apple.com/documentation/Darwin/Conceptual/KernelProgramming/synchronization/chapter_15_section_2.html
167  */
168 #include <mach/semaphore.h>
169  typedef semaphore_t rt_sem_t;
170 
171  static inline int rtos_sem_init(rt_sem_t* m, int value )
172  {
173  return semaphore_create(mach_task_self(), m, SYNC_POLICY_FIFO, value);
174  }
175 
176  static inline int rtos_sem_destroy(rt_sem_t* m )
177  {
178  return semaphore_destroy(mach_task_self(), *m);
179  }
180 
181  static inline int rtos_sem_signal(rt_sem_t* m )
182  {
183  return semaphore_signal(*m);
184  }
185 
186  static inline int rtos_sem_wait(rt_sem_t* m )
187  {
188  return semaphore_wait(*m);
189  }
190 
191  static inline int rtos_sem_wait_timed(rt_sem_t* m, NANO_TIME delay )
192  {
193  TIME_SPEC delayvl = ticks2timespec(delay);
194  mach_timespec_t mach_delayvl = {
195 #ifdef __cplusplus
196  static_cast<unsigned int>(delayvl.tv_sec),
197  static_cast<clock_res_t> (delayvl.tv_nsec)
198 #else
199  delayvl.tv_sec,
200  delayvl.tv_nsec
201 #endif
202  };
203 
204  return semaphore_timedwait( *m, mach_delayvl);
205  }
206 
207  static inline int rtos_sem_trywait(rt_sem_t* m )
208  {
209  return rtos_sem_wait_timed(m,0);
210  }
211 
212  static inline int rtos_sem_wait_until(rt_sem_t* m, NANO_TIME abs_time )
213  {
214  TIME_SPEC timevl, delayvl;
215  TIME_SPEC arg_time = ticks2timespec( abs_time );
216  clock_gettime(CLOCK_REALTIME, &timevl);
217 
219 
220  // calculate delay from abs_time
221  delayvl.tv_sec = arg_time.tv_sec - timevl.tv_sec;
222  delayvl.tv_nsec = arg_time.tv_nsec - timevl.tv_nsec;
223  // tv_nsec is signed long in 10.6 (see sys/_structs.h)
224  if ( delayvl.tv_nsec >= 1000000000) {
225  ++delayvl.tv_sec;
226  delayvl.tv_nsec -= 1000000000;
227  }
228  if ( delayvl.tv_nsec < 0) {
229  --delayvl.tv_sec;
230  delayvl.tv_nsec += 1000000000;
231  }
232 
233  assert( 0 <= delayvl.tv_sec);
234  assert( 0 <= delayvl.tv_nsec);
235  assert( delayvl.tv_nsec < 1000000000 );
236 
237  mach_timespec_t mach_delayvl = {
238 #ifdef __cplusplus
239  static_cast<unsigned int>(delayvl.tv_sec),
240  static_cast<clock_res_t> (delayvl.tv_nsec)
241 #else
242  delayvl.tv_sec,
243  delayvl.tv_nsec
244 #endif
245  };
246  int rc = semaphore_timedwait( *m, mach_delayvl);
247  // map to return values from gnulinux, and expected by the calling layer
248  return (KERN_OPERATION_TIMED_OUT == rc ? -1 : 0);
249  }
250 
251  // semaphore_value is not supported on darwin
252  /* static inline int rtos_sem_value(rt_sem_t* m ) */
253  /* { */
254  /* int val = 0; */
255  /* if ( sem_getvalue(m, &val) == 0) */
256  /* return val; */
257  /* return -1; */
258  /* } */
259 
260  // Mutex functions - support only those needed by TLSF
261  // opaque type
263  // type created by tlsf.c (must work in C, not C++ code)
265  int rtos_mutex_init(rt_mutex_t* m);
267  int rtos_mutex_lock( rt_mutex_t* m);
268  int rtos_mutex_unlock( rt_mutex_t* m);
269 
270  static inline void rtos_enable_rt_warning()
271  {
272  }
273 
274  static inline void rtos_disable_rt_warning()
275  {
276  }
277 
278 
279 #define rtos_printf printf
280 
281 #ifdef __cplusplus
282 }
283 
284 #endif
285 #endif
int rtos_mutex_init(rt_mutex_t *m)
static int rtos_sem_init(rt_sem_t *m, int value)
Definition: macosx/fosi.h:171
long long NANO_TIME
Definition: ecos/fosi.h:67
long long TICK_TIME
Definition: macosx/fosi.h:62
static NANO_TIME rtos_get_time_ticks()
Definition: macosx/fosi.h:142
static int rtos_sem_wait(rt_sem_t *m)
Definition: macosx/fosi.h:186
static int rtos_sem_wait_timed(rt_sem_t *m, NANO_TIME delay)
Definition: macosx/fosi.h:191
#define LLONG_MAX
Definition: oro_limits.h:47
long long NANO_TIME
Definition: macosx/fosi.h:61
static int rtos_sem_destroy(rt_sem_t *m)
Definition: macosx/fosi.h:176
static int rtos_sem_signal(rt_sem_t *m)
Definition: macosx/fosi.h:181
static NANO_TIME rtos_get_time_ns(void)
Definition: macosx/fosi.h:126
cyg_tick_count_t TICK_TIME
Definition: ecos/fosi.h:68
static int rtos_nanosleep(const TIME_SPEC *rqtp, TIME_SPEC *rmtp)
Definition: macosx/fosi.h:147
static void rtos_disable_rt_warning()
Definition: macosx/fosi.h:274
int rtos_mutex_unlock(rt_mutex_t *m)
static int rtos_sem_wait_until(rt_sem_t *m, NANO_TIME abs_time)
Definition: macosx/fosi.h:212
rt_mutex_impl_t * rt_mutex_t
Definition: macosx/fosi.h:264
static long long nano2ticks(long long nano)
Definition: macosx/fosi.h:153
#define CLOCK_REALTIME
Definition: macosx/fosi.h:101
static int rtos_sem_trywait(rt_sem_t *m)
Definition: macosx/fosi.h:207
int rtos_mutex_lock(rt_mutex_t *m)
static const NANO_TIME InfiniteNSecs
Definition: macosx/fosi.h:65
static int clock_gettime(int clk_id, struct timespec *tp)
Definition: macosx/fosi.h:102
static long long ticks2nano(long long count)
Definition: macosx/fosi.h:158
static void rtos_enable_rt_warning()
Definition: macosx/fosi.h:270
static const double InfiniteSeconds
Definition: macosx/fosi.h:66
int rtos_mutex_destroy(rt_mutex_t *m)
static const TICK_TIME InfiniteTicks
Definition: macosx/fosi.h:64
struct MyTask RTOS_TASK
struct timespec TIME_SPEC
Definition: ecos/fosi.h:109
def attr(e, n)
Definition: svn2log.py:70
cyg_sem_t rt_sem_t
Definition: ecos/fosi.h:143
semaphore_t rt_sem_t
Definition: macosx/fosi.h:169
static TIME_SPEC ticks2timespec(TICK_TIME hrt)
Definition: macosx/fosi.h:118
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