From 9822dcc4c04acc9bbbc5f53fe056f95f53c2c78b Mon Sep 17 00:00:00 2001 From: Christopher Snowhill Date: Fri, 9 Dec 2022 21:17:45 -0800 Subject: [PATCH] Audio Player: Only wait for unstopped input Input thread now signals when it has stopped and is about to return, in case the input thread returns before the BufferChain dealloc function would be waiting for it to terminate. Somehow, even though the Semaphore is being signaled at this point, the BufferChain still ends up waiting the default of 2.5 seconds for the signal that apparently never comes, delaying file stoppage. This prevents the wait action entirely. Must have been some sort of race condition. Signed-off-by: Christopher Snowhill --- Audio/Chain/BufferChain.m | 3 ++- Audio/Chain/InputNode.h | 1 + Audio/Chain/InputNode.m | 3 +++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Audio/Chain/BufferChain.m b/Audio/Chain/BufferChain.m index a4e1fae38..1ff7a08c8 100644 --- a/Audio/Chain/BufferChain.m +++ b/Audio/Chain/BufferChain.m @@ -156,7 +156,8 @@ [inputNode setShouldContinue:NO]; [[inputNode exitAtTheEndOfTheStream] signal]; [[inputNode semaphore] signal]; - [[inputNode exitAtTheEndOfTheStream] wait]; // wait for decoder to be closed (see InputNode's -(void)process ) + if(![inputNode threadExited]) + [[inputNode exitAtTheEndOfTheStream] wait]; // wait for decoder to be closed (see InputNode's -(void)process ) DLog(@"Bufferchain dealloc"); } diff --git a/Audio/Chain/InputNode.h b/Audio/Chain/InputNode.h index 5a33ae47c..02c7a69c9 100644 --- a/Audio/Chain/InputNode.h +++ b/Audio/Chain/InputNode.h @@ -34,6 +34,7 @@ Semaphore *exitAtTheEndOfTheStream; } @property(readonly) Semaphore *exitAtTheEndOfTheStream; +@property(readonly) BOOL threadExited; - (BOOL)openWithSource:(id)source; - (BOOL)openWithDecoder:(id)d; diff --git a/Audio/Chain/InputNode.m b/Audio/Chain/InputNode.m index a071fa34a..98fa02c5d 100644 --- a/Audio/Chain/InputNode.m +++ b/Audio/Chain/InputNode.m @@ -21,12 +21,14 @@ static void *kInputNodeContext = &kInputNodeContext; +@synthesize threadExited; @synthesize exitAtTheEndOfTheStream; - (id)initWithController:(id)c previous:(id)p { self = [super initWithController:c previous:p]; if(self) { exitAtTheEndOfTheStream = [[Semaphore alloc] init]; + threadExited = NO; } return self; @@ -223,6 +225,7 @@ static void *kInputNodeContext = &kInputNodeContext; [decoder close]; [exitAtTheEndOfTheStream signal]; + threadExited = YES; DLog("Input node thread stopping"); }