mirror of
https://repo.dactyloidae.xyz/Dactyloidae/UXP.git
synced 2026-09-29 20:07:30 +09:00
[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:
parent
e77b184480
commit
efa2f805fe
4 changed files with 14 additions and 10 deletions
|
|
@ -11,10 +11,7 @@
|
||||||
|
|
||||||
#include <limits>
|
#include <limits>
|
||||||
|
|
||||||
nsIncrementalStreamLoader::nsIncrementalStreamLoader()
|
nsIncrementalStreamLoader::nsIncrementalStreamLoader() = default;
|
||||||
: mData(), mBytesConsumed(0)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
nsIncrementalStreamLoader::~nsIncrementalStreamLoader()
|
nsIncrementalStreamLoader::~nsIncrementalStreamLoader()
|
||||||
{
|
{
|
||||||
|
|
@ -49,7 +46,7 @@ NS_IMPL_ISUPPORTS(nsIncrementalStreamLoader, nsIIncrementalStreamLoader,
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsIncrementalStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
|
nsIncrementalStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
|
||||||
{
|
{
|
||||||
*aNumBytes = mBytesConsumed + mData.length();
|
*aNumBytes = mBytesRead;
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -180,7 +177,6 @@ nsIncrementalStreamLoader::WriteSegmentFun(nsIInputStream *inStr,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
self->mBytesConsumed += consumedCount;
|
|
||||||
*writeCount = count;
|
*writeCount = count;
|
||||||
|
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
|
|
@ -198,6 +194,8 @@ nsIncrementalStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctx
|
||||||
uint32_t countRead;
|
uint32_t countRead;
|
||||||
nsresult rv = inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
|
nsresult rv = inStr->ReadSegments(WriteSegmentFun, this, count, &countRead);
|
||||||
mRequest = nullptr;
|
mRequest = nullptr;
|
||||||
|
NS_ENSURE_SUCCESS(rv, rv);
|
||||||
|
mBytesRead += countRead;
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,8 +47,9 @@ protected:
|
||||||
// available.
|
// available.
|
||||||
mozilla::Vector<uint8_t, 0> mData;
|
mozilla::Vector<uint8_t, 0> mData;
|
||||||
|
|
||||||
// Number of consumed bytes from the mData.
|
// Number of bytes read, which may differ from the number of bytes in mData,
|
||||||
size_t mBytesConsumed;
|
// since we incrementally remove from there.
|
||||||
|
mozilla::Atomic<uint32_t, mozilla::Relaxed> mBytesRead;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // nsIncrementalStreamLoader_h__
|
#endif // nsIncrementalStreamLoader_h__
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,7 @@ NS_IMPL_ISUPPORTS(nsStreamLoader, nsIStreamLoader,
|
||||||
NS_IMETHODIMP
|
NS_IMETHODIMP
|
||||||
nsStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
|
nsStreamLoader::GetNumBytesRead(uint32_t* aNumBytes)
|
||||||
{
|
{
|
||||||
*aNumBytes = mData.length();
|
*aNumBytes = mBytesRead;
|
||||||
return NS_OK;
|
return NS_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -150,7 +150,10 @@ nsStreamLoader::OnDataAvailable(nsIRequest* request, nsISupports *ctxt,
|
||||||
uint64_t sourceOffset, uint32_t count)
|
uint64_t sourceOffset, uint32_t count)
|
||||||
{
|
{
|
||||||
uint32_t countRead;
|
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
|
void
|
||||||
|
|
|
||||||
|
|
@ -47,6 +47,8 @@ protected:
|
||||||
nsCOMPtr<nsIRequest> mRequest;
|
nsCOMPtr<nsIRequest> mRequest;
|
||||||
nsCOMPtr<nsIRequestObserver> mRequestObserver;
|
nsCOMPtr<nsIRequestObserver> mRequestObserver;
|
||||||
|
|
||||||
|
mozilla::Atomic<uint32_t, mozilla::Relaxed> mBytesRead;
|
||||||
|
|
||||||
// Buffer to accumulate incoming data. We preallocate if contentSize is
|
// Buffer to accumulate incoming data. We preallocate if contentSize is
|
||||||
// available.
|
// available.
|
||||||
mozilla::Vector<uint8_t, 0> mData;
|
mozilla::Vector<uint8_t, 0> mData;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue