PeriodicTask.cpp
Go to the documentation of this file.
00001 // -*- C++ -*-
00020 #include <coil/PeriodicTask.h>
00021 #include <coil/Time.h>
00022 
00023 namespace coil
00024 {
00032   PeriodicTask::PeriodicTask()
00033     : m_period(0.0), m_nowait(false),
00034       m_func(0), m_deleteInDtor(true),
00035       m_alive(false), m_suspend(false),
00036       m_execCount(0), m_execCountMax(10),
00037       m_periodCount(0), m_periodCountMax(10)
00038   {
00039   }
00040   
00048   PeriodicTask::~PeriodicTask()
00049   {
00050     finalize();
00051     wait();
00052     if (m_func != 0 && m_deleteInDtor)
00053       {
00054         delete m_func;
00055       }
00056   }
00057 
00065   void PeriodicTask::activate()
00066   {
00067     Guard guard(m_alive.mutex);
00068     if (m_func == 0)   { return; }
00069     if (m_alive.value) { return; }
00070 
00071     m_alive.value = true;
00072     Task::activate();
00073   }
00074 
00082   void PeriodicTask::finalize()
00083   {
00084     Guard gaurd(m_alive.mutex);
00085     m_alive.value = false;
00086 
00087     Guard suspend_gaurd(m_suspend.mutex);
00088     m_suspend.suspend = false;
00089     m_suspend.cond.signal();
00090   }
00091 
00099   int PeriodicTask::suspend(void)
00100   {
00101     Guard gaurd(m_suspend.mutex);
00102     m_suspend.suspend = true;
00103     return 0;
00104   }
00105 
00113   int PeriodicTask::resume(void)
00114   {
00115     m_periodTime.reset();
00116     m_execTime.reset();
00117 
00118     Guard gaurd(m_suspend.mutex);
00119     m_suspend.suspend = false;
00120     m_suspend.cond.signal();
00121     return 0;
00122   }
00123 
00131   void PeriodicTask::signal(void)
00132   {
00133     Guard gaurd(m_suspend.mutex);
00134     m_suspend.cond.signal();
00135   }
00136 
00144   bool PeriodicTask::setTask(TaskFuncBase* func, bool delete_in_dtor)
00145   {
00146     if (func == 0) { return false; }
00147     m_deleteInDtor = delete_in_dtor;
00148     m_func = func;
00149     return true;
00150   }
00151   
00159   void PeriodicTask::setPeriod(double period)
00160   {
00161     m_period = period;
00162 
00163     if (m_period.sec() == 0 && m_period.usec() == 0)
00164       {
00165         m_nowait = true;
00166         return;
00167       }
00168     m_nowait = false;
00169     return;
00170   }
00171 
00179   void PeriodicTask::setPeriod(TimeValue& period)
00180   {
00181     m_period = period;
00182 
00183     if (m_period.sec() == 0 && m_period.usec() == 0)
00184       {
00185         m_nowait = true;
00186         return;
00187       }
00188     m_nowait = false;
00189     return;
00190   }
00191 
00199   void PeriodicTask::executionMeasure(bool value)
00200   {
00201     m_execMeasure = value;
00202   }
00203 
00211   void PeriodicTask::executionMeasureCount(int n)
00212   {
00213     m_execCountMax = n;
00214   }
00215 
00223   void PeriodicTask::periodicMeasure(bool value)
00224   {
00225     m_periodMeasure = value;
00226   }
00227 
00235   void PeriodicTask::periodicMeasureCount(int n)
00236   {
00237     m_periodCountMax = n;
00238   }
00239 
00247   TimeMeasure::Statistics PeriodicTask::getExecStat()
00248   {
00249     Guard guard(m_execStat.mutex);
00250     return m_execStat.stat;
00251   }
00252 
00260   TimeMeasure::Statistics PeriodicTask::getPeriodStat()
00261   {
00262     Guard guard(m_periodStat.mutex);
00263     return m_periodStat.stat;
00264   }
00265 
00266   //----------------------------------------------------------------------
00267   // protected functions
00268   //----------------------------------------------------------------------
00276   int PeriodicTask::svc()
00277   {
00278     while (m_alive.value) // needs lock?
00279       {
00280         if (m_periodMeasure) { m_periodTime.tack(); }
00281         { // wait if suspended
00282           Guard suspend_gaurd(m_suspend.mutex);
00283           if (m_suspend.suspend)
00284             {
00285               m_suspend.cond.wait();
00286               // break if finalized
00287               if (!m_alive.value)
00288                 {
00289                   return 0;
00290                 }
00291             }
00292         }
00293         if (m_periodMeasure) { m_periodTime.tick(); }
00294 
00295         // task execution
00296         if (m_execMeasure) { m_execTime.tick(); }
00297         (*m_func)();
00298         if (m_execMeasure) { m_execTime.tack(); }
00299 
00300         // wait for next period
00301         updateExecStat();
00302         sleep();
00303         updatePeriodStat();
00304       }
00305     return 0;
00306   }
00307 
00308   //----------------------------------------------------------------------
00309   // protected member function
00310   //----------------------------------------------------------------------
00311 
00319   void PeriodicTask::sleep()
00320   { 
00321     if (m_nowait)
00322       {
00323         return;
00324       }
00325     coil::sleep(m_period - m_execTime.interval());
00326   }
00327 
00335   void PeriodicTask::updateExecStat()
00336   {
00337     if (m_execCount > m_execCountMax)
00338       {
00339         Guard guard(m_execStat.mutex);
00340         m_execStat.stat = m_execTime.getStatistics();
00341         m_execCount = 0;
00342       }
00343     ++m_execCount;
00344   }
00345 
00353   void PeriodicTask::updatePeriodStat()
00354   {
00355     if (m_periodCount > m_periodCountMax)
00356       {
00357         Guard guard(m_periodStat.mutex);
00358         m_periodStat.stat = m_periodTime.getStatistics();
00359         m_periodCount = 0;
00360       }
00361     ++m_periodCount;
00362   }
00363 
00364 }; // namespace coil
00365 


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Sat Jun 8 2019 18:49:05