OpenThreads
ReadWriteMutex
Go to the documentation of this file.
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>
19
20namespace OpenThreads {
21
23{
24 public:
25
28
29 virtual ~ReadWriteMutex() {}
30
31 virtual int readLock()
32 {
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 {
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
72 ReadWriteMutex& operator = (const ReadWriteMutex&) { return *(this); }
73
74#if 0
77#else
80#endif
81 unsigned int _readCount;
82
83};
84
86{
87 public:
88
89 ScopedReadLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.readLock(); }
90 ~ScopedReadLock() { _mutex.readUnlock(); }
91
92 protected:
94
95 ScopedReadLock& operator = (const ScopedReadLock&) { return *this; }
96};
97
98
100{
101 public:
102
103 ScopedWriteLock(ReadWriteMutex& mutex):_mutex(mutex) { _mutex.writeLock(); }
104 ~ScopedWriteLock() { _mutex.writeUnlock(); }
105
106 protected:
108
109 ScopedWriteLock& operator = (const ScopedWriteLock&) { return *this; }
110};
111
112}
113
114#endif
Definition Affinity:26
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
Definition ReadWriteMutex:23
ReadWriteMutex & operator=(const ReadWriteMutex &)
Definition ReadWriteMutex:72
ReadWriteMutex()
Definition ReadWriteMutex:26
OpenThreads::Mutex _readWriteMutex
Definition ReadWriteMutex:78
unsigned int _readCount
Definition ReadWriteMutex:81
virtual ~ReadWriteMutex()
Definition ReadWriteMutex:29
virtual int readUnlock()
Definition ReadWriteMutex:44
OpenThreads::Mutex _readCountMutex
Definition ReadWriteMutex:79
ReadWriteMutex(const ReadWriteMutex &)
Definition ReadWriteMutex:71
virtual int writeUnlock()
Definition ReadWriteMutex:64
virtual int readLock()
Definition ReadWriteMutex:31
virtual int writeLock()
Definition ReadWriteMutex:59
ReadWriteMutex & _mutex
Definition ReadWriteMutex:93
ScopedReadLock & operator=(const ScopedReadLock &)
Definition ReadWriteMutex:95
~ScopedReadLock()
Definition ReadWriteMutex:90
ScopedReadLock(ReadWriteMutex &mutex)
Definition ReadWriteMutex:89
ScopedWriteLock & operator=(const ScopedWriteLock &)
Definition ReadWriteMutex:109
ScopedWriteLock(ReadWriteMutex &mutex)
Definition ReadWriteMutex:103
ReadWriteMutex & _mutex
Definition ReadWriteMutex:107
~ScopedWriteLock()
Definition ReadWriteMutex:104
Definition ReentrantMutex:24
Definition ScopedLock:25

osg logo
Generated at Wed Jul 23 2025 00:00:00 for the OpenThreads by doxygen 1.14.0.