Skip to content

Instantly share code, notes, and snippets.

@mikeash
Created March 15, 2013 20:18
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mikeash/5172803 to your computer and use it in GitHub Desktop.
Save mikeash/5172803 to your computer and use it in GitHub Desktop.
Main thread watchdog
- (void)watchdog {
NSTimeInterval pingInterval = 1.0/60.0;
NSTimeInterval watchdogInterval = 1.0/30.0;
__block NSTimeInterval lastPing = 0;
NSProcessInfo *pi = [NSProcessInfo processInfo];
dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, pingInterval * NSEC_PER_SEC, 0);
dispatch_source_set_event_handler(timer, ^{
lastPing = [pi systemUptime];
});
dispatch_resume(timer);
dispatch_source_t watchdog = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
dispatch_source_set_timer(watchdog, DISPATCH_TIME_NOW, pingInterval * NSEC_PER_SEC, 0);
NSLog(@"Set timer to %f", pingInterval * NSEC_PER_SEC);
dispatch_source_set_event_handler(watchdog, ^{
NSTimeInterval now = [pi systemUptime];
NSTimeInterval delta = now - lastPing;
if(delta > watchdogInterval) {
NSLog(@"Jammed for %f seconds", delta);
}
});
dispatch_resume(watchdog);
CFBridgingRetain(timer);
CFBridgingRetain(watchdog);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment