diff options
Diffstat (limited to 'src/corelib/io/qwindowspipewriter.cpp')
-rw-r--r-- | src/corelib/io/qwindowspipewriter.cpp | 195 |
1 files changed, 99 insertions, 96 deletions
diff --git a/src/corelib/io/qwindowspipewriter.cpp b/src/corelib/io/qwindowspipewriter.cpp index 2db3cb4060..9d0f6a8a3e 100644 --- a/src/corelib/io/qwindowspipewriter.cpp +++ b/src/corelib/io/qwindowspipewriter.cpp @@ -1,46 +1,11 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Copyright (C) 2021 Alex Trotsenko <alex1973tr@gmail.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) 2021 Alex Trotsenko <alex1973tr@gmail.com> +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only #include "qwindowspipewriter_p.h" #include <qcoreapplication.h> #include <QMutexLocker> +#include <QPointer> QT_BEGIN_NAMESPACE @@ -52,6 +17,7 @@ QWindowsPipeWriter::QWindowsPipeWriter(HANDLE pipeWriteEnd, QObject *parent) waitObject(NULL), pendingBytesWrittenValue(0), lastError(ERROR_SUCCESS), + completionState(NoError), stopped(true), writeSequenceStarted(false), bytesWrittenPending(false), @@ -82,7 +48,7 @@ void QWindowsPipeWriter::setHandle(HANDLE hPipeWriteEnd) handle = hPipeWriteEnd; QMutexLocker locker(&mutex); - startAsyncWriteLocked(&locker); + startAsyncWriteHelper(&locker); } /*! @@ -124,49 +90,64 @@ qint64 QWindowsPipeWriter::bytesToWrite() const } /*! + Returns \c true if async operation is in progress. +*/ +bool QWindowsPipeWriter::isWriteOperationActive() const +{ + return completionState == NoError && bytesToWrite() != 0; +} + +/*! Writes a shallow copy of \a ba to the internal buffer. */ -bool QWindowsPipeWriter::write(const QByteArray &ba) +void QWindowsPipeWriter::write(const QByteArray &ba) { - return writeImpl(ba); + if (completionState != WriteDisabled) + writeImpl(ba); } /*! Writes data to the internal buffer. */ -bool QWindowsPipeWriter::write(const char *data, qint64 size) +void QWindowsPipeWriter::write(const char *data, qint64 size) { - return writeImpl(data, size); + if (completionState != WriteDisabled) + writeImpl(data, size); } template <typename... Args> -inline bool QWindowsPipeWriter::writeImpl(Args... args) +inline void QWindowsPipeWriter::writeImpl(Args... args) { QMutexLocker locker(&mutex); - if (lastError != ERROR_SUCCESS) - return false; - writeBuffer.append(args...); - if (writeSequenceStarted) - return true; + if (writeSequenceStarted || (lastError != ERROR_SUCCESS)) + return; stopped = false; // If we don't have an assigned handle yet, defer writing until // setHandle() is called. - if (handle == INVALID_HANDLE_VALUE) - return true; + if (handle != INVALID_HANDLE_VALUE) + startAsyncWriteHelper(&locker); +} + +void QWindowsPipeWriter::startAsyncWriteHelper(QMutexLocker<QMutex> *locker) +{ + startAsyncWriteLocked(); + + // Do not post the event, if the write operation will be completed asynchronously. + if (!bytesWrittenPending && lastError == ERROR_SUCCESS) + return; - startAsyncWriteLocked(&locker); - return true; + notifyCompleted(locker); } /*! Starts a new write sequence. */ -void QWindowsPipeWriter::startAsyncWriteLocked(QMutexLocker<QMutex> *locker) +void QWindowsPipeWriter::startAsyncWriteLocked() { while (!writeBuffer.isEmpty()) { // WriteFile() returns true, if the write operation completes synchronously. @@ -188,22 +169,6 @@ void QWindowsPipeWriter::startAsyncWriteLocked(QMutexLocker<QMutex> *locker) if (!writeCompleted(errorCode, numberOfBytesWritten)) break; } - - // Do not post the event, if the write operation will be completed asynchronously. - if (!bytesWrittenPending) - return; - - if (!winEventActPosted) { - winEventActPosted = true; - locker->unlock(); - QCoreApplication::postEvent(this, new QEvent(QEvent::WinEventAct)); - } else { - locker->unlock(); - } - - // We set the event only after unlocking to avoid additional context - // switches due to the released thread immediately running into the lock. - SetEvent(syncHandle); } /*! @@ -235,16 +200,12 @@ void QWindowsPipeWriter::waitCallback(PTP_CALLBACK_INSTANCE instance, PVOID cont pipeWriter->writeSequenceStarted = false; - if (pipeWriter->writeCompleted(errorCode, numberOfBytesTransfered)) { - pipeWriter->startAsyncWriteLocked(&locker); - } else { - // The write operation failed, so we must unblock the main thread, - // which can wait for the event. We set the event only after unlocking - // to avoid additional context switches due to the released thread - // immediately running into the lock. - locker.unlock(); - SetEvent(pipeWriter->syncHandle); - } + if (pipeWriter->writeCompleted(errorCode, numberOfBytesTransfered)) + pipeWriter->startAsyncWriteLocked(); + + // We post the notification even if the write operation failed, + // to unblock the main thread, in case it is waiting for the event. + pipeWriter->notifyCompleted(&locker); } /*! @@ -253,22 +214,46 @@ void QWindowsPipeWriter::waitCallback(PTP_CALLBACK_INSTANCE instance, PVOID cont */ bool QWindowsPipeWriter::writeCompleted(DWORD errorCode, DWORD numberOfBytesWritten) { - if (errorCode == ERROR_SUCCESS) { + switch (errorCode) { + case ERROR_SUCCESS: bytesWrittenPending = true; pendingBytesWrittenValue += numberOfBytesWritten; writeBuffer.free(numberOfBytesWritten); return true; + case ERROR_PIPE_NOT_CONNECTED: // the other end has closed the pipe + case ERROR_OPERATION_ABORTED: // the operation was canceled + case ERROR_NO_DATA: // the pipe is being closed + break; + default: + qErrnoWarning(errorCode, "QWindowsPipeWriter: write failed."); + break; } + // The buffer is not cleared here, because the write progress + // should appear on the main thread synchronously. lastError = errorCode; - writeBuffer.clear(); - // The other end has closed the pipe. This can happen in QLocalSocket. Do not warn. - if (errorCode != ERROR_OPERATION_ABORTED && errorCode != ERROR_NO_DATA) - qErrnoWarning(errorCode, "QWindowsPipeWriter: write failed."); return false; } /*! + Posts a notification event to the main thread. + */ +void QWindowsPipeWriter::notifyCompleted(QMutexLocker<QMutex> *locker) +{ + if (!winEventActPosted) { + winEventActPosted = true; + locker->unlock(); + QCoreApplication::postEvent(this, new QEvent(QEvent::WinEventAct)); + } else { + locker->unlock(); + } + + // We set the event only after unlocking to avoid additional context + // switches due to the released thread immediately running into the lock. + SetEvent(syncHandle); +} + +/*! Receives notification that the write operation has completed. */ bool QWindowsPipeWriter::event(QEvent *e) @@ -293,14 +278,13 @@ bool QWindowsPipeWriter::consumePendingAndEmit(bool allowWinActPosting) if (allowWinActPosting) winEventActPosted = false; - if (!bytesWrittenPending) - return false; - - // Reset the state even if we don't emit bytesWritten(). - // It's a defined behavior to not re-emit this signal recursively. - bytesWrittenPending = false; - qint64 numberOfBytesWritten = pendingBytesWrittenValue; - pendingBytesWrittenValue = 0; + const qint64 numberOfBytesWritten = pendingBytesWrittenValue; + const bool emitBytesWritten = bytesWrittenPending; + if (emitBytesWritten) { + bytesWrittenPending = false; + pendingBytesWrittenValue = 0; + } + const DWORD dwError = lastError; locker.unlock(); @@ -308,8 +292,27 @@ bool QWindowsPipeWriter::consumePendingAndEmit(bool allowWinActPosting) if (stopped) return false; - emit bytesWritten(numberOfBytesWritten); - return true; + // Trigger 'ErrorDetected' state only once. This state must be set before + // emitting the bytesWritten() signal. Otherwise, the write sequence will + // be considered not finished, and we may hang if a slot connected + // to bytesWritten() calls waitForBytesWritten(). + if (dwError != ERROR_SUCCESS && completionState == NoError) { + QPointer<QWindowsPipeWriter> alive(this); + completionState = ErrorDetected; + if (emitBytesWritten) + emit bytesWritten(numberOfBytesWritten); + if (alive) { + writeBuffer.clear(); + completionState = WriteDisabled; + emit writeFailed(); + } + } else if (emitBytesWritten) { + emit bytesWritten(numberOfBytesWritten); + } + + return emitBytesWritten; } QT_END_NAMESPACE + +#include "moc_qwindowspipewriter_p.cpp" |