|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qsystemreadwritelock_p.h" |
|
43 |
|
44 #include <QSharedMemory> |
|
45 #include <QSystemSemaphore> |
|
46 |
|
47 ///#define QSYSTEMREADWRITELOCK_DEBUG 1 |
|
48 #ifdef QSYSTEMREADWRITELOCK_DEBUG |
|
49 #include <QDebug> |
|
50 #endif |
|
51 |
|
52 QT_USE_NAMESPACE |
|
53 |
|
54 QTM_BEGIN_NAMESPACE |
|
55 |
|
56 class QSystemReadWriteLockPrivate |
|
57 { |
|
58 public: |
|
59 QSystemReadWriteLockPrivate(const QString &key, QSystemReadWriteLock::AccessMode mode); |
|
60 ~QSystemReadWriteLockPrivate(); |
|
61 |
|
62 int &accessCount(); |
|
63 unsigned int &waitingReaders(); |
|
64 unsigned int &waitingWriters(); |
|
65 |
|
66 QSystemReadWriteLock::SystemReadWriteLockError convertError(QSystemSemaphore::SystemSemaphoreError error); |
|
67 QSystemReadWriteLock::SystemReadWriteLockError convertError(QSharedMemory::SharedMemoryError error); |
|
68 |
|
69 QString m_key; |
|
70 |
|
71 QSharedMemory m_counts; |
|
72 QSystemSemaphore m_readerWait; |
|
73 QSystemSemaphore m_writerWait; |
|
74 bool m_isInitialized; |
|
75 |
|
76 QSystemReadWriteLock::SystemReadWriteLockError m_error; |
|
77 QString m_errorString; |
|
78 }; |
|
79 |
|
80 QSystemReadWriteLockPrivate::QSystemReadWriteLockPrivate(const QString &key, QSystemReadWriteLock::AccessMode mode) |
|
81 :m_key(key), m_counts(key + QLatin1String("_counts")), |
|
82 m_readerWait(key + QLatin1String("_readerWait"), 0, (mode==QSystemReadWriteLock::Create)?QSystemSemaphore::Create:QSystemSemaphore::Open), |
|
83 m_writerWait(key + QLatin1String("_writerWait"), 0, (mode==QSystemReadWriteLock::Create)?QSystemSemaphore::Create:QSystemSemaphore::Open), |
|
84 m_isInitialized(false), m_error(QSystemReadWriteLock::NoError), m_errorString(QString()) |
|
85 { |
|
86 bool isAttached = false; |
|
87 int retries=5; |
|
88 for (int i=0; i < retries; ++i){ |
|
89 if (m_counts.attach()) { |
|
90 isAttached = true; |
|
91 break; |
|
92 } |
|
93 else { |
|
94 if (m_counts.error() == QSharedMemory::NotFound) { |
|
95 if (m_counts.create(3 * sizeof(int))) { |
|
96 isAttached = true; |
|
97 break; |
|
98 } |
|
99 } |
|
100 } |
|
101 } |
|
102 |
|
103 if (!isAttached) { |
|
104 m_error = convertError(m_counts.error()); |
|
105 m_errorString = QObject::tr("QSystemReadWriteLockPrivate::QSystemReadWriteLockPrivate: " |
|
106 "Unable to create/attach to shared memory"); |
|
107 return; |
|
108 } else { |
|
109 if (mode == QSystemReadWriteLock::Create) { |
|
110 if(!m_counts.lock()) { |
|
111 m_error = convertError(m_counts.error()); |
|
112 m_errorString = QObject::tr("QSystemReadWriteLockPrivate::QSystemReadWriteLockPrivate: " |
|
113 "Unable to initialize shared memory"); |
|
114 return; |
|
115 } |
|
116 int * data = (int *)m_counts.data(); |
|
117 ::memset(data, 0, 3 * sizeof(int)); |
|
118 m_counts.unlock(); |
|
119 } |
|
120 m_isInitialized = true; |
|
121 } |
|
122 } |
|
123 |
|
124 QSystemReadWriteLockPrivate::~QSystemReadWriteLockPrivate() |
|
125 { |
|
126 } |
|
127 |
|
128 QSystemReadWriteLock::SystemReadWriteLockError QSystemReadWriteLockPrivate::convertError(QSystemSemaphore::SystemSemaphoreError error) |
|
129 { |
|
130 switch(error) { |
|
131 case QSystemSemaphore::NoError: |
|
132 return QSystemReadWriteLock::NoError; |
|
133 case QSystemSemaphore::PermissionDenied: |
|
134 return QSystemReadWriteLock::PermissionDenied; |
|
135 case QSystemSemaphore::KeyError: |
|
136 return QSystemReadWriteLock::KeyError; |
|
137 case QSystemSemaphore::NotFound: |
|
138 return QSystemReadWriteLock::NotFound; |
|
139 case QSystemSemaphore::OutOfResources: |
|
140 return QSystemReadWriteLock::OutOfResources; |
|
141 case QSystemSemaphore::AlreadyExists: |
|
142 case QSystemSemaphore::UnknownError: |
|
143 default: |
|
144 return QSystemReadWriteLock::UnknownError; |
|
145 } |
|
146 } |
|
147 |
|
148 QSystemReadWriteLock::SystemReadWriteLockError QSystemReadWriteLockPrivate::convertError(QSharedMemory::SharedMemoryError error) |
|
149 { |
|
150 switch(error){ |
|
151 case QSharedMemory::NoError: |
|
152 return QSystemReadWriteLock::NoError; |
|
153 case QSharedMemory::PermissionDenied: |
|
154 return QSystemReadWriteLock::PermissionDenied; |
|
155 case QSharedMemory::KeyError: |
|
156 return QSystemReadWriteLock::KeyError; |
|
157 case QSharedMemory::NotFound: |
|
158 return QSystemReadWriteLock::NotFound; |
|
159 case QSharedMemory::LockError: |
|
160 return QSystemReadWriteLock::LockError; |
|
161 case QSharedMemory::UnknownError: |
|
162 default: |
|
163 return QSystemReadWriteLock::UnknownError; |
|
164 } |
|
165 } |
|
166 |
|
167 QSystemReadWriteLock::QSystemReadWriteLock(const QString &key, AccessMode mode) |
|
168 :d(new QSystemReadWriteLockPrivate(key,mode)) |
|
169 { |
|
170 } |
|
171 |
|
172 QSystemReadWriteLock::~QSystemReadWriteLock() |
|
173 { |
|
174 Q_ASSERT(d); |
|
175 delete d; |
|
176 d = 0; |
|
177 } |
|
178 |
|
179 bool QSystemReadWriteLock::lockForRead() |
|
180 { |
|
181 #if defined QSYSTEMREADWRITELOCK_DEBUG |
|
182 qDebug() << "QSystemReadWriteLock::lockForRead() <start>\naccessCount =" |
|
183 << d->accessCount() << "\twaitingWriters=" << d->waitingWriters() << "\twaitingReaders=" << d->waitingReaders(); |
|
184 #endif |
|
185 if (!d->m_isInitialized) { |
|
186 d->m_error = QSystemReadWriteLock::FailedToInitialize; |
|
187 d->m_errorString = QObject::tr("QSystemReadWriteLock::lockForRead(): cannot peform operation, lock initialization had not been successful"); |
|
188 return false; |
|
189 } |
|
190 |
|
191 if(!d->m_counts.lock()) { |
|
192 d->m_error = d->convertError(d->m_counts.error()); |
|
193 d->m_errorString = QObject::tr("QSystemReadWriteLock::lockForRead(): cannot perform operation, locking of shared memory was unsuccessful"); |
|
194 return false; |
|
195 } |
|
196 |
|
197 while(d->accessCount() < 0 || d->waitingWriters() > 0 ) { |
|
198 ++d->waitingReaders(); |
|
199 d->m_counts.unlock(); |
|
200 d->m_readerWait.acquire(); |
|
201 d->m_counts.lock(); |
|
202 --d->waitingReaders(); |
|
203 } |
|
204 ++d->accessCount(); |
|
205 Q_ASSERT_X(d->accessCount() > 0, "QSystemReadWriteLock::lockForRead()", "Overflow in lock counter"); |
|
206 #if defined QSYSTEMREADWRITELOCK_DEBUG |
|
207 qDebug() << "QSystemReadWriteLock::lockForRead() <end>\naccessCount =" |
|
208 << d->accessCount() << "\twaitingWriters=" << d->waitingWriters() << "\twaitingReaders=" << d->waitingReaders(); |
|
209 #endif |
|
210 |
|
211 d->m_error = QSystemReadWriteLock::NoError; |
|
212 d->m_errorString.clear(); |
|
213 d->m_counts.unlock(); |
|
214 return true; |
|
215 } |
|
216 |
|
217 bool QSystemReadWriteLock::lockForWrite() |
|
218 { |
|
219 #if defined QSYSTEMREADWRITELOCK_DEBUG |
|
220 qDebug() << "QSystemReadWriteLock::lockForWrite() <start>\naccessCount =" |
|
221 << d->accessCount() << "\twaitingWriters=" << d->waitingWriters() << "\twaitingReaders=" << d->waitingReaders(); |
|
222 #endif |
|
223 if (!d->m_isInitialized) { |
|
224 d->m_error = QSystemReadWriteLock::FailedToInitialize; |
|
225 d->m_errorString = QObject::tr("QSystemReadWriteLock::lockForWrite(): cannot peform operation, lock initialization had not been successful"); |
|
226 return false; |
|
227 } |
|
228 |
|
229 if(!d->m_counts.lock()) { |
|
230 d->m_error = d->convertError(d->m_counts.error()); |
|
231 d->m_errorString = QObject::tr("QSystemReadWriteLock::lockForwrite(): cannot perform operation, locking of shared memory was unsuccessful"); |
|
232 return false; |
|
233 } |
|
234 |
|
235 #if defined QSYSTEMREADWRITELOCK_DEBUG |
|
236 qDebug() << "QSystemReadWriteLock::lockForWrite() <start>\naccessCount =" |
|
237 << d->accessCount() << "\twaitingWriters=" << d->waitingWriters() << "\twaitingReaders=" << d->waitingReaders(); |
|
238 #endif |
|
239 while(d->accessCount() != 0) { |
|
240 ++d->waitingWriters(); |
|
241 d->m_counts.unlock(); |
|
242 d->m_writerWait.acquire(); |
|
243 d->m_counts.lock(); |
|
244 --d->waitingWriters(); |
|
245 } |
|
246 --d->accessCount(); |
|
247 #if defined QSYSTEMREADWRITELOCK_DEBUG |
|
248 qDebug() << "QSystemReadWriteLock::lockForWrite() <end>\naccessCount =" |
|
249 << d->accessCount() << "\twaitingWriters=" << d->waitingWriters() << "\twaitingReaders=" << d->waitingReaders(); |
|
250 #endif |
|
251 |
|
252 Q_ASSERT_X(d->accessCount() < 0, "QSystemReadWriteLock::lockForWrite()", "Overflow in lock counter"); |
|
253 |
|
254 d->m_error = QSystemReadWriteLock::NoError; |
|
255 d->m_errorString.clear(); |
|
256 d->m_counts.unlock(); |
|
257 return true; |
|
258 } |
|
259 |
|
260 void QSystemReadWriteLock::unlock() |
|
261 { |
|
262 if (!d->m_isInitialized) { |
|
263 d->m_error = QSystemReadWriteLock::FailedToInitialize; |
|
264 d->m_errorString = QObject::tr("QSystemReadWriteLock::unlock(): cannot peform operation, lock initialization had not been successful"); |
|
265 return; |
|
266 } |
|
267 |
|
268 if(!d->m_counts.lock()) { |
|
269 d->m_error = d->convertError(d->m_counts.error()); |
|
270 d->m_errorString = QObject::tr("QSystemReadWriteLock::lockForwrite(): cannot perform operation, locking of shared memory was unsuccessful"); |
|
271 return; |
|
272 } |
|
273 |
|
274 Q_ASSERT_X(d->accessCount() != 0, "QSystemReadWriteLock::unlock()", "Cannot unlock an unlocked lock"); |
|
275 |
|
276 bool unlocked = false; |
|
277 if (d->accessCount() > 0) { |
|
278 unlocked = --d->accessCount() == 0; |
|
279 } else if (d->accessCount() < 0 && ++d->accessCount() == 0) { |
|
280 unlocked = true; |
|
281 } |
|
282 |
|
283 if (unlocked) { |
|
284 if (d->waitingWriters() > 0 ) { |
|
285 d->m_writerWait.release(); |
|
286 } else if (d->waitingReaders()) { |
|
287 d->m_readerWait.release(d->waitingReaders()); |
|
288 } |
|
289 } |
|
290 #if defined QSYSTEMREADWRITELOCK_DEBUG |
|
291 qDebug() << "QSystemReadWriteLock::unlock() <end>\naccessCount =" |
|
292 << d->accessCount() << "\twaitingWriters=" << d->waitingWriters() << "\twaitingReaders=" << d->waitingReaders(); |
|
293 #endif |
|
294 d->m_error = QSystemReadWriteLock::NoError; |
|
295 d->m_errorString.clear(); |
|
296 d->m_counts.unlock(); |
|
297 return; |
|
298 } |
|
299 |
|
300 int& QSystemReadWriteLockPrivate::accessCount() |
|
301 { |
|
302 return *((int *)m_counts.data()); |
|
303 } |
|
304 |
|
305 unsigned int &QSystemReadWriteLockPrivate::waitingReaders() |
|
306 { |
|
307 return *(((unsigned int*)m_counts.data())+1); |
|
308 } |
|
309 |
|
310 unsigned int &QSystemReadWriteLockPrivate::waitingWriters() |
|
311 { |
|
312 return *(((unsigned int*)m_counts.data())+2); |
|
313 } |
|
314 |
|
315 QSystemReadWriteLock::SystemReadWriteLockError QSystemReadWriteLock::error() const |
|
316 { |
|
317 return d->m_error; |
|
318 } |
|
319 |
|
320 QString QSystemReadWriteLock::errorString() const |
|
321 { |
|
322 return d->m_errorString; |
|
323 } |
|
324 |
|
325 QString QSystemReadWriteLock::key() const |
|
326 { |
|
327 return d->m_key; |
|
328 } |
|
329 |
|
330 QTM_END_NAMESPACE |