From 0307c008bf71e1272385d0e7f7c8d2c5a1ba6526 Mon Sep 17 00:00:00 2001 From: Joerg Bornemann Date: Mon, 7 Mar 2016 15:57:45 +0100 Subject: Make QWindowsPipeWriter thread-free. Re-work QWindowsPipeWriter to not use a thread anymore but the WriteFileEx API, similar to QWindowsPipeReader. This saves us a lot of thread synchronization code and enables us to directly write data without yet another buffering layer. Also, this fixes the dreaded deadlocks in the QWindowsPipeWriter destructor that could occur when the reading end was closed before the write was finished. Task-number: QTBUG-23378 Task-number: QTBUG-38185 Change-Id: If0ae96dcd756f716ddf6fa38016080095bf3bd4e Reviewed-by: Oswald Buddenhagen --- src/corelib/io/qwindowspipewriter_p.h | 70 +++++++++++++++++------------------ 1 file changed, 35 insertions(+), 35 deletions(-) (limited to 'src/corelib/io/qwindowspipewriter_p.h') diff --git a/src/corelib/io/qwindowspipewriter_p.h b/src/corelib/io/qwindowspipewriter_p.h index 78d43e6efe..808d303262 100644 --- a/src/corelib/io/qwindowspipewriter_p.h +++ b/src/corelib/io/qwindowspipewriter_p.h @@ -46,16 +46,11 @@ // #include -#include -#include -#include +#include #include QT_BEGIN_NAMESPACE - -#ifndef QT_NO_THREAD - #define SLEEPMIN 10 #define SLEEPMAX 500 @@ -104,45 +99,50 @@ private: int nextSleep; }; -class Q_CORE_EXPORT QWindowsPipeWriter : public QThread +class Q_CORE_EXPORT QWindowsPipeWriter : public QObject { Q_OBJECT - -Q_SIGNALS: - void canWrite(); - void bytesWritten(qint64 bytes); - public: - explicit QWindowsPipeWriter(HANDLE writePipe, QObject * parent = 0); + explicit QWindowsPipeWriter(HANDLE pipeWriteEnd, QObject *parent = 0); ~QWindowsPipeWriter(); - bool waitForWrite(int msecs); qint64 write(const char *data, qint64 maxlen); + void stop(); + bool waitForWrite(int msecs); + qint64 bytesToWrite() const; - qint64 bytesToWrite() const - { - QMutexLocker locker(&lock); - return data.size(); - } - - bool hadWritten() const - { - return hasWritten; - } - -protected: - void run(); +Q_SIGNALS: + void canWrite(); + void bytesWritten(qint64 bytes); + void _q_queueBytesWritten(QPrivateSignal); private: - QByteArray data; - QWaitCondition waitCondition; - mutable QMutex lock; - HANDLE writePipe; - volatile bool quitNow; - bool hasWritten; -}; + static void CALLBACK writeFileCompleted(DWORD errorCode, DWORD numberOfBytesTransfered, + OVERLAPPED *overlappedBase); + void notified(DWORD errorCode, DWORD numberOfBytesWritten); + bool waitForNotification(int timeout); + void emitPendingBytesWrittenValue(); -#endif //QT_NO_THREAD + class Overlapped : public OVERLAPPED + { + Q_DISABLE_COPY(Overlapped) + public: + explicit Overlapped(QWindowsPipeWriter *pipeWriter); + void clear(); + + QWindowsPipeWriter *pipeWriter; + }; + + HANDLE handle; + Overlapped overlapped; + qint64 numberOfBytesToWrite; + qint64 pendingBytesWrittenValue; + bool stopped; + bool writeSequenceStarted; + bool notifiedCalled; + bool bytesWrittenPending; + bool inBytesWritten; +}; QT_END_NAMESPACE -- cgit v1.2.3