fosi.h
Go to the documentation of this file.
00001 /***************************************************************************
00002     copyright            : (C) 2008 Klaas Gadeyne
00003     email                : firstname dot lastname at gmail dot com
00004 
00005 ***************************************************************************
00006 *                                                                         *
00007 *   This program is free software; you can redistribute it and/or modify  *
00008 *   it under the terms of the GNU General Public License as published by  *
00009 *   the Free Software Foundation; either version 2 of the License, or     *
00010 *   (at your option) any later version.                                   *
00011 *                                                                         *
00012 ***************************************************************************/
00013 
00014 
00015 #ifndef __FOSI_H
00016 #define __FOSI_H
00017 
00018 #define _DARWIN_C_SOURCE
00019 
00020 #define HAVE_FOSI_API
00021 
00022 #ifdef __cplusplus
00023 extern "C"
00024 {
00025 #endif
00026 
00027 
00028 #include <stdio.h>
00029 #include <pthread.h>
00030 #include <mach/mach_init.h>
00031 #include <mach/task.h>
00032 
00033 #include <errno.h>
00034 
00035 #include <limits.h>
00036 #include <float.h>
00037 #include <assert.h>
00038 
00039     typedef long long NANO_TIME;
00040     typedef long long TICK_TIME;
00041 
00042     static const TICK_TIME InfiniteTicks = LLONG_MAX;
00043     static const NANO_TIME InfiniteNSecs = LLONG_MAX;
00044     static const double    InfiniteSeconds = DBL_MAX;
00045 
00046 #define ORO_WAIT_ABS 0 
00048 #define ORO_WAIT_REL 1 
00051     typedef struct {
00052         pthread_t thread;
00053         pthread_attr_t attr;
00054 
00055         NANO_TIME periodMark;
00056         NANO_TIME period;
00057 
00058         char* name;
00059 
00060         int priority;
00061         int wait_policy;
00062     } RTOS_TASK;
00063 
00064 
00065 #define ORO_SCHED_RT    SCHED_FIFO 
00066 #define ORO_SCHED_OTHER SCHED_OTHER 
00068     /*
00069      * Time Related stuff
00070      */
00071 #include <sys/time.h>
00072 #include <time.h>
00073 #include <unistd.h>
00074 
00075     typedef struct timespec TIME_SPEC;
00076 
00077     /* fake clock_gettime for systems like darwin */
00078     #define  CLOCK_REALTIME 0
00079     static inline int clock_gettime(int clk_id /*ignored*/, struct timespec *tp)
00080     {
00081         struct timeval now;
00082         int rv = gettimeofday(&now, NULL);
00083         if (rv != 0){
00084             tp->tv_sec = 0;
00085             tp->tv_nsec = 0;
00086             return rv;
00087         }
00088         tp->tv_sec = now.tv_sec;
00089         tp->tv_nsec = now.tv_usec * 1000;
00090         return 0;
00091     }
00092 
00093     // high-resolution time to timespec
00094     static inline TIME_SPEC ticks2timespec(TICK_TIME hrt)
00095     {
00096         TIME_SPEC timevl;
00097         timevl.tv_sec = hrt / 1000000000LL;
00098         timevl.tv_nsec = hrt % 1000000000LL;
00099         return timevl;
00100     }
00101 
00102     static inline NANO_TIME rtos_get_time_ns( void )
00103     {
00104         TIME_SPEC tv;
00105         clock_gettime(CLOCK_REALTIME, &tv);
00106         // we can not include the C++ Time.hpp header !
00107 #ifdef __cplusplus
00108         return NANO_TIME( tv.tv_sec ) * 1000000000LL + NANO_TIME( tv.tv_nsec );
00109 #else
00110         return ( NANO_TIME ) ( tv.tv_sec * 1000000000LL ) + ( NANO_TIME ) ( tv.tv_nsec );
00111 #endif
00112     }
00113 
00118     static inline NANO_TIME rtos_get_time_ticks()
00119     {
00120         return rtos_get_time_ns();
00121     }
00122 
00123     static inline int rtos_nanosleep( const TIME_SPEC * rqtp, TIME_SPEC * rmtp )
00124     {
00125         //    return usleep(rqtp->tv_nsec/1000L);
00126         return nanosleep( rqtp, rmtp );
00127     }
00128 
00129     static inline long long nano2ticks( long long nano )
00130     {
00131         return nano;
00132     }
00133 
00134     static inline long long ticks2nano( long long count )
00135     {
00136         return count;
00137     }
00138 
00139     /*
00140      * Semaphore functions
00141      * See
00142      * http://developer.apple.com/documentation/Darwin/Conceptual/KernelProgramming/synchronization/chapter_15_section_2.html
00143      */
00144 #include <mach/semaphore.h>
00145     typedef semaphore_t rt_sem_t;
00146 
00147     static inline int rtos_sem_init(rt_sem_t* m, int value )
00148     {
00149         return semaphore_create(mach_task_self(), m, SYNC_POLICY_FIFO, value);
00150     }
00151 
00152     static inline int rtos_sem_destroy(rt_sem_t* m )
00153     {
00154         return semaphore_destroy(mach_task_self(), *m);
00155     }
00156 
00157     static inline int rtos_sem_signal(rt_sem_t* m )
00158     {
00159         return semaphore_signal(*m);
00160     }
00161 
00162     static inline int rtos_sem_wait(rt_sem_t* m )
00163     {
00164         return semaphore_wait(*m);
00165     }
00166 
00167     static inline int rtos_sem_wait_timed(rt_sem_t* m, NANO_TIME delay )
00168     {
00169         TIME_SPEC delayvl = ticks2timespec(delay);
00170         mach_timespec_t mach_delayvl = { delayvl.tv_sec, delayvl.tv_nsec };
00171 
00172         return semaphore_timedwait( *m, mach_delayvl);
00173     }
00174 
00175     static inline int rtos_sem_trywait(rt_sem_t* m )
00176     {
00177         return rtos_sem_wait_timed(m,0);
00178     }
00179 
00180     static inline int rtos_sem_wait_until(rt_sem_t* m, NANO_TIME abs_time )
00181     {
00182         TIME_SPEC timevl, delayvl;
00183         TIME_SPEC arg_time = ticks2timespec( abs_time );
00184         clock_gettime(CLOCK_REALTIME, &timevl);
00185 
00187 
00188         // calculate delay from abs_time
00189         delayvl.tv_sec = arg_time.tv_sec - timevl.tv_sec;
00190         delayvl.tv_nsec = arg_time.tv_nsec - timevl.tv_nsec;
00191                 // tv_nsec is signed long in 10.6 (see sys/_structs.h)
00192         if ( delayvl.tv_nsec >= 1000000000) {
00193             ++delayvl.tv_sec;
00194             delayvl.tv_nsec -= 1000000000;
00195         }
00196         if ( delayvl.tv_nsec < 0) {
00197             --delayvl.tv_sec;
00198             delayvl.tv_nsec += 1000000000;
00199         }
00200 
00201         assert( 0 <= delayvl.tv_sec);
00202         assert( 0 <= delayvl.tv_nsec);
00203         assert( delayvl.tv_nsec < 1000000000 );
00204 
00205         mach_timespec_t mach_delayvl = { delayvl.tv_sec, delayvl.tv_nsec };
00206         int rc = semaphore_timedwait( *m, mach_delayvl);
00207         // map to return values from gnulinux, and expected by the calling layer
00208         return (KERN_OPERATION_TIMED_OUT == rc ? -1 : 0);
00209     }
00210 
00211     // semaphore_value is not supported on darwin
00212     /*     static inline int rtos_sem_value(rt_sem_t* m ) */
00213     /*     { */
00214     /*          int val = 0; */
00215     /*         if ( sem_getvalue(m, &val) == 0) */
00216     /*                  return val; */
00217     /*          return -1; */
00218     /*     } */
00219 
00220     // Mutex functions - support only those needed by TLSF
00221         // opaque type
00222     typedef struct rt_mutex_impl_t rt_mutex_impl_t;
00223         // type created by tlsf.c (must work in C, not C++ code)
00224     typedef rt_mutex_impl_t* rt_mutex_t;
00225     int rtos_mutex_init(rt_mutex_t* m);
00226     int rtos_mutex_destroy(rt_mutex_t* m);
00227         int rtos_mutex_lock( rt_mutex_t* m);
00228         int rtos_mutex_unlock( rt_mutex_t* m);
00229 
00230     static inline void rtos_enable_rt_warning()
00231     {
00232     }
00233 
00234     static inline void rtos_disable_rt_warning()
00235     {
00236     }
00237 
00238 
00239 #define rtos_printf printf
00240 
00241 #ifdef __cplusplus
00242 }
00243 
00244 #endif
00245 #endif


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:11