| | 95 | /** BlockCount is a block that can be used to halt a thread that is waiting for a specified number of operations to be completed.*/ |
|---|
| | 96 | class BlockCount |
|---|
| | 97 | { |
|---|
| | 98 | public: |
|---|
| | 99 | |
|---|
| | 100 | BlockCount(unsigned int blockCount): |
|---|
| | 101 | _blockCount(blockCount), |
|---|
| | 102 | _currentCount(0) {} |
|---|
| | 103 | |
|---|
| | 104 | ~BlockCount() |
|---|
| | 105 | { |
|---|
| | 106 | _blockCount = 0; |
|---|
| | 107 | release(); |
|---|
| | 108 | } |
|---|
| | 109 | |
|---|
| | 110 | inline void completed() |
|---|
| | 111 | { |
|---|
| | 112 | OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut); |
|---|
| | 113 | if (_currentCount>0) |
|---|
| | 114 | { |
|---|
| | 115 | --_currentCount; |
|---|
| | 116 | |
|---|
| | 117 | if (_currentCount==0) |
|---|
| | 118 | { |
|---|
| | 119 | // osg::notify(osg::NOTICE)<<"Released"<<std::endl; |
|---|
| | 120 | _cond.broadcast(); |
|---|
| | 121 | } |
|---|
| | 122 | } |
|---|
| | 123 | } |
|---|
| | 124 | |
|---|
| | 125 | inline void block() |
|---|
| | 126 | { |
|---|
| | 127 | OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut); |
|---|
| | 128 | if (_currentCount) |
|---|
| | 129 | _cond.wait(&_mut); |
|---|
| | 130 | } |
|---|
| | 131 | |
|---|
| | 132 | inline void reset() |
|---|
| | 133 | { |
|---|
| | 134 | OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut); |
|---|
| | 135 | if (_currentCount!=_blockCount) |
|---|
| | 136 | { |
|---|
| | 137 | if (_blockCount==0) _cond.broadcast(); |
|---|
| | 138 | _currentCount = _blockCount; |
|---|
| | 139 | } |
|---|
| | 140 | } |
|---|
| | 141 | |
|---|
| | 142 | inline void release() |
|---|
| | 143 | { |
|---|
| | 144 | OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut); |
|---|
| | 145 | if (_currentCount) |
|---|
| | 146 | { |
|---|
| | 147 | _currentCount = 0; |
|---|
| | 148 | _cond.broadcast(); |
|---|
| | 149 | } |
|---|
| | 150 | } |
|---|
| | 151 | |
|---|
| | 152 | inline void setBlockCount(unsigned int blockCount) { _blockCount = blockCount; } |
|---|
| | 153 | |
|---|
| | 154 | inline unsigned int getBlockCount() const { return _blockCount; } |
|---|
| | 155 | |
|---|
| | 156 | inline unsigned int getCurrentCount() const { return _currentCount; } |
|---|
| | 157 | |
|---|
| | 158 | protected: |
|---|
| | 159 | |
|---|
| | 160 | |
|---|
| | 161 | OpenThreads::Mutex _mut; |
|---|
| | 162 | OpenThreads::Condition _cond; |
|---|
| | 163 | unsigned int _blockCount; |
|---|
| | 164 | unsigned int _currentCount; |
|---|
| | 165 | }; |
|---|
| | 166 | |
|---|