QWindowsPipeReader::read(): do not switch the mutex twice

We can get better performance if we omit unlocking the mutex before
relocking it in startAsyncRead().

Change-Id: Ia012a71b95876d4f90c1dc4b7db5d7b267fca7a6
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
bb10
Alex Trotsenko 2021-06-21 16:16:52 +03:00
parent d904274176
commit 3fe44ddb9d
2 changed files with 10 additions and 5 deletions

View File

@ -186,8 +186,9 @@ qint64 QWindowsPipeReader::bytesAvailable() const
*/
qint64 QWindowsPipeReader::read(char *data, qint64 maxlen)
{
mutex.lock();
QMutexLocker locker(&mutex);
qint64 readSoFar;
// If startAsyncRead() has read data, copy it to its destination.
if (maxlen == 1 && actualReadBufferSize > 0) {
*data = readBuffer.getChar();
@ -197,10 +198,9 @@ qint64 QWindowsPipeReader::read(char *data, qint64 maxlen)
readSoFar = readBuffer.read(data, qMin(actualReadBufferSize, maxlen));
actualReadBufferSize -= readSoFar;
}
mutex.unlock();
if (!pipeBroken) {
startAsyncRead();
startAsyncReadHelper(&locker);
if (readSoFar == 0)
return -2; // signal EWOULDBLOCK
}
@ -223,7 +223,11 @@ bool QWindowsPipeReader::canReadLine() const
void QWindowsPipeReader::startAsyncRead()
{
QMutexLocker locker(&mutex);
startAsyncReadHelper(&locker);
}
void QWindowsPipeReader::startAsyncReadHelper(QMutexLocker<QMutex> *locker)
{
if (readSequenceStarted || lastError != ERROR_SUCCESS)
return;
@ -236,10 +240,10 @@ void QWindowsPipeReader::startAsyncRead()
if (!winEventActPosted) {
winEventActPosted = true;
locker.unlock();
locker->unlock();
QCoreApplication::postEvent(this, new QEvent(QEvent::WinEventAct));
} else {
locker.unlock();
locker->unlock();
}
SetEvent(syncHandle);

View File

@ -96,6 +96,7 @@ protected:
private:
enum State { Stopped, Running, Draining };
void startAsyncReadHelper(QMutexLocker<QMutex> *locker);
void startAsyncReadLocked();
void cancelAsyncRead(State newState);
static void CALLBACK waitCallback(PTP_CALLBACK_INSTANCE instance, PVOID context,