This repository has been archived by the owner on Nov 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 233
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid use of CFAbsoluteTime as much as possible.
A helper class SPDYStopwatch is introduced here to abstract common time management duties from the rest of the code. Since CFAbsoluteTimeGetCurrent() is affected by clock and timezone changes, it's use should be avoided. mach_absolute_time() is unaffected by clock changes, but is susceptible to overflow issues (which can be worked around) and doesn't increment while the system is asleep (which cannot be worked around). However, it has more appropriate behavior. Our use of CFAbsoluteTimeGetCurrent() for the timers is unavoidable, since Cocoa timers are based on it. This seems like it would present undesirable behavior for our deferrable and connect timers if the clock were to change, but there's not much to do about it.
- Loading branch information
Showing
7 changed files
with
205 additions
and
32 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// SPDYStopwatch.h | ||
// SPDY | ||
// | ||
// Copyright (c) 2014 Twitter, Inc. All rights reserved. | ||
// Licensed under the Apache License v2.0 | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Created by Kevin Goodier. | ||
// | ||
|
||
typedef double SPDYTimeInterval; | ||
|
||
@interface SPDYStopwatch : NSObject | ||
+ (SPDYTimeInterval)currentSystemTime; | ||
- (id)init; | ||
- (void)reset; | ||
- (SPDYTimeInterval)elapsedSeconds; | ||
@end |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
// | ||
// SPDYStopwatch.m | ||
// SPDY | ||
// | ||
// Copyright (c) 2014 Twitter, Inc. All rights reserved. | ||
// Licensed under the Apache License v2.0 | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Created by Kevin Goodier | ||
// | ||
|
||
#import <mach/mach_time.h> | ||
#import "SPDYStopwatch.h" | ||
|
||
@implementation SPDYStopwatch | ||
{ | ||
SPDYTimeInterval _startTime; | ||
} | ||
|
||
static dispatch_once_t __initTimebase; | ||
static double __machTimebaseToSeconds; | ||
static mach_timebase_info_data_t __machTimebase; | ||
|
||
+ (void)initialize | ||
{ | ||
dispatch_once(&__initTimebase, ^{ | ||
kern_return_t status = mach_timebase_info(&__machTimebase); | ||
// Everything will be 0 if this fails. | ||
if (status != KERN_SUCCESS) { | ||
__machTimebase.numer = 0; | ||
__machTimebase.denom = 1; | ||
} | ||
__machTimebaseToSeconds = (double)__machTimebase.numer / ((double)__machTimebase.denom * 1000000000.0); | ||
}); | ||
} | ||
|
||
+ (SPDYTimeInterval)currentSystemTime | ||
{ | ||
uint64_t now = mach_absolute_time(); | ||
return (SPDYTimeInterval)now * __machTimebaseToSeconds; | ||
} | ||
|
||
- (id)init | ||
{ | ||
self = [super init]; | ||
if (self) { | ||
_startTime = [SPDYStopwatch currentSystemTime]; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)reset | ||
{ | ||
_startTime = [SPDYStopwatch currentSystemTime]; | ||
} | ||
|
||
- (SPDYTimeInterval)elapsedSeconds | ||
{ | ||
return [SPDYStopwatch currentSystemTime] - _startTime; | ||
} | ||
|
||
@end |
Oops, something went wrong.