OperationThread
Go to the documentation of this file.
/* -*-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.
*/
#ifndef OSG_OPERATIONTHREAD
#define OSG_OPERATIONTHREAD 1
#include <osg/observer_ptr>
#include <osg/Object>
#include <OpenThreads/Thread>
#include <OpenThreads/Barrier>
#include <OpenThreads/Condition>
#include <OpenThreads/Block>
#include <list>
#include <set>
namespace osg {
class RefBlock : virtual public osg::Referenced, public OpenThreads::Block
{
public:
RefBlock():
osg::Referenced(true) {}
};
class RefBlockCount : virtual public osg::Referenced, public OpenThreads::BlockCount
{
public:
RefBlockCount(unsigned blockCount):
osg::Referenced(true),
OpenThreads::BlockCount(blockCount) {}
};
class Operation : virtual public Referenced
{
public:
Operation(const std::string& name, bool keep):
_name(name),
_keep(keep) {}
void setName(const std::string& name) { _name = name; }
const std::string& getName() const { return _name; }
void setKeep(bool keep) { _keep = keep; }
bool getKeep() const { return _keep; }
virtual void release() {}
virtual void operator () (Object*) = 0;
protected:
Operation():
_keep(false) {}
virtual ~Operation() {}
std::string _name;
bool _keep;
};
class OperationThread;
class OSG_EXPORT OperationQueue : public Referenced
{
public:
OperationQueue();
osg::ref_ptr<Operation> getNextOperation(bool blockIfEmpty = false);
bool empty();
unsigned int getNumOperationsInQueue();
void add(Operation* operation);
void remove(Operation* operation);
void remove(const std::string& name);
void removeAllOperations();
void runOperations(Object* callingObject=0);
void releaseAllOperations();
void releaseOperationsBlock();
typedef std::set<OperationThread*> OperationThreads;
const OperationThreads& getOperationThreads() const { return _operationThreads; }
protected:
virtual ~OperationQueue();
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;
};
class OSG_EXPORT OperationThread : public Referenced, public OpenThreads::Thread
{
public:
OperationThread();
void setParent(Object* parent) { _parent = parent; }
Object* getParent() { return _parent.get(); }
const Object* getParent() const { return _parent.get(); }
void setOperationQueue(OperationQueue* opq);
OperationQueue* getOperationQueue() { return _operationQueue.get(); }
const OperationQueue* getOperationQueue() const { return _operationQueue.get(); }
void add(Operation* operation);
void remove(Operation* operation);
void remove(const std::string& name);
void removeAllOperations();
osg::ref_ptr<Operation> getCurrentOperation() { return _currentOperation; }
virtual void run();
void setDone(bool done);
bool getDone() const { return _done!=0; }
virtual int cancel();
protected:
virtual ~OperationThread();
observer_ptr<Object> _parent;
OpenThreads::Atomic _done;
OpenThreads::Mutex _threadMutex;
osg::ref_ptr<OperationQueue> _operationQueue;
osg::ref_ptr<Operation> _currentOperation;
};
typedef OperationThread OperationsThread;
}
#endif
Barrier
Block
Condition
Object
Thread
OpenThreads::Atomic
This class provides an atomic increment and decrement operation.
Definition Atomic:48
OpenThreads::BlockCount
Definition Block:101
OpenThreads::BlockCount::BlockCount
BlockCount(unsigned int blockCount)
Definition Block:104
OpenThreads::Block
Definition Block:26
OpenThreads::Mutex
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
OpenThreads::Thread
This class provides an object-oriented thread interface.
Definition Thread:48
osg::Object
Definition Object:61
osg::OperationQueue
Definition OperationThread:91
osg::OperationQueue::getNextOperation
osg::ref_ptr< Operation > getNextOperation(bool blockIfEmpty=false)
osg::OperationQueue::~OperationQueue
virtual ~OperationQueue()
osg::OperationQueue::OperationQueue
OperationQueue()
osg::OperationQueue::_operationThreads
OperationThreads _operationThreads
Definition OperationThread:149
osg::OperationQueue::getNumOperationsInQueue
unsigned int getNumOperationsInQueue()
osg::OperationQueue::addOperationThread
void addOperationThread(OperationThread *thread)
osg::OperationQueue::_currentOperationIterator
Operations::iterator _currentOperationIterator
Definition OperationThread:147
osg::OperationQueue::removeOperationThread
void removeOperationThread(OperationThread *thread)
osg::OperationQueue::releaseOperationsBlock
void releaseOperationsBlock()
osg::OperationQueue::removeAllOperations
void removeAllOperations()
osg::OperationQueue::getOperationThreads
const OperationThreads & getOperationThreads() const
Definition OperationThread:131
osg::OperationQueue::_operationsMutex
OpenThreads::Mutex _operationsMutex
Definition OperationThread:144
osg::OperationQueue::empty
bool empty()
osg::OperationQueue::runOperations
void runOperations(Object *callingObject=0)
osg::OperationQueue::OperationThreads
std::set< OperationThread * > OperationThreads
Definition OperationThread:128
osg::OperationQueue::OperationThread
friend class OperationThread
Definition OperationThread:137
osg::OperationQueue::_operationsBlock
osg::ref_ptr< osg::RefBlock > _operationsBlock
Definition OperationThread:145
osg::OperationQueue::_operations
Operations _operations
Definition OperationThread:146
osg::OperationQueue::remove
void remove(const std::string &name)
osg::OperationQueue::releaseAllOperations
void releaseAllOperations()
osg::OperationQueue::Operations
std::list< osg::ref_ptr< Operation > > Operations
Definition OperationThread:142
osg::OperationQueue::remove
void remove(Operation *operation)
osg::OperationQueue::add
void add(Operation *operation)
osg::OperationThread
Definition OperationThread:154
osg::OperationThread::getOperationQueue
OperationQueue * getOperationQueue()
Definition OperationThread:169
osg::OperationThread::setParent
void setParent(Object *parent)
Definition OperationThread:158
osg::OperationThread::getParent
const Object * getParent() const
Definition OperationThread:162
osg::OperationThread::remove
void remove(Operation *operation)
osg::OperationThread::_operationQueue
osg::ref_ptr< OperationQueue > _operationQueue
Definition OperationThread:211
osg::OperationThread::_currentOperation
osg::ref_ptr< Operation > _currentOperation
Definition OperationThread:212
osg::OperationThread::getOperationQueue
const OperationQueue * getOperationQueue() const
Definition OperationThread:172
osg::OperationThread::setDone
void setDone(bool done)
osg::OperationThread::_parent
observer_ptr< Object > _parent
Definition OperationThread:206
osg::OperationThread::_threadMutex
OpenThreads::Mutex _threadMutex
Definition OperationThread:210
osg::OperationThread::getDone
bool getDone() const
Definition OperationThread:197
osg::OperationThread::setOperationQueue
void setOperationQueue(OperationQueue *opq)
osg::OperationThread::removeAllOperations
void removeAllOperations()
osg::OperationThread::getParent
Object * getParent()
Definition OperationThread:160
osg::OperationThread::~OperationThread
virtual ~OperationThread()
osg::OperationThread::add
void add(Operation *operation)
osg::OperationThread::cancel
virtual int cancel()
osg::OperationThread::_done
OpenThreads::Atomic _done
Definition OperationThread:208
osg::OperationThread::getCurrentOperation
osg::ref_ptr< Operation > getCurrentOperation()
Definition OperationThread:190
osg::OperationThread::run
virtual void run()
osg::OperationThread::remove
void remove(const std::string &name)
osg::OperationThread::OperationThread
OperationThread()
osg::Operation
Definition OperationThread:51
osg::Operation::operator()
virtual void operator()(Object *)=0
osg::Operation::setKeep
void setKeep(bool keep)
Definition OperationThread:66
osg::Operation::getKeep
bool getKeep() const
Definition OperationThread:69
osg::Operation::getName
const std::string & getName() const
Definition OperationThread:63
osg::Operation::_keep
bool _keep
Definition OperationThread:85
osg::Operation::Operation
Operation(const std::string &name, bool keep)
Definition OperationThread:54
osg::Operation::_name
std::string _name
Definition OperationThread:84
osg::Operation::setName
void setName(const std::string &name)
Definition OperationThread:60
osg::Operation::release
virtual void release()
Definition OperationThread:72
osg::Operation::Operation
Operation()
Definition OperationThread:79
osg::Operation::~Operation
virtual ~Operation()
Definition OperationThread:82
osg::RefBlockCount::RefBlockCount
RefBlockCount(unsigned blockCount)
Definition OperationThread:43
osg::RefBlock::RefBlock
RefBlock()
Definition OperationThread:34
osg::Referenced
Definition Referenced:44
osg::Referenced::Referenced
Referenced()
osg::observer_ptr
Definition observer_ptr:39
osg::ref_ptr
Definition ref_ptr:32
OpenThreads
Definition Affinity:26
osg
Definition AlphaFunc:19
osg::OperationsThread
OperationThread OperationsThread
Definition OperationThread:216
observer_ptr
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39