root/OpenSceneGraph/trunk/include/OpenThreads/ReadWriteMutex @ 13041

Revision 13041, 2.8 kB (checked in by robert, 2 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*-c++-*- OpenThreads - Copyright (C) 1998-2007 Robert Osfield
2 *
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.
7 *
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.
12*/
13
14#ifndef _OPENTHREADS_READWRITEMUTEX_
15#define _OPENTHREADS_READWRITEMUTEX_
16
17#include <OpenThreads/Thread>
18#include <OpenThreads/ReentrantMutex>
19
20namespace OpenThreads {
21
22class ReadWriteMutex
23{
24    public:
25
26        ReadWriteMutex():
27            _readCount(0) {}
28
29        virtual ~ReadWriteMutex() {}
30
31        virtual int readLock()
32        {
33            OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
34            int result = 0;
35            if (_readCount==0)
36            {
37                result = _readWriteMutex.lock();
38            }
39            ++_readCount;
40            return result;
41        }
42
43
44        virtual int readUnlock()
45        {
46            OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_readCountMutex);
47            int result = 0;
48            if (_readCount>0)
49            {
50                --_readCount;
51                if (_readCount==0)
52                {
53                    result = _readWriteMutex.unlock();
54                }
55            }
56            return result;
57        }
58
59        virtual int writeLock()
60        {
61            return _readWriteMutex.lock();
62        }
63
64        virtual int writeUnlock()
65        {
66            return _readWriteMutex.unlock();
67        }
68
69    protected:
70
71        ReadWriteMutex(const ReadWriteMutex&) {}
72        ReadWriteMutex& operator = (const ReadWriteMutex&) { return *(this); }
73
74#if 0
75        ReentrantMutex  _readWriteMutex;
76        ReentrantMutex  _readCountMutex;
77#else
78        OpenThreads::Mutex  _readWriteMutex;
79        OpenThreads::Mutex  _readCountMutex;
80#endif
81        unsigned int    _readCount;
82
83};
84
85class ScopedReadLock
86{
87    public:
88
89        ScopedReadLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.readLock(); }
90        ~ScopedReadLock() { _mutex.readUnlock(); }
91
92    protected:
93        ReadWriteMutex& _mutex;
94
95        ScopedReadLock& operator = (const ScopedReadLock&) { return *this; }
96};
97
98
99class ScopedWriteLock
100{
101    public:
102
103        ScopedWriteLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.writeLock(); }
104        ~ScopedWriteLock() { _mutex.writeUnlock(); }
105
106    protected:
107        ReadWriteMutex& _mutex;
108
109        ScopedWriteLock& operator = (const ScopedWriteLock&) { return *this; }
110};
111
112}
113
114#endif
Note: See TracBrowser for help on using the browser.