summaryrefslogtreecommitdiffstats
path: root/src/corelib/thread/qreadwritelock_p.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/corelib/thread/qreadwritelock_p.h')
-rw-r--r--src/corelib/thread/qreadwritelock_p.h103
1 files changed, 54 insertions, 49 deletions
diff --git a/src/corelib/thread/qreadwritelock_p.h b/src/corelib/thread/qreadwritelock_p.h
index f9725f05af..68fa642a73 100644
--- a/src/corelib/thread/qreadwritelock_p.h
+++ b/src/corelib/thread/qreadwritelock_p.h
@@ -1,42 +1,6 @@
-/****************************************************************************
-**
-** Copyright (C) 2016 The Qt Company Ltd.
-** Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
-** Contact: https://www.qt.io/licensing/
-**
-** This file is part of the QtCore module of the Qt Toolkit.
-**
-** $QT_BEGIN_LICENSE:LGPL$
-** Commercial License Usage
-** Licensees holding valid commercial Qt licenses may use this file in
-** accordance with the commercial license agreement provided with the
-** Software or, alternatively, in accordance with the terms contained in
-** a written agreement between you and The Qt Company. For licensing terms
-** and conditions see https://www.qt.io/terms-conditions. For further
-** information use the contact form at https://www.qt.io/contact-us.
-**
-** GNU Lesser General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU Lesser
-** General Public License version 3 as published by the Free Software
-** Foundation and appearing in the file LICENSE.LGPL3 included in the
-** packaging of this file. Please review the following information to
-** ensure the GNU Lesser General Public License version 3 requirements
-** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
-**
-** GNU General Public License Usage
-** Alternatively, this file may be used under the terms of the GNU
-** General Public License version 2.0 or (at your option) the GNU General
-** Public license version 3 or any later version approved by the KDE Free
-** Qt Foundation. The licenses are as published by the Free Software
-** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
-** included in the packaging of this file. Please review the following
-** information to ensure the GNU General Public License requirements will
-** be met: https://www.gnu.org/licenses/gpl-2.0.html and
-** https://www.gnu.org/licenses/gpl-3.0.html.
-**
-** $QT_END_LICENSE$
-**
-****************************************************************************/
+// Copyright (C) 2016 The Qt Company Ltd.
+// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
+// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#ifndef QREADWRITELOCK_P_H
#define QREADWRITELOCK_P_H
@@ -52,23 +16,39 @@
// We mean it.
//
-#include <QtCore/private/qglobal_p.h>
+#include <QtCore/private/qlocking_p.h>
#include <QtCore/private/qwaitcondition_p.h>
+#include <QtCore/qreadwritelock.h>
#include <QtCore/qvarlengtharray.h>
QT_REQUIRE_CONFIG(thread);
QT_BEGIN_NAMESPACE
+namespace QReadWriteLockStates {
+enum {
+ StateMask = 0x3,
+ StateLockedForRead = 0x1,
+ StateLockedForWrite = 0x2,
+};
+enum StateForWaitCondition {
+ LockedForRead,
+ LockedForWrite,
+ Unlocked,
+ RecursivelyLocked
+};
+}
+
class QReadWriteLockPrivate
{
public:
explicit QReadWriteLockPrivate(bool isRecursive = false)
: recursive(isRecursive) {}
- QtPrivate::mutex mutex;
- QtPrivate::condition_variable writerCond;
- QtPrivate::condition_variable readerCond;
+ alignas(QtPrivate::IdealMutexAlignment) std::condition_variable writerCond;
+ std::condition_variable readerCond;
+
+ alignas(QtPrivate::IdealMutexAlignment) std::mutex mutex;
int readerCount = 0;
int writerCount = 0;
int waitingReaders = 0;
@@ -76,8 +56,8 @@ public:
const bool recursive;
//Called with the mutex locked
- bool lockForWrite(std::unique_lock<QtPrivate::mutex> &lock, int timeout);
- bool lockForRead(std::unique_lock<QtPrivate::mutex> &lock, int timeout);
+ bool lockForWrite(std::unique_lock<std::mutex> &lock, QDeadlineTimer timeout);
+ bool lockForRead(std::unique_lock<std::mutex> &lock, QDeadlineTimer timeout);
void unlock();
//memory management
@@ -85,7 +65,7 @@ public:
void release();
static QReadWriteLockPrivate *allocate();
- // Recusive mutex handling
+ // Recursive mutex handling
Qt::HANDLE currentWriter = {};
struct Reader {
@@ -96,11 +76,36 @@ public:
QVarLengthArray<Reader, 16> currentReaders;
// called with the mutex unlocked
- bool recursiveLockForWrite(int timeout);
- bool recursiveLockForRead(int timeout);
+ bool recursiveLockForWrite(QDeadlineTimer timeout);
+ bool recursiveLockForRead(QDeadlineTimer timeout);
void recursiveUnlock();
+
+ static QReadWriteLockStates::StateForWaitCondition
+ stateForWaitCondition(const QReadWriteLock *lock);
};
-Q_DECLARE_TYPEINFO(QReadWriteLockPrivate::Reader, Q_PRIMITIVE_TYPE);
+Q_DECLARE_TYPEINFO(QReadWriteLockPrivate::Reader, Q_PRIMITIVE_TYPE);\
+
+/*! \internal Helper for QWaitCondition::wait */
+inline QReadWriteLockStates::StateForWaitCondition
+QReadWriteLockPrivate::stateForWaitCondition(const QReadWriteLock *q)
+{
+ using namespace QReadWriteLockStates;
+ QReadWriteLockPrivate *d = q->d_ptr.loadAcquire();
+ switch (quintptr(d) & StateMask) {
+ case StateLockedForRead: return LockedForRead;
+ case StateLockedForWrite: return LockedForWrite;
+ }
+
+ if (!d)
+ return Unlocked;
+ const auto lock = qt_scoped_lock(d->mutex);
+ if (d->writerCount > 1)
+ return RecursivelyLocked;
+ else if (d->writerCount == 1)
+ return LockedForWrite;
+ return LockedForRead;
+
+}
QT_END_NAMESPACE