Go to the documentation of this file.00001
00002
00003 #include "hrpEC.h"
00004 #include <rtm/ECFactory.h>
00005 #include "hrpsys/io/iob.h"
00006
00007 #include <iostream>
00008
00009 #include <stdio.h>
00010 #include <errno.h>
00011 #include <sched.h>
00012 #include <signal.h>
00013 #include <sys/time.h>
00014 #include <sys/mman.h>
00015 #include <unistd.h>
00016
00017 #define MAX_SAFE_STACK (8*1024)
00018
00019
00020 void stack_prefault(void) {
00021
00022 unsigned char dummy[MAX_SAFE_STACK];
00023
00024 memset(&dummy, 0, MAX_SAFE_STACK);
00025 return;
00026 }
00027
00028 namespace RTC
00029 {
00030 hrpExecutionContext::hrpExecutionContext()
00031 #ifndef OPENRTM_VERSION_TRUNK
00032 : PeriodicExecutionContext(),
00033 #else
00034 : RTC_exp::PeriodicExecutionContext(),
00035 #endif
00036 m_priority(49)
00037 {
00038 resetProfile();
00039 rtclog.setName("hrpEC");
00040 coil::Properties& prop(Manager::instance().getConfig());
00041
00042
00043 getProperty(prop, "exec_cxt.periodic.priority", m_priority);
00044 getProperty(prop, "exec_cxt.periodic.rtpreempt.priority", m_priority);
00045 RTC_DEBUG(("Priority: %d", m_priority));
00046 }
00047
00048 bool hrpExecutionContext::waitForNextPeriod()
00049 {
00050 int nsubstep = number_of_substeps();
00051 while(1){
00052 if (wait_for_iob_signal()){
00053 perror("wait_for_iob_signal()");
00054 return false;
00055 }
00056 if (read_iob_frame() % nsubstep == 0) break;
00057 }
00058 return true;
00059 }
00060
00061 bool hrpExecutionContext::enterRT()
00062 {
00063 struct sched_param param;
00064 param.sched_priority = m_priority;
00065
00066 #ifndef __APPLE__
00067 if (sched_setscheduler(0, SCHED_FIFO, ¶m) == -1) {
00068 perror("sched_setscheduler");
00069 std::cerr << "If you are running this program on normal linux kernel for debug purpose, you can ignore the error message displayed above. If not, this program must have superuser privilege." << std::endl;
00070
00071 }else{
00072
00073 if(mlockall(MCL_CURRENT|MCL_FUTURE) == -1) {
00074 perror("mlockall failed");
00075 }
00076 }
00077 #endif
00078
00079 stack_prefault();
00080
00081 return true;
00082 }
00083 bool hrpExecutionContext::exitRT()
00084 {
00085 return true;
00086 }
00087 };
00088
00089
00090 extern "C"
00091 {
00092 void hrpECInit(RTC::Manager* manager)
00093 {
00094 #ifndef OPENRTM_VERSION_TRUNK
00095 manager->registerECFactory("hrpExecutionContext",
00096 RTC::ECCreate<RTC::hrpExecutionContext>,
00097 RTC::ECDelete<RTC::hrpExecutionContext>);
00098 #else
00099 RTC::ExecutionContextFactory::
00100 instance().addFactory("hrpExecutionContext",
00101 ::coil::Creator< ::RTC::ExecutionContextBase,
00102 RTC::hrpExecutionContext>,
00103 ::coil::Destructor< ::RTC::ExecutionContextBase,
00104 RTC::hrpExecutionContext>);
00105 #endif
00106 std::cerr << "hrpExecutionContext is registered" << std::endl;
00107 }
00108 };
00109