diff options
author | Moonchild <moonchild@palemoon.org> | 2020-10-23 08:10:01 +0000 |
---|---|---|
committer | Moonchild <moonchild@palemoon.org> | 2020-10-23 08:10:01 +0000 |
commit | 28119b040d73e1e2c70ed18342a1eeca3c847293 (patch) | |
tree | 9c34e8e81f45e604de1cb14c21c40335dd906dac | |
parent | 3b224b2b26a553e575ebee805e3cc74c8f35e4b3 (diff) | |
download | uxp-28119b040d73e1e2c70ed18342a1eeca3c847293.tar.gz |
[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.
-rw-r--r-- | netwerk/base/nsIncrementalStreamLoader.cpp | 10 | ||||
-rw-r--r-- | netwerk/base/nsIncrementalStreamLoader.h | 5 | ||||
-rw-r--r-- | netwerk/base/nsStreamLoader.cpp | 7 | ||||
-rw-r--r-- | netwerk/base/nsStreamLoader.h | 2 |
4 files changed, 14 insertions, 10 deletions
diff --git a/netwerk/base/nsIncrementalStreamLoader.cpp b/netwerk/base/nsIncrementalStreamLoader.cpp index a7298be3f0..8890333f84 100644 --- a/netwerk/base/nsIncrementalStreamLoader.cpp +++ b/netwerk/base/nsIncrementalStreamLoader.cpp @@ -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; } diff --git a/netwerk/base/nsIncrementalStreamLoader.h b/netwerk/base/nsIncrementalStreamLoader.h index f04d4a9583..0e2b532e6d 100644 --- a/netwerk/base/nsIncrementalStreamLoader.h +++ b/netwerk/base/nsIncrementalStreamLoader.h @@ -47,8 +47,9 @@ protected: // available. mozilla::Vector<uint8_t, 0> mData; - // Number of consumed bytes from the mData. - size_t mBytesConsumed; + // Number of bytes read, which may differ from the number of bytes in mData, + // since we incrementally remove from there. + mozilla::Atomic<uint32_t, mozilla::Relaxed> mBytesRead; }; #endif // nsIncrementalStreamLoader_h__ diff --git a/netwerk/base/nsStreamLoader.cpp b/netwerk/base/nsStreamLoader.cpp index a73b038a7e..9990d18822 100644 --- a/netwerk/base/nsStreamLoader.cpp +++ b/netwerk/base/nsStreamLoader.cpp @@ -54,7 +54,7 @@ NS_IMPL_ISUPPORTS(nsStreamLoader, nsIStreamLoader, NS_IMETHODIMP nsStreamLoader::GetNumBytesRead(uint32_t* aNumBytes) { - *aNumBytes = mData.length(); + *aNumBytes = mBytesRead; return NS_OK; } @@ -150,7 +150,10 @@ nsStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctxt, uint64_t sourceOffset, uint32_t count) { uint32_t countRead; - return inStr->ReadSegments(WriteSegmentFun, this, count, &countRead); + nsresult rv = inStr->ReadSegments(WriteSegmentFun, this, count, &countRead); + NS_ENSURE_SUCCESS(rv, rv); + mBytesRead += countRead; + return NS_OK; } void diff --git a/netwerk/base/nsStreamLoader.h b/netwerk/base/nsStreamLoader.h index 671fc441ff..b611a1aedd 100644 --- a/netwerk/base/nsStreamLoader.h +++ b/netwerk/base/nsStreamLoader.h @@ -47,6 +47,8 @@ protected: nsCOMPtr<nsIRequest> mRequest; nsCOMPtr<nsIRequestObserver> mRequestObserver; + mozilla::Atomic<uint32_t, mozilla::Relaxed> mBytesRead; + // Buffer to accumulate incoming data. We preallocate if contentSize is // available. mozilla::Vector<uint8_t, 0> mData; |