Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
periodicthread.hpp
00001 
00002 #ifndef DDF_PERIODICTHREAD_HPP
00003 #define DDF_PERIODICTHREAD_HPP
00004 
00005 #include "stopwatch.hpp"
00006 
00007 namespace jafar
00008 {
00009   namespace ddf
00010   {
00011 
00017     class Timer_Command
00018     {
00019     private:
00020       bool    m_kill;   // if true then the thread function returns
00021       bool    m_run;    // if true then execute the thread function
00022       bool    m_initialized;
00023       mutable   boost::mutex  m_mutex;  // mutex in case the process is controlled by different entities
00024       
00025     public:
00026       Timer_Command(): m_kill(false), m_run(false), m_initialized(false) { }
00027 
00028       void Kill(bool val) { boost::mutex::scoped_lock lock(m_mutex); m_kill = val; }
00029       void Run(bool val) { boost::mutex::scoped_lock lock(m_mutex); m_run = val; m_initialized = !m_run; }
00030       bool IsRunning() {  return m_run; }
00031       bool IsInitialized() { boost::mutex::scoped_lock lock(m_mutex); return m_initialized;}
00032       void SetInitialized() { boost::mutex::scoped_lock lock(m_mutex); m_initialized = true; }
00033       bool IsKilled() { return m_kill; }
00034     };
00035 
00040     template<typename T>
00041     class Thread_Periodic_Func
00042     {
00043    
00044     private:
00045       time  m_period;       // time interval between two calls of the process function
00046       Timer_Command &m_pth_cmd;   // control the execution of the thread
00047       T *m_parent;        // a pointer on the parent, which created the thread
00048 
00049     public:
00050 
00051       Thread_Periodic_Func(time period, Timer_Command &pth_cmd, T *obj)
00052         : m_period(period), m_pth_cmd(pth_cmd) { m_parent = obj; }
00053 
00054       ~Thread_Periodic_Func() { }
00055 
00056       void operator()()
00057       {
00058         StopWatch period_check;
00059         time  horizon = time::current();
00060         time  elapsed, t_tmp(0,20000);
00061         bool first = true;
00062         int call_nr = 0;
00063 
00064         while(1)
00065             {
00066               if(m_pth_cmd.IsRunning())
00067               {
00068                 if(first) // do it only once after Start() is called
00069                 {
00070                   horizon = m_parent->GetNextHorizon();
00071                   period_check.Restart();
00072                   first = false;
00073                   m_pth_cmd.SetInitialized();
00074                 }
00075                 else
00076                 {
00077                   horizon = horizon + m_period;
00078                   m_parent->SetNextHorizon(horizon);
00079                 }
00080 
00081               // Wait until the next time horizon
00082               horizon.sleep_until();
00083                
00084               if (m_pth_cmd.IsRunning())
00085               {
00086                 // Get the elapsed time since the previous call
00087                 elapsed = period_check.ElapsedAndReset();
00088                      
00089                 // Exec main func
00090                 m_parent->ExecSyncThreadFunc(horizon, elapsed, call_nr++);
00091               }
00092             }
00093             else {
00094               first = true;
00095               call_nr = 0;
00096               t_tmp.sleep_period(); // for avoiding the sensornode starvation (cmd.Run, Kill etc.)
00097               // give a chance to the other process to get the mutex
00098             }
00099             
00100             if(m_pth_cmd.IsKilled()) break;
00101           }
00102       }
00103     };
00104 
00105   } // namespace ddf
00106 } // namespace jafar
00107 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:17 for Jafar by doxygen 1.7.6.1
LAAS-CNRS