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:
|
2007-09-03 17:43:33 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
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; }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Set whether the operation should be kept once its been applied.*/
|
2007-07-10 03:04:36 +08:00
|
|
|
void setKeep(bool keep) { _keep = keep; }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Get whether the operation should be kept once its been applied.*/
|
2007-07-10 03:04:36 +08:00
|
|
|
bool getKeep() const { return _keep; }
|
|
|
|
|
|
|
|
/** if this operation is a barrier then release it.*/
|
|
|
|
virtual void release() {}
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Do the actual task of this operation.*/
|
2007-07-10 03:04:36 +08:00
|
|
|
virtual void operator () (Object*) = 0;
|
|
|
|
|
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-09-03 17:43:33 +08:00
|
|
|
Operation():
|
|
|
|
Referenced(true),
|
|
|
|
_keep(false) {}
|
|
|
|
|
|
|
|
Operation(const Operation& op):
|
|
|
|
Referenced(true),
|
|
|
|
_name(op._name),
|
|
|
|
_keep(op._keep) {}
|
2007-07-10 03:04:36 +08:00
|
|
|
|
|
|
|
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:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
OperationQueue();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
/** 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. */
|
2011-01-20 20:32:20 +08:00
|
|
|
bool empty();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-01-18 17:17:32 +08:00
|
|
|
/** Return the num of pending operations that are sitting in the OperationQueue.*/
|
2011-01-20 20:32:20 +08:00
|
|
|
unsigned int getNumOperationsInQueue();
|
2007-07-10 03:04:36 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Add operation to end of OperationQueue, this will be
|
2007-07-10 03:04:36 +08:00
|
|
|
* executed by the operation thread once this operation gets to the head of the queue.*/
|
|
|
|
void add(Operation* operation);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
/** 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();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-08-11 18:28:14 +08:00
|
|
|
/** Run the operations. */
|
|
|
|
void runOperations(Object* callingObject=0);
|
2007-07-11 01:36:01 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
/** Call release on all operations. */
|
2012-03-22 01:36:20 +08:00
|
|
|
void releaseAllOperations();
|
2007-07-11 01:36:01 +08:00
|
|
|
|
|
|
|
/** 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;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
/** 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;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
void addOperationThread(OperationThread* thread);
|
|
|
|
void removeOperationThread(OperationThread* thread);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
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(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
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(); }
|
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
/** Add operation to end of OperationQueue, this will be
|
2007-07-10 03:04:36 +08:00
|
|
|
* executed by the graphics thread once this operation gets to the head of the queue.*/
|
|
|
|
void add(Operation* operation);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
/** 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
|
|
|
|
2012-03-22 01:36:20 +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();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-10 03:04:36 +08:00
|
|
|
void setDone(bool done);
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2014-11-04 18:46:59 +08:00
|
|
|
bool getDone() const { return _done!=0; }
|
2007-07-10 03:04:36 +08:00
|
|
|
|
2012-03-22 01:36:20 +08:00
|
|
|
/** Cancel this graphics thread.*/
|
2007-07-10 03:04:36 +08:00
|
|
|
virtual int cancel();
|
|
|
|
|
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-12 20:15:42 +08:00
|
|
|
virtual ~OperationThread();
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2007-07-11 01:36:01 +08:00
|
|
|
observer_ptr<Object> _parent;
|
2007-07-10 03:04:36 +08:00
|
|
|
|
2014-11-04 18:46:59 +08:00
|
|
|
OpenThreads::Atomic _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
|