|
reSIProcate/repro
9694
|
A generic thread-bank class. More...
#include <Dispatcher.hxx>

Public Member Functions | |
| Dispatcher (std::auto_ptr< Worker > prototype, resip::SipStack *stack, int workers=2, bool startImmediately=true) | |
| virtual | ~Dispatcher () |
| virtual bool | post (std::auto_ptr< resip::ApplicationMessage > &work) |
| Posts a message to this thread bank. | |
| size_t | fifoCountDepth () const |
| time_t | fifoTimeDepth () const |
| int | workPoolSize () const |
| void | stop () |
| This Dispatcher will stop accepting new work, but processing will continue normally on the messages already in the queue. | |
| void | resume () |
| Resumes accepting work. | |
| void | shutdownAll () |
| Shuts down the thread-bank. | |
| void | startAll () |
| Starts the thread bank. | |
Public Attributes | |
| resip::SipStack * | mStack |
Protected Attributes | |
| resip::TimeLimitFifo < resip::ApplicationMessage > | mFifo |
| bool | mAcceptingWork |
| bool | mShutdown |
| bool | mStarted |
| Worker * | mWorkerPrototype |
| resip::RWMutex | mMutex |
| std::vector< WorkerThread * > | mWorkerThreads |
Private Member Functions | |
| Dispatcher (const Dispatcher &toCopy) | |
| Dispatcher & | operator= (const Dispatcher &toCopy) |
A generic thread-bank class.
This is intended to be a generic thread-bank class that operates under the paradigm of accepting messages, modifying those messages, and posting the messages back to the originator through the SipStack. (Since this uses ApplicationMessages, the message will automatically get back to the correct TransactionUser; what happens from there is the coder's business.)
This class gets generality by using prototyping of a very simple class, Worker. All the user must do is subclass Worker to do the work needed, and pass an instance of this class when constructing the Dispatcher. Dispatcher will clone this Worker as many times as needed to fill the thread bank.
Definition at line 41 of file Dispatcher.hxx.
| repro::Dispatcher::Dispatcher | ( | std::auto_ptr< Worker > | prototype, |
| resip::SipStack * | stack, | ||
| int | workers = 2, |
||
| bool | startImmediately = true |
||
| ) |
| prototype | The prototypical instance of Worker. |
| stack | The stack to post messages to. |
| workers | The number of threads in this bank. |
| startImmediately | Whether to start this thread bank on construction. |
Definition at line 10 of file Dispatcher.cxx.
: mStack(stack), mFifo(0,0), mAcceptingWork(false), mShutdown(false), mStarted(false), mWorkerPrototype(prototype.release()) { for(int i=0; i<workers;i++) { mWorkerThreads.push_back(new WorkerThread(mWorkerPrototype->clone(),mFifo,mStack)); } if(startImmediately) { startAll(); } }
| repro::Dispatcher::~Dispatcher | ( | ) | [virtual] |
Definition at line 32 of file Dispatcher.cxx.
{
shutdownAll();
std::vector<WorkerThread*>::iterator i;
for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
{
delete *i;
}
mWorkerThreads.clear();
while(!mFifo.empty())
{
delete mFifo.getNext();
}
delete mWorkerPrototype;
}
| repro::Dispatcher::Dispatcher | ( | const Dispatcher & | toCopy | ) | [private] |
| size_t repro::Dispatcher::fifoCountDepth | ( | ) | const |
Definition at line 72 of file Dispatcher.cxx.
{
return mFifo.getCountDepth();
}
| time_t repro::Dispatcher::fifoTimeDepth | ( | ) | const |
Definition at line 78 of file Dispatcher.cxx.
{
return mFifo.getTimeDepth();
}
| Dispatcher& repro::Dispatcher::operator= | ( | const Dispatcher & | toCopy | ) | [private] |
| bool repro::Dispatcher::post | ( | std::auto_ptr< resip::ApplicationMessage > & | work | ) | [virtual] |
Posts a message to this thread bank.
| work | The message that conveys the work that needs to be done. It is understood that any information that needs to be conveyed back to the originator will be placed in the message by the Workers in the thread bank. |
Definition at line 54 of file Dispatcher.cxx.
{
resip::ReadLock r(mMutex);
if(mAcceptingWork)
{
mFifo.add(work.release(),
resip::TimeLimitFifo<resip::ApplicationMessage>::InternalElement);
return true;
}
return false;
//If we aren't accepting work, the auto ptr is not released. (We don't
// take ownership, and the caller gets to handle the contents of the
// auto_ptr)
}
| void repro::Dispatcher::resume | ( | ) |
Resumes accepting work.
Definition at line 97 of file Dispatcher.cxx.
{
resip::WriteLock w(mMutex);
mAcceptingWork = !mShutdown;
}
| void repro::Dispatcher::shutdownAll | ( | ) |
Shuts down the thread-bank.
Definition at line 104 of file Dispatcher.cxx.
{
resip::WriteLock w(mMutex);
if(!mShutdown)
{
mAcceptingWork=false;
mShutdown=true;
std::vector<WorkerThread*>::iterator i;
for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
{
(*i)->shutdown();
(*i)->join();
}
}
}
| void repro::Dispatcher::startAll | ( | ) |
Starts the thread bank.
Definition at line 122 of file Dispatcher.cxx.
{
resip::WriteLock w(mMutex);
if(!mShutdown && !mStarted)
{
std::vector<WorkerThread*>::iterator i;
for(i=mWorkerThreads.begin(); i!=mWorkerThreads.end(); ++i)
{
(*i)->run();
}
mStarted=true;
mAcceptingWork=true;
}
}
| void repro::Dispatcher::stop | ( | ) |
This Dispatcher will stop accepting new work, but processing will continue normally on the messages already in the queue.
Definition at line 90 of file Dispatcher.cxx.
{
resip::WriteLock w(mMutex);
mAcceptingWork=false;
}
| int repro::Dispatcher::workPoolSize | ( | ) | const |
Definition at line 84 of file Dispatcher.cxx.
{
return (int)mWorkerThreads.size();
}
bool repro::Dispatcher::mAcceptingWork [protected] |
Definition at line 120 of file Dispatcher.hxx.
Definition at line 119 of file Dispatcher.hxx.
resip::RWMutex repro::Dispatcher::mMutex [protected] |
Definition at line 125 of file Dispatcher.hxx.
bool repro::Dispatcher::mShutdown [protected] |
Definition at line 121 of file Dispatcher.hxx.
Definition at line 114 of file Dispatcher.hxx.
bool repro::Dispatcher::mStarted [protected] |
Definition at line 122 of file Dispatcher.hxx.
Worker* repro::Dispatcher::mWorkerPrototype [protected] |
Definition at line 123 of file Dispatcher.hxx.
std::vector<WorkerThread*> repro::Dispatcher::mWorkerThreads [protected] |
Definition at line 127 of file Dispatcher.hxx.
1.7.5.1