2007-07-10 03:04:36 +08:00
|
|
|
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
|
|
|
|
*
|
|
|
|
* This library is open source and may be redistributed and/or modified under
|
|
|
|
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
|
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
#ifndef OSG_OPERATIONTHREAD
|
|
|
|
#define OSG_OPERATIONTHREAD 1
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
#include <osg/observer_ptr>
|
|
|
|
#include <osg/Object>
|
|
|
|
|
|
|
|
#include <OpenThreads/Thread>
|
|
|
|
#include <OpenThreads/Barrier>
|
|
|
|
#include <OpenThreads/Condition>
|
|
|
|
#include <OpenThreads/Block>
|
|
|
|
|
|
|
|
#include <list>
|
2007-07-12 20:15:42 +08:00
|
|
|
#include <set>
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
namespace osg {
|
|
|
|
|
2007-07-19 23:24:11 +08:00
|
|
|
class RefBlock : virtual public osg::Referenced, public OpenThreads::Block
|
2007-07-10 03:04:36 +08:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
2007-07-11 01:36:01 +08:00
|
|
|
RefBlock():
|
|
|
|
osg::Referenced(true) {}
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-07-19 23:24:11 +08:00
|
|
|
class RefBlockCount : virtual public osg::Referenced, public OpenThreads::BlockCount
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
RefBlockCount(unsigned blockCount):
|
|
|
|
osg::Referenced(true),
|
|
|
|
OpenThreads::BlockCount(blockCount) {}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
/** Base class for implementing graphics operations.*/
|
|
|
|
class Operation : virtual public Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Operation(const std::string& name, bool keep):
|
2007-07-11 01:36:01 +08:00
|
|
|
osg::Referenced(true),
|
2007-07-10 03:04:36 +08:00
|
|
|
_name(name),
|
|
|
|
_keep(keep) {}
|
|
|
|
|
|
|
|
|
|
|
|
/** Set the human readable name of the operation.*/
|
|
|
|
void setName(const std::string& name) { _name = name; }
|
|
|
|
|
|
|
|
/** Get the human readable name of the operation.*/
|
|
|
|
const std::string& getName() const { return _name; }
|
|
|
|
|
|
|
|
/** Set whether the operation should be kept once its been applied.*/
|
|
|
|
void setKeep(bool keep) { _keep = keep; }
|
|
|
|
|
|
|
|
/** Get whether the operation should be kept once its been applied.*/
|
|
|
|
bool getKeep() const { return _keep; }
|
|
|
|
|
|
|
|
/** if this operation is a barrier then release it.*/
|
|
|
|
virtual void release() {}
|
|
|
|
|
|
|
|
/** Do the actual task of this operation.*/
|
|
|
|
virtual void operator () (Object*) = 0;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~Operation() {}
|
|
|
|
|
|
|
|
std::string _name;
|
|
|
|
bool _keep;
|
|
|
|
};
|
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
class OperationThread;
|
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
class OSG_EXPORT OperationQueue : public Referenced
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
OperationQueue();
|
|
|
|
|
|
|
|
/** Get the next operation from the operation queue.
|
|
|
|
* Return null ref_ptr<> if no operations are left in queue. */
|
2007-07-11 01:36:01 +08:00
|
|
|
osg::ref_ptr<Operation> getNextOperation(bool blockIfEmpty = false);
|
|
|
|
|
|
|
|
/** Return true if the operation queue is empty. */
|
|
|
|
bool empty() const { return _operations.empty(); }
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
/** Add operation to end of OperationQueue, this will be
|
|
|
|
* executed by the operation thread once this operation gets to the head of the queue.*/
|
|
|
|
void add(Operation* operation);
|
|
|
|
|
|
|
|
/** Remove operation from OperationQueue.*/
|
|
|
|
void remove(Operation* operation);
|
|
|
|
|
|
|
|
/** Remove named operation from OperationQueue.*/
|
|
|
|
void remove(const std::string& name);
|
|
|
|
|
|
|
|
/** Remove all operations from OperationQueue.*/
|
|
|
|
void removeAllOperations();
|
2007-07-11 01:36:01 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
/** Call release on all operations. */
|
2007-07-11 01:36:01 +08:00
|
|
|
void releaseAllOperations();
|
|
|
|
|
|
|
|
/** Release operations block that is used to block threads that are waiting on an empty operations queue.*/
|
|
|
|
void releaseOperationsBlock();
|
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
typedef std::set<OperationThread*> OperationThreads;
|
|
|
|
|
|
|
|
/** Get the set of OperationThreads that are sharing this OperationQueue. */
|
|
|
|
const OperationThreads& getOperationThreads() const { return _operationThreads; }
|
2007-07-11 01:36:01 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
protected:
|
|
|
|
|
|
|
|
virtual ~OperationQueue();
|
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
friend class OperationThread;
|
|
|
|
|
|
|
|
void addOperationThread(OperationThread* thread);
|
|
|
|
void removeOperationThread(OperationThread* thread);
|
|
|
|
|
|
|
|
typedef std::list< osg::ref_ptr<Operation> > Operations;
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
OpenThreads::Mutex _operationsMutex;
|
|
|
|
osg::ref_ptr<osg::RefBlock> _operationsBlock;
|
|
|
|
Operations _operations;
|
|
|
|
Operations::iterator _currentOperationIterator;
|
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
OperationThreads _operationThreads;
|
2007-07-10 03:04:36 +08:00
|
|
|
};
|
|
|
|
|
2007-07-14 01:25:35 +08:00
|
|
|
/** OperationThread is a helper class for running Operation within a single thread.*/
|
2007-07-12 20:15:42 +08:00
|
|
|
class OSG_EXPORT OperationThread : public Referenced, public OpenThreads::Thread
|
2007-07-10 03:04:36 +08:00
|
|
|
{
|
|
|
|
public:
|
2007-07-12 20:15:42 +08:00
|
|
|
OperationThread();
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
void setParent(Object* parent) { _parent = parent; }
|
|
|
|
|
|
|
|
Object* getParent() { return _parent.get(); }
|
|
|
|
|
|
|
|
const Object* getParent() const { return _parent.get(); }
|
|
|
|
|
2007-07-11 01:36:01 +08:00
|
|
|
|
|
|
|
/** Set the OperationQueue. */
|
|
|
|
void setOperationQueue(OperationQueue* opq);
|
|
|
|
|
|
|
|
/** Get the OperationQueue. */
|
|
|
|
OperationQueue* getOperationQueue() { return _operationQueue.get(); }
|
|
|
|
|
|
|
|
/** Get the const OperationQueue. */
|
|
|
|
const OperationQueue* getOperationQueue() const { return _operationQueue.get(); }
|
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
/** Add operation to end of OperationQueue, this will be
|
|
|
|
* executed by the graphics thread once this operation gets to the head of the queue.*/
|
|
|
|
void add(Operation* operation);
|
|
|
|
|
|
|
|
/** Remove operation from OperationQueue.*/
|
|
|
|
void remove(Operation* operation);
|
|
|
|
|
|
|
|
/** Remove named operation from OperationQueue.*/
|
|
|
|
void remove(const std::string& name);
|
|
|
|
|
|
|
|
/** Remove all operations from OperationQueue.*/
|
|
|
|
void removeAllOperations();
|
2007-07-11 01:36:01 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
/** Get the operation currently being run.*/
|
|
|
|
osg::ref_ptr<Operation> getCurrentOperation() { return _currentOperation; }
|
|
|
|
|
2007-07-12 23:54:45 +08:00
|
|
|
/** Run does the opertion thread run loop.*/
|
2007-07-10 03:04:36 +08:00
|
|
|
virtual void run();
|
|
|
|
|
|
|
|
void setDone(bool done);
|
|
|
|
|
|
|
|
bool getDone() const { return _done; }
|
|
|
|
|
|
|
|
/** Cancel this graphics thread.*/
|
|
|
|
virtual int cancel();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
virtual ~OperationThread();
|
2007-07-10 03:04:36 +08:00
|
|
|
|
2007-07-11 01:36:01 +08:00
|
|
|
observer_ptr<Object> _parent;
|
2007-07-10 03:04:36 +08:00
|
|
|
|
2007-07-11 01:36:01 +08:00
|
|
|
bool _done;
|
2007-07-10 03:04:36 +08:00
|
|
|
|
2007-07-11 01:36:01 +08:00
|
|
|
OpenThreads::Mutex _threadMutex;
|
|
|
|
osg::ref_ptr<OperationQueue> _operationQueue;
|
|
|
|
osg::ref_ptr<Operation> _currentOperation;
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
typedef OperationThread OperationsThread;
|
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|