winrt: Guard pendingReadOps with mutex

As the list is changed inside a native callback
(handleReadyRead) which can be run inside another
thread it has to be protected by a mutex.

Change-Id: I145a866a36a12b7ea9bfa9f99ad9f7add872a021
Reviewed-by: Maurice Kalinowski <maurice.kalinowski@qt.io>
Reviewed-by: David Faure <david.faure@kdab.com>
bb10
Oliver Wolff 2016-11-01 14:48:16 +01:00
parent e1f29814da
commit b787977ba0
2 changed files with 12 additions and 0 deletions

View File

@ -329,6 +329,7 @@ bool QNativeSocketEngine::initialize(qintptr socketDescriptor, QAbstractSocket::
hr = stream->ReadAsync(buffer.Get(), READ_BUFFER_SIZE, InputStreamOptions_Partial, readOp.GetAddressOf());
RETURN_OK_IF_FAILED_WITH_ARGS("initialize(): Failed to read from the socket buffer (%s).",
socketDescription(this).constData());
QMutexLocker locker(&d->readOperationsMutex);
d->pendingReadOps.append(readOp);
d->socketState = socketState;
hr = readOp->put_Completed(Callback<SocketReadCompletedHandler>(d, &QNativeSocketEnginePrivate::handleReadyRead).Get());
@ -574,6 +575,7 @@ void QNativeSocketEngine::close()
}
#endif // _MSC_VER >= 1900
QMutexLocker locker(&d->readOperationsMutex);
for (ComPtr<IAsyncBufferOperation> readOp : d->pendingReadOps) {
ComPtr<IAsyncInfo> info;
hr = readOp.As(&info);
@ -585,6 +587,7 @@ void QNativeSocketEngine::close()
Q_ASSERT_SUCCEEDED(hr);
}
}
locker.unlock();
if (d->socketDescriptor != -1) {
ComPtr<IClosable> socket;
@ -945,6 +948,7 @@ void QNativeSocketEngine::establishRead()
ComPtr<IAsyncBufferOperation> readOp;
hr = stream->ReadAsync(buffer.Get(), READ_BUFFER_SIZE, InputStreamOptions_Partial, readOp.GetAddressOf());
RETURN_HR_IF_FAILED("establishRead(): Failed to initiate socket read");
QMutexLocker locker(&d->readOperationsMutex);
d->pendingReadOps.append(readOp);
hr = readOp->put_Completed(Callback<SocketReadCompletedHandler>(d, &QNativeSocketEnginePrivate::handleReadyRead).Get());
RETURN_HR_IF_FAILED("establishRead(): Failed to register read callback");
@ -1421,12 +1425,14 @@ HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *async
}
Q_Q(QNativeSocketEngine);
QMutexLocker locker(&readOperationsMutex);
for (int i = 0; i < pendingReadOps.count(); ++i) {
if (pendingReadOps.at(i).Get() == asyncInfo) {
pendingReadOps.takeAt(i);
break;
}
}
locker.unlock();
static QMutex mutex;
mutex.lock();
@ -1503,6 +1509,7 @@ HRESULT QNativeSocketEnginePrivate::handleReadyRead(IAsyncBufferOperation *async
socketDescription(q).constData());
return S_OK;
}
QMutexLocker locker(&readOperationsMutex);
pendingReadOps.append(readOp);
hr = readOp->put_Completed(Callback<SocketReadCompletedHandler>(this, &QNativeSocketEnginePrivate::handleReadyRead).Get());
if (FAILED(hr)) {

View File

@ -214,6 +214,8 @@ private:
{ return reinterpret_cast<ABI::Windows::Networking::Sockets::IDatagramSocket *>(socketDescriptor); }
Microsoft::WRL::ComPtr<ABI::Windows::Networking::Sockets::IStreamSocketListener> tcpListener;
Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncAction> connectOp;
// Protected by readOperationsMutex. Written in handleReadyRead (native callback)
QVector<Microsoft::WRL::ComPtr<ABI::Windows::Foundation::IAsyncOperationWithProgress<ABI::Windows::Storage::Streams::IBuffer *, UINT32>>> pendingReadOps;
// Protected by readMutex. Written in handleReadyRead (native callback)
@ -224,6 +226,9 @@ private:
// handleNewDatagrams/putIntoPendingDatagramsList)
mutable QMutex readMutex;
// As pendingReadOps is changed inside handleReadyRead(native callback) it has to be protected
QMutex readOperationsMutex;
bool emitOnNewDatagram;
// Protected by readMutex. Written in handleReadyRead (native callback)