This is Unofficial EPICS BASE Doxygen Site
epics::pvData::Timer Class Reference

Support for delayed or periodic callback execution. More...

#include "timer.h"

+ Inheritance diagram for epics::pvData::Timer:
+ Collaboration diagram for epics::pvData::Timer:

Public Member Functions

 POINTER_DEFINITIONS (Timer)
 
 Timer (std::string threadName, ThreadPriority priority)
 
virtual ~Timer ()
 
void close ()
 Prevent new callbacks from being scheduled, and cancel pending callbacks. More...
 
void scheduleAfterDelay (TimerCallbackPtr const &timerCallback, double delay)
 
void schedulePeriodic (TimerCallbackPtr const &timerCallback, double delay, double period)
 
bool cancel (TimerCallbackPtr const &timerCallback)
 
bool isScheduled (TimerCallbackPtr const &timerCallback) const
 
void dump (std::ostream &o) const
 

Detailed Description

Support for delayed or periodic callback execution.

Definition at line 71 of file timer.h.

Constructor & Destructor Documentation

epics::pvData::Timer::Timer ( std::string  threadName,
ThreadPriority  priority 
)

Create a new timer queue

Parameters
threadNamename for the timer thread.
prioritythread priority

Definition at line 30 of file timer.cpp.

31  :waitForWork(false)
32  ,waiting(false)
33  ,alive(true)
34  ,thread(threadName,priority,this)
35 {}
epicsEventId waitForWork
Definition: errlog.c:66
epics::pvData::Timer::~Timer ( )
virtual

Definition at line 137 of file timer.cpp.

137  {
138  close();
139 }
void close()
Prevent new callbacks from being scheduled, and cancel pending callbacks.
Definition: timer.cpp:141

Member Function Documentation

bool epics::pvData::Timer::cancel ( TimerCallbackPtr const &  timerCallback)

cancel a callback.

Parameters
timerCallbackthe timerCallback to cancel.
Returns
true if the timer was queued, and now is cancelled

Definition at line 60 of file timer.cpp.

61 {
62  Lock xx(mutex);
63  if(!timerCallback->onList) return false;
64  if(!alive) {
65  timerCallback->onList = false;
66  return true;
67  }
68  for(queue_t::iterator it(queue.begin()), end(queue.end()); it != end; ++it)
69  {
70  TimerCallbackPtr& cur = *it;
71  if(cur.get() == timerCallback.get()) {
72  cur->onList = false;
73  queue.erase(it); // invalidates cur and it
74  return true;
75  }
76  }
77  throw std::logic_error("Timer::cancel() onList==true, but not found");
78 }
std::tr1::shared_ptr< TimerCallback > TimerCallbackPtr
Definition: timer.h:32
void epics::pvData::Timer::close ( )

Prevent new callbacks from being scheduled, and cancel pending callbacks.

Definition at line 141 of file timer.cpp.

141  {
142  {
143  Lock xx(mutex);
144  if(!alive)
145  return; // already closed
146  alive = false;
147  }
148  waitForWork.signal();
149  thread.exitWait();
150 
151  queue_t temp;
152  temp.swap(queue);
153 
154  for(;!temp.empty(); temp.pop_front()) {
155  TimerCallbackPtr& head = temp.front();
156  head->onList = false;
157  head->timerStopped();
158  }
159 }
std::tr1::shared_ptr< TimerCallback > TimerCallbackPtr
Definition: timer.h:32
epicsEventId waitForWork
Definition: errlog.c:66
void epics::pvData::Timer::dump ( std::ostream &  o) const

show the elements in the timer queue.

Parameters
oThe output stream for the output

Definition at line 197 of file timer.cpp.

198 {
199  Lock xx(mutex);
200  if(!alive) return;
201  epicsTime now(epicsTime::getCurrent());
202 
203  for(queue_t::const_iterator it(queue.begin()), end(queue.end()); it!=end; ++it) {
204  const TimerCallbackPtr& nodeToCall = *it;
205  o << "timeToRun " << (nodeToCall->timeToRun - now)
206  << " period " << nodeToCall->period << "\n";
207  }
208 }
std::tr1::shared_ptr< TimerCallback > TimerCallbackPtr
Definition: timer.h:32
bool epics::pvData::Timer::isScheduled ( TimerCallbackPtr const &  timerCallback) const

Is the callback scheduled to be called?

Parameters
timerCallbackthe timerCallback.
Returns
(false,true) if (not, is) scheduled.

Definition at line 80 of file timer.cpp.

81 {
82  Lock xx(mutex);
83  return timerCallback->onList;
84 }
epics::pvData::Timer::POINTER_DEFINITIONS ( Timer  )
void epics::pvData::Timer::scheduleAfterDelay ( TimerCallbackPtr const &  timerCallback,
double  delay 
)

schedule a callback after a delay.

Parameters
timerCallbackthe timerCallback instance.
delaynumber of seconds before calling callback.

Definition at line 161 of file timer.cpp.

164 {
165  schedulePeriodic(timerCallback,delay,0.0);
166 }
void schedulePeriodic(TimerCallbackPtr const &timerCallback, double delay, double period)
Definition: timer.cpp:168
void epics::pvData::Timer::schedulePeriodic ( TimerCallbackPtr const &  timerCallback,
double  delay,
double  period 
)

schedule a periodic callback.`

Parameters
timerCallbackthe timerCallback instance.
delaynumber of seconds before first callback.
periodtime in seconds between each callback.

Definition at line 168 of file timer.cpp.

172 {
173  epicsTime now(epicsTime::getCurrent());
174 
175  bool wakeup;
176  {
177  Lock xx(mutex);
178  if(timerCallback->onList) {
179  throw std::logic_error(string("already queued"));
180  }
181 
182  if(!alive) {
183  xx.unlock();
184  timerCallback->timerStopped();
185  return;
186  }
187 
188  timerCallback->timeToRun = now + delay;
189  timerCallback->period = period;
190 
191  addElement(timerCallback);
192  wakeup = waiting && queue.front()==timerCallback;
193  }
194  if(wakeup) waitForWork.signal();
195 }
epicsEventId waitForWork
Definition: errlog.c:66

The documentation for this class was generated from the following files: