From 033775ac3d64fc58490a82eb34aab38d098945bc Mon Sep 17 00:00:00 2001 From: Garrett Moon Date: Mon, 18 Sep 2017 09:06:28 -0700 Subject: [PATCH] Fix a deadlock with display link --- Source/ASImageNode+AnimatedImage.mm | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/Source/ASImageNode+AnimatedImage.mm b/Source/ASImageNode+AnimatedImage.mm index 0d12351d..9ef672b8 100644 --- a/Source/ASImageNode+AnimatedImage.mm +++ b/Source/ASImageNode+AnimatedImage.mm @@ -65,13 +65,12 @@ NSString *const ASAnimatedImageDefaultRunLoopMode = NSRunLoopCommonModes; }; } + animatedImage.playbackReadyCallback = ^{ + // In this case the lock is already gone we have to call the unlocked version therefore + [weakSelf setShouldAnimate:YES]; + }; if (animatedImage.playbackReady) { [self _locked_setShouldAnimate:YES]; - } else { - animatedImage.playbackReadyCallback = ^{ - // In this case the lock is already gone we have to call the unlocked version therefore - [weakSelf setShouldAnimate:YES]; - }; } } @@ -217,11 +216,13 @@ NSString *const ASAnimatedImageDefaultRunLoopMode = NSRunLoopCommonModes; NSLog(@"starting animation: %p", self); #endif + // Get frame interval before holding display link lock to avoid deadlock + NSUInteger frameInterval = self.animatedImage.frameInterval; ASDN::MutexLocker l(_displayLinkLock); if (_displayLink == nil) { _playHead = 0; _displayLink = [CADisplayLink displayLinkWithTarget:[ASWeakProxy weakProxyWithTarget:self] selector:@selector(displayLinkFired:)]; - _displayLink.frameInterval = self.animatedImage.frameInterval; + _displayLink.frameInterval = frameInterval; [_displayLink addToRunLoop:[NSRunLoop mainRunLoop] forMode:_animatedImageRunLoopMode]; } else { @@ -262,7 +263,9 @@ NSString *const ASAnimatedImageDefaultRunLoopMode = NSRunLoopCommonModes; if (self.animatedImage.coverImageReady) { [self setCoverImage:self.animatedImage.coverImage]; } - [self startAnimating]; + if (self.animatedImage.playbackReady) { + [self startAnimating]; + } } - (void)didExitVisibleState