1 martin 1.15 //%LICENSE////////////////////////////////////////////////////////////////
2 //
3 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
16 //
17 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
19 //
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 martin 1.15 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 mike 1.2 //
|
28 martin 1.15 //////////////////////////////////////////////////////////////////////////
|
29 mike 1.2 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_ReadWriteSem_h
33 #define Pegasus_ReadWriteSem_h
34
35 #include <Pegasus/Common/Config.h>
36 #include <Pegasus/Common/Linkage.h>
37 #include <Pegasus/Common/Semaphore.h>
38 #include <Pegasus/Common/Mutex.h>
39 #include <Pegasus/Common/AtomicInt.h>
40
41 PEGASUS_NAMESPACE_BEGIN
42
|
43 marek 1.10 /**
44 Every platform should decide which implementation for read/write locks
45 should be used in OpenPegasus by defining one of the following:
46 1.) PEGASUS_USE_POSIX_RWLOCK - POSIX standard based implementation
47 2.) PEGASUS_USE_SEMAPHORE_RWLOCK - mutex based implementation
48
49 The definition for each platform can be found in the according platform
50 header file: pegasus/src/Pegasus/Common/Platform_<Platform>.h
51 */
|
52 mike 1.2
|
53 marek 1.10 #if !defined(PEGASUS_USE_POSIX_RWLOCK) && !defined(PEGASUS_USE_SEMAPHORE_RWLOCK)
54 # error "Unsupported platform: ReadWriteSem.h implementation type missing"
|
55 karl 1.11
|
56 mike 1.2 #endif
57
58 //==============================================================================
59 //
60 // ReadWriteSemRep
61 //
62 //==============================================================================
63
64 #ifdef PEGASUS_USE_POSIX_RWLOCK
65 struct ReadWriteSemRep
66 {
67 pthread_rwlock_t rwlock;
68 };
69 #endif /* PEGASUS_USE_POSIX_RWLOCK */
70
71 #ifdef PEGASUS_USE_SEMAPHORE_RWLOCK
72 struct ReadWriteSemRep
73 {
74 Semaphore _rlock;
75 Mutex _wlock;
76 Mutex _internal_lock;
|
77 kumpf 1.13 AtomicInt _readers;
78 AtomicInt _writers;
|
79 kumpf 1.3 ReadWriteSemRep() :
|
80 kumpf 1.13 _rlock(10), _wlock(), _internal_lock(), _readers(0), _writers(0)
|
81 mike 1.2 {
82 }
83 };
84 #endif /* PEGASUS_USE_POSIX_RWLOCK */
85
86 //==============================================================================
87 //
88 // ReadWriteSem
89 //
90 //==============================================================================
91
92 class PEGASUS_COMMON_LINKAGE ReadWriteSem
93 {
94 public:
95
96 ReadWriteSem();
97
98 ~ReadWriteSem();
99
|
100 kumpf 1.13 void waitRead();
|
101 mike 1.2
|
102 kumpf 1.13 void waitWrite();
|
103 mike 1.2
|
104 kumpf 1.13 void unlockRead();
|
105 mike 1.2
|
106 kumpf 1.13 void unlockWrite();
|
107 mike 1.2
|
108 kumpf 1.7 private:
|
109 mike 1.2
110 ReadWriteSemRep _rwlock;
111 };
112
113 //==============================================================================
114 //
115 // ReadLock
116 //
117 //==============================================================================
118
119 class ReadLock
120 {
121 public:
122
123 ReadLock(ReadWriteSem& rwsem) : _rwsem(rwsem)
124 {
|
125 kumpf 1.13 _rwsem.waitRead();
|
126 mike 1.2 }
127
128 ~ReadLock()
129 {
|
130 kumpf 1.13 _rwsem.unlockRead();
|
131 mike 1.2 }
132
133 private:
134 ReadWriteSem & _rwsem;
135 };
136
137 //==============================================================================
138 //
139 // WriteLock
140 //
141 //==============================================================================
142
143 class WriteLock
144 {
145 public:
146
147 WriteLock(ReadWriteSem& rwsem) : _rwsem(rwsem)
148 {
|
149 kumpf 1.13 _rwsem.waitWrite();
|
150 mike 1.2 }
151
152 ~WriteLock()
153 {
|
154 kumpf 1.13 _rwsem.unlockWrite();
|
155 mike 1.2 }
156
157 private:
158 ReadWriteSem & _rwsem;
159 };
160
161 PEGASUS_NAMESPACE_END
162
163 #endif /* Pegasus_ReadWriteSem_h */
|