root/OpenSceneGraph/trunk/include/osg/OperationThread @ 13041

Revision 13041, 6.6 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
34        RefBlock():
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
49/** Base class for implementing graphics operations.*/
50class Operation : virtual public Referenced
51{
52    public:
53
54        Operation(const std::string& name, bool keep):
55            osg::Referenced(true),
56            _name(name),
57            _keep(keep) {}
58
59
60        /** Set the human readable name of the operation.*/
61        void setName(const std::string& name) { _name = name; }
62
63        /** Get the human readable name of the operation.*/
64        const std::string& getName() const { return _name; }
65
66        /** Set whether the operation should be kept once its been applied.*/
67        void setKeep(bool keep) { _keep = keep; }
68
69        /** Get whether the operation should be kept once its been applied.*/
70        bool getKeep() const { return _keep; }
71
72        /** if this operation is a barrier then release it.*/
73        virtual void release() {}
74
75        /** Do the actual task of this operation.*/
76        virtual void operator () (Object*) = 0;
77
78    protected:
79
80        Operation():
81            Referenced(true),
82            _keep(false) {}
83
84        Operation(const Operation& op):
85            Referenced(true),
86            _name(op._name),
87            _keep(op._keep) {}
88
89        virtual ~Operation() {}
90
91        std::string _name;
92        bool        _keep;
93};
94
95class OperationThread;
96
97class OSG_EXPORT OperationQueue : public Referenced
98{
99    public:
100
101        OperationQueue();
102
103        /** Get the next operation from the operation queue.
104          * Return null ref_ptr<> if no operations are left in queue. */
105        osg::ref_ptr<Operation> getNextOperation(bool blockIfEmpty = false);
106
107        /** Return true if the operation queue is empty. */
108        bool empty();
109
110        /** Return the num of pending operations that are sitting in the OperationQueue.*/
111        unsigned int getNumOperationsInQueue();
112
113        /** Add operation to end of OperationQueue, this will be
114          * executed by the operation thread once this operation gets to the head of the queue.*/
115        void add(Operation* operation);
116
117        /** Remove operation from OperationQueue.*/
118        void remove(Operation* operation);
119
120        /** Remove named operation from OperationQueue.*/
121        void remove(const std::string& name);
122
123        /** Remove all operations from OperationQueue.*/
124        void removeAllOperations();
125
126        /** Run the operations. */
127        void runOperations(Object* callingObject=0);
128
129        /** Call release on all operations. */
130        void releaseAllOperations();
131
132        /** Release operations block that is used to block threads that are waiting on an empty operations queue.*/
133        void releaseOperationsBlock();
134
135        typedef std::set<OperationThread*> OperationThreads;
136
137        /** Get the set of OperationThreads that are sharing this OperationQueue. */
138        const OperationThreads& getOperationThreads() const { return _operationThreads; }
139
140    protected:
141
142        virtual ~OperationQueue();
143
144        friend class OperationThread;
145
146        void addOperationThread(OperationThread* thread);
147        void removeOperationThread(OperationThread* thread);
148
149        typedef std::list< osg::ref_ptr<Operation> > Operations;
150
151        OpenThreads::Mutex          _operationsMutex;
152        osg::ref_ptr<osg::RefBlock> _operationsBlock;
153        Operations                  _operations;
154        Operations::iterator        _currentOperationIterator;
155
156        OperationThreads            _operationThreads;
157};
158
159/** OperationThread is a helper class for running Operation within a single thread.*/
160class OSG_EXPORT OperationThread : public Referenced, public OpenThreads::Thread
161{
162    public:
163        OperationThread();
164
165        void setParent(Object* parent) { _parent = parent; }
166
167        Object* getParent() { return _parent.get(); }
168
169        const Object* getParent() const { return _parent.get(); }
170
171
172        /** Set the OperationQueue. */
173        void setOperationQueue(OperationQueue* opq);
174
175        /** Get the OperationQueue. */
176        OperationQueue* getOperationQueue() { return _operationQueue.get(); }
177
178        /** Get the const OperationQueue. */
179        const OperationQueue* getOperationQueue() const { return _operationQueue.get(); }
180
181
182        /** Add operation to end of OperationQueue, this will be
183          * executed by the graphics thread once this operation gets to the head of the queue.*/
184        void add(Operation* operation);
185
186        /** Remove operation from OperationQueue.*/
187        void remove(Operation* operation);
188
189        /** Remove named operation from OperationQueue.*/
190        void remove(const std::string& name);
191
192        /** Remove all operations from OperationQueue.*/
193        void removeAllOperations();
194
195
196        /** Get the operation currently being run.*/
197        osg::ref_ptr<Operation> getCurrentOperation() { return _currentOperation; }
198
199        /** Run does the opertion thread run loop.*/
200        virtual void run();
201
202        void setDone(bool done);
203
204        bool getDone() const { return _done; }
205
206        /** Cancel this graphics thread.*/
207        virtual int cancel();
208
209    protected:
210
211        virtual ~OperationThread();
212
213        observer_ptr<Object>            _parent;
214
215        bool                            _done;
216
217        OpenThreads::Mutex              _threadMutex;
218        osg::ref_ptr<OperationQueue>    _operationQueue;
219        osg::ref_ptr<Operation>         _currentOperation;
220
221};
222
223typedef OperationThread OperationsThread;
224
225}
226
227#endif
Note: See TracBrowser for help on using the browser.