diff options
Diffstat (limited to 'cpp/src/Ice/ThreadPool.cpp')
-rw-r--r-- | cpp/src/Ice/ThreadPool.cpp | 114 |
1 files changed, 62 insertions, 52 deletions
diff --git a/cpp/src/Ice/ThreadPool.cpp b/cpp/src/Ice/ThreadPool.cpp index ab41198104c..63fc22a7f11 100644 --- a/cpp/src/Ice/ThreadPool.cpp +++ b/cpp/src/Ice/ThreadPool.cpp @@ -554,74 +554,84 @@ IceInternal::ThreadPool::run() handler->_serializing = false; } - // - // First we reap threads that have been destroyed before. - // - int sz = static_cast<int>(_threads.size()); - assert(_running <= sz); - if(_running < sz) + if(_size < _sizeMax) // Dynamic thread pool { - vector<IceUtil::ThreadPtr>::iterator start = - partition(_threads.begin(), _threads.end(), IceUtil::constMemFun(&IceUtil::Thread::isAlive)); - - for(vector<IceUtil::ThreadPtr>::iterator p = start; p != _threads.end(); ++p) + // + // First we reap threads that have been destroyed before. + // + int sz = static_cast<int>(_threads.size()); + assert(_running <= sz); + if(_running < sz) { - (*p)->getThreadControl().join(); - } + vector<IceUtil::ThreadPtr>::iterator start = + partition(_threads.begin(), _threads.end(), + IceUtil::constMemFun(&IceUtil::Thread::isAlive)); - _threads.erase(start, _threads.end()); - } - - // - // Now we check if this thread can be destroyed, based - // on a load factor. - // + for(vector<IceUtil::ThreadPtr>::iterator p = start; p != _threads.end(); ++p) + { + (*p)->getThreadControl().join(); + } - // - // The load factor jumps immediately to the number of - // threads that are currently in use, but decays - // exponentially if the number of threads in use is - // smaller than the load factor. This reflects that we - // create threads immediately when they are needed, - // but want the number of threads to slowly decline to - // the configured minimum. - // - double inUse = static_cast<double>(_inUse); - if(_load < inUse) - { - _load = inUse; - } - else - { - const double loadFactor = 0.05; // TODO: Configurable? - const double oneMinusLoadFactor = 1 - loadFactor; - _load = _load * oneMinusLoadFactor + inUse * loadFactor; - } + _threads.erase(start, _threads.end()); + } - if(_running > _size) - { - int load = static_cast<int>(_load + 0.5); + // + // Now we check if this thread can be destroyed, based + // on a load factor. + // // - // We add one to the load factor because on - // additional thread is needed for select(). + // The load factor jumps immediately to the number of + // threads that are currently in use, but decays + // exponentially if the number of threads in use is + // smaller than the load factor. This reflects that we + // create threads immediately when they are needed, + // but want the number of threads to slowly decline to + // the configured minimum. // - if(load + 1 < _running) + double inUse = static_cast<double>(_inUse); + if(_load < inUse) + { + _load = inUse; + } + else { - assert(_inUse > 0); - --_inUse; + const double loadFactor = 0.05; // TODO: Configurable? + const double oneMinusLoadFactor = 1 - loadFactor; + _load = _load * oneMinusLoadFactor + inUse * loadFactor; + } + + if(_running > _size) + { + int load = static_cast<int>(_load + 0.5); + + // + // We add one to the load factor because on + // additional thread is needed for select(). + // + if(load + 1 < _running) + { + assert(_inUse > 0); + --_inUse; - assert(_running > 0); - --_running; + assert(_running > 0); + --_running; - return false; + return false; + } } } - + assert(_inUse > 0); --_inUse; } - + + // + // Do not wait to be promoted again to release these objects. + // + handler = 0; + workItem = 0; + while(!_promote) { wait(); |