1 martin 1.14 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.15 //
|
3 martin 1.14 // 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 martin 1.15 //
|
10 martin 1.14 // 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 martin 1.15 //
|
17 martin 1.14 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.15 //
|
20 martin 1.14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.14 // 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 martin 1.15 //
|
28 martin 1.14 //////////////////////////////////////////////////////////////////////////
|
29 mike 1.2 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #include "ReadWriteSem.h"
33 #include "Time.h"
34 #include "PegasusAssert.h"
35 #include "Threads.h"
|
36 kumpf 1.13 #include "Exception.h"
37 #include "System.h"
|
38 mike 1.2
39 PEGASUS_NAMESPACE_BEGIN
40
41 //==============================================================================
42 //
43 // PEGASUS_USE_POSIX_RWLOCK
44 //
45 //==============================================================================
46
47 #ifdef PEGASUS_USE_POSIX_RWLOCK
48
|
49 kumpf 1.12 ReadWriteSem::ReadWriteSem()
|
50 mike 1.2 {
51 pthread_rwlock_init(&_rwlock.rwlock, NULL);
52 }
53
54 ReadWriteSem::~ReadWriteSem()
55 {
|
56 kumpf 1.5 int r = 0;
|
57 mike 1.6 while ((r = pthread_rwlock_destroy(&_rwlock.rwlock)) == EBUSY ||
|
58 kumpf 1.5 (r == -1 && errno == EBUSY))
|
59 mike 1.2 {
60 Threads::yield();
61 }
62 }
63
|
64 kumpf 1.12 void ReadWriteSem::waitRead()
|
65 mike 1.2 {
|
66 kumpf 1.13 int r = pthread_rwlock_rdlock(&_rwlock.rwlock);
67
68 if (r != 0)
|
69 mike 1.2 {
|
70 kumpf 1.13 if (r != -1)
71 {
72 // Special behavior for Single UNIX Specification, Version 3
73 errno = r;
74 }
75
76 throw Exception(MessageLoaderParms(
77 "Common.InternalException.READ_LOCK_FAILED",
78 "Failed to acquire read lock: $0",
79 PEGASUS_SYSTEM_ERRORMSG_NLS));
|
80 mike 1.2 }
81 }
82
|
83 kumpf 1.12 void ReadWriteSem::waitWrite()
|
84 mike 1.2 {
|
85 kumpf 1.13 int r = pthread_rwlock_wrlock(&_rwlock.rwlock);
86
87 if (r != 0)
|
88 mike 1.2 {
|
89 kumpf 1.13 if (r != -1)
90 {
91 // Special behavior for Single UNIX Specification, Version 3
92 errno = r;
93 }
94
95 throw Exception(MessageLoaderParms(
96 "Common.InternalException.WRITE_LOCK_FAILED",
97 "Failed to acquire write lock: $0",
98 PEGASUS_SYSTEM_ERRORMSG_NLS));
|
99 mike 1.2 }
|
100 kumpf 1.12 }
|
101 kumpf 1.10
|
102 kumpf 1.12 void ReadWriteSem::unlockRead()
103 {
|
104 kumpf 1.10 int rc = pthread_rwlock_unlock(&_rwlock.rwlock);
105 // All documented error codes represent coding errors.
106 PEGASUS_ASSERT(rc == 0);
|
107 mike 1.2 }
108
|
109 kumpf 1.12 void ReadWriteSem::unlockWrite()
|
110 mike 1.2 {
|
111 kumpf 1.12 int rc = pthread_rwlock_unlock(&_rwlock.rwlock);
112 // All documented error codes represent coding errors.
113 PEGASUS_ASSERT(rc == 0);
|
114 mike 1.2 }
115
116 #endif /* PEGASUS_USE_POSIX_RWLOCK */
117
118 //==============================================================================
119 //
120 // PEGASUS_USE_SEMAPHORE_RWLOCK
121 //
122 //==============================================================================
123
124 #if defined(PEGASUS_USE_SEMAPHORE_RWLOCK)
125
126 // // If i get cancelled, I MUST ensure:
127 // 1) I do not hold the internal mutex
128 // 2) I do not hold the write lock
129 // 3) I am not using a reader slot
130
|
131 kumpf 1.12 ReadWriteSem::ReadWriteSem() : _rwlock()
|
132 mike 1.2 {
133 }
134
135 ReadWriteSem::~ReadWriteSem()
136 {
137 // lock everyone out of this object
138 try
139 {
140 _rwlock._internal_lock.lock();
141 }
|
142 kumpf 1.13 catch (...)
|
143 mike 1.2 {
144 PEGASUS_ASSERT(0);
145 }
|
146 kumpf 1.12 while (_rwlock._readers.get() > 0 || _rwlock._writers.get() > 0)
|
147 mike 1.2 {
148 Threads::yield();
149 }
150 _rwlock._internal_lock.unlock();
151 }
152
|
153 kumpf 1.12 void ReadWriteSem::waitRead()
|
154 mike 1.2 {
|
155 kumpf 1.12 // Lock the internal mutex to ensure only one waiter is processed at a time.
156 AutoMutex lock(_rwlock._internal_lock);
157
158 // Wait for the existing writer (if any) to clear.
159 while (_rwlock._writers.get() > 0)
160 {
161 Threads::yield();
|
162 mike 1.2 }
|
163 kumpf 1.12
164 // Wait for a reader slot to open up.
165 _rwlock._rlock.wait();
166
167 // Increment the number of readers.
168 _rwlock._readers++;
169 }
170
171 void ReadWriteSem::waitWrite()
172 {
173 // Lock the internal mutex to ensure only one waiter is processed at a time.
174 AutoMutex lock(_rwlock._internal_lock);
175
176 // Allow all the readers to exit.
177 while (_rwlock._readers.get() > 0)
178 {
179 Threads::yield();
180 }
181
182 // Obtain the write mutex.
183 _rwlock._wlock.lock();
184 kumpf 1.12
185 // Set the writer count to one.
186 _rwlock._writers = 1;
|
187 mike 1.2 }
188
|
189 kumpf 1.12 void ReadWriteSem::unlockRead()
|
190 mike 1.2 {
|
191 kumpf 1.12 PEGASUS_ASSERT(_rwlock._readers.get() > 0);
192 _rwlock._readers--;
193 _rwlock._rlock.signal();
|
194 mike 1.2 }
195
|
196 kumpf 1.12 void ReadWriteSem::unlockWrite()
|
197 mike 1.2 {
|
198 kumpf 1.12 PEGASUS_ASSERT(_rwlock._writers.get() == 1);
199 _rwlock._writers = 0;
200 _rwlock._wlock.unlock();
|
201 mike 1.2 }
202
203 #endif /* !PEGASUS_USE_SEMAPHORE_RWLOCK */
204
205 PEGASUS_NAMESPACE_END
|