Created
March 15, 2013 20:18
-
-
Save mikeash/5172803 to your computer and use it in GitHub Desktop.
Main thread watchdog
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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