osgEarth 2.1.1
|
Public Member Functions | |
TaskService (const std::string &name="", int numThreads=4) | |
void | add (TaskRequest *request) |
void | setName (const std::string &value) |
const std::string & | getName () const |
int | getStamp () const |
void | setStamp (int stamp) |
int | getNumThreads () const |
void | setNumThreads (int numThreads) |
unsigned int | getNumRequests () const |
Private Types | |
typedef std::list< TaskThread * > | TaskThreads |
Private Member Functions | |
void | adjustThreadCount () |
void | removeFinishedThreads () |
virtual | ~TaskService () |
Private Attributes | |
OpenThreads::ReentrantMutex | _threadMutex |
TaskThreads | _threads |
osg::ref_ptr< TaskRequestQueue > | _queue |
int | _numThreads |
int | _lastRemoveFinishedThreadsStamp |
std::string | _name |
Manages a priority task queue and associated thread pool.
Definition at line 164 of file TaskService.
typedef std::list<TaskThread*> osgEarth::TaskService::TaskThreads [private] |
Definition at line 190 of file TaskService.
TaskService::TaskService | ( | const std::string & | name = "" , |
int | numThreads = 4 |
||
) |
Definition at line 242 of file TaskService.cpp.
: osg::Referenced( true ), _lastRemoveFinishedThreadsStamp(0), _name(name) { _queue = new TaskRequestQueue(); setNumThreads( numThreads ); }
TaskService::~TaskService | ( | ) | [private, virtual] |
void TaskService::add | ( | TaskRequest * | request | ) |
Definition at line 258 of file TaskService.cpp.
{ //OE_INFO << LC << "TS [" << _name << "] adding request [" << request->getName() << "]" << std::endl; _queue->add( request ); }
void TaskService::adjustThreadCount | ( | ) | [private] |
Definition at line 315 of file TaskService.cpp.
{ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_threadMutex); removeFinishedThreads(); int numActiveThreads = 0; for( TaskThreads::iterator i = _threads.begin(); i != _threads.end(); i++ ) { if (!(*i)->getDone()) numActiveThreads++; } int diff = _numThreads - numActiveThreads; if (diff > 0) { OE_DEBUG << LC << "Adding " << diff << " threads to TaskService " << std::endl; //We need to add some threads for (int i = 0; i < diff; ++i) { TaskThread* thread = new TaskThread( _queue.get() ); _threads.push_back( thread ); thread->start(); } } else if (diff < 0) { diff = osg::absolute( diff ); OE_DEBUG << LC << "Removing " << diff << " threads from TaskService " << std::endl; int numRemoved = 0; //We need to remove some threads for( TaskThreads::iterator i = _threads.begin(); i != _threads.end(); i++ ) { if (!(*i)->getDone()) { (*i)->setDone( true ); numRemoved++; if (numRemoved == diff) break; } } } OE_INFO << LC << "TaskService [" << _name << "] using " << _numThreads << " threads" << std::endl; }
const std::string& osgEarth::TaskService::getName | ( | ) | const [inline] |
Definition at line 172 of file TaskService.
{ return _name; }
unsigned int TaskService::getNumRequests | ( | ) | const |
Gets the number of requets left in the queue
Definition at line 252 of file TaskService.cpp.
{ return _queue->getNumRequests(); }
int TaskService::getNumThreads | ( | ) | const |
Definition at line 299 of file TaskService.cpp.
{ return _numThreads; }
int TaskService::getStamp | ( | ) | const |
Definition at line 281 of file TaskService.cpp.
{ return _queue->getStamp(); }
void TaskService::removeFinishedThreads | ( | ) | [private] |
Definition at line 358 of file TaskService.cpp.
{ OpenThreads::ScopedLock<OpenThreads::ReentrantMutex> lock(_threadMutex); unsigned int numRemoved = 0; for (TaskThreads::iterator i = _threads.begin(); i != _threads.end();) { //Erase the threads are not running if (!(*i)->isRunning()) { i = _threads.erase( i ); numRemoved++; } else { i++; } } if (numRemoved > 0) { OE_DEBUG << LC << "Removed " << numRemoved << " finished threads " << std::endl; } }
void osgEarth::TaskService::setName | ( | const std::string & | value | ) | [inline] |
Definition at line 171 of file TaskService.
{ _name = value; }
void TaskService::setNumThreads | ( | int | numThreads | ) |
Definition at line 305 of file TaskService.cpp.
{ if ( _numThreads != numThreads ) { _numThreads = osg::maximum(1, numThreads); adjustThreadCount(); } }
void TaskService::setStamp | ( | int | stamp | ) |
Definition at line 287 of file TaskService.cpp.
{ _queue->setStamp( stamp ); //Remove finished threads every 60 frames if (stamp - _lastRemoveFinishedThreadsStamp > 60) { removeFinishedThreads(); _lastRemoveFinishedThreadsStamp = stamp; } }
int osgEarth::TaskService::_lastRemoveFinishedThreadsStamp [private] |
Definition at line 194 of file TaskService.
std::string osgEarth::TaskService::_name [private] |
Definition at line 195 of file TaskService.
int osgEarth::TaskService::_numThreads [private] |
Definition at line 193 of file TaskService.
osg::ref_ptr<TaskRequestQueue> osgEarth::TaskService::_queue [private] |
Definition at line 192 of file TaskService.
OpenThreads::ReentrantMutex osgEarth::TaskService::_threadMutex [private] |
Definition at line 189 of file TaskService.
TaskThreads osgEarth::TaskService::_threads [private] |
Definition at line 191 of file TaskService.