There is no need to use the main queue just for perform selector.

We were using the main queue to queue up a perform selector and then the code
[self sendStoredCrashReports] was immediately doing a dispatch_async.
This unnecessary thread switching is not needed.

We simplify the above logic and use dispatch_after to queue the block on
the
internal queue after a delay

Note that main queue is typically more loaded and it is better for
non-UI code
to not use the main queue. This may also help improve crash log upload.

This change also switches from @synchronized to dispatch_once as that is
faster
Reference:
http://googlemac.blogspot.com/2006/10/synchronized-swimming.html

BUG=

Change-Id: I81035149cbbf13a3058ca3a11e6efd23980f19ad
Reviewed-on: https://chromium-review.googlesource.com/441364
Reviewed-by: Joshua Peraza <jperaza@chromium.org>
This commit is contained in:
George Kola 2017-02-10 14:33:03 -08:00 committed by Joshua Peraza
parent d4676b89a0
commit 96b79e9bf8

View File

@ -93,11 +93,12 @@ NSString* GetPlatform() {
@implementation BreakpadController @implementation BreakpadController
+ (BreakpadController*)sharedInstance { + (BreakpadController*)sharedInstance {
@synchronized(self) { static dispatch_once_t onceToken;
static BreakpadController* sharedInstance_ = static BreakpadController* sharedInstance ;
[[BreakpadController alloc] initSingleton]; dispatch_once(&onceToken, ^{
return sharedInstance_; sharedInstance = [[BreakpadController alloc] initSingleton];
} });
return sharedInstance;
} }
- (id)init { - (id)init {
@ -179,10 +180,9 @@ NSString* GetPlatform() {
enableUploads_ = YES; enableUploads_ = YES;
[self sendStoredCrashReports]; [self sendStoredCrashReports];
} else { } else {
// disable the enableUpload_ flag.
// sendDelay checks this flag and disables the upload of logs by sendStoredCrashReports
enableUploads_ = NO; enableUploads_ = NO;
[NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(sendStoredCrashReports)
object:nil];
} }
}); });
} }
@ -317,8 +317,8 @@ NSString* GetPlatform() {
[userDefaults synchronize]; [userDefaults synchronize];
} }
// This method must be called from the breakpad queue.
- (void)sendStoredCrashReports { - (void)sendStoredCrashReports {
dispatch_async(queue_, ^{
if (BreakpadGetCrashReportCount(breakpadRef_) == 0) if (BreakpadGetCrashReportCount(breakpadRef_) == 0)
return; return;
@ -341,14 +341,11 @@ NSString* GetPlatform() {
// A report must be sent later. // A report must be sent later.
if (timeToWait > 0) { if (timeToWait > 0) {
// performSelector: doesn't work on queue_ dispatch_time_t delay = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(timeToWait * NSEC_PER_SEC));
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_after(delay, queue_, ^{
[self performSelector:@selector(sendStoredCrashReports) [self sendStoredCrashReports];
withObject:nil
afterDelay:timeToWait];
}); });
} }
});
} }
@end @end