Skip to content

Commit

Permalink
Make the worker threads clean up properly
Browse files Browse the repository at this point in the history
Seems our WorkerThreads never came off the m_lstThreads deque, and also never
hit the destructor.  Fixed this in the ThreadTerminating function, and using a
"deleteLater"

Also added a bit more logging to help track the issue.
(cherry picked from commit 6a1b7e3)
  • Loading branch information
Beirdo committed Jan 16, 2011
1 parent 798359f commit 945c673
Showing 1 changed file with 26 additions and 24 deletions.
50 changes: 26 additions & 24 deletions mythtv/libs/libmythupnp/threadpool.cpp
Expand Up @@ -367,15 +367,13 @@ WorkerThread *ThreadPool::GetWorkerThread()
// --------------------------------------------------------------

m_mList.lock();
if ( m_lstAvailableThreads.size() > 0)
{
if ( m_lstAvailableThreads.size() > 0)
{
pThread = m_lstAvailableThreads.front();
m_lstAvailableThreads.pop_front();
}

nThreadCount = m_lstThreads.size();
pThread = m_lstAvailableThreads.front();
m_lstAvailableThreads.pop_front();
}

nThreadCount = m_lstThreads.size();
m_mList.unlock();

if (pThread == NULL)
Expand Down Expand Up @@ -411,6 +409,7 @@ WorkerThread *ThreadPool::GetWorkerThread()
WorkerThread *ThreadPool::AddWorkerThread( bool bMakeAvailable, long nTimeout )
{
QString sName = m_sName + "_WorkerThread";
long nThreadCount;

VERBOSE( VB_UPNP, QString( "ThreadPool:AddWorkerThread - %1" ).arg( sName ));

Expand All @@ -428,23 +427,21 @@ WorkerThread *ThreadPool::AddWorkerThread( bool bMakeAvailable, long nTimeout )
// ------------------------------------------------------

m_mList.lock();
{
m_lstThreads.push_back( pThread );
nThreadCount = m_lstThreads.size();

m_lstThreads.push_back( pThread );

if (bMakeAvailable)
{
m_lstAvailableThreads.push_back( pThread );
VERBOSE(VB_IMPORTANT|VB_EXTRA, QString("ThreadPool:%1: thread pool size %2") .arg(m_sName) .arg(nThreadCount));

m_threadAvail.wakeAll();
}
if (bMakeAvailable)
{
m_lstAvailableThreads.push_back( pThread );
m_threadAvail.wakeAll();
}
m_mList.unlock();

}
else
{

// ------------------------------------------------------
// It's taking longer than 5 seconds to initialize this thread....
// give up on it.
Expand Down Expand Up @@ -478,16 +475,21 @@ void ThreadPool::ThreadAvailable ( WorkerThread *pThread )

void ThreadPool::ThreadTerminating ( WorkerThread *pThread )
{
long nThreadCount;

m_mList.lock();
{
WorkerThreadList::iterator it =
find(m_lstAvailableThreads.begin(),
m_lstAvailableThreads.end(), pThread);
m_lstAvailableThreads.erase(it);
WorkerThreadList::iterator it = find(m_lstAvailableThreads.begin(),
m_lstAvailableThreads.end(), pThread);
m_lstAvailableThreads.erase(it);

it = find(m_lstThreads.begin(), m_lstThreads.end(), pThread);
m_lstThreads.erase(it);

nThreadCount = m_lstThreads.size();
VERBOSE(VB_IMPORTANT|VB_EXTRA, QString("ThreadPool:%1: thread pool size %2") .arg(m_sName) .arg(nThreadCount));

pThread->deleteLater();

// Need to leave in m_lstThreads so that we can
// delete the ptr in destructor
}
m_mList.unlock();
}

0 comments on commit 945c673

Please sign in to comment.