diff options
author | Alex Trotsenko <alex1973tr@gmail.com> | 2020-10-05 19:22:49 +0300 |
---|---|---|
committer | Alex Trotsenko <alex1973tr@gmail.com> | 2020-11-17 12:45:50 +0200 |
commit | ee122077b09430da54ca09750589b37326a22d85 (patch) | |
tree | fa06b0006bddc56fa68045d827275dc52c14f1ee /src/corelib/io/qwindowspipewriter_p.h | |
parent | 6be39809b038768a665b0e29a3a3668fdc424d9a (diff) |
Allow QWindowsPipe{Reader,Writer} to work with foreign event loops
When a foreign event loop that does not enter an alertable wait state
is running (which is also the case when a native dialog window is
modal), pipe handlers would freeze temporarily due to their APC
callbacks not being invoked.
We address this problem by moving the I/O callbacks to the Windows
thread pool, and only posting completion events to the main loop
from there. That makes the actual I/O completely independent from
any main loop, while the signal delivery works also with foreign
loops (because Qt event delivery uses Windows messages, which foreign
loops typically handle correctly).
As a nice side effect, performance (and in particular scalability)
is improved.
Several other approaches have been tried:
1) Using QWinEventNotifier was about a quarter slower and scaled much
worse. Additionally, it also required a rather egregious hack to
handle the (pathological) case of a single thread talking to both
ends of a QLocalSocket synchronously.
2) Queuing APCs from the thread pool to the main thread and also
posting wake-up events to its event loop, and handling I/O on the
main thread; this performed roughly like this solution , but scaled
half as well, and the separate wake-up path was still deemed hacky.
3) Only posting wake-up events to the main thread from the thread pool,
and still handling I/O on the main thread; this still performed
comparably to 2), and the pathological case was not handled at all.
4) Using this approach for reads and that of 3) for writes was slightly
faster with big amounts of data, but scaled slightly worse, and the
diverging implementations were deemed not desirable.
Fixes: QTBUG-64443
Change-Id: I1cd87c07db39f3b46a2683ce236d7eb67b5be549
Reviewed-by: Oswald Buddenhagen <oswald.buddenhagen@gmx.de>
Diffstat (limited to 'src/corelib/io/qwindowspipewriter_p.h')
-rw-r--r-- | src/corelib/io/qwindowspipewriter_p.h | 42 |
1 files changed, 21 insertions, 21 deletions
diff --git a/src/corelib/io/qwindowspipewriter_p.h b/src/corelib/io/qwindowspipewriter_p.h index 39e8ffe40a..b5a48e926f 100644 --- a/src/corelib/io/qwindowspipewriter_p.h +++ b/src/corelib/io/qwindowspipewriter_p.h @@ -1,6 +1,6 @@ /**************************************************************************** ** -** Copyright (C) 2016 The Qt Company Ltd. +** Copyright (C) 2020 The Qt Company Ltd. ** Contact: https://www.qt.io/licensing/ ** ** This file is part of the QtCore module of the Qt Toolkit. @@ -54,7 +54,9 @@ #include <QtCore/private/qglobal_p.h> #include <qelapsedtimer.h> #include <qobject.h> -#include <qbytearray.h> +#include <qmutex.h> +#include <private/qringbuffer_p.h> + #include <qt_windows.h> QT_BEGIN_NAMESPACE @@ -117,39 +119,37 @@ public: bool write(const QByteArray &ba); void stop(); bool waitForWrite(int msecs); - bool isWriteOperationActive() const { return writeSequenceStarted; } + bool isWriteOperationActive() const; qint64 bytesToWrite() const; Q_SIGNALS: void canWrite(); void bytesWritten(qint64 bytes); - void _q_queueBytesWritten(QPrivateSignal); + +protected: + bool event(QEvent *e) override; private: - static void CALLBACK writeFileCompleted(DWORD errorCode, DWORD numberOfBytesTransfered, - OVERLAPPED *overlappedBase); - void notified(DWORD errorCode, DWORD numberOfBytesWritten); + void startAsyncWriteLocked(); + static void CALLBACK waitCallback(PTP_CALLBACK_INSTANCE instance, PVOID context, + PTP_WAIT wait, TP_WAIT_RESULT waitResult); + void writeCompleted(DWORD errorCode, DWORD numberOfBytesWritten); bool waitForNotification(int timeout); - void emitPendingBytesWrittenValue(); - - class Overlapped : public OVERLAPPED - { - Q_DISABLE_COPY_MOVE(Overlapped) - public: - explicit Overlapped(QWindowsPipeWriter *pipeWriter); - void clear(); - - QWindowsPipeWriter *pipeWriter; - }; + bool emitPendingSignals(bool allowWinActPosting); HANDLE handle; - Overlapped overlapped; - QByteArray buffer; + HANDLE eventHandle; + HANDLE syncHandle; + PTP_WAIT waitObject; + OVERLAPPED overlapped; + QRingBuffer writeBuffer; qint64 pendingBytesWrittenValue; + mutable QMutex mutex; + DWORD lastError; bool stopped; bool writeSequenceStarted; - bool notifiedCalled; bool bytesWrittenPending; + bool winEventActPosted; bool inBytesWritten; }; |