SctService::WorkerGroup< T > Class Template Reference

Control part of the observer/worker pattern. More...

#include <WorkerGroup.h>

Inheritance diagram for SctService::WorkerGroup< T >:

Inheritance graph
[legend]
Collaboration diagram for SctService::WorkerGroup< T >:

Collaboration graph
[legend]

Queue @{ the queue itself contains pointers to objects to be

worked on.

void addTime (double t)
 timer watch
DataType unlockedPopFrontOrBack ()
 gets from front or back of queue (no locking) depending on m_fifo
std::list< DataTypem_queue
 timer watch
boost::thread_group m_thread_group
 boost thread group from which threads can be created
volatile bool m_run
 if false, threads are to stop.
volatile bool m_pause
 does not execute work() if true
volatile unsigned m_sleeptime
 time to sleep if queue is empty
std::string m_reportTo
 name to report to IS
volatile int m_reportInterval
 name to report to IS
boost::mutex m_queueAccess
 queueLock
boost::mutex m_vectorAccess
 vectorOfWorkersLock
boost::recursive_mutex m_reportAccess
 vectorOfWorkersLock
volatile unsigned long m_nDone
 count of number of jobs done
volatile double m_time
 time taken working
bool m_fifo
 FIFO if true, otherwise FILO.
class Worker
 timer watch
class Reporter
 timer watch

Public Types

typedef T DataType

Public Member Functions

void go (unsigned nWorker=1)
 Start this worker group.
unsigned queueSize () const
 get the number of objects in the queue
unsigned nWorkers () const
 get the number of workers in the map
unsigned long nDone () const
 get the number of jobs done
unsigned busy () const
 get the number of threads which are busy.
void stop ()
 Raise flag to stop all worker threads.
void setPaused (bool paused=true)
void join ()
 Call this to make sure that Worker threads continue to completion.
unsigned paused () const
 get the number of paused threads
void setSleepTime (unsigned msec)
 Set sleep time for all workers, if no items are on the queue.
unsigned getSleepTime () const
 Get the sleep time.
DataType pop ()
void push (DataType ob)
 Push an object onto the back of the queue.
void setFifo (bool val)
bool isFifo () const
virtual ~WorkerGroup ()
 destructor
 WorkerGroup ()
 Constructor.
void reportTo (std::string isname)
 set name to report to IS as
void reportInterval (int msec)
 set interval in msec between reports

Protected Types

typedef std::vector< Worker * > WorkerStore
typedef WorkerStore::iterator Iterator
typedef WorkerStore::const_iterator ConstIterator

Protected Member Functions

virtual void reportThreadMain (Reporter &reporter) throw ()
 thread which reports the progress of the workers to IS
virtual void threadMain (Worker &worker) throw ()
 The threadMain method.
virtual bool popAndWork (Worker &worker)
 If the queue length != 0, removes an element from the queue and class work.
virtual void waitForData ()
 Called if there is no data on the queue.
virtual void work (DataType data)=0 throw ()
 To be overriden! Should actually do something with the data!
void wait (int msec)
 Causes this thread to sleep for msec milliseconds.
void addWorker (Worker &worker)
 threadMain should call this with its parameter.

Protected Attributes

Worker thread information @{
This map allows each thread to access different particular information from its own Worker.

WorkerStore m_workers

Data Structures

class  Reporter
 holds data used by the reporting thread More...
class  Worker
 Inner class to hold thread specific stuff. More...

Detailed Description

template<class T>
class SctService::WorkerGroup< T >

Control part of the observer/worker pattern.

Contains the queue to which objects of type T are pushed. Also useful functions to control and query the worker threads. The template class ``T'' is the data/instructions which the workers at to work on. Derive this class to implement/modify the behaviour of the worker threads. At the least, the work method needs an implementation. T must be DefaultConstructible For a simple example, see the test/MyWorkerGroup.h file

Definition at line 28 of file WorkerGroup.h.


Constructor & Destructor Documentation

template<class T>
virtual SctService::WorkerGroup< T >::~WorkerGroup  )  [inline, virtual]
 

destructor

Definition at line 100 of file WorkerGroup.h.

template<class T>
SctService::WorkerGroup< T >::WorkerGroup  )  [inline]
 

Constructor.

Definition at line 103 of file WorkerGroup.h.


Member Function Documentation

template<class W>
void SctService::WorkerGroup< W >::addTime double  t  )  [inline, protected]
 

timer watch

Definition at line 435 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_nDone, SctService::WorkerGroup< T >::m_reportAccess, and SctService::WorkerGroup< T >::m_time.

Referenced by SctService::WorkerGroup< T >::popAndWork().

template<class T>
void SctService::WorkerGroup< T >::addWorker Worker worker  )  [inline, protected]
 

threadMain should call this with its parameter.

Definition at line 302 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_vectorAccess, and SctService::WorkerGroup< T >::m_workers.

template<class W>
unsigned SctService::WorkerGroup< W >::busy  )  const [inline]
 

get the number of threads which are busy.

Note:
for information only, not to be used in thread-control.

Definition at line 394 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_workers.

Referenced by SctFitter::Fitter::busy(), SctArchiving::Archiver::busy(), SctAnalysis::AnalysisService::busy(), SctArchiving::Archiver::getStatus(), SctFitter::Fitter::internal_status(), SctAnalysis::AnalysisService::queueLength(), and SctArchiving::Archiver::~Archiver().

template<class T>
unsigned SctService::WorkerGroup< T >::getSleepTime  )  const [inline]
 

Get the sleep time.

Definition at line 78 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::waitForData().

template<class T>
void SctService::WorkerGroup< T >::go unsigned  nWorker = 1  )  [inline]
 

Start this worker group.

Parameters:
the number of worker threads to be started

Definition at line 226 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_thread_group.

Referenced by SctFitter::Fitter::go(), and SctAnalysis::AnalysisService::run().

template<class W>
void SctService::WorkerGroup< W >::join  )  [inline]
 

Call this to make sure that Worker threads continue to completion.

Definition at line 375 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_thread_group.

Referenced by SctService::WorkerGroup< T >::stop().

template<class W>
unsigned long SctService::WorkerGroup< W >::nDone  )  const [inline]
 

get the number of jobs done

Definition at line 359 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_nDone.

template<class W>
unsigned SctService::WorkerGroup< W >::nWorkers  )  const [inline]
 

get the number of workers in the map

Definition at line 364 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_workers.

Referenced by SctFitter::Fitter::getAverageTimePerScan(), SctArchiving::Archiver::getStatus(), and SctFitter::Fitter::internal_status().

template<class W>
unsigned SctService::WorkerGroup< W >::paused  )  const [inline]
 

get the number of paused threads

Definition at line 385 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_workers.

template<class W>
bool SctService::WorkerGroup< W >::popAndWork Worker worker  )  [inline, protected, virtual]
 

If the queue length != 0, removes an element from the queue and class work.

Returns:
false if the queue length is 0.

Definition at line 337 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::addTime(), SctService::WorkerGroup< T >::m_queue, SctService::WorkerGroup< T >::m_queueAccess, SctService::WorkerGroup< T >::Worker::setBusy(), SctService::WorkerGroup< T >::unlockedPopFrontOrBack(), and SctService::WorkerGroup< T >::work().

Here is the call graph for this function:

template<class T>
void SctService::WorkerGroup< T >::push DataType  ob  )  [inline]
 

Push an object onto the back of the queue.

Definition at line 308 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_queue, and SctService::WorkerGroup< T >::m_queueAccess.

Referenced by SctArchiving::Archiver::addCommand(), SctArchiving::Archiver::archiveISName(), SctFitter::Fitter::fitAll(), SctArchiving::Archiver::retrieveArchName(), and SctAnalysis::AnalysisService::scanResultCallback().

template<class W>
unsigned SctService::WorkerGroup< W >::queueSize  )  const [inline]
 

get the number of objects in the queue

Note:
for information only, not to be used in thread-control.

Definition at line 354 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_queue.

Referenced by SctArchiving::Archiver::getStatus(), SctFitter::Fitter::internal_status(), SctFitter::Fitter::queueLength(), SctArchiving::Archiver::queueLength(), and SctArchiving::Archiver::~Archiver().

template<class T>
void SctService::WorkerGroup< T >::reportInterval int  msec  )  [inline]
 

set interval in msec between reports

Definition at line 427 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_reportAccess, and SctService::WorkerGroup< T >::m_reportInterval.

template<class T>
void SctService::WorkerGroup< T >::reportThreadMain Reporter reporter  )  throw () [inline, protected, virtual]
 

thread which reports the progress of the workers to IS

Definition at line 255 of file WorkerGroup.h.

References Sct::ISUtilities::addOrUpdateOrThrow(), and Sct::Throwable::sendToMrs().

Referenced by SctService::WorkerGroup< T >::Reporter::operator()().

Here is the call graph for this function:

template<class T>
void SctService::WorkerGroup< T >::reportTo std::string  isname  ) 
 

set name to report to IS as

Definition at line 421 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_reportAccess, and SctService::WorkerGroup< T >::m_reportTo.

Referenced by SctAnalysis::AnalysisService::AnalysisService(), and SctArchiving::Archiver::Archiver().

template<class W>
void SctService::WorkerGroup< W >::setSleepTime unsigned  msec  )  [inline]
 

Set sleep time for all workers, if no items are on the queue.

Parameters:
sleep time in msec

Definition at line 403 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_sleeptime.

template<class W>
void SctService::WorkerGroup< W >::stop  )  [inline]
 

Raise flag to stop all worker threads.

Definition at line 369 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::join(), and SctService::WorkerGroup< T >::m_run.

Referenced by SctFitter::Fitter::shutdown(), and SctAnalysis::AnalysisService::shutdown().

Here is the call graph for this function:

template<class T>
void SctService::WorkerGroup< T >::threadMain Worker worker  )  throw () [inline, protected, virtual]
 

The threadMain method.

Can be overriden if sub-classes want to completly change the behaviour! Current implementation checks various flags for pausing, stopping etc, then calls popAndWork. If popAndWork returns false, then it calls waitForData

Definition at line 235 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::Worker::operator()().

template<class T>
T SctService::WorkerGroup< T >::unlockedPopFrontOrBack  )  [inline, protected]
 

gets from front or back of queue (no locking) depending on m_fifo

Definition at line 315 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::m_fifo, and SctService::WorkerGroup< T >::m_queue.

Referenced by SctService::WorkerGroup< T >::pop(), and SctService::WorkerGroup< T >::popAndWork().

template<class W>
void SctService::WorkerGroup< W >::wait int  msec  )  [inline, protected]
 

Causes this thread to sleep for msec milliseconds.

Definition at line 408 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::waitForData().

template<class W>
void SctService::WorkerGroup< W >::waitForData  )  [inline, protected, virtual]
 

Called if there is no data on the queue.

Calls wait in default implementation.

Definition at line 416 of file WorkerGroup.h.

References SctService::WorkerGroup< T >::getSleepTime(), and SctService::WorkerGroup< T >::wait().

Here is the call graph for this function:

template<class T>
virtual void SctService::WorkerGroup< T >::work DataType  data  )  throw () [protected, pure virtual]
 

To be overriden! Should actually do something with the data!

Implemented in SctFitter::FitterWorkerGroup.

Referenced by SctService::WorkerGroup< T >::popAndWork().


Friends And Related Function Documentation

template<class T>
friend class Reporter [friend]
 

timer watch

Definition at line 215 of file WorkerGroup.h.

template<class T>
friend class Worker [friend]
 

timer watch

Definition at line 214 of file WorkerGroup.h.


Field Documentation

template<class T>
bool SctService::WorkerGroup< T >::m_fifo [protected]
 

FIFO if true, otherwise FILO.

Definition at line 217 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::isFifo(), SctService::WorkerGroup< T >::setFifo(), and SctService::WorkerGroup< T >::unlockedPopFrontOrBack().

template<class T>
volatile unsigned long SctService::WorkerGroup< T >::m_nDone [protected]
 

count of number of jobs done

Definition at line 212 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::addTime(), and SctService::WorkerGroup< T >::nDone().

template<class T>
volatile bool SctService::WorkerGroup< T >::m_pause [protected]
 

does not execute work() if true

Definition at line 204 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::setPaused().

template<class T>
std::list<DataType> SctService::WorkerGroup< T >::m_queue [protected]
 

timer watch

Definition at line 197 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::popAndWork(), SctService::WorkerGroup< T >::push(), SctService::WorkerGroup< T >::queueSize(), and SctService::WorkerGroup< T >::unlockedPopFrontOrBack().

template<class T>
boost::mutex SctService::WorkerGroup< T >::m_queueAccess [mutable, protected]
 

queueLock

Definition at line 209 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::isFifo(), SctService::WorkerGroup< T >::pop(), SctService::WorkerGroup< T >::popAndWork(), SctService::WorkerGroup< T >::push(), and SctService::WorkerGroup< T >::setFifo().

template<class T>
boost::recursive_mutex SctService::WorkerGroup< T >::m_reportAccess [mutable, protected]
 

vectorOfWorkersLock

Definition at line 211 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::addTime(), SctService::WorkerGroup< T >::reportInterval(), and SctService::WorkerGroup< T >::reportTo().

template<class T>
volatile int SctService::WorkerGroup< T >::m_reportInterval [protected]
 

name to report to IS

Definition at line 207 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::reportInterval().

template<class T>
std::string SctService::WorkerGroup< T >::m_reportTo [protected]
 

name to report to IS

Definition at line 206 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::reportTo().

template<class T>
volatile bool SctService::WorkerGroup< T >::m_run [protected]
 

if false, threads are to stop.

Definition at line 203 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::stop().

template<class T>
volatile unsigned SctService::WorkerGroup< T >::m_sleeptime [protected]
 

time to sleep if queue is empty

Definition at line 205 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< DataType >::getSleepTime(), and SctService::WorkerGroup< T >::setSleepTime().

template<class T>
boost::thread_group SctService::WorkerGroup< T >::m_thread_group [protected]
 

boost thread group from which threads can be created

Definition at line 201 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::go(), and SctService::WorkerGroup< T >::join().

template<class T>
volatile double SctService::WorkerGroup< T >::m_time [protected]
 

time taken working

Definition at line 213 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::addTime().

template<class T>
boost::mutex SctService::WorkerGroup< T >::m_vectorAccess [mutable, protected]
 

vectorOfWorkersLock

Definition at line 210 of file WorkerGroup.h.

Referenced by SctService::WorkerGroup< T >::addWorker().


The documentation for this class was generated from the following file:
Generated on Mon Feb 6 14:09:57 2006 for SCT DAQ/DCS Software - C++ by  doxygen 1.4.6