rtems/osal.c
Go to the documentation of this file.
1 /*
2  * Licensed under the GNU General Public License version 2 with exceptions. See
3  * LICENSE file in the project root for full license information
4  */
5 
6 #include <time.h>
7 #include <sys/time.h>
8 #include <unistd.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <osal.h>
12 
13 #define USECS_PER_SEC 1000000
14 
15 int osal_usleep (uint32 usec)
16 {
17  struct timespec ts;
18  ts.tv_sec = usec / USECS_PER_SEC;
19  ts.tv_nsec = (usec % USECS_PER_SEC) * 1000;
20  /* usleep is deprecated, use nanosleep instead */
21  return nanosleep(&ts, NULL);
22 }
23 
24 int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
25 {
26  struct timespec ts;
27  int return_value;
28  (void)tz; /* Not used */
29 
30  /* Use clock_gettime to prevent possible live-lock.
31  * Gettimeofday uses CLOCK_REALTIME that can get NTP timeadjust.
32  * If this function preempts timeadjust and it uses vpage it live-locks.
33  * Also when using XENOMAI, only clock_gettime is RT safe */
34  return_value = clock_gettime(CLOCK_MONOTONIC, &ts);
35  tv->tv_sec = ts.tv_sec;
36  tv->tv_usec = ts.tv_nsec / 1000;
37  return return_value;
38 }
39 
41 {
42  struct timeval current_time;
43  ec_timet return_value;
44 
45  osal_gettimeofday(&current_time, 0);
46  return_value.sec = current_time.tv_sec;
47  return_value.usec = current_time.tv_usec;
48  return return_value;
49 }
50 
51 void osal_time_diff(ec_timet *start, ec_timet *end, ec_timet *diff)
52 {
53  if (end->usec < start->usec) {
54  diff->sec = end->sec - start->sec - 1;
55  diff->usec = end->usec + 1000000 - start->usec;
56  }
57  else {
58  diff->sec = end->sec - start->sec;
59  diff->usec = end->usec - start->usec;
60  }
61 }
62 
63 void osal_timer_start(osal_timert * self, uint32 timeout_usec)
64 {
65  struct timeval start_time;
66  struct timeval timeout;
67  struct timeval stop_time;
68 
69  osal_gettimeofday(&start_time, 0);
70  timeout.tv_sec = timeout_usec / USECS_PER_SEC;
71  timeout.tv_usec = timeout_usec % USECS_PER_SEC;
72  timeradd(&start_time, &timeout, &stop_time);
73 
74  self->stop_time.sec = stop_time.tv_sec;
75  self->stop_time.usec = stop_time.tv_usec;
76 }
77 
79 {
80  struct timeval current_time;
81  struct timeval stop_time;
82  int is_not_yet_expired;
83 
84  osal_gettimeofday(&current_time, 0);
85  stop_time.tv_sec = self->stop_time.sec;
86  stop_time.tv_usec = self->stop_time.usec;
87  is_not_yet_expired = timercmp(&current_time, &stop_time, <);
88 
89  return is_not_yet_expired == FALSE;
90 }
91 
92 void *osal_malloc(size_t size)
93 {
94  return malloc(size);
95 }
96 
97 void osal_free(void *ptr)
98 {
99  free(ptr);
100 }
101 
102 int osal_thread_create(void *thandle, int stacksize, void *func, void *param)
103 {
104  int ret;
105  pthread_attr_t attr;
106  pthread_t *threadp;
107 
108  threadp = thandle;
109  pthread_attr_init(&attr);
110  pthread_attr_setstacksize(&attr, stacksize);
111  ret = pthread_create(threadp, &attr, func, param);
112  if(ret < 0)
113  {
114  return 0;
115  }
116  return 1;
117 }
118 
119 int osal_thread_create_rt(void *thandle, int stacksize, void *func, void *param)
120 {
121  int ret;
122  pthread_attr_t attr;
123  struct sched_param schparam;
124  pthread_t *threadp;
125 
126  threadp = thandle;
127  pthread_attr_init(&attr);
128  pthread_attr_setstacksize(&attr, stacksize);
129  ret = pthread_create(threadp, &attr, func, param);
130  pthread_attr_destroy(&attr);
131  if(ret < 0)
132  {
133  return 0;
134  }
135  memset(&schparam, 0, sizeof(schparam));
136  schparam.sched_priority = 40;
137  ret = pthread_setschedparam(*threadp, SCHED_FIFO, &schparam);
138  if(ret < 0)
139  {
140  return 0;
141  }
142 
143  return 1;
144 }
void osal_timer_start(osal_timert *self, uint32 timeout_usec)
Definition: rtems/osal.c:63
void * osal_malloc(size_t size)
Definition: rtems/osal.c:92
uint32 usec
Definition: osal.h:39
int osal_thread_create_rt(void *thandle, int stacksize, void *func, void *param)
Definition: rtems/osal.c:119
Definition: osal.h:36
#define USECS_PER_SEC
Definition: rtems/osal.c:13
int osal_gettimeofday(struct timeval *tv, struct timezone *tz)
Definition: rtems/osal.c:24
#define FALSE
Definition: osal.h:22
#define timercmp(a, b, CMP)
Definition: rtk/osal.c:12
#define timeradd(a, b, result)
Definition: rtk/osal.c:16
ec_timet osal_current_time(void)
Definition: rtems/osal.c:40
int osal_usleep(uint32 usec)
Definition: rtems/osal.c:15
void osal_time_diff(ec_timet *start, ec_timet *end, ec_timet *diff)
Definition: rtems/osal.c:51
uint32 sec
Definition: osal.h:38
uint32_t uint32
Definition: osal.h:30
void osal_free(void *ptr)
Definition: rtems/osal.c:97
boolean osal_timer_is_expired(osal_timert *self)
Definition: rtems/osal.c:78
int osal_thread_create(void *thandle, int stacksize, void *func, void *param)
Definition: rtems/osal.c:102


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Sat Jun 27 2020 03:48:21