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

osgEarth::TaskServiceManager Class Reference

Inheritance diagram for osgEarth::TaskServiceManager:
Collaboration diagram for osgEarth::TaskServiceManager:

List of all members.

Public Member Functions

 TaskServiceManager (int numThreads=4)
void setNumThreads (int numThreads)
int getNumThreads () const
TaskServiceadd (UID uid, float weight=1.0f)
TaskServiceget (UID uid) const
TaskServicegetOrAdd (UID uid, float weight=1.0f)
void remove (TaskService *service)
void remove (UID uid)
void setWeight (TaskService *service, float weight)

Private Types

typedef std::pair
< osg::ref_ptr< TaskService >
, float > 
WeightedTaskService
typedef std::map< UID,
WeightedTaskService
TaskServiceMap

Private Member Functions

void reallocate (int targetNumThreads)

Private Attributes

TaskServiceMap _services
int _numThreads
int _targetNumThreads
OpenThreads::Mutex _taskServiceMgrMutex

Detailed Description

Manages a pool of TaskService objects, automatically allocating threads among them based on a weighting metric.

Definition at line 203 of file TaskService.


Member Typedef Documentation

Definition at line 256 of file TaskService.

typedef std::pair< osg::ref_ptr<TaskService>, float > osgEarth::TaskServiceManager::WeightedTaskService [private]

Definition at line 255 of file TaskService.


Constructor & Destructor Documentation

TaskServiceManager::TaskServiceManager ( int  numThreads = 4)

Creates a new manager, and sets the target number of threads to allocate across all managed task services.

Definition at line 383 of file TaskService.cpp.

                                                       :
_numThreads( 0 ),
_targetNumThreads( numThreads )
{
    //nop
}

Member Function Documentation

TaskService * TaskServiceManager::add ( UID  uid,
float  weight = 1.0f 
)

Adds a new task service to the manager and reallocates the thread pool across the remaining services.

Definition at line 397 of file TaskService.cpp.

{
    ScopedLock<Mutex> lock( _taskServiceMgrMutex );

    if ( weight <= 0.0f )
        weight = 0.001;

    TaskServiceMap::iterator i = _services.find( uid );
    if ( i != _services.end() )
    {
        i->second.second = weight;
        reallocate( _targetNumThreads );
        return i->second.first.get();
    }
    else
    {
        TaskService* newService = new TaskService( "", 1 );
        _services[uid] = WeightedTaskService( newService, weight );
        reallocate( _targetNumThreads );
        return newService;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

TaskService * TaskServiceManager::get ( UID  uid) const

Gets the names task service.

Definition at line 421 of file TaskService.cpp.

{
    ScopedLock<Mutex> lock( const_cast<TaskServiceManager*>(this)->_taskServiceMgrMutex );
    TaskServiceMap::const_iterator i = _services.find(uid);
    return i != _services.end() ? i->second.first.get() : 0L;
}

Here is the caller graph for this function:

int osgEarth::TaskServiceManager::getNumThreads ( ) const [inline]

Gets the total number of threads allocated across all managed task services.

Definition at line 223 of file TaskService.

{ return _numThreads; }
TaskService * TaskServiceManager::getOrAdd ( UID  uid,
float  weight = 1.0f 
)

Gets a task service by name, creating it if it does not yet exist

Definition at line 429 of file TaskService.cpp.

{
    TaskService* service = get( uid );
    return service ? service : add( uid, weight );
}

Here is the call graph for this function:

void TaskServiceManager::reallocate ( int  targetNumThreads) [private]

Definition at line 481 of file TaskService.cpp.

{
    // first, total up all the weights.
    float totalWeight = 0.0f;
    for( TaskServiceMap::const_iterator i = _services.begin(); i != _services.end(); ++i )
        totalWeight += i->second.second;

    // next divide the total thread pool size by the relative weight of each service.
    _numThreads = 0;
    for( TaskServiceMap::const_iterator i = _services.begin(); i != _services.end(); ++i )
    {
        int threads = osg::maximum( 1, (int)( (float)_targetNumThreads * (i->second.second / totalWeight) ) );
        i->second.first->setNumThreads( threads );
        _numThreads += threads;
    }
}

Here is the caller graph for this function:

void TaskServiceManager::remove ( UID  uid)

Definition at line 451 of file TaskService.cpp.

{
    ScopedLock<Mutex> lock( _taskServiceMgrMutex );
    _services.erase( uid );
    reallocate( _targetNumThreads );
}

Here is the call graph for this function:

void TaskServiceManager::remove ( TaskService service)

Removes a task service from management, and reallocates the thread pool across the remaining services.

Definition at line 436 of file TaskService.cpp.

{
    ScopedLock<Mutex> lock( _taskServiceMgrMutex );
    for( TaskServiceMap::iterator i = _services.begin(); i != _services.end(); ++i )
    {
        if ( i->second.first.get() == service ) 
        {
            _services.erase( i );
            reallocate( _targetNumThreads );
            break;
        }
    }
}

Here is the call graph for this function:

void TaskServiceManager::setNumThreads ( int  numThreads)

Sets a new total target thread count to allocate across all task services under management. (The actual thread count may be higher since each service is guaranteed at least one thread.)

Definition at line 391 of file TaskService.cpp.

{
    reallocate( numThreads );
}

Here is the call graph for this function:

void TaskServiceManager::setWeight ( TaskService service,
float  weight 
)

Assigned a weight value to a particular task service. The manager will re-distribute available threads for all registered task services.

Definition at line 459 of file TaskService.cpp.

{
    ScopedLock<Mutex> lock( _taskServiceMgrMutex );

    if ( weight <= 0.0f )
        weight = 0.001;

    if ( !service )
        return;

    for( TaskServiceMap::iterator i = _services.begin(); i != _services.end(); ++i )
    {
        if ( i->second.first.get() == service )
        {
            i->second.second = weight;
            reallocate( _targetNumThreads );
            break;
        }
    }    
}

Here is the call graph for this function:


Member Data Documentation

Definition at line 258 of file TaskService.

Definition at line 257 of file TaskService.

Definition at line 258 of file TaskService.

Definition at line 259 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