Files
swift-mirror/stdlib/public/SDK/Dispatch/Time.swift
Matt Wright 8ac413a0b5 [libdispatch] Post-beta API changes and bug fixes
* Fix DispatchSourceSignal initialisation such that it no longer
    registers for the wrong source type.

    * Remove (group:) option from DispatchWorkItem, introduce group
    options to `.async` methods that accept DispatchWorkItem.

    * Rename `DispatchSourceType` to `DispatchSourceProtocol`

    * Rework DispatchQueue attributes and flags into a less confusing
    approach.

    * Fixes:

	SR-1817, SR-1771, SR-1770, SR-1769

	<rdar://problem/26725156> <rdar://problem/26873917>
	<rdar://problem/26918843> <rdar://problem/26810149>
	<rdar://problem/27117023> <rdar://problem/27121422>
	<rdar://problem/27236887> <rdar://problem/27337555>
2016-07-18 13:22:23 -07:00

140 lines
4.4 KiB
Swift

//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
public struct DispatchTime : Comparable {
public let rawValue: dispatch_time_t
public static func now() -> DispatchTime {
let t = __dispatch_time(0, 0)
return DispatchTime(rawValue: t)
}
public static let distantFuture = DispatchTime(rawValue: ~0)
private init(rawValue: dispatch_time_t) {
self.rawValue = rawValue
}
/// Creates a `DispatchTime` relative to the system clock that
/// ticks since boot.
///
/// - Parameters:
/// - uptimeNanoseconds: The number of nanoseconds since boot, excluding
/// time the system spent asleep
/// - Returns: A new `DispatchTime`
/// - Discussion: This clock is the same as the value returned by
/// `mach_absolute_time` when converted into nanoseconds.
public init(uptimeNanoseconds: UInt64) {
self.rawValue = dispatch_time_t(uptimeNanoseconds)
}
public var uptimeNanoseconds: UInt64 {
return UInt64(self.rawValue)
}
}
public func <(a: DispatchTime, b: DispatchTime) -> Bool {
if a.rawValue == ~0 || b.rawValue == ~0 { return false }
return a.rawValue < b.rawValue
}
public func ==(a: DispatchTime, b: DispatchTime) -> Bool {
return a.rawValue == b.rawValue
}
public struct DispatchWallTime : Comparable {
public let rawValue: dispatch_time_t
public static func now() -> DispatchWallTime {
return DispatchWallTime(rawValue: __dispatch_walltime(nil, 0))
}
public static let distantFuture = DispatchWallTime(rawValue: ~0)
private init(rawValue: dispatch_time_t) {
self.rawValue = rawValue
}
public init(timespec: timespec) {
var t = timespec
self.rawValue = __dispatch_walltime(&t, 0)
}
}
public func <(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
if a.rawValue == ~0 || b.rawValue == ~0 { return false }
return -Int64(a.rawValue) < -Int64(b.rawValue)
}
public func ==(a: DispatchWallTime, b: DispatchWallTime) -> Bool {
return a.rawValue == b.rawValue
}
@available(*, deprecated, renamed: "DispatchWallTime")
public typealias DispatchWalltime = DispatchWallTime
public enum DispatchTimeInterval {
case seconds(Int)
case milliseconds(Int)
case microseconds(Int)
case nanoseconds(Int)
internal var rawValue: UInt64 {
switch self {
case .seconds(let s): return UInt64(s) * NSEC_PER_SEC
case .milliseconds(let ms): return UInt64(ms) * NSEC_PER_MSEC
case .microseconds(let us): return UInt64(us) * NSEC_PER_USEC
case .nanoseconds(let ns): return UInt64(ns)
}
}
}
public func +(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
let t = __dispatch_time(time.rawValue, Int64(interval.rawValue))
return DispatchTime(rawValue: t)
}
public func -(time: DispatchTime, interval: DispatchTimeInterval) -> DispatchTime {
let t = __dispatch_time(time.rawValue, -Int64(interval.rawValue))
return DispatchTime(rawValue: t)
}
public func +(time: DispatchTime, seconds: Double) -> DispatchTime {
let t = __dispatch_time(time.rawValue, Int64(seconds * Double(NSEC_PER_SEC)))
return DispatchTime(rawValue: t)
}
public func -(time: DispatchTime, seconds: Double) -> DispatchTime {
let t = __dispatch_time(time.rawValue, Int64(-seconds * Double(NSEC_PER_SEC)))
return DispatchTime(rawValue: t)
}
public func +(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
let t = __dispatch_time(time.rawValue, Int64(interval.rawValue))
return DispatchWallTime(rawValue: t)
}
public func -(time: DispatchWallTime, interval: DispatchTimeInterval) -> DispatchWallTime {
let t = __dispatch_time(time.rawValue, -Int64(interval.rawValue))
return DispatchWallTime(rawValue: t)
}
public func +(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
let t = __dispatch_time(time.rawValue, Int64(seconds * Double(NSEC_PER_SEC)))
return DispatchWallTime(rawValue: t)
}
public func -(time: DispatchWallTime, seconds: Double) -> DispatchWallTime {
let t = __dispatch_time(time.rawValue, Int64(-seconds * Double(NSEC_PER_SEC)))
return DispatchWallTime(rawValue: t)
}