winrt: Proper guarding by readMutex

Commented its purpose and the guarded members for
readMutex.

Fixed places where guarded members were accessed without
using the mutex.

Use QMutexLocker instead of manually (un-)locking the
mutex.

Task-number: QTBUG-44357
Change-Id: I0d46f9592d5a9d1b52e73df961785a6f6c9e80be
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Oliver Wolff 2016-11-01 14:48:16 +01:00
parent 71166288cb
commit e1f29814da
2 changed files with 19 additions and 6 deletions

View File

@ -659,6 +659,7 @@ qint64 QNativeSocketEngine::read(char *data, qint64 maxlen)
if (d->socketType != QAbstractSocket::TcpSocket)
return -1;
QMutexLocker mutexLocker(&d->readMutex);
// There will be a read notification when the socket was closed by the remote host. If that
// happens and there isn't anything left in the buffer, we have to return -1 in order to signal
// the closing of the socket.
@ -667,7 +668,6 @@ qint64 QNativeSocketEngine::read(char *data, qint64 maxlen)
return -1;
}
QMutexLocker mutexLocker(&d->readMutex);
qint64 b = d->readBytes.read(data, maxlen);
d->bytesAvailable = d->readBytes.size() - d->readBytes.pos();
return b;
@ -701,7 +701,7 @@ qint64 QNativeSocketEngine::readDatagram(char *data, qint64 maxlen, QIpPacketHea
{
#ifndef QT_NO_UDPSOCKET
Q_D(QNativeSocketEngine);
d->readMutex.lock();
QMutexLocker locker(&d->readMutex);
if (d->socketType != QAbstractSocket::UdpSocket || d->pendingDatagrams.isEmpty()) {
if (header)
header->clear();
@ -721,7 +721,7 @@ qint64 QNativeSocketEngine::readDatagram(char *data, qint64 maxlen, QIpPacketHea
} else {
readOrigin = datagram.data;
}
d->readMutex.unlock();
locker.unlock();
memcpy(data, readOrigin, qMin(maxlen, qint64(datagram.data.length())));
return readOrigin.length();
#else
@ -772,12 +772,14 @@ qint64 QNativeSocketEngine::writeDatagram(const char *data, qint64 len, const QI
bool QNativeSocketEngine::hasPendingDatagrams() const
{
Q_D(const QNativeSocketEngine);
QMutexLocker locker(&d->readMutex);
return d->pendingDatagrams.length() > 0;
}
qint64 QNativeSocketEngine::pendingDatagramSize() const
{
Q_D(const QNativeSocketEngine);
QMutexLocker locker(&d->readMutex);
if (d->pendingDatagrams.isEmpty())
return -1;
@ -1465,7 +1467,7 @@ HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *async
hr = byteArrayAccess->Buffer(&data);
Q_ASSERT_SUCCEEDED(hr);
readMutex.lock();
QMutexLocker readLocker(&readMutex);
if (readBytes.atEnd()) // Everything has been read; the buffer is safe to reset
readBytes.close();
if (!readBytes.isOpen())
@ -1476,7 +1478,7 @@ HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *async
readBytes.write(reinterpret_cast<const char*>(data), qint64(bufferLength));
readBytes.seek(readPos);
bytesAvailable = readBytes.size() - readBytes.pos();
readMutex.unlock();
readLocker.unlock();
if (notifyOnRead)
emit q->readReady();

View File

@ -215,12 +215,23 @@ private:
Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocketListener> tcpListener;
Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncAction> connectOp;
QVector<Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncOperationWithProgress<ABI::Windows::Storage::Streams::IBuffer *, UINT32>>> pendingReadOps;
// Protected by readMutex. Written in handleReadyRead (native callback)
QBuffer readBytes;
QMutex readMutex;
// In case of TCP readMutex protects readBytes and bytesAvailable. In case of UDP it is
// pendingDatagrams. They are written inside native callbacks (handleReadyRead and
// handleNewDatagrams/putIntoPendingDatagramsList)
mutable QMutex readMutex;
bool emitOnNewDatagram;
// Protected by readMutex. Written in handleReadyRead (native callback)
QAtomicInteger<int> bytesAvailable;
// Protected by readMutex. Written in handleNewDatagrams/putIntoPendingDatagramsList
QList<WinRtDatagram> pendingDatagrams;
QList<ABI::Windows::Networking::Sockets::IStreamSocket *> pendingConnections;
QList<ABI::Windows::Networking::Sockets::IStreamSocket *> currentConnections;
QEventLoop eventLoop;