From e062494e888e4dfbb42cb0b2f2c9d41dd76b9bfb Mon Sep 17 00:00:00 2001
From: tobtoht <tob@featherwallet.org>
Date: Mon, 31 Oct 2022 00:38:39 +0100
Subject: [PATCH] Revert "Re-apply "QReadWriteLock: replace (QWaitCondition,
 QMutex) with std::(condition_variable, mutex)""

This reverts commit fd8f81f3859fe92082caec925223e08cf61c69d4.
---
 src/corelib/thread/qreadwritelock.cpp | 35 ++++++++++++---------------
 src/corelib/thread/qreadwritelock_p.h | 12 ++++-----
 2 files changed, 22 insertions(+), 25 deletions(-)

diff --git a/qtbase/src/corelib/thread/qreadwritelock.cpp b/qtbase/src/corelib/thread/qreadwritelock.cpp
index c31782d684..a46b70180c 100644
--- a/qtbase/src/corelib/thread/qreadwritelock.cpp
+++ b/qtbase/src/corelib/thread/qreadwritelock.cpp
@@ -31,9 +31,6 @@ QT_BEGIN_NAMESPACE
  */
 
 namespace {
-
-using ms = std::chrono::milliseconds;
-
 enum {
     StateMask = 0x3,
     StateLockedForRead = 0x1,
@@ -243,7 +240,7 @@ bool QReadWriteLock::tryLockForRead(int timeout)
             d = d_ptr.loadAcquire();
             continue;
         }
-        return d->lockForRead(lock, timeout);
+        return d->lockForRead(timeout);
     }
 }
 
@@ -347,7 +344,7 @@ bool QReadWriteLock::tryLockForWrite(int timeout)
             d = d_ptr.loadAcquire();
             continue;
         }
-        return d->lockForWrite(lock, timeout);
+        return d->lockForWrite(timeout);
     }
 }
 
@@ -431,9 +428,9 @@ QReadWriteLock::StateForWaitCondition QReadWriteLock::stateForWaitCondition() co
 
 }
 
-bool QReadWriteLockPrivate::lockForRead(std::unique_lock<QtPrivate::mutex> &lock, int timeout)
+bool QReadWriteLockPrivate::lockForRead(int timeout)
 {
-    Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
+    Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
 
     QElapsedTimer t;
     if (timeout > 0)
@@ -447,10 +444,10 @@ bool QReadWriteLockPrivate::lockForRead(std::unique_lock<QtPrivate::mutex> &lock
             if (elapsed > timeout)
                 return false;
             waitingReaders++;
-            readerCond.wait_for(lock, ms{timeout - elapsed});
+            readerCond.wait(&mutex, QDeadlineTimer(timeout - elapsed));
         } else {
             waitingReaders++;
-            readerCond.wait(lock);
+            readerCond.wait(&mutex);
         }
         waitingReaders--;
     }
@@ -459,9 +456,9 @@ bool QReadWriteLockPrivate::lockForRead(std::unique_lock<QtPrivate::mutex> &lock
     return true;
 }
 
-bool QReadWriteLockPrivate::lockForWrite(std::unique_lock<QtPrivate::mutex> &lock, int timeout)
+bool QReadWriteLockPrivate::lockForWrite(int timeout)
 {
-    Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
+    Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
 
     QElapsedTimer t;
     if (timeout > 0)
@@ -476,15 +473,15 @@ bool QReadWriteLockPrivate::lockForWrite(std::unique_lock<QtPrivate::mutex> &loc
                 if (waitingReaders && !waitingWriters && !writerCount) {
                     // We timed out and now there is no more writers or waiting writers, but some
                     // readers were queued (probably because of us). Wake the waiting readers.
-                    readerCond.notify_all();
+                    readerCond.wakeAll();
                 }
                 return false;
             }
             waitingWriters++;
-            writerCond.wait_for(lock, ms{timeout - elapsed});
+            writerCond.wait(&mutex, QDeadlineTimer(timeout - elapsed));
         } else {
             waitingWriters++;
-            writerCond.wait(lock);
+            writerCond.wait(&mutex);
         }
         waitingWriters--;
     }
@@ -497,11 +494,11 @@ bool QReadWriteLockPrivate::lockForWrite(std::unique_lock<QtPrivate::mutex> &loc
 
 void QReadWriteLockPrivate::unlock()
 {
-    Q_ASSERT(!mutex.try_lock()); // mutex must be locked when entering this function
+    Q_ASSERT(!mutex.tryLock()); // mutex must be locked when entering this function
     if (waitingWriters)
-        writerCond.notify_one();
+        writerCond.wakeOne();
     else if (waitingReaders)
-        readerCond.notify_all();
+        readerCond.wakeAll();
 }
 
 static auto handleEquals(Qt::HANDLE handle)
@@ -523,7 +520,7 @@ bool QReadWriteLockPrivate::recursiveLockForRead(int timeout)
         return true;
     }
 
-    if (!lockForRead(lock, timeout))
+    if (!lockForRead(timeout))
         return false;
 
     Reader r = {self, 1};
@@ -542,7 +539,7 @@ bool QReadWriteLockPrivate::recursiveLockForWrite(int timeout)
         return true;
     }
 
-    if (!lockForWrite(lock, timeout))
+    if (!lockForWrite(timeout))
         return false;
 
     currentWriter = self;
diff --git a/qtbase/src/corelib/thread/qreadwritelock_p.h b/qtbase/src/corelib/thread/qreadwritelock_p.h
index e1d42fbbf3..c88eb15e1b 100644
--- a/qtbase/src/corelib/thread/qreadwritelock_p.h
+++ b/qtbase/src/corelib/thread/qreadwritelock_p.h
@@ -17,8 +17,8 @@
 //
 
 #include <QtCore/private/qglobal_p.h>
-#include <QtCore/private/qwaitcondition_p.h>
 #include <QtCore/qvarlengtharray.h>
+#include <QtCore/qwaitcondition.h>
 
 QT_REQUIRE_CONFIG(thread);
 
@@ -30,9 +30,9 @@ public:
     explicit QReadWriteLockPrivate(bool isRecursive = false)
         : recursive(isRecursive) {}
 
-    QtPrivate::mutex mutex;
-    QtPrivate::condition_variable writerCond;
-    QtPrivate::condition_variable readerCond;
+    QMutex mutex;
+    QWaitCondition writerCond;
+    QWaitCondition readerCond;
     int readerCount = 0;
     int writerCount = 0;
     int waitingReaders = 0;
@@ -40,8 +40,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(int timeout);
+    bool lockForRead(int timeout);
     void unlock();
 
     //memory management
-- 
2.38.1