Skip to content

Commit bacc2aa

Browse files
ktopley-appledas
authored andcommitted
Port changes from swift repository.
(Radar 31585625) Signed-off-by: Daniel A. Steffen <dsteffen@apple.com>
1 parent 446f7b8 commit bacc2aa

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

PATCHES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,3 +323,4 @@ github commits starting with 29bdc2f from
323323
[d137aa4] APPLIED rdar://32283666
324324
[a69853f] APPLIED rdar://32283666
325325
[eea0667] APPLIED rdar://32283666
326+
[f84d21d] APPLIED rdar://32283666

src/swift/Time.swift

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
import CDispatch
1818

1919
public struct DispatchTime : Comparable {
20+
#if HAVE_MACH
21+
private static let timebaseInfo: mach_timebase_info_data_t = {
22+
var info = mach_timebase_info_data_t(numer: 1, denom: 1)
23+
mach_timebase_info(&info)
24+
return info
25+
}()
26+
#endif
27+
2028
public let rawValue: dispatch_time_t
2129

2230
public static func now() -> DispatchTime {
@@ -39,16 +47,34 @@ public struct DispatchTime : Comparable {
3947
/// - Returns: A new `DispatchTime`
4048
/// - Discussion: This clock is the same as the value returned by
4149
/// `mach_absolute_time` when converted into nanoseconds.
50+
/// On some platforms, the nanosecond value is rounded up to a
51+
/// multiple of the Mach timebase, using the conversion factors
52+
/// returned by `mach_timebase_info()`. The nanosecond equivalent
53+
/// of the rounded result can be obtained by reading the
54+
/// `uptimeNanoseconds` property.
4255
/// Note that `DispatchTime(uptimeNanoseconds: 0)` is
4356
/// equivalent to `DispatchTime.now()`, that is, its value
4457
/// represents the number of nanoseconds since boot (excluding
4558
/// system sleep time), not zero nanoseconds since boot.
4659
public init(uptimeNanoseconds: UInt64) {
47-
self.rawValue = dispatch_time_t(uptimeNanoseconds)
60+
var rawValue = uptimeNanoseconds
61+
#if HAVE_MACH
62+
if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) {
63+
rawValue = (rawValue * UInt64(DispatchTime.timebaseInfo.denom)
64+
+ UInt64(DispatchTime.timebaseInfo.numer - 1)) / UInt64(DispatchTime.timebaseInfo.numer)
65+
}
66+
#endif
67+
self.rawValue = dispatch_time_t(rawValue)
4868
}
4969

5070
public var uptimeNanoseconds: UInt64 {
51-
return UInt64(self.rawValue)
71+
var result = self.rawValue
72+
#if HAVE_MACH
73+
if (DispatchTime.timebaseInfo.numer != DispatchTime.timebaseInfo.denom) {
74+
result = result * UInt64(DispatchTime.timebaseInfo.numer) / UInt64(DispatchTime.timebaseInfo.denom)
75+
}
76+
#endif
77+
return result
5278
}
5379
}
5480

@@ -81,8 +107,12 @@ public struct DispatchWallTime : Comparable {
81107
}
82108

83109
public func <(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
84-
if a.rawValue == ~0 || b.rawValue == ~0 { return false }
85-
return -Int64(a.rawValue) < -Int64(b.rawValue)
110+
if b.rawValue == ~0 {
111+
return a.rawValue != ~0
112+
} else if a.rawValue == ~0 {
113+
return false
114+
}
115+
return -Int64(bitPattern: a.rawValue) < -Int64(bitPattern: b.rawValue)
86116
}
87117

88118
public func ==(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
@@ -147,7 +177,7 @@ public func +(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
147177
}
148178

149179
public func -(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
150-
let interval = seconds * Double(NSEC_PER_SEC)
180+
let interval = -seconds * Double(NSEC_PER_SEC)
151181
let t = CDispatch.dispatch_time(time.rawValue,
152182
interval.isInfinite || interval.isNaN ? Int64.min : Int64(interval))
153183
return DispatchWallTime(rawValue: t)

0 commit comments

Comments
 (0)