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_READWRITEMUTEX_
15#define _OPENTHREADS_READWRITEMUTEX_
17#include <OpenThreads/Thread>
18#include <OpenThreads/ReentrantMutex>
20namespace OpenThreads {
29 virtual ~ReadWriteMutex() {}
31 virtual int readLock()
33 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
37 result = _readWriteMutex.lock();
44 virtual int readUnlock()
46 OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
53 result = _readWriteMutex.unlock();
59 virtual int writeLock()
61 return _readWriteMutex.lock();
64 virtual int writeUnlock()
66 return _readWriteMutex.unlock();
71 ReadWriteMutex(const ReadWriteMutex&) {}
72 ReadWriteMutex& operator = (const ReadWriteMutex&) { return *(this); }
75 ReentrantMutex _readWriteMutex;
76 ReentrantMutex _readCountMutex;
78 OpenThreads::Mutex _readWriteMutex;
79 OpenThreads::Mutex _readCountMutex;
81 unsigned int _readCount;
89 ScopedReadLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.readLock(); }
90 ~ScopedReadLock() { _mutex.readUnlock(); }
93 ReadWriteMutex& _mutex;
95 ScopedReadLock& operator = (const ScopedReadLock&) { return *this; }
103 ScopedWriteLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.writeLock(); }
104 ~ScopedWriteLock() { _mutex.writeUnlock(); }
107 ReadWriteMutex& _mutex;
109 ScopedWriteLock& operator = (const ScopedWriteLock&) { return *this; }