Replaced use of while(isRunning()) { YieldCurrentThread(); } style loops with use of join() to avoid false positives being reported by valgrind when using the helgrind tool for thread debugging.

git-svn-id: http://svn.openscenegraph.org/osg/OpenSceneGraph/trunk@14460 16af8721-9629-0410-8352-f15c8da7e697
This commit is contained in:
Robert Osfield 2014-11-04 10:46:59 +00:00
parent 997ee30039
commit 28a676e105
17 changed files with 195 additions and 233 deletions

View File

@ -59,10 +59,10 @@ class ViewerFrameThread : public OpenThreads::Thread
~ViewerFrameThread() ~ViewerFrameThread()
{ {
cancel(); if (isRunning())
while(isRunning())
{ {
OpenThreads::Thread::YieldCurrentThread(); cancel();
join();
} }
} }

View File

@ -42,7 +42,11 @@ public:
{ {
_done = true; _done = true;
while(isRunning()) OpenThreads::Thread::YieldCurrentThread(); if (isRunning())
{
cancel();
join();
}
} }
void addFileName(const std::string& filename) void addFileName(const std::string& filename)

View File

@ -475,9 +475,10 @@ public:
~NotifyThread() ~NotifyThread()
{ {
_done = true; _done = true;
while(isRunning()) if (isRunning())
{ {
OpenThreads::Thread::YieldCurrentThread(); cancel();
join();
} }
} }

View File

@ -365,9 +365,6 @@ int main(int argc, char** argv)
} }
thread.cancel(); thread.cancel();
while (thread.isRunning()) thread.join();
{
OpenThreads::Thread::YieldCurrentThread();
}
} }

View File

@ -177,8 +177,11 @@ CRenderingThread::CRenderingThread( cOSG* ptr )
CRenderingThread::~CRenderingThread() CRenderingThread::~CRenderingThread()
{ {
_done = true; _done = true;
while( isRunning() ) if (isRunning())
OpenThreads::Thread::YieldCurrentThread(); {
cancel();
join();
}
} }
void CRenderingThread::run() void CRenderingThread::run()

View File

@ -201,7 +201,7 @@ class OSG_EXPORT OperationThread : public Referenced, public OpenThreads::Thread
void setDone(bool done); void setDone(bool done);
bool getDone() const { return _done; } bool getDone() const { return _done!=0; }
/** Cancel this graphics thread.*/ /** Cancel this graphics thread.*/
virtual int cancel(); virtual int cancel();
@ -212,7 +212,7 @@ class OSG_EXPORT OperationThread : public Referenced, public OpenThreads::Thread
observer_ptr<Object> _parent; observer_ptr<Object> _parent;
bool _done; OpenThreads::Atomic _done;
OpenThreads::Mutex _threadMutex; OpenThreads::Mutex _threadMutex;
osg::ref_ptr<OperationQueue> _operationQueue; osg::ref_ptr<OperationQueue> _operationQueue;

View File

@ -456,6 +456,10 @@ Thread::~Thread()
// Kill the thread when it is destructed // Kill the thread when it is destructed
// //
cancel(); cancel();
// wait till the thread is stopped before finishing.
join();
} }
delete pd; delete pd;

View File

@ -82,6 +82,8 @@ Thread::~Thread()
{ {
std::cout<<"Error: Thread "<< this <<" still running in destructor"<<std::endl; std::cout<<"Error: Thread "<< this <<" still running in destructor"<<std::endl;
cancel(); cancel();
join();
} }
delete pd; delete pd;
_prvData = 0; _prvData = 0;

View File

@ -380,9 +380,7 @@ Thread::~Thread()
// //
cancel(); cancel();
while (pd->isRunning == true) { join();
::usleep(1);
}
} }

View File

@ -276,6 +276,8 @@ Thread::~Thread()
std::cout<<"Error: Thread "<<this<<" still running in destructor"<<std::endl; std::cout<<"Error: Thread "<<this<<" still running in destructor"<<std::endl;
pd->cancelMode = 0; pd->cancelMode = 0;
cancel(); cancel();
join();
} }
delete pd; delete pd;

View File

@ -264,7 +264,7 @@ void OperationQueue::removeOperationThread(OperationThread* thread)
OperationThread::OperationThread(): OperationThread::OperationThread():
osg::Referenced(true), osg::Referenced(true),
_parent(0), _parent(0),
_done(false) _done(0)
{ {
setOperationQueue(new OperationQueue); setOperationQueue(new OperationQueue);
} }
@ -293,9 +293,10 @@ void OperationThread::setOperationQueue(OperationQueue* opq)
void OperationThread::setDone(bool done) void OperationThread::setDone(bool done)
{ {
if (_done==done) return; unsigned d = done?0:1;
if (_done==d) return;
_done = true; _done.exchange(d);
if (done) if (done)
{ {
@ -322,7 +323,7 @@ int OperationThread::cancel()
if( isRunning() ) if( isRunning() )
{ {
_done = true; _done.exchange(1);
OSG_INFO<<" Doing cancel "<<this<<std::endl; OSG_INFO<<" Doing cancel "<<this<<std::endl;
@ -339,27 +340,7 @@ int OperationThread::cancel()
} }
// then wait for the the thread to stop running. // then wait for the the thread to stop running.
while(isRunning()) join();
{
#if 1
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadMutex);
if (_operationQueue.valid())
{
_operationQueue->releaseOperationsBlock();
// _operationQueue->releaseAllOperations();
}
if (_currentOperation.valid()) _currentOperation->release();
}
#endif
// commenting out debug info as it was cashing crash on exit, presumable
// due to OSG_NOTIFY or std::cout destructing earlier than this destructor.
OSG_DEBUG<<" Waiting for OperationThread to cancel "<<this<<std::endl;
OpenThreads::Thread::YieldCurrentThread();
}
} }
OSG_INFO<<" OperationThread::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl; OSG_INFO<<" OperationThread::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl;

View File

@ -662,14 +662,7 @@ int DatabasePager::DatabaseThread::cancel()
break; break;
} }
// then wait for the the thread to stop running. join();
while(isRunning())
{
// commenting out debug info as it was cashing crash on exit, presumable
// due to OSG_NOTICE or std::cout destructing earlier than this destructor.
// OSG_INFO<<"Waiting for DatabasePager::DatabaseThread to cancel"<<std::endl;
OpenThreads::Thread::YieldCurrentThread();
}
// _startThreadCalled = false; // _startThreadCalled = false;
} }

View File

@ -158,17 +158,11 @@ int ImagePager::ImageThread::cancel()
// _databasePagerThreadBlock->release(); // _databasePagerThreadBlock->release();
// then wait for the the thread to stop running. // then wait for the the thread to stop running.
while(isRunning()) join();
{
// commenting out debug info as it was cashing crash on exit, presumable
// due to osg::notify or std::cout destructing earlier than this destructor.
// OSG_DEBUG<<"Waiting for DatabasePager to cancel"<<std::endl;
OpenThreads::Thread::YieldCurrentThread();
}
// _startThreadCalled = false; // _startThreadCalled = false;
} }
//std::cout<<"DatabasePager::~DatabasePager() stopped running"<<std::endl; //std::cout<<"ImagePager::cancel() thread stopped running"<<std::endl;
return result; return result;
} }

View File

@ -152,19 +152,6 @@ RenderSurface::RenderSurface( void )
RenderSurface::~RenderSurface( void ) RenderSurface::~RenderSurface( void )
{ {
#if 0
cancel();
_fini();
while (isRunning())
{
//std::cout << "waiting for RenderSurface to cancel"<<std::endl;
OpenThreads::Thread::YieldCurrentThread();
}
delete _threadReady;
#endif
} }

View File

@ -95,11 +95,10 @@ public:
virtual void quit( bool waitForThreadToExit=true ) virtual void quit( bool waitForThreadToExit=true )
{ {
_done = true; _done = true;
if ( waitForThreadToExit ) if (isRunning() && waitForThreadToExit)
{ {
while( isRunning() ) cancel();
OpenThreads::Thread::YieldCurrentThread(); join();
OSG_DEBUG<<"GifImageStream thread quitted"<<std::endl;
} }
} }

View File

@ -120,13 +120,9 @@ void QuicktimeImageStream::quit(bool wiatForThreadToExit)
OSG_DEBUG<<"Sending quit"<<this<<std::endl; OSG_DEBUG<<"Sending quit"<<this<<std::endl;
setCmd(THREAD_QUIT); setCmd(THREAD_QUIT);
if (wiatForThreadToExit) if (isRunning() && wiatForThreadToExit)
{ {
while(isRunning()) join();
{
OSG_DEBUG<<"Waiting for QuicktimeImageStream to quit"<<this<<std::endl;
OpenThreads::Thread::YieldCurrentThread();
}
OSG_DEBUG<<"QuicktimeImageStream has quit"<<this<<std::endl; OSG_DEBUG<<"QuicktimeImageStream has quit"<<this<<std::endl;
} }
} }

View File

@ -73,9 +73,10 @@ class LibVncImage : public osgWidget::VncImage
virtual ~RfbThread() virtual ~RfbThread()
{ {
_done = true; _done = true;
while(isRunning()) if (isRunning())
{ {
OpenThreads::Thread::YieldCurrentThread(); cancel();
join();
} }
} }