Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Related Pages  

Timer Class Reference

Used to perform functions at a future time, and provide a utility to do easy timeouts. More...

#include <Timer.h>

Inheritance diagram for Timer::

BasicObject CallbackTimer ThreadSpawningTimer List of all members.

Public Methods

void addEvent (int millisec, void *(*func)(void *), void *arg, bool *set)
 This is used to execute a function sometime in the future. More...

virtual void toStream (std::ostream &)
 Prints out the times of all scheduled events. More...

void setName (string)
 Allows you to set a name for this timer, rpimarily for debugging purposes, since the timer resides in a separate thread, and there are multiple timers, it helps to know which is which. More...

string getName ()
 Get the name of the timer. More...


Protected Methods

 Timer ()
 no one is allowed to create a timer object, only subclasses. More...

virtual ~Timer ()
 Destroy the Timer, and its associated thread. More...

void timerThreadImpl ()
 This is a thread that waits on the event at the head of the queue, and when the time comes for the event to be fired, it calls doAction() if the event is still valid. More...

virtual void doAction (TimerNode *)=0
 a Template Method, subclasses must override. More...


Static Protected Methods

void * timerThread (void *)
 Thread function. More...


Protected Attributes

pthread_t m_timerThreadId
 ID of the timer thread. More...

Condition m_eventListSignal
 Signalled if there has been a change to the contents of the event list. More...

Mutex m_eventListMutex
 Used to lock the event list. More...

list< TimerNode *> m_eventList
 the event list. More...

bool m_destroyed
 true if we want to destroy this object. More...

string m_name
 the name of this timer. More...


Detailed Description

Used to perform functions at a future time, and provide a utility to do easy timeouts.

Events are registered using the "addEvent" function. Events can be cancelled by setting the boolean pointer passed in to addEvent to false.

Subclasses of Timer do different things with the function passed in as an event. The CallbackTimer executes the function in-line, while the ThreadSpawningTimer executes the function in a new thread.

Definition at line 14 of file Timer.h.


Constructor & Destructor Documentation

Timer::Timer [protected]
 

no one is allowed to create a timer object, only subclasses.

Definition at line 18 of file Timer.cpp.

Timer::~Timer [protected, virtual]
 

Destroy the Timer, and its associated thread.

Definition at line 31 of file Timer.cpp.


Member Function Documentation

void Timer::addEvent int millisec,
void *(* func)(void *),
void * arg,
bool * set
 

This is used to execute a function sometime in the future.

You can cancel the event by changing the bool value pointed to by "set" to "false". This function adds an event to Timer's internal queue.

Parameters:
millisec   The number of milliseconds to wait until the function is executed.
func   A function pointer of the form "void* func(void* arg)" which will be called after millisec.
arg   The argument to pass the function func when it is called (can be NULL).
set   Whether the event is still valid. (you can cancel an event later if you want).

Definition at line 143 of file Timer.cpp.

Referenced by SendWindow::reliableSend(), SendWindow::sentPacketTimeoutImpl(), and ~Timer().

virtual void Timer::doAction TimerNode * timerNode [protected, pure virtual]
 

a Template Method, subclasses must override.

Reimplemented in CallbackTimer, and ThreadSpawningTimer.

Referenced by timerThreadImpl().

string Timer::getName
 

Get the name of the timer.

Definition at line 61 of file Timer.cpp.

Referenced by timerThreadImpl().

void Timer::setName string name
 

Allows you to set a name for this timer, rpimarily for debugging purposes, since the timer resides in a separate thread, and there are multiple timers, it helps to know which is which.

Definition at line 52 of file Timer.cpp.

Referenced by SendWindow::SendWindow(), and ThreadSpawningTimer::instance().

void * Timer::timerThread void * arg [static, protected]
 

Thread function.

Parameters:
arg   A Timer object.

Definition at line 71 of file Timer.cpp.

void Timer::timerThreadImpl [protected]
 

This is a thread that waits on the event at the head of the queue, and when the time comes for the event to be fired, it calls doAction() if the event is still valid.

This function follows the Template Method design pattern. There is only one template method: "doAction".

Definition at line 87 of file Timer.cpp.

Referenced by timerThread().

void Timer::toStream std::ostream & out [virtual]
 

Prints out the times of all scheduled events.

Reimplemented from BasicObject.

Definition at line 174 of file Timer.cpp.


Member Data Documentation

bool Timer::m_destroyed [protected]
 

true if we want to destroy this object.

Definition at line 48 of file Timer.h.

list<TimerNode*> Timer::m_eventList [protected]
 

the event list.

Definition at line 45 of file Timer.h.

Mutex Timer::m_eventListMutex [protected]
 

Used to lock the event list.

Definition at line 42 of file Timer.h.

Condition Timer::m_eventListSignal [protected]
 

Signalled if there has been a change to the contents of the event list.

Definition at line 39 of file Timer.h.

string Timer::m_name [protected]
 

the name of this timer.

Definition at line 51 of file Timer.h.

pthread_t Timer::m_timerThreadId [protected]
 

ID of the timer thread.

Definition at line 36 of file Timer.h.


The documentation for this class was generated from the following files:
Generated at Thu Jul 11 13:31:59 2002 for Peekabooty by doxygen1.2.9 written by Dimitri van Heesch, © 1997-2001