2012-03-22 01:36:20 +08:00
|
|
|
/* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
|
2008-04-01 18:49:53 +08:00
|
|
|
*
|
2012-03-22 01:36:20 +08:00
|
|
|
* 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
|
2008-04-01 18:49:53 +08:00
|
|
|
* (at your option) any later version. The full license is in LICENSE file
|
|
|
|
* included with this distribution, and on the openscenegraph.org website.
|
2012-03-22 01:36:20 +08:00
|
|
|
*
|
2008-04-01 18:49:53 +08:00
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2012-03-22 01:36:20 +08:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2008-04-01 18:49:53 +08:00
|
|
|
* OpenSceneGraph Public License for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _OPENTHREADS_READWRITEMUTEX_
|
|
|
|
#define _OPENTHREADS_READWRITEMUTEX_
|
|
|
|
|
|
|
|
#include <OpenThreads/Thread>
|
|
|
|
#include <OpenThreads/ReentrantMutex>
|
|
|
|
|
|
|
|
namespace OpenThreads {
|
|
|
|
|
|
|
|
class ReadWriteMutex
|
|
|
|
{
|
|
|
|
public:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-01 18:49:53 +08:00
|
|
|
ReadWriteMutex():
|
|
|
|
_readCount(0) {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-01 18:49:53 +08:00
|
|
|
virtual ~ReadWriteMutex() {}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-01 18:49:53 +08:00
|
|
|
virtual int readLock()
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
|
|
|
|
int result = 0;
|
|
|
|
if (_readCount==0)
|
|
|
|
{
|
|
|
|
result = _readWriteMutex.lock();
|
|
|
|
}
|
|
|
|
++_readCount;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
virtual int readUnlock()
|
|
|
|
{
|
|
|
|
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
|
|
|
|
int result = 0;
|
|
|
|
if (_readCount>0)
|
|
|
|
{
|
|
|
|
--_readCount;
|
|
|
|
if (_readCount==0)
|
|
|
|
{
|
|
|
|
result = _readWriteMutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int writeLock()
|
|
|
|
{
|
|
|
|
return _readWriteMutex.lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual int writeUnlock()
|
|
|
|
{
|
|
|
|
return _readWriteMutex.unlock();
|
|
|
|
}
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-01 18:49:53 +08:00
|
|
|
protected:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-01 18:49:53 +08:00
|
|
|
ReadWriteMutex(const ReadWriteMutex&) {}
|
|
|
|
ReadWriteMutex& operator = (const ReadWriteMutex&) { return *(this); }
|
|
|
|
|
|
|
|
#if 0
|
2012-03-22 01:36:20 +08:00
|
|
|
ReentrantMutex _readWriteMutex;
|
2008-04-01 18:49:53 +08:00
|
|
|
ReentrantMutex _readCountMutex;
|
|
|
|
#else
|
2012-03-22 01:36:20 +08:00
|
|
|
OpenThreads::Mutex _readWriteMutex;
|
2008-04-01 18:49:53 +08:00
|
|
|
OpenThreads::Mutex _readCountMutex;
|
2012-03-22 01:36:20 +08:00
|
|
|
#endif
|
2008-04-01 18:49:53 +08:00
|
|
|
unsigned int _readCount;
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-01 18:49:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
class ScopedReadLock
|
|
|
|
{
|
|
|
|
public:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-01 18:49:53 +08:00
|
|
|
ScopedReadLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.readLock(); }
|
|
|
|
~ScopedReadLock() { _mutex.readUnlock(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
protected:
|
2008-04-01 18:49:53 +08:00
|
|
|
ReadWriteMutex& _mutex;
|
2009-01-07 18:32:59 +08:00
|
|
|
|
|
|
|
ScopedReadLock& operator = (const ScopedReadLock&) { return *this; }
|
2008-04-01 18:49:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class ScopedWriteLock
|
|
|
|
{
|
|
|
|
public:
|
2012-03-22 01:36:20 +08:00
|
|
|
|
2008-04-01 18:49:53 +08:00
|
|
|
ScopedWriteLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.writeLock(); }
|
|
|
|
~ScopedWriteLock() { _mutex.writeUnlock(); }
|
2012-03-22 01:36:20 +08:00
|
|
|
|
|
|
|
protected:
|
2008-04-01 18:49:53 +08:00
|
|
|
ReadWriteMutex& _mutex;
|
2009-01-07 18:32:59 +08:00
|
|
|
|
|
|
|
ScopedWriteLock& operator = (const ScopedWriteLock&) { return *this; }
|
2008-04-01 18:49:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|