[netwerk] Make nsIncrementalStreamLoader's GetNumBytesRead threadsafe.

This prevents a potential race and simplifies the code a bit by keeping the
bytes read separate instead of using mData, which is modified from another
thread from OnDataAvailable.
Relaxed atomics are fine for these, since they don't guard any memory.
This commit is contained in:
Moonchild 2020-10-23 08:10:01 +00:00 committed by roytam1
commit efa2f805fe
4 changed files with 14 additions and 10 deletions

View file

@ -11,10 +11,7 @@
#include <limits>
nsIncrementalStreamLoader::nsIncrementalStreamLoader()
: mData(), mBytesConsumed(0)
{
}
nsIncrementalStreamLoader::nsIncrementalStreamLoader() = default;
nsIncrementalStreamLoader::~nsIncrementalStreamLoader()
{
@ -49,7 +46,7 @@ NS_IMPL_ISUPPORTS(nsIncrementalStreamLoader, nsIIncrementalStreamLoader,
NS_IMETHODIMP
nsIncrementalStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
{
*aNumBytes = mBytesConsumed + mData.length();
*aNumBytes = mBytesRead;
return NS_OK;
}
@ -180,7 +177,6 @@ nsIncrementalStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
}
}
self->mBytesConsumed += consumedCount;
*writeCount = count;
return NS_OK;
@ -198,6 +194,8 @@ nsIncrementalStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctx
uint32_t countRead;
nsresult rv = inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
mRequest = nullptr;
NS_ENSURE_SUCCESS(rv, rv);
mBytesRead += countRead;
return rv;
}