openscenegraph
Block
Go to the documentation of this file.
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>
21
22namespace OpenThreads {
23
25class Block
26{
27 public:
28
31
33 {
34 release();
35 }
36
37 inline bool block()
38 {
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 {
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 {
66 if (!_released)
67 {
68 _released = true;
70 }
71 }
72
73 inline void reset()
74 {
76 _released = false;
77 }
78
79 inline void set(bool doRelease)
80 {
82 {
83 if (doRelease) release();
84 else reset();
85 }
86 }
87
88 protected:
89
93
94 private:
95
96 Block(const Block&) {}
97};
98
101{
102 public:
103
104 BlockCount(unsigned int blockCount):
105 _blockCount(blockCount),
106 _currentCount(0) {}
107
109 {
110 _blockCount = 0;
111 release();
112 }
113
114 inline void completed()
115 {
117 if (_currentCount>0)
118 {
120
121 if (_currentCount==0)
122 {
123 // osg::notify(osg::NOTICE)<<"Released"<<std::endl;
125 }
126 }
127 }
128
129 inline void block()
130 {
132 if (_currentCount)
133 _cond.wait(&_mut);
134 }
135
136 inline void reset()
137 {
140 {
141 if (_blockCount==0) _cond.broadcast();
143 }
144 }
145
146 inline void release()
147 {
149 if (_currentCount)
150 {
151 _currentCount = 0;
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
166 unsigned int _blockCount;
167 unsigned int _currentCount;
168
169 private:
170
171 BlockCount(const BlockCount&) {}
172
173};
174
175}
176
177#endif
Definition Block:101
void setBlockCount(unsigned int blockCount)
Definition Block:156
BlockCount(unsigned int blockCount)
Definition Block:104
OpenThreads::Mutex _mut
Definition Block:164
unsigned int getBlockCount() const
Definition Block:158
unsigned int _currentCount
Definition Block:167
~BlockCount()
Definition Block:108
void completed()
Definition Block:114
void reset()
Definition Block:136
OpenThreads::Condition _cond
Definition Block:165
unsigned int getCurrentCount() const
Definition Block:160
void release()
Definition Block:146
void block()
Definition Block:129
unsigned int _blockCount
Definition Block:166
Definition Block:26
bool block(unsigned long timeout)
Definition Block:50
void reset()
Definition Block:73
bool block()
Definition Block:37
void set(bool doRelease)
Definition Block:79
void release()
Definition Block:63
Condition _cond
Definition Block:91
Mutex _mut
Definition Block:90
bool _released
Definition Block:92
Block()
Definition Block:29
~Block()
Definition Block:32
This class provides an object-oriented thread condition interface.
Definition Condition:32
virtual int wait(Mutex *mutex)
virtual int broadcast()
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
Definition ScopedLock:25
Definition Affinity:26