os_lxrt.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // This file is part of FZIs ic_workspace.
5 //
6 // This program is free software licensed under the LGPL
7 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
8 // You can find a copy of this license in LICENSE folder in the top
9 // directory of the source code.
10 //
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 //
13 // -- END LICENSE BLOCK ------------------------------------------------
14 
15 //----------------------------------------------------------------------
26 //----------------------------------------------------------------------
27 #include "icl_core/os_lxrt.h"
28 
29 #include <cstring>
30 #include <fstream>
31 
32 #ifdef _SYSTEM_POSIX_
33 #include <sys/mman.h>
34 #endif
35 
36 #ifdef _SYSTEM_LXRT_
37 # include <stdlib.h>
38 # include <rtai_config.h>
39 # include <rtai_lxrt.h>
40 # include <rtai_posix.h>
41 # include <rtai_version.h>
42 # if defined(_SYSTEM_LXRT_33_) || defined(_SYSTEM_LXRT_35_)
43 # include <mca_lxrt_extension.h>
44 # endif
45 #endif
46 
47 using std::memset;
48 using std::perror;
49 using std::printf;
50 using std::strncmp;
51 
52 //#define LOCAL_PRINTF PRINTF
53 #define LOCAL_PRINTF(arg)
54 
55 namespace icl_core {
56 namespace os {
57 
58 bool global_lxrt_available = false;
60 
62 {
63 #ifdef _SYSTEM_LXRT_
64  LOCAL_PRINTF("LXRT startup: Check for LXRT ... ");
65  checkForLxrt();
66  LOCAL_PRINTF("Done\n");
67 
68  LOCAL_PRINTF("LXRT startup: Making the task RT ... ");
70  LOCAL_PRINTF("Done\n");
71 #endif
72 }
73 
75 {
76 #ifdef _SYSTEM_LXRT_
77  LOCAL_PRINTF("LXRT shutdown: Making the task plain Linux ... ");
79  LOCAL_PRINTF("Done\n");
80 #endif
81 }
82 
83 bool checkKernelModule(const char *name)
84 {
85  std::ifstream modules("/proc/modules");
86  char line[200];
87  while (modules.good())
88  {
89  memset(line, 0, sizeof(line));
90  modules.getline(line, 200);
91  if (!strncmp(line, name, strlen(name)))
92  {
93  return true;
94  }
95  }
96  return false;
97 }
98 
99 bool checkForLxrt(void)
100 {
101 #ifdef _SYSTEM_LXRT_
102 # if defined(_SYSTEM_LXRT_33_) || defined(_SYSTEM_LXRT_35_)
103  if (!checkKernelModule("mca_lxrt_extension"))
104  {
105  printf("LXRT: No mca_lxrt_extension module loaded\n LXRT functions not available!\n");
106  return false;
107  }
108 # else
109  if (!checkKernelModule("rtai_hal"))
110  {
111  printf("LXRT: No rtai_hal module loaded\n LXRT functions not available!\n");
112  return false;
113  }
114  if (!checkKernelModule("rtai_lxrt"))
115  {
116  printf("LXRT: No rtai_lxrt module loaded\n LXRT functions not available!\n");
117  return false;
118  }
119  if (!checkKernelModule("rtai_sem"))
120  {
121  printf("LXRT: No rtai_sem module loaded\n LXRT functions not available!\n");
122  return false;
123  }
124 # endif
125 
126  // Only print this the first time the function is called!
127  if (!global_lxrt_available)
128  {
129  printf("LXRT: available\n");
130  }
131 
132  global_lxrt_available = 1;
133 
134  return true;
135 #else
136  // no lxrt configured
137  return false;
138 #endif
139 }
140 
142 {
143 #ifdef _SYSTEM_LXRT_
144  return global_lxrt_available;
145 #else
146  return false;
147 #endif
148 }
149 
151 {
152 #ifdef _SYSTEM_LXRT_
153  return global_lxrt_available && rt_buddy();
154 #else
155  return false;
156 #endif
157 }
158 
159 bool isThisHRT()
160 {
161 #ifdef _SYSTEM_LXRT_
162  return global_lxrt_available && rt_buddy() && rt_is_hard_real_time(rt_buddy());
163 #else
164  return false;
165 #endif
166 }
167 
169 {
170 #ifdef _SYSTEM_LXRT_
171  if (isThisHRT())
172  {
173  rt_make_soft_real_time();
174  return true;
175  }
176 #endif
177  return false;
178 }
179 
180 void makeHRT()
181 {
182 #ifdef _SYSTEM_LXRT_
183  rt_make_hard_real_time();
184 #endif
185 }
186 
188 {
189 #ifdef _SYSTEM_LXRT_
190  if (!global_lxrt_available)
191  {
192  printf("LXRT not available: MakeThisAnLXRTTask impossible\n");
193  return 0;
194  }
195 
196  //INFOMSG("Making an LXRT thread\n");
197  if (isThisLxrtTask())
198  {
199  printf("LXRT: this is alread an lxrt task\n");
200  return 0;
201  }
202 
203  rt_allow_nonroot_hrt();
204 
205  // Lock the process memory.
206  if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
207  {
208  perror("LXRT: Could not lock the process memory:");
209  return 0;
210  }
211 
212  //printf("LXRT: setting scheduler to SCHED_FIFO\n");
213  struct sched_param mysched;
214  mysched.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
215  if (sched_setscheduler(0, SCHED_FIFO, &mysched) == -1)
216  {
217  printf("LXRT: ERROR IN SETTING THE SCHEDULER");
218  perror("LXRT: errno");
219  exit(1);
220  }
221 
222  if (!rt_task_init(getpid() + pthread_self_rt(), 100, 0, 0))
223  {
224  printf("LXRT: CANNOT INIT THREAD \n");
225  exit(1);
226  }
227 
228  if ((hard_timer_running = rt_is_hard_timer_running()))
229  {
230  printf("WARNING: Timer is already on - not activating.");
231  }
232  else
233  {
234 #if CONFIG_RTAI_VERSION_MINOR < 5
235  rt_set_periodic_mode();
236  rt_linux_use_fpu(1);
237  //rt_task_use_fpu(rt_task, 1);
238 #else
239  rt_set_oneshot_mode();
240 #endif
241  int hp = start_rt_timer(nano2count(500000));
242  printf("LXRT: starting TIMER with hp = %i", hp);
243  }
244 
245  return 1;
246 #else
247  printf("LXRT: Not compiled for LXRT: Cannot switch this task into an lxrt one.\n");
248  return 0;
249 #endif
250 }
251 
252 
254 {
255 #ifdef _SYSTEM_LXRT_
256 
257  if (isThisLxrtTask())
258  {
259  // DEM("MakeThisALinuxTask soft\n");
260  rt_make_soft_real_time();
261 
262  // DEM("MakeThisALinuxTask join\n");
263 #if CONFIG_RTAI_VERSION_MINOR < 5
264  RT_TASK *rt_task = rt_buddy();
265  if (rt_task)
266  {
267  rt_task_delete(rt_task);
268  }
269 #else
270  pthread_join_rt(pthread_self_rt(), NULL);
271 #endif
272 
273  /* DO NOT STOP THE TIMER!!
274  * AFTER STOPPING THE TIMER, the system will not run correclty any more
275  *
276  * Above all: we would have to ensure that no other threads are running.
277  *
278  * DEM("MakeThisALinuxTask timer stop\n");
279  * // Stopping timer now
280  * if ((hard_timer_running = rt_is_hard_timer_running()))
281  * {
282  * DEM("Timer still on, stopping rt timer now.");
283  * stop_rt_timer();
284  * }
285  * else
286  * {
287  * DEM("No timer running, STRANGE -- Doing nothing.");
288  * }
289  */
290  }
291 
292 #else
293 
294  printf("LXRT: Not compiled for LXRT: Cannot switch this task from LXRT to linux.\n");
295 
296 #endif
297 }
298 
299 #ifdef _SYSTEM_LXRT_
300 struct timeval lxrtGetExecTime()
301 {
302  struct timeval time;
303 # if defined(_SYSTEM_LXRT_33_) || defined(_SYSTEM_LXRT_35_)
304  mcalxrt_exectime(&time);
305 # else
306  RTIME exectime[3];
307  rt_get_exectime(rt_agent(), exectime);
308  struct timespec ts;
309  count2timespec(exectime[0], &ts);
310  time.tv_sec = ts.tv_sec;
311  time.tv_usec = ts.tv_nsec / 1000;
312 # endif
313  return time;
314 }
315 #endif
316 
318 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
319 
320 void LxrtStartup() { lxrtStartup(); }
321 
322 void LxrtShutdown() { lxrtShutdown(); }
323 
324 bool CheckKernelModule(const char *name) { return checkKernelModule(name); }
325 
326 bool CheckForLxrt(void) { return checkForLxrt(); }
327 
328 bool IsLxrtAvailable() { return isLxrtAvailable(); }
329 
330 bool IsThisLxrtTask() { return isThisLxrtTask(); }
331 
332 bool IsThisHRT() { return isThisHRT(); }
333 
334 #ifdef _SYSTEM_LXRT_
335 struct timeval LxrtGetExecTime() { return lxrtGetExecTime(); }
336 #endif
337 
338 bool EnsureNoHRT() { return ensureNoHRT(); }
339 
340 void MakeHRT() { makeHRT(); }
341 
342 int MakeThisAnLxrtTask() { return makeThisAnLxrtTask(); }
343 
344 void MakeThisALinuxTask() { return makeThisALinuxTask(); }
345 
346 #endif
347 
349 }
350 }
void lxrtShutdown()
Definition: os_lxrt.cpp:74
int makeThisAnLxrtTask()
Definition: os_lxrt.cpp:187
int hard_timer_running
Definition: os_lxrt.cpp:59
void makeHRT()
Definition: os_lxrt.cpp:180
void lxrtStartup()
Definition: os_lxrt.cpp:61
bool checkKernelModule(const char *name)
Definition: os_lxrt.cpp:83
time_t tv_sec
Seconds.
Definition: os_win32_time.h:36
long tv_nsec
Nanoseconds.
Definition: os_win32_time.h:38
bool isLxrtAvailable()
Definition: os_lxrt.cpp:141
bool isThisHRT()
Definition: os_lxrt.cpp:159
Contains global LXRT functions.
pid_t getpid()
Definition: os_thread.h:59
bool isThisLxrtTask()
Definition: os_lxrt.cpp:150
void * memset(void *dest, int c, size_t count)
Definition: os_mem.h:46
bool global_lxrt_available
Definition: os_lxrt.cpp:58
#define LOCAL_PRINTF(arg)
Definition: os_lxrt.cpp:53
void makeThisALinuxTask()
Definition: os_lxrt.cpp:253
bool ensureNoHRT()
Definition: os_lxrt.cpp:168
struct timeval ICL_CORE_IMPORT_EXPORT lxrtGetExecTime()
bool checkForLxrt(void)
Definition: os_lxrt.cpp:99


fzi_icl_core
Author(s):
autogenerated on Mon Jun 10 2019 13:17:58