1/* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
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.
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.
14#ifndef _OPENTHREADS_BLOCK_
15#define _OPENTHREADS_BLOCK_
17#include <OpenThreads/Thread>
18#include <OpenThreads/Barrier>
19#include <OpenThreads/Condition>
20#include <OpenThreads/ScopedLock>
22namespace OpenThreads {
24/** Block is a block that can be used to halt a thread that is waiting another thread to release it.*/
39 ScopedLock<OpenThreads::Mutex> mutlock(_mut);
42 return _cond.wait(&_mut)==0;
50 inline bool block(unsigned long timeout)
52 ScopedLock<OpenThreads::Mutex> mutlock(_mut);
55 return _cond.wait(&_mut, timeout)==0;
65 ScopedLock<OpenThreads::Mutex> mutlock(_mut);
75 ScopedLock<OpenThreads::Mutex> mutlock(_mut);
79 inline void set(bool doRelease)
81 if (doRelease!=_released)
83 if (doRelease) release();
96 Block(const Block&) {}
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.*/
104 BlockCount(unsigned int blockCount):
105 _blockCount(blockCount),
114 inline void completed()
116 OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
121 if (_currentCount==0)
123 // osg::notify(osg::NOTICE)<<"Released"<<std::endl;
131 OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
138 OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
139 if (_currentCount!=_blockCount)
141 if (_blockCount==0) _cond.broadcast();
142 _currentCount = _blockCount;
146 inline void release()
148 OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
156 inline void setBlockCount(unsigned int blockCount) { _blockCount = blockCount; }
158 inline unsigned int getBlockCount() const { return _blockCount; }
160 inline unsigned int getCurrentCount() const { return _currentCount; }
164 OpenThreads::Mutex _mut;
165 OpenThreads::Condition _cond;
166 unsigned int _blockCount;
167 unsigned int _currentCount;
171 BlockCount(const BlockCount&) {}