Skip to content

Instantly share code, notes, and snippets.

@vinhnx
Forked from steipete/gist:3933090
Created December 19, 2013 05:22
Show Gist options
  • Save vinhnx/8034790 to your computer and use it in GitHub Desktop.
Save vinhnx/8034790 to your computer and use it in GitHub Desktop.
code from https://gist.github.com/steipete/3933090 "Simple main thread usage detector that I'm using in PSPDFKit to find performance problems early on."
// Smart little helper to find main thread hangs. Enable in appDidFinishLaunching.
// Only available with source code in DEBUG mode.
@interface PSPDFHangDetector : NSObject
+ (void)startHangDetector;
@end
@implementation PSPDFHangDetector
+ (void)startHangDetector {
#ifdef DEBUG
NSThread *hangDetectionThread = [[NSThread alloc] initWithTarget:self selector:@selector(deadThreadMain) object:nil];
[hangDetectionThread start];
#endif
}
#ifdef DEBUG
static volatile NSInteger DEAD_SIGNAL = 0;
+ (void)deadThreadTick {
if (DEAD_SIGNAL == 1) {
NSLog(@"Main Thread doesn't answer...");
}
DEAD_SIGNAL = 1;
dispatch_async(dispatch_get_main_queue(), ^{DEAD_SIGNAL = 0;});
}
+ (void)deadThreadMain {
[NSThread currentThread].name = @"PSPDFHangDetection";
@autoreleasepool {
[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(deadThreadTick) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];
}
}
#endif
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment