PeriodicTask.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
20 #include <coil/PeriodicTask.h>
21 #include <coil/Time.h>
22 
23 namespace coil
24 {
33  : m_period(0.0), m_nowait(false),
34  m_func(0), m_deleteInDtor(true),
35  m_alive(false), m_suspend(false),
36  m_execCount(0), m_execCountMax(10),
37  m_periodCount(0), m_periodCountMax(10)
38  {
39  }
40 
49  {
50  finalize();
51  wait();
52  if (m_func != 0 && m_deleteInDtor)
53  {
54  delete m_func;
55  }
56  }
57 
66  {
67  Guard guard(m_alive.mutex);
68  if (m_func == 0) { return; }
69  if (m_alive.value) { return; }
70 
71  m_alive.value = true;
73  }
74 
83  {
84  Guard gaurd(m_alive.mutex);
85  m_alive.value = false;
86 
87  Guard suspend_gaurd(m_suspend.mutex);
88  m_suspend.suspend = false;
90  }
91 
100  {
101  Guard gaurd(m_suspend.mutex);
102  m_suspend.suspend = true;
103  return 0;
104  }
105 
114  {
116  m_execTime.reset();
117 
118  Guard gaurd(m_suspend.mutex);
119  m_suspend.suspend = false;
121  return 0;
122  }
123 
132  {
133  Guard gaurd(m_suspend.mutex);
135  }
136 
144  bool PeriodicTask::setTask(TaskFuncBase* func, bool delete_in_dtor)
145  {
146  if (func == 0) { return false; }
147  m_deleteInDtor = delete_in_dtor;
148  m_func = func;
149  return true;
150  }
151 
159  void PeriodicTask::setPeriod(double period)
160  {
161  m_period = period;
162 
163  if (m_period.sec() == 0 && m_period.usec() == 0)
164  {
165  m_nowait = true;
166  return;
167  }
168  m_nowait = false;
169  return;
170  }
171 
180  {
181  m_period = period;
182 
183  if (m_period.sec() == 0 && m_period.usec() == 0)
184  {
185  m_nowait = true;
186  return;
187  }
188  m_nowait = false;
189  return;
190  }
191 
200  {
201  m_execMeasure = value;
202  }
203 
212  {
213  m_execCountMax = n;
214  }
215 
224  {
225  m_periodMeasure = value;
226  }
227 
236  {
237  m_periodCountMax = n;
238  }
239 
248  {
249  Guard guard(m_execStat.mutex);
250  return m_execStat.stat;
251  }
252 
261  {
262  Guard guard(m_periodStat.mutex);
263  return m_periodStat.stat;
264  }
265 
266  //----------------------------------------------------------------------
267  // protected functions
268  //----------------------------------------------------------------------
277  {
278  while (m_alive.value) // needs lock?
279  {
280  if (m_periodMeasure) { m_periodTime.tack(); }
281  { // wait if suspended
282  Guard suspend_gaurd(m_suspend.mutex);
283  if (m_suspend.suspend)
284  {
285  m_suspend.cond.wait();
286  // break if finalized
287  if (!m_alive.value)
288  {
289  return 0;
290  }
291  }
292  }
293  if (m_periodMeasure) { m_periodTime.tick(); }
294 
295  // task execution
296  if (m_execMeasure) { m_execTime.tick(); }
297  (*m_func)();
298  if (m_execMeasure) { m_execTime.tack(); }
299 
300  // wait for next period
301  updateExecStat();
302  sleep();
304  }
305  return 0;
306  }
307 
308  //----------------------------------------------------------------------
309  // protected member function
310  //----------------------------------------------------------------------
311 
320  {
321  if (m_nowait)
322  {
323  return;
324  }
326  }
327 
336  {
338  {
339  Guard guard(m_execStat.mutex);
341  m_execCount = 0;
342  }
343  ++m_execCount;
344  }
345 
354  {
356  {
357  Guard guard(m_periodStat.mutex);
359  m_periodCount = 0;
360  }
361  ++m_periodCount;
362  }
363 
364 }; // namespace coil
365 
PeriodicTask()
Constructor.
bool m_deleteInDtor
Task execution function delete flag.
Definition: PeriodicTask.h:379
coil::TimeMeasure m_periodTime
Task periodic time measurement infomation.
Definition: PeriodicTask.h:532
unsigned int m_execCountMax
Task execution time measurement max count.
Definition: PeriodicTask.h:469
TaskFuncBase class.
bool getStatistics(double &max_interval, double &min_interval, double &mean_interval, double &stddev)
Get total statistics.
Structure for time statistics.
Definition: TimeMeasure.h:63
virtual void periodicMeasure(bool value)
Validate a Task period time measurement.
unsigned int sleep(unsigned int seconds)
Stop a processing at specified second time.
Definition: ace/coil/Time.h:40
coil::TimeValue & interval()
Get a interval time.
Definition: TimeMeasure.cpp:93
virtual void updateExecStat()
Update for execute state.
virtual void executionMeasure(bool value)
Validate a Task execute time measurement.
virtual int resume(void)
Resuming the suspended task.
coil::Condition< coil::Mutex > cond
Definition: PeriodicTask.h:419
virtual void activate()
Starting the task.
virtual int svc()
Thread execution for PeriodicTask.
unsigned int m_execCount
Task execution time measurement count.
Definition: PeriodicTask.h:460
virtual void periodicMeasureCount(int n)
Task period time measurement count.
bool m_nowait
Thread sleep flag.
Definition: PeriodicTask.h:361
virtual void sleep()
Thread sleep.
virtual void signal()
Executing the suspended task one tick.
virtual TimeMeasure::Statistics getExecStat()
Get a result in task execute time measurement.
virtual int wait(void)
Waiting for the thread terminate.
void tack()
Finish time measurement for time statistics.
Definition: TimeMeasure.cpp:72
statistics_t m_execStat
Task execution time measurement statistics.
Definition: PeriodicTask.h:478
coil::TimeMeasure::Statistics stat
Definition: PeriodicTask.h:440
unsigned int m_periodCount
Task periodic time measurement count.
Definition: PeriodicTask.h:505
coil::TimeValue m_period
Task execution period.
Definition: PeriodicTask.h:352
virtual bool setTask(TaskFuncBase *func, bool delete_in_dtor=true)
Setting task execution function.
unsigned int m_periodCountMax
Task periodic time measurement max count.
Definition: PeriodicTask.h:514
void reset()
Initialize for statistics related data.
statistics_t m_periodStat
Task periodic time measurement statistics.
Definition: PeriodicTask.h:523
virtual ~PeriodicTask()
Destructor.
bool m_execMeasure
Task execution time measurement flag.
Definition: PeriodicTask.h:451
virtual void setPeriod(double period)
Setting task execution period.
virtual void executionMeasureCount(int n)
Task execute time measurement period.
coil::TimeMeasure m_execTime
Task execution time measurement infomation.
Definition: PeriodicTask.h:487
long int sec() const
Get value of second time scale.
Definition: TimeValue.h:110
TaskFuncBase * m_func
Task execution function.
Definition: PeriodicTask.h:370
suspend_t m_suspend
Task suspend infomation.
Definition: PeriodicTask.h:429
long int usec() const
Get value of micro second time scale.
Definition: TimeValue.h:131
virtual void updatePeriodStat()
Update for period state.
virtual void finalize()
Finalizing the task.
bool m_periodMeasure
Task periodic time measurement flag.
Definition: PeriodicTask.h:496
void tick()
Begin time measurement for time statistics.
Definition: TimeMeasure.cpp:60
virtual TimeMeasure::Statistics getPeriodStat()
Get a result in task period time measurement.
alive_t m_alive
Task alive flag.
Definition: PeriodicTask.h:405
virtual int suspend(void)
Suspending the task.
virtual void activate()
Create a thread.
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Mon Feb 28 2022 23:00:44