From 70e571fed97fda762cfe941b6ee693ba5f614a2b Mon Sep 17 00:00:00 2001 From: Chris Moeller Date: Sun, 13 Oct 2013 05:59:19 -0700 Subject: [PATCH] Now handling a race condition where the buffer is emptied between where it is read and where it is told how much has been read from it --- Audio/ThirdParty/VirtualRingBuffer/VirtualRingBuffer.m | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Audio/ThirdParty/VirtualRingBuffer/VirtualRingBuffer.m b/Audio/ThirdParty/VirtualRingBuffer/VirtualRingBuffer.m index 421a9570c..cdfeb4105 100644 --- a/Audio/ThirdParty/VirtualRingBuffer/VirtualRingBuffer.m +++ b/Audio/ThirdParty/VirtualRingBuffer/VirtualRingBuffer.m @@ -69,7 +69,7 @@ static void deallocateVirtualBuffer(void *buffer, UInt32 bufferLength); - (BOOL)isEmpty { - return (readPointer != NULL && writePointer != NULL); + return (readPointer == NULL && writePointer == NULL); } @@ -127,6 +127,12 @@ static void deallocateVirtualBuffer(void *buffer, UInt32 bufferLength); void *newReadPointer; newReadPointer = readPointer + length; + + if (newReadPointer == (void *)(intptr_t) length) { + // Someone somehow read from us before another thread emptied the buffer + newReadPointer = NULL; + } + if (newReadPointer >= bufferEnd) newReadPointer -= bufferLength;