OpenSceneGraph 3.6.5
OperationThread
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
28namespace osg {
29
30class RefBlock : virtual public osg::Referenced, public OpenThreads::Block
31{
32 public:
33
35 osg::Referenced(true) {}
36
37};
38
39class RefBlockCount : virtual public osg::Referenced, public OpenThreads::BlockCount
40{
41 public:
42
43 RefBlockCount(unsigned blockCount):
44 osg::Referenced(true),
45 OpenThreads::BlockCount(blockCount) {}
46
47};
48
50class 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
77protected:
78
80 _keep(false) {}
81
82 virtual ~Operation() {}
83
84 std::string _name;
85 bool _keep;
86};
87
88class OperationThread;
89
91{
92 public:
93
95
98 osg::ref_ptr<Operation> getNextOperation(bool blockIfEmpty = false);
99
101 bool empty();
102
105
108 void add(Operation* operation);
109
111 void remove(Operation* operation);
112
114 void remove(const std::string& name);
115
118
120 void runOperations(Object* callingObject=0);
121
124
127
128 typedef std::set<OperationThread*> OperationThreads;
129
132
133 protected:
134
136
137 friend class OperationThread;
138
141
142 typedef std::list< osg::ref_ptr<Operation> > Operations;
143
144 OpenThreads::Mutex _operationsMutex;
147 Operations::iterator _currentOperationIterator;
148
150};
151
153class OSG_EXPORT OperationThread : public Referenced, public OpenThreads::Thread
154{
155 public:
157
158 void setParent(Object* parent) { _parent = parent; }
159
160 Object* getParent() { return _parent.get(); }
161
162 const Object* getParent() const { return _parent.get(); }
163
164
167
170
172 const OperationQueue* getOperationQueue() const { return _operationQueue.get(); }
173
174
177 void add(Operation* operation);
178
180 void remove(Operation* operation);
181
183 void remove(const std::string& name);
184
187
188
191
193 virtual void run();
194
195 void setDone(bool done);
196
197 bool getDone() const { return _done!=0; }
198
200 virtual int cancel();
201
202 protected:
203
205
207
208 OpenThreads::Atomic _done;
209
210 OpenThreads::Mutex _threadMutex;
213
214};
215
217
218}
219
220#endif
The core osg library provides the basic scene graph classes such as Nodes, State and Drawables,...
Definition AlphaFunc:19
OperationThread OperationsThread
Definition OperationThread:216
Base class/standard interface for objects which require IO support, cloning and reference counting.
Definition Object:61
Smart pointer for observed objects, that automatically set pointers to them to null when they are del...
Definition observer_ptr:39
RefBlock()
Definition OperationThread:34
RefBlockCount(unsigned blockCount)
Definition OperationThread:43
Base class for implementing graphics operations.
Definition OperationThread:51
virtual void operator()(Object *)=0
Do the actual task of this operation.
void setKeep(bool keep)
Set whether the operation should be kept once its been applied.
Definition OperationThread:66
bool getKeep() const
Get whether the operation should be kept once its been applied.
Definition OperationThread:69
const std::string & getName() const
Get the human readable name of the operation.
Definition OperationThread:63
bool _keep
Definition OperationThread:85
Operation(const std::string &name, bool keep)
Definition OperationThread:54
std::string _name
Definition OperationThread:84
void setName(const std::string &name)
Set the human readable name of the operation.
Definition OperationThread:60
virtual void release()
if this operation is a barrier then release it.
Definition OperationThread:72
Operation()
Definition OperationThread:79
virtual ~Operation()
Definition OperationThread:82
Definition OperationThread:91
osg::ref_ptr< Operation > getNextOperation(bool blockIfEmpty=false)
Get the next operation from the operation queue.
virtual ~OperationQueue()
OperationThreads _operationThreads
Definition OperationThread:149
unsigned int getNumOperationsInQueue()
Return the num of pending operations that are sitting in the OperationQueue.
void addOperationThread(OperationThread *thread)
Operations::iterator _currentOperationIterator
Definition OperationThread:147
void removeOperationThread(OperationThread *thread)
void releaseOperationsBlock()
Release operations block that is used to block threads that are waiting on an empty operations queue.
void removeAllOperations()
Remove all operations from OperationQueue.
const OperationThreads & getOperationThreads() const
Get the set of OperationThreads that are sharing this OperationQueue.
Definition OperationThread:131
OpenThreads::Mutex _operationsMutex
Definition OperationThread:144
bool empty()
Return true if the operation queue is empty.
void runOperations(Object *callingObject=0)
Run the operations.
std::set< OperationThread * > OperationThreads
Definition OperationThread:128
friend class OperationThread
Definition OperationThread:137
osg::ref_ptr< osg::RefBlock > _operationsBlock
Definition OperationThread:145
Operations _operations
Definition OperationThread:146
void remove(const std::string &name)
Remove named operation from OperationQueue.
void releaseAllOperations()
Call release on all operations.
std::list< osg::ref_ptr< Operation > > Operations
Definition OperationThread:142
void remove(Operation *operation)
Remove operation from OperationQueue.
void add(Operation *operation)
Add operation to end of OperationQueue, this will be executed by the operation thread once this opera...
OperationThread is a helper class for running Operation within a single thread.
Definition OperationThread:154
OperationQueue * getOperationQueue()
Get the OperationQueue.
Definition OperationThread:169
void setParent(Object *parent)
Definition OperationThread:158
const Object * getParent() const
Definition OperationThread:162
void remove(Operation *operation)
Remove operation from OperationQueue.
osg::ref_ptr< OperationQueue > _operationQueue
Definition OperationThread:211
osg::ref_ptr< Operation > _currentOperation
Definition OperationThread:212
const OperationQueue * getOperationQueue() const
Get the const OperationQueue.
Definition OperationThread:172
void setDone(bool done)
observer_ptr< Object > _parent
Definition OperationThread:206
OpenThreads::Mutex _threadMutex
Definition OperationThread:210
bool getDone() const
Definition OperationThread:197
void setOperationQueue(OperationQueue *opq)
Set the OperationQueue.
void removeAllOperations()
Remove all operations from OperationQueue.
Object * getParent()
Definition OperationThread:160
virtual ~OperationThread()
void add(Operation *operation)
Add operation to end of OperationQueue, this will be executed by the graphics thread once this operat...
virtual int cancel()
Cancel this graphics thread.
OpenThreads::Atomic _done
Definition OperationThread:208
osg::ref_ptr< Operation > getCurrentOperation()
Get the operation currently being run.
Definition OperationThread:190
virtual void run()
Run does the opertion thread run loop.
void remove(const std::string &name)
Remove named operation from OperationQueue.
Smart pointer for handling referenced counted objects.
Definition ref_ptr:32
Base class for providing reference counted objects.
Definition Referenced:44
#define OSG_EXPORT
Definition Export:39

osg logo
Generated at Sun Jul 20 2025 00:00:00 for the OpenSceneGraph by doxygen 1.14.0.