os_lxrt.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 
00003 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00004 // This file is part of FZIs ic_workspace.
00005 //
00006 // This program is free software licensed under the LGPL
00007 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00008 // You can find a copy of this license in LICENSE folder in the top
00009 // directory of the source code.
00010 //
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 //
00013 // -- END LICENSE BLOCK ------------------------------------------------
00014 
00015 //----------------------------------------------------------------------
00026 //----------------------------------------------------------------------
00027 #include "icl_core/os_lxrt.h"
00028 
00029 #include <cstring>
00030 #include <fstream>
00031 
00032 #ifdef _SYSTEM_POSIX_
00033 #include <sys/mman.h>
00034 #endif
00035 
00036 #ifdef _SYSTEM_LXRT_
00037 # include <stdlib.h>
00038 # include <rtai_config.h>
00039 # include <rtai_lxrt.h>
00040 # include <rtai_posix.h>
00041 # include <rtai_version.h>
00042 # if defined(_SYSTEM_LXRT_33_) || defined(_SYSTEM_LXRT_35_)
00043 #  include <mca_lxrt_extension.h>
00044 # endif
00045 #endif
00046 
00047 using std::memset;
00048 using std::perror;
00049 using std::printf;
00050 using std::strncmp;
00051 
00052 //#define LOCAL_PRINTF PRINTF
00053 #define LOCAL_PRINTF(arg)
00054 
00055 namespace icl_core {
00056 namespace os {
00057 
00058 bool global_lxrt_available = false;
00059 int hard_timer_running = 0;
00060 
00061 void lxrtStartup()
00062 {
00063 #ifdef _SYSTEM_LXRT_
00064   LOCAL_PRINTF("LXRT startup: Check for LXRT ... ");
00065   checkForLxrt();
00066   LOCAL_PRINTF("Done\n");
00067 
00068   LOCAL_PRINTF("LXRT startup: Making the task RT ... ");
00069   makeThisAnLxrtTask();
00070   LOCAL_PRINTF("Done\n");
00071 #endif
00072 }
00073 
00074 void lxrtShutdown()
00075 {
00076 #ifdef _SYSTEM_LXRT_
00077   LOCAL_PRINTF("LXRT shutdown: Making the task plain Linux ... ");
00078   makeThisALinuxTask();
00079   LOCAL_PRINTF("Done\n");
00080 #endif
00081 }
00082 
00083 bool checkKernelModule(const char *name)
00084 {
00085   std::ifstream modules("/proc/modules");
00086   char line[200];
00087   while (modules.good())
00088   {
00089     memset(line, 0, sizeof(line));
00090     modules.getline(line, 200);
00091     if (!strncmp(line, name, strlen(name)))
00092     {
00093       return true;
00094     }
00095   }
00096   return false;
00097 }
00098 
00099 bool checkForLxrt(void)
00100 {
00101 #ifdef _SYSTEM_LXRT_
00102 # if defined(_SYSTEM_LXRT_33_) || defined(_SYSTEM_LXRT_35_)
00103   if (!checkKernelModule("mca_lxrt_extension"))
00104   {
00105     printf("LXRT: No mca_lxrt_extension module loaded\n LXRT functions not available!\n");
00106     return false;
00107   }
00108 # else
00109   if (!checkKernelModule("rtai_hal"))
00110   {
00111     printf("LXRT: No rtai_hal module loaded\n LXRT functions not available!\n");
00112     return false;
00113   }
00114   if (!checkKernelModule("rtai_lxrt"))
00115   {
00116     printf("LXRT: No rtai_lxrt module loaded\n LXRT functions not available!\n");
00117     return false;
00118   }
00119   if (!checkKernelModule("rtai_sem"))
00120   {
00121     printf("LXRT: No rtai_sem module loaded\n LXRT functions not available!\n");
00122     return false;
00123   }
00124 # endif
00125 
00126   // Only print this the first time the function is called!
00127   if (!global_lxrt_available)
00128   {
00129     printf("LXRT: available\n");
00130   }
00131 
00132   global_lxrt_available = 1;
00133 
00134   return true;
00135 #else
00136   // no lxrt configured
00137   return false;
00138 #endif
00139 }
00140 
00141 bool isLxrtAvailable()
00142 {
00143 #ifdef _SYSTEM_LXRT_
00144   return global_lxrt_available;
00145 #else
00146   return false;
00147 #endif
00148 }
00149 
00150 bool isThisLxrtTask()
00151 {
00152 #ifdef _SYSTEM_LXRT_
00153   return global_lxrt_available && rt_buddy();
00154 #else
00155   return false;
00156 #endif
00157 }
00158 
00159 bool isThisHRT()
00160 {
00161 #ifdef _SYSTEM_LXRT_
00162   return global_lxrt_available && rt_buddy() && rt_is_hard_real_time(rt_buddy());
00163 #else
00164   return false;
00165 #endif
00166 }
00167 
00168 bool ensureNoHRT()
00169 {
00170 #ifdef _SYSTEM_LXRT_
00171   if (isThisHRT())
00172   {
00173     rt_make_soft_real_time();
00174     return true;
00175   }
00176 #endif
00177   return false;
00178 }
00179 
00180 void makeHRT()
00181 {
00182 #ifdef _SYSTEM_LXRT_
00183   rt_make_hard_real_time();
00184 #endif
00185 }
00186 
00187 int makeThisAnLxrtTask()
00188 {
00189 #ifdef _SYSTEM_LXRT_
00190   if (!global_lxrt_available)
00191   {
00192     printf("LXRT not available: MakeThisAnLXRTTask impossible\n");
00193     return 0;
00194   }
00195 
00196   //INFOMSG("Making an LXRT thread\n");
00197   if (isThisLxrtTask())
00198   {
00199     printf("LXRT: this is alread an lxrt task\n");
00200     return 0;
00201   }
00202 
00203   rt_allow_nonroot_hrt();
00204 
00205   // Lock the process memory.
00206   if (mlockall(MCL_CURRENT | MCL_FUTURE) != 0)
00207   {
00208     perror("LXRT: Could not lock the process memory:");
00209     return 0;
00210   }
00211 
00212   //printf("LXRT: setting scheduler to SCHED_FIFO\n");
00213   struct sched_param mysched;
00214   mysched.sched_priority = sched_get_priority_max(SCHED_FIFO) - 1;
00215   if (sched_setscheduler(0, SCHED_FIFO, &mysched) == -1)
00216   {
00217     printf("LXRT: ERROR IN SETTING THE SCHEDULER");
00218     perror("LXRT: errno");
00219     exit(1);
00220   }
00221 
00222   if (!rt_task_init(getpid() + pthread_self_rt(), 100, 0, 0))
00223   {
00224     printf("LXRT: CANNOT INIT THREAD \n");
00225     exit(1);
00226   }
00227 
00228   if ((hard_timer_running = rt_is_hard_timer_running()))
00229   {
00230     printf("WARNING: Timer is already on - not activating.");
00231   }
00232   else
00233   {
00234 #if CONFIG_RTAI_VERSION_MINOR < 5
00235     rt_set_periodic_mode();
00236     rt_linux_use_fpu(1);
00237     //rt_task_use_fpu(rt_task, 1);
00238 #else
00239     rt_set_oneshot_mode();
00240 #endif
00241     int hp = start_rt_timer(nano2count(500000));
00242     printf("LXRT: starting TIMER with hp = %i", hp);
00243   }
00244 
00245   return 1;
00246 #else
00247   printf("LXRT: Not compiled for LXRT: Cannot switch this task into an lxrt one.\n");
00248   return 0;
00249 #endif
00250 }
00251 
00252 
00253 void makeThisALinuxTask()
00254 {
00255 #ifdef _SYSTEM_LXRT_
00256 
00257   if (isThisLxrtTask())
00258   {
00259     // DEM("MakeThisALinuxTask soft\n");
00260     rt_make_soft_real_time();
00261 
00262     // DEM("MakeThisALinuxTask join\n");
00263 #if CONFIG_RTAI_VERSION_MINOR < 5
00264     RT_TASK *rt_task = rt_buddy();
00265     if (rt_task)
00266     {
00267       rt_task_delete(rt_task);
00268     }
00269 #else
00270     pthread_join_rt(pthread_self_rt(), NULL);
00271 #endif
00272 
00273     /* DO NOT STOP THE TIMER!!
00274      * AFTER STOPPING THE TIMER, the system will not run correclty any more
00275      *
00276      * Above all: we would have to ensure that no other threads are running.
00277      *
00278      * DEM("MakeThisALinuxTask timer stop\n");
00279      * // Stopping timer now
00280      * if ((hard_timer_running = rt_is_hard_timer_running()))
00281      * {
00282      *   DEM("Timer still on, stopping rt timer now.");
00283      *   stop_rt_timer();
00284      * }
00285      * else
00286      * {
00287      *   DEM("No timer running, STRANGE -- Doing nothing.");
00288      * }
00289      */
00290   }
00291 
00292 #else
00293 
00294   printf("LXRT: Not compiled for LXRT: Cannot switch this task from LXRT to linux.\n");
00295 
00296 #endif
00297 }
00298 
00299 #ifdef _SYSTEM_LXRT_
00300 struct timeval lxrtGetExecTime()
00301 {
00302   struct timeval time;
00303 # if defined(_SYSTEM_LXRT_33_) || defined(_SYSTEM_LXRT_35_)
00304   mcalxrt_exectime(&time);
00305 # else
00306   RTIME exectime[3];
00307   rt_get_exectime(rt_agent(), exectime);
00308   struct timespec ts;
00309   count2timespec(exectime[0], &ts);
00310   time.tv_sec = ts.tv_sec;
00311   time.tv_usec = ts.tv_nsec / 1000;
00312 # endif
00313   return time;
00314 }
00315 #endif
00316 
00318 #ifdef _IC_BUILDER_DEPRECATED_STYLE_
00319 
00320 void LxrtStartup() { lxrtStartup(); }
00321 
00322 void LxrtShutdown() { lxrtShutdown(); }
00323 
00324 bool CheckKernelModule(const char *name) { return checkKernelModule(name); }
00325 
00326 bool CheckForLxrt(void) { return checkForLxrt(); }
00327 
00328 bool IsLxrtAvailable() { return isLxrtAvailable(); }
00329 
00330 bool IsThisLxrtTask() { return isThisLxrtTask(); }
00331 
00332 bool IsThisHRT() { return isThisHRT(); }
00333 
00334 #ifdef _SYSTEM_LXRT_
00335 struct timeval LxrtGetExecTime() { return lxrtGetExecTime(); }
00336 #endif
00337 
00338 bool EnsureNoHRT() { return ensureNoHRT(); }
00339 
00340 void MakeHRT() { makeHRT(); }
00341 
00342 int MakeThisAnLxrtTask() { return makeThisAnLxrtTask(); }
00343 
00344 void MakeThisALinuxTask() { return makeThisALinuxTask(); }
00345 
00346 #endif
00347 
00348 
00349 }
00350 }


fzi_icl_core
Author(s):
autogenerated on Tue Aug 8 2017 02:28:03