Bug 1752345 - Allow calling AvailableWrite or AvailableRead on any thread. r=alwu

This is safe in fact safe. The data might be a bit late, but it's going to be
correct.

Differential Revision: https://phabricator.services.mozilla.com/D137302
This commit is contained in:
Paul Adenot 2022-02-03 15:18:13 +00:00
parent 05e420b119
commit f0f4d715ec

View File

@ -225,18 +225,14 @@ class SPSCRingBufferBase {
/**
* Get the number of available elements for consuming.
*
* Only safely called on the consumer thread. This can be less than the actual
* number of elements in the queue, since the mWriteIndex is updated at the
* very end of the Enqueue method on the producer thread, but consequently
* always returns a number of elements such that a call to Dequeue return this
* number of elements.
* This can be less than the actual number of elements in the queue, since the
* mWriteIndex is updated at the very end of the Enqueue method on the
* producer thread, but consequently always returns a number of elements such
* that a call to Dequeue return this number of elements.
*
* @return The number of available elements for reading.
*/
int AvailableRead() const {
#ifdef DEBUG
AssertCorrectThread(mConsumerId);
#endif
return AvailableReadInternal(
mReadIndex.load(std::memory_order::memory_order_relaxed),
mWriteIndex.load(std::memory_order::memory_order_relaxed));
@ -244,18 +240,14 @@ class SPSCRingBufferBase {
/**
* Get the number of available elements for writing.
*
* Only safely called on the producer thread. This can be less than than the
* actual number of slots that are available, because mReadIndex is update at
* the very end of the Deque method. It always returns a number such that a
* call to Enqueue with this number will succeed in enqueuing this number of
* elements.
* This can be less than than the actual number of slots that are available,
* because mReadIndex is updated at the very end of the Deque method. It
* always returns a number such that a call to Enqueue with this number will
* succeed in enqueuing this number of elements.
*
* @return The number of empty slots in the buffer, available for writing.
*/
int AvailableWrite() const {
#ifdef DEBUG
AssertCorrectThread(mProducerId);
#endif
return AvailableWriteInternal(
mReadIndex.load(std::memory_order::memory_order_relaxed),
mWriteIndex.load(std::memory_order::memory_order_relaxed));