OSG  3.4.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Block.h
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>
20 #include <OpenThreads/ScopedLock>
21 
22 namespace OpenThreads {
23 
25 class Block
26 {
27  public:
28 
29  Block():
30  _released(false) {}
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;
69  _cond.broadcast();
70  }
71  }
72 
73  inline void reset()
74  {
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 
92  bool _released;
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  {
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  {
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;
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 
166  unsigned int _blockCount;
167  unsigned int _currentCount;
168 
169  private:
170 
171  BlockCount(const BlockCount&) {}
172 
173 };
174 
175 }
176 
177 #endif
OpenThreads::Condition _cond
Definition: Block.h:165
BlockCount(unsigned int blockCount)
Definition: Block.h:104
This class provides an object-oriented thread mutex interface.
Definition: Mutex.h:31
void reset()
Definition: Block.h:73
virtual int broadcast()
bool block(unsigned long timeout)
Definition: Block.h:50
This class provides an object-oriented thread condition interface.
Definition: Condition.h:32
void set(bool doRelease)
Definition: Block.h:79
unsigned int getCurrentCount() const
Definition: Block.h:160
OpenThreads::Mutex _mut
Definition: Block.h:164
virtual int wait(Mutex *mutex)
unsigned int _blockCount
Definition: Block.h:166
Condition _cond
Definition: Block.h:91
bool block()
Definition: Block.h:37
unsigned int getBlockCount() const
Definition: Block.h:158
void setBlockCount(unsigned int blockCount)
Definition: Block.h:156
void release()
Definition: Block.h:63
bool _released
Definition: Block.h:92
unsigned int _currentCount
Definition: Block.h:167