osgEarth 2.1.1
|
Public Member Functions | |
TaskServiceManager (int numThreads=4) | |
void | setNumThreads (int numThreads) |
int | getNumThreads () const |
TaskService * | add (UID uid, float weight=1.0f) |
TaskService * | get (UID uid) const |
TaskService * | getOrAdd (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 |
Manages a pool of TaskService objects, automatically allocating threads among them based on a weighting metric.
Definition at line 203 of file TaskService.
typedef std::map< UID, WeightedTaskService > osgEarth::TaskServiceManager::TaskServiceMap [private] |
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.
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 }
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; } }
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; }
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 ); }
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; } }
void TaskServiceManager::remove | ( | UID | uid | ) |
Definition at line 451 of file TaskService.cpp.
{ ScopedLock<Mutex> lock( _taskServiceMgrMutex ); _services.erase( uid ); reallocate( _targetNumThreads ); }
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; } } }
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 ); }
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; } } }
int osgEarth::TaskServiceManager::_numThreads [private] |
Definition at line 258 of file TaskService.
Definition at line 257 of file TaskService.
int osgEarth::TaskServiceManager::_targetNumThreads [private] |
Definition at line 258 of file TaskService.
OpenThreads::Mutex osgEarth::TaskServiceManager::_taskServiceMgrMutex [private] |
Definition at line 259 of file TaskService.