Timer.cpp
Go to the documentation of this file.
1 // -*- C++ -*-
19 #include <coil/Listener.h>
20 #include <coil/Timer.h>
21 #include <coil/Time.h>
22 namespace coil
23 {
32  : m_interval(interval), m_running(false)
33  {
34  }
35 
44  {
45  stop();
46  wait();
47  }
48 
56  int Timer::open(void *args)
57  {
58  activate();
59  return 0;
60  }
61 
69  int Timer::svc(void)
70  {
71  TimeValue t_curr, t_pre, tm;;
72  while (m_running)
73  {
74  invoke();
76  }
77  return 0;
78  }
79 
80  //============================================================
81  // public functions
82  //============================================================
90  void Timer::start()
91  {
92  Guard guard(m_runningMutex);
93  if (!m_running)
94  {
95  m_running = true;
96  open(0);
97  }
98  }
99 
107  void Timer::stop()
108  {
109  Guard guard(m_runningMutex);
110  m_running = false;
111  }
112 
121  {
122  Guard guard(m_taskMutex);
123  for (size_t i(0), len(m_tasks.size()); i < len; ++i)
124  {
125  m_tasks[i].remains = m_tasks[i].remains - m_interval;
126  if (m_tasks[i].remains.sign() <= 0)
127  {
128  m_tasks[i].listener->invoke();
129  m_tasks[i].remains = m_tasks[i].period;
130  }
131  }
132  }
133 
142  {
143  Guard guard(m_taskMutex);
144 
145  for (size_t i(0), len(m_tasks.size()); i < len; ++i)
146  {
147  if (m_tasks[i].listener == listener)
148  {
149  m_tasks[i].period = tm;
150  m_tasks[i].remains = tm;
151  return listener;
152  }
153  }
154  m_tasks.push_back(Task(listener, tm));
155  return listener;
156  }
157 
166  {
167  Guard guard(m_taskMutex);
168  std::vector<Task>::iterator it;
169  it = m_tasks.begin();
170 
171  for (size_t i(0), len(m_tasks.size()); i < len; ++i, ++it)
172  {
173  if (m_tasks[i].listener == id)
174  {
175  m_tasks.erase(it);
176  return true;
177  }
178  }
179  return false;
180  }
181 }; // namespace RTC
unsigned int sleep(unsigned int seconds)
Stop a processing at specified second time.
Definition: ace/coil/Time.h:40
std::vector< Task > m_tasks
Definition: Timer.h:333
ListenerId registerListener(ListenerBase *listener, TimeValue tm)
Register listener.
Definition: Timer.cpp:141
virtual int wait(void)
Waiting for the thread terminate.
void stop()
Stop Timer task.
Definition: Timer.cpp:107
Mutex m_taskMutex
Definition: Timer.h:334
Mutex m_runningMutex
Definition: Timer.h:319
void invoke()
Invoke Timer task.
Definition: Timer.cpp:120
ListenerBase class.
Definition: Listener.h:43
TimeValue m_interval
Definition: Timer.h:317
virtual ~Timer()
Destructor.
Definition: Timer.cpp:43
Task()
Constructor.
void start()
Start Timer task.
Definition: Timer.cpp:90
Timer(TimeValue &interval)
Constructor.
Definition: Timer.cpp:31
bool unregisterListener(ListenerId id)
Unregister listener.
Definition: Timer.cpp:165
virtual int svc(void)
Thread execution function for Timer.
Definition: Timer.cpp:69
bool m_running
Definition: Timer.h:320
virtual int open(void *args)
Create thread for Timer.
Definition: Timer.cpp:56
virtual void activate()
Create a thread.
Common Object Interface Layer.


openrtm_aist
Author(s): Noriaki Ando
autogenerated on Thu Jun 6 2019 19:26:00