Skip to content

Fixes incorrect implementation of DispatchTime.init(uptimeNanoseconds:) #7432

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 21, 2017
Merged

Fixes incorrect implementation of DispatchTime.init(uptimeNanoseconds:) #7432

merged 3 commits into from
Feb 21, 2017

Conversation

ktopley-apple
Copy link
Contributor

@ktopley-apple ktopley-apple commented Feb 13, 2017

Internally, DispatchTime works in Mach absolute time, but the DispatchTime(uptimeNanoseconds:) initializer allows time to be given as nanoseconds since boot. This needs to be converted to Mach absolute time, but the conversion was not being performed. As a result, on platforms where the conversion factor is not 1, the wrong time is stored.

The fix is to convert the given time to Mach absolute time when the conversion factor is not 1. This may cause some loss of precision, so the uptimeNanoseconds property will not hold the same value as the one passed to the initializer, unless that value was an exact multiple of the conversion factor. One way to fix this would be to store both the actual value that was given and the converted value. However, dispatch does not use the original value and keeping it would add another field to the DispatchTime object, just on the off-chance that the application might want the exact initial value. Instead of saving the actual value, I added a comment to the initializer to indicate that the value read from the uptimeNanoseconds property will be the effective value, which might be rounded up from the original.

(Radar 29660448)
rdar://problem/29660448

…its argument to Mach absolute time instead of treating it as Mach absolute time.

(Radar 29660448)
…convert its argument to Mach absolute time instead of treating it as Mach absolute time."

This reverts commit 1377cae.
…its argument to Mach absolute time instead of treating it as Mach absolute time.

(Radar 29660448)
@ktopley-apple
Copy link
Contributor Author

@moiseev @mwwa @das please review

@ktopley-apple
Copy link
Contributor Author

@swift-ci please smoke test

/// Note that `DispatchTime(uptimeNanoseconds: 0)` is
/// equivalent to `DispatchTime.now()`, that is, its value
/// represents the number of nanoseconds since boot (excluding
/// system sleep time), not zero nanoseconds since boot.
public init(uptimeNanoseconds: UInt64) {
self.rawValue = dispatch_time_t(uptimeNanoseconds)
var rawValue = uptimeNanoseconds
if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More of a stylistic thing, but at the expense of an else block you can make this immutable.

@ktopley-apple
Copy link
Contributor Author

@swift-ci please smoke test and merge

@swift-ci swift-ci merged commit 6c981c8 into swiftlang:master Feb 21, 2017
@ktopley-apple ktopley-apple deleted the dispatch-after-mach branch February 21, 2017 22:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants