Renamed OperationsThread to OperationThread and add two way link between OperationQueue and OperationThread

This commit is contained in:
Robert Osfield
2007-07-12 12:15:42 +00:00
parent 751c638015
commit 4d7e8b12ae
2 changed files with 53 additions and 21 deletions

View File

@@ -11,8 +11,8 @@
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_OPERATIONSTHREAD
#define OSG_OPERATIONSTHREAD 1
#ifndef OSG_OPERATIONTHREAD
#define OSG_OPERATIONTHREAD 1
#include <osg/observer_ptr>
#include <osg/Object>
@@ -23,6 +23,7 @@
#include <OpenThreads/Block>
#include <list>
#include <set>
namespace osg {
@@ -71,6 +72,8 @@ class Operation : virtual public Referenced
bool _keep;
};
class OperationThread;
class OSG_EXPORT OperationQueue : public Referenced
{
public:
@@ -103,25 +106,35 @@ class OSG_EXPORT OperationQueue : public Referenced
/** Release operations block that is used to block threads that are waiting on an empty operations queue.*/
void releaseOperationsBlock();
typedef std::set<OperationThread*> OperationThreads;
/** Get the set of OperationThreads that are sharing this OperationQueue. */
const OperationThreads& getOperationThreads() const { return _operationThreads; }
protected:
virtual ~OperationQueue();
typedef std::list< ref_ptr<Operation> > Operations;
friend class OperationThread;
void addOperationThread(OperationThread* thread);
void removeOperationThread(OperationThread* thread);
typedef std::list< osg::ref_ptr<Operation> > Operations;
OpenThreads::Mutex _operationsMutex;
osg::ref_ptr<osg::RefBlock> _operationsBlock;
Operations _operations;
Operations::iterator _currentOperationIterator;
OperationThreads _operationThreads;
};
/** GraphicsThread is a helper class for running OpenGL GraphicsOperation within a single thread assigned to a specific GraphicsContext.*/
class OSG_EXPORT OperationsThread : public Referenced, public OpenThreads::Thread
class OSG_EXPORT OperationThread : public Referenced, public OpenThreads::Thread
{
public:
OperationsThread();
OperationThread();
void setParent(Object* parent) { _parent = parent; }
@@ -169,7 +182,7 @@ class OSG_EXPORT OperationsThread : public Referenced, public OpenThreads::Threa
protected:
virtual ~OperationsThread();
virtual ~OperationThread();
observer_ptr<Object> _parent;
@@ -181,6 +194,8 @@ class OSG_EXPORT OperationsThread : public Referenced, public OpenThreads::Threa
};
typedef OperationThread OperationsThread;
}
#endif

View File

@@ -199,20 +199,30 @@ void OperationQueue::releaseOperationsBlock()
}
void OperationQueue::addOperationThread(OperationThread* thread)
{
_operationThreads.insert(thread);
}
void OperationQueue::removeOperationThread(OperationThread* thread)
{
_operationThreads.erase(thread);
}
/////////////////////////////////////////////////////////////////////////////
//
// OperationsThread
// OperationThread
//
OperationsThread::OperationsThread():
OperationThread::OperationThread():
osg::Referenced(true),
_parent(0),
_done(false)
{
_operationQueue = new OperationQueue;
setOperationQueue(new OperationQueue);
}
OperationsThread::~OperationsThread()
OperationThread::~OperationThread()
{
//osg::notify(osg::NOTICE)<<"Destructing graphics thread "<<this<<std::endl;
@@ -221,13 +231,20 @@ OperationsThread::~OperationsThread()
//osg::notify(osg::NOTICE)<<"Done Destructing graphics thread "<<this<<std::endl;
}
void OperationsThread::setOperationQueue(OperationQueue* opq)
void OperationThread::setOperationQueue(OperationQueue* opq)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadMutex);
if (_operationQueue == opq) return;
if (_operationQueue.valid()) _operationQueue->removeOperationThread(this);
_operationQueue = opq;
if (_operationQueue.valid()) _operationQueue->addOperationThread(this);
}
void OperationsThread::setDone(bool done)
void OperationThread::setDone(bool done)
{
if (_done==done) return;
@@ -250,9 +267,9 @@ void OperationsThread::setDone(bool done)
}
}
int OperationsThread::cancel()
int OperationThread::cancel()
{
osg::notify(osg::INFO)<<"Cancelling OperationsThread "<<this<<" isRunning()="<<isRunning()<<std::endl;
osg::notify(osg::INFO)<<"Cancelling OperationThread "<<this<<" isRunning()="<<isRunning()<<std::endl;
int result = 0;
if( isRunning() )
@@ -293,42 +310,42 @@ int OperationsThread::cancel()
#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::notify(osg::INFO)<<" Waiting for OperationsThread to cancel "<<this<<std::endl;
osg::notify(osg::INFO)<<" Waiting for OperationThread to cancel "<<this<<std::endl;
OpenThreads::Thread::YieldCurrentThread();
}
}
osg::notify(osg::INFO)<<" OperationsThread::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl;
osg::notify(osg::INFO)<<" OperationThread::cancel() thread cancelled "<<this<<" isRunning()="<<isRunning()<<std::endl;
return result;
}
void OperationsThread::add(Operation* operation)
void OperationThread::add(Operation* operation)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadMutex);
if (!_operationQueue) _operationQueue = new OperationQueue;
_operationQueue->add(operation);
}
void OperationsThread::remove(Operation* operation)
void OperationThread::remove(Operation* operation)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadMutex);
if (_operationQueue.valid()) _operationQueue->remove(operation);
}
void OperationsThread::remove(const std::string& name)
void OperationThread::remove(const std::string& name)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadMutex);
if (_operationQueue.valid()) _operationQueue->remove(name);
}
void OperationsThread::removeAllOperations()
void OperationThread::removeAllOperations()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_threadMutex);
if (_operationQueue.valid()) _operationQueue->removeAllOperations();
}
void OperationsThread::run()
void OperationThread::run()
{
// make the graphics context current.
GraphicsContext* graphicsContext = dynamic_cast<GraphicsContext*>(_parent.get());