osgEarth 2.1.1
Public Member Functions | Private Types | Private Member Functions | Private Attributes

osgEarth::TaskService Class Reference

Inheritance diagram for osgEarth::TaskService:
Collaboration diagram for osgEarth::TaskService:

List of all members.

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

Detailed Description

Manages a priority task queue and associated thread pool.

Definition at line 164 of file TaskService.


Member Typedef Documentation

typedef std::list<TaskThread*> osgEarth::TaskService::TaskThreads [private]

Definition at line 190 of file TaskService.


Constructor & Destructor Documentation

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 );
}

Here is the call graph for this function:

TaskService::~TaskService ( ) [private, virtual]

Definition at line 264 of file TaskService.cpp.

{
    _queue->setDone();

    for( TaskThreads::iterator i = _threads.begin(); i != _threads.end(); i++ )
    {
        (*i)->setDone(true);
    }

    for( TaskThreads::iterator i = _threads.begin(); i != _threads.end(); i++ )
    {
        (*i)->cancel();
        delete (*i);
    }
}

Member Function Documentation

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 );
}

Here is the caller graph for this function:

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;
}

Here is the call graph for this function:

Here is the caller graph for this function:

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;
    }
}

Here is the caller graph for this function:

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();
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

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;
    }
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 194 of file TaskService.

std::string osgEarth::TaskService::_name [private]

Definition at line 195 of file TaskService.

Definition at line 193 of file TaskService.

Definition at line 192 of file TaskService.

OpenThreads::ReentrantMutex osgEarth::TaskService::_threadMutex [private]

Definition at line 189 of file TaskService.

Definition at line 191 of file TaskService.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines