root/OpenSceneGraph/trunk/include/OpenThreads/Block @ 13041

Revision 13041, 4.3 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++-*- OpenThreads - Copyright (C) 1998-2007 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 _OPENTHREADS_BLOCK_
15#define _OPENTHREADS_BLOCK_
16
17#include <OpenThreads/Thread>
18#include <OpenThreads/Barrier>
19#include <OpenThreads/Condition>
20#include <OpenThreads/ScopedLock>
21
22namespace OpenThreads {
23
24/** Block is a block that can be used to halt a thread that is waiting another thread to release it.*/
25class Block
26{
27    public:
28
29        Block():
30            _released(false) {}
31
32        ~Block()
33        {
34            release();
35        }
36
37        inline bool block()
38        {
39            ScopedLock<OpenThreads::Mutex> mutlock(_mut);
40            if( !_released )
41            {
42                return _cond.wait(&_mut)==0;
43            }
44            else
45            {
46                return true;
47            }
48        }
49
50        inline bool block(unsigned long timeout)
51        {
52            ScopedLock<OpenThreads::Mutex> mutlock(_mut);
53            if( !_released )
54            {
55                return _cond.wait(&_mut, timeout)==0;
56            }
57            else
58            {
59                return true;
60            }
61        }
62
63        inline void release()
64        {
65            ScopedLock<OpenThreads::Mutex> mutlock(_mut);
66            if (!_released)
67            {
68                _released = true;
69                _cond.broadcast();
70            }
71        }
72
73        inline void reset()
74        {
75            ScopedLock<OpenThreads::Mutex> mutlock(_mut);
76            _released = false;
77        }
78
79        inline void set(bool doRelease)
80        {
81            if (doRelease!=_released)
82            {
83                if (doRelease) release();
84                else reset();
85            }
86        }
87
88    protected:
89
90        Mutex _mut;
91        Condition _cond;
92        bool _released;
93
94    private:
95
96        Block(const Block&) {}
97};
98
99/** BlockCount is a block that can be used to halt a thread that is waiting for a specified number of operations to be completed.*/
100class BlockCount
101{
102    public:
103
104        BlockCount(unsigned int blockCount):
105            _blockCount(blockCount),
106            _currentCount(0) {}
107
108        ~BlockCount()
109        {
110            _blockCount = 0;
111            release();
112        }
113
114        inline void completed()
115        {
116            OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
117            if (_currentCount>0)
118            {
119                --_currentCount;
120
121                if (_currentCount==0)
122                {
123                    // osg::notify(osg::NOTICE)<<"Released"<<std::endl;
124                    _cond.broadcast();
125                }
126            }
127        }
128
129        inline void block()
130        {
131            OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
132            if (_currentCount)
133                _cond.wait(&_mut);
134        }
135
136        inline void reset()
137        {
138            OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
139            if (_currentCount!=_blockCount)
140            {
141                if (_blockCount==0) _cond.broadcast();
142                _currentCount = _blockCount;
143            }
144        }
145
146        inline void release()
147        {
148            OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
149            if (_currentCount)
150            {
151                _currentCount = 0;
152                _cond.broadcast();
153            }
154        }
155
156        inline void setBlockCount(unsigned int blockCount) { _blockCount = blockCount; }
157
158        inline unsigned int getBlockCount() const { return _blockCount; }
159
160        inline unsigned int getCurrentCount() const { return _currentCount; }
161
162    protected:
163
164        OpenThreads::Mutex _mut;
165        OpenThreads::Condition _cond;
166        unsigned int _blockCount;
167        unsigned int _currentCount;
168
169    private:
170
171        BlockCount(const BlockCount&) {}
172
173};
174
175}
176
177#endif
Note: See TracBrowser for help on using the browser.