Added better control for cancel GraphicsThreads.

This commit is contained in:
Robert Osfield
2005-11-10 15:25:06 +00:00
parent b19c005178
commit c2f1527fe0
3 changed files with 172 additions and 39 deletions

View File

@@ -25,25 +25,25 @@ struct ThreadExitTidyUp
_context(context),
_closeContextOnExit(closeContextOnExit)
{
osg::notify(osg::NOTICE)<<"starting thread context "<<_context<<std::endl;
osg::notify(osg::INFO)<<"starting thread context "<<_context<<std::endl;
}
~ThreadExitTidyUp()
{
osg::notify(osg::NOTICE)<<"exit thread"<<std::endl;
osg::notify(osg::INFO)<<"exit thread"<<std::endl;
if (_context)
{
if (_closeContextOnExit)
{
osg::notify(osg::NOTICE)<<" - close context "<<_context<<std::endl;
osg::notify(osg::INFO)<<" - close context "<<_context<<std::endl;
_context->closeImplementation();
osg::notify(osg::NOTICE)<<" - done close context "<<_context<<std::endl;
osg::notify(osg::INFO)<<" - done close context "<<_context<<std::endl;
}
else
{
osg::notify(osg::NOTICE)<<" - releaseContext "<<_context<<std::endl;
osg::notify(osg::INFO)<<" - releaseContext "<<_context<<std::endl;
//_context->releaseContext();
}
@@ -64,10 +64,15 @@ struct BlockOperation : public GraphicsThread::Operation, public Block
reset();
}
virtual void release()
{
Block::release();
}
virtual void operator () (GraphicsContext*)
{
glFlush();
release();
Block::release();
}
};
@@ -86,6 +91,30 @@ GraphicsThread::~GraphicsThread()
cancel();
}
void GraphicsThread::setDone(bool done)
{
if (_done==done) return;
_done = true;
if (done)
{
osg::notify(osg::INFO)<<"set done "<<this<<std::endl;
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
if (_currentOperation.valid())
{
osg::notify(osg::INFO)<<"releasing "<<_currentOperation.get()<<std::endl;
_currentOperation->release();
}
}
_operationsBlock->release();
}
}
int GraphicsThread::cancel()
{
osg::notify(osg::INFO)<<"Cancelling graphics thread "<<this<<std::endl;
@@ -102,37 +131,28 @@ int GraphicsThread::cancel()
itr != _operations.end();
++itr)
{
BarrierOperation* barrier = dynamic_cast<BarrierOperation*>(itr->get());
if (barrier)
{
barrier->release();
//barrier->invalidate();
osg::notify(osg::INFO)<<" Invalidating barrier "<<this<<std::endl;
}
(*itr)->release();
}
/*
// cancel the thread..
result = Thread::cancel();
//join();
*/
// release the frameBlock and _databasePagerThreadBlock incase its holding up thread cancelation.
_operationsBlock->release();
// then wait for the the thread to stop running.
while(isRunning())
{
for(OperationQueue::iterator itr = _operations.begin();
itr != _operations.end();
++itr)
_operationsBlock->release();
{
BarrierOperation* barrier = dynamic_cast<BarrierOperation*>(itr->get());
if (barrier)
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
for(OperationQueue::iterator itr = _operations.begin();
itr != _operations.end();
++itr)
{
barrier->release();
//barrier->invalidate();
osg::notify(osg::INFO)<<" Invalidating barrier "<<this<<std::endl;
(*itr)->release();
}
if (_currentOperation.valid()) _currentOperation->release();
}
// commenting out debug info as it was cashing crash on exit, presumable
@@ -174,6 +194,46 @@ void GraphicsThread::add(Operation* operation, bool waitForCompletion)
}
}
void GraphicsThread::remove(Operation* operation)
{
osg::notify(osg::INFO)<<"Doing remove operation"<<std::endl;
// aquire the lock on the operations queue to prevent anyone else for modifying it at the same time
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
for(OperationQueue::iterator itr = _operations.begin();
itr!=_operations.end();)
{
if ((*itr)==operation) itr = _operations.erase(itr);
else ++itr;
}
}
void GraphicsThread::remove(const std::string& name)
{
osg::notify(osg::INFO)<<"Doing remove named operation"<<std::endl;
// aquire the lock on the operations queue to prevent anyone else for modifying it at the same time
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
// find the remove all operations with specificed name
for(OperationQueue::iterator itr = _operations.begin();
itr!=_operations.end();)
{
if ((*itr)->getName()==name) itr = _operations.erase(itr);
else ++itr;
}
}
void GraphicsThread::removeAllOperations()
{
osg::notify(osg::INFO)<<"Doing remove all operations"<<std::endl;
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
_operations.clear();
}
void GraphicsThread::run()
{
bool contextRealizedInThisThread = false;
@@ -202,12 +262,15 @@ void GraphicsThread::run()
do
{
osg::notify(osg::INFO)<<"In main loop"<<std::endl;
osg::notify(osg::INFO)<<"In main loop "<<this<<std::endl;
if (_operations.empty())
{
_operationsBlock->block();
// exit from loop if _done is set.
if (_done) break;
itr = _operations.begin();
}
else
@@ -215,21 +278,19 @@ void GraphicsThread::run()
if (itr == _operations.end()) itr = _operations.begin();
}
osg::notify(osg::INFO)<<"get op"<<std::endl;
osg::notify(osg::INFO)<<"get op "<<_done<<" "<<this<<std::endl;
ref_ptr<Operation> operation;
// get the front of the file request list.
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
if (!_operations.empty())
{
// get the next item
operation = *itr;
_currentOperation = *itr;
if (!operation->getKeep())
if (!_currentOperation->getKeep())
{
osg::notify(osg::INFO)<<"removing "<<operation->getName()<<std::endl;
osg::notify(osg::INFO)<<"removing "<<_currentOperation->getName()<<std::endl;
// remove it from the opeations queue
itr = _operations.erase(itr);
@@ -244,7 +305,7 @@ void GraphicsThread::run()
}
else
{
osg::notify(osg::INFO)<<"increment "<<operation->getName()<<std::endl;
osg::notify(osg::INFO)<<"increment "<<_currentOperation->getName()<<std::endl;
// move on to the next operation in the list.
++itr;
@@ -255,12 +316,17 @@ void GraphicsThread::run()
}
if (operation.valid())
if (_currentOperation.valid())
{
osg::notify(osg::INFO)<<"Doing op "<<operation->getName()<<" "<<this<<std::endl;
osg::notify(osg::INFO)<<"Doing op "<<_currentOperation->getName()<<" "<<this<<std::endl;
// call the graphics operation.
(*operation)(_graphicsContext);
(*_currentOperation)(_graphicsContext);
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_operationsMutex);
_currentOperation = 0;
}
}
if (firstTime)
@@ -273,7 +339,7 @@ void GraphicsThread::run()
} while (!testCancel() && !_done);
osg::notify(osg::NOTICE)<<"exit loop"<<std::endl;
osg::notify(osg::INFO)<<"exit loop "<<this<<std::endl;
}
@@ -285,6 +351,11 @@ void SwapBuffersOperation::operator () (GraphicsContext* context)
}
}
void BarrierOperation::release()
{
Barrier::release();
}
void BarrierOperation::operator () (GraphicsContext*)
{
if (_preBlockOp==GL_FLUSH) glFlush();
@@ -293,6 +364,12 @@ void BarrierOperation::operator () (GraphicsContext*)
block();
}
void ReleaseContext_Block_MakeCurrentOperation::release()
{
Block::release();
}
void ReleaseContext_Block_MakeCurrentOperation::operator () (GraphicsContext* context)
{
if (!context) return;