Block
Go to the documentation of this file.
/* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef _OPENTHREADS_BLOCK_
#define _OPENTHREADS_BLOCK_
#include <OpenThreads/Thread>
#include <OpenThreads/Barrier>
#include <OpenThreads/Condition>
#include <OpenThreads/ScopedLock>
namespace OpenThreads {
class Block
{
public:
Block():
_released(false) {}
~Block()
{
release();
}
inline bool block()
{
ScopedLock<OpenThreads::Mutex> mutlock(_mut);
if( !_released )
{
return _cond.wait(&_mut)==0;
}
else
{
return true;
}
}
inline bool block(unsigned long timeout)
{
ScopedLock<OpenThreads::Mutex> mutlock(_mut);
if( !_released )
{
return _cond.wait(&_mut, timeout)==0;
}
else
{
return true;
}
}
inline void release()
{
ScopedLock<OpenThreads::Mutex> mutlock(_mut);
if (!_released)
{
_released = true;
_cond.broadcast();
}
}
inline void reset()
{
ScopedLock<OpenThreads::Mutex> mutlock(_mut);
_released = false;
}
inline void set(bool doRelease)
{
if (doRelease!=_released)
{
if (doRelease) release();
else reset();
}
}
protected:
Mutex _mut;
Condition _cond;
bool _released;
private:
Block(const Block&) {}
};
class BlockCount
{
public:
BlockCount(unsigned int blockCount):
_blockCount(blockCount),
_currentCount(0) {}
~BlockCount()
{
_blockCount = 0;
release();
}
inline void completed()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
if (_currentCount>0)
{
--_currentCount;
if (_currentCount==0)
{
// osg::notify(osg::NOTICE)<<"Released"<<std::endl;
_cond.broadcast();
}
}
}
inline void block()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
if (_currentCount)
_cond.wait(&_mut);
}
inline void reset()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
if (_currentCount!=_blockCount)
{
if (_blockCount==0) _cond.broadcast();
_currentCount = _blockCount;
}
}
inline void release()
{
OpenThreads::ScopedLock<OpenThreads::Mutex> mutlock(_mut);
if (_currentCount)
{
_currentCount = 0;
_cond.broadcast();
}
}
inline void setBlockCount(unsigned int blockCount) { _blockCount = blockCount; }
inline unsigned int getBlockCount() const { return _blockCount; }
inline unsigned int getCurrentCount() const { return _currentCount; }
protected:
OpenThreads::Mutex _mut;
OpenThreads::Condition _cond;
unsigned int _blockCount;
unsigned int _currentCount;
private:
BlockCount(const BlockCount&) {}
};
}
#endif
Barrier
Condition
ScopedLock
Thread
OpenThreads::BlockCount::setBlockCount
void setBlockCount(unsigned int blockCount)
Definition Block:156
OpenThreads::BlockCount::BlockCount
BlockCount(unsigned int blockCount)
Definition Block:104
OpenThreads::BlockCount::_mut
OpenThreads::Mutex _mut
Definition Block:164
OpenThreads::BlockCount::getBlockCount
unsigned int getBlockCount() const
Definition Block:158
OpenThreads::BlockCount::_currentCount
unsigned int _currentCount
Definition Block:167
OpenThreads::BlockCount::~BlockCount
~BlockCount()
Definition Block:108
OpenThreads::BlockCount::completed
void completed()
Definition Block:114
OpenThreads::BlockCount::reset
void reset()
Definition Block:136
OpenThreads::BlockCount::_cond
OpenThreads::Condition _cond
Definition Block:165
OpenThreads::BlockCount::getCurrentCount
unsigned int getCurrentCount() const
Definition Block:160
OpenThreads::BlockCount::release
void release()
Definition Block:146
OpenThreads::BlockCount::block
void block()
Definition Block:129
OpenThreads::BlockCount::_blockCount
unsigned int _blockCount
Definition Block:166
OpenThreads::Block::block
bool block(unsigned long timeout)
Definition Block:50
OpenThreads::Block::reset
void reset()
Definition Block:73
OpenThreads::Block::block
bool block()
Definition Block:37
OpenThreads::Block::set
void set(bool doRelease)
Definition Block:79
OpenThreads::Block::release
void release()
Definition Block:63
OpenThreads::Block::_cond
Condition _cond
Definition Block:91
OpenThreads::Block::_mut
Mutex _mut
Definition Block:90
OpenThreads::Block::_released
bool _released
Definition Block:92
OpenThreads::Block::Block
Block()
Definition Block:29
OpenThreads::Block::~Block
~Block()
Definition Block:32
OpenThreads::Condition
This class provides an object-oriented thread condition interface.
Definition Condition:32
OpenThreads::Mutex
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
OpenThreads::ScopedLock
Definition ScopedLock:25
OpenThreads
Definition Affinity:26