OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OperationThread.h
Go to the documentation of this file.
1 /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2  *
3  * This library is open source and may be redistributed and/or modified under
4  * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
5  * (at your option) any later version. The full license is in LICENSE file
6  * included with this distribution, and on the openscenegraph.org website.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * OpenSceneGraph Public License for more details.
12 */
13 
14 #ifndef OSG_OPERATIONTHREAD
15 #define OSG_OPERATIONTHREAD 1
16 
17 #include <osg/observer_ptr>
18 #include <osg/Object>
19 
20 #include <OpenThreads/Thread>
21 #include <OpenThreads/Barrier>
22 #include <OpenThreads/Condition>
23 #include <OpenThreads/Block>
24 
25 #include <list>
26 #include <set>
27 
28 namespace osg {
29 
30 class RefBlock : virtual public osg::Referenced, public OpenThreads::Block
31 {
32  public:
33 
35  osg::Referenced(true) {}
36 
37 };
38 
40 {
41  public:
42 
43  RefBlockCount(unsigned blockCount):
44  osg::Referenced(true),
45  OpenThreads::BlockCount(blockCount) {}
46 
47 };
48 
50 class Operation : virtual public Referenced
51 {
52  public:
53 
54  Operation(const std::string& name, bool keep):
55  _name(name),
56  _keep(keep) {}
57 
58 
60  void setName(const std::string& name) { _name = name; }
61 
63  const std::string& getName() const { return _name; }
64 
66  void setKeep(bool keep) { _keep = keep; }
67 
69  bool getKeep() const { return _keep; }
70 
72  virtual void release() {}
73 
75  virtual void operator () (Object*) = 0;
76 
77 protected:
78 
80  _keep(false) {}
81 
82  Operation(const Operation& op):
83  _name(op._name),
84  _keep(op._keep) {}
85 
86  virtual ~Operation() {}
87 
88  std::string _name;
89  bool _keep;
90 };
91 
92 class OperationThread;
93 
95 {
96  public:
97 
99 
102  osg::ref_ptr<Operation> getNextOperation(bool blockIfEmpty = false);
103 
105  bool empty();
106 
108  unsigned int getNumOperationsInQueue();
109 
112  void add(Operation* operation);
113 
115  void remove(Operation* operation);
116 
118  void remove(const std::string& name);
119 
121  void removeAllOperations();
122 
124  void runOperations(Object* callingObject=0);
125 
127  void releaseAllOperations();
128 
130  void releaseOperationsBlock();
131 
132  typedef std::set<OperationThread*> OperationThreads;
133 
135  const OperationThreads& getOperationThreads() const { return _operationThreads; }
136 
137  protected:
138 
139  virtual ~OperationQueue();
140 
141  friend class OperationThread;
142 
143  void addOperationThread(OperationThread* thread);
144  void removeOperationThread(OperationThread* thread);
145 
146  typedef std::list< osg::ref_ptr<Operation> > Operations;
147 
150  Operations _operations;
151  Operations::iterator _currentOperationIterator;
152 
153  OperationThreads _operationThreads;
154 };
155 
158 {
159  public:
160  OperationThread();
161 
162  void setParent(Object* parent) { _parent = parent; }
163 
164  Object* getParent() { return _parent.get(); }
165 
166  const Object* getParent() const { return _parent.get(); }
167 
168 
170  void setOperationQueue(OperationQueue* opq);
171 
173  OperationQueue* getOperationQueue() { return _operationQueue.get(); }
174 
176  const OperationQueue* getOperationQueue() const { return _operationQueue.get(); }
177 
178 
181  void add(Operation* operation);
182 
184  void remove(Operation* operation);
185 
187  void remove(const std::string& name);
188 
190  void removeAllOperations();
191 
192 
194  osg::ref_ptr<Operation> getCurrentOperation() { return _currentOperation; }
195 
197  virtual void run();
198 
199  void setDone(bool done);
200 
201  bool getDone() const { return _done!=0; }
202 
204  virtual int cancel();
205 
206  protected:
207 
208  virtual ~OperationThread();
209 
211 
213 
217 
218 };
219 
221 
222 }
223 
224 #endif
#define OSG_EXPORT
Definition: Export.h:43
OpenThreads::Mutex _threadMutex
BlockCount(unsigned int blockCount)
Definition: Block.h:104
OpenThreads::Atomic _done
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
bool getKeep() const
virtual void release()
osg::ref_ptr< osg::RefBlock > _operationsBlock
Operation(const Operation &op)
void setName(const std::string &name)
std::list< osg::ref_ptr< Operation > > Operations
OperationThreads _operationThreads
virtual void operator()(Object *)=0
virtual ~Operation()
RefBlockCount(unsigned blockCount)
void setParent(Object *parent)
std::string _name
This class provides an atomic increment and decrement operation.
Definition: Atomic.h:48
Operation(const std::string &name, bool keep)
Operations::iterator _currentOperationIterator
const OperationThreads & getOperationThreads() const
This class provides an object-oriented thread interface.
Definition: Thread.h:49
osg::ref_ptr< OperationQueue > _operationQueue
void setKeep(bool keep)
OperationQueue * getOperationQueue()
const std::string & getName() const
OperationThread OperationsThread
const OperationQueue * getOperationQueue() const
osg::ref_ptr< Operation > _currentOperation
const Object * getParent() const
Definition: AlphaFunc.h:19
observer_ptr< Object > _parent
std::set< OperationThread * > OperationThreads
osg::ref_ptr< Operation > getCurrentOperation()
OpenThreads::Mutex _operationsMutex