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