|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +// Only support 64bit |
| 14 | +#if !(os(iOS) && (arch(i386) || arch(arm))) |
| 15 | + |
| 16 | +import Combine |
| 17 | + |
| 18 | +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 19 | +private func clampedIntProduct(_ m1: Int, _ m2: UInt64) -> Int { |
| 20 | + assert(m2 > 0, "multiplier must be positive") |
| 21 | + guard m1 < Int.max, m2 < Int.max else { return Int.max } |
| 22 | + let (result, overflow) = m1.multipliedReportingOverflow(by: Int(m2)) |
| 23 | + if overflow { |
| 24 | + return m1 > 0 ? Int.max : Int.min |
| 25 | + } |
| 26 | + return result |
| 27 | +} |
| 28 | + |
| 29 | +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 30 | +extension DispatchTimeInterval { |
| 31 | + fileprivate var nanoseconds: Int { |
| 32 | + switch self { |
| 33 | + case .seconds(let s): return clampedIntProduct(s, NSEC_PER_SEC) |
| 34 | + case .milliseconds(let ms): return clampedIntProduct(ms, NSEC_PER_MSEC) |
| 35 | + case .microseconds(let us): return clampedIntProduct(us, NSEC_PER_USEC) |
| 36 | + case .nanoseconds(let ns): return ns |
| 37 | + case .never: return Int.max |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +// This is Strideable except: <rdar://problem/35158274> |
| 43 | +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 44 | +extension DispatchTime /* : Strideable */ { |
| 45 | + typealias Stride = DispatchTimeInterval |
| 46 | + |
| 47 | + public func distance(to other: DispatchTime) -> DispatchTimeInterval { |
| 48 | + let lhs = other.rawValue |
| 49 | + let rhs = rawValue |
| 50 | + if lhs >= rhs { |
| 51 | + return DispatchTimeInterval.nanoseconds(Int(lhs - rhs)) |
| 52 | + } else { |
| 53 | + return DispatchTimeInterval.nanoseconds(0 - Int(rhs - lhs)) |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + public func advanced(by n: DispatchTimeInterval) -> DispatchTime { |
| 58 | + return self + n |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *) |
| 63 | +extension DispatchQueue: Scheduler { |
| 64 | + /// The scheduler time type used by the dispatch queue. |
| 65 | + public struct SchedulerTimeType: Strideable, Codable, Hashable { |
| 66 | + /// The dispatch time represented by this type. |
| 67 | + public var dispatchTime: DispatchTime |
| 68 | + |
| 69 | + /// Creates a dispatch queue time type instance. |
| 70 | + /// |
| 71 | + /// - Parameter time: The dispatch time to represent. |
| 72 | + public init(_ time: DispatchTime) { |
| 73 | + dispatchTime = time |
| 74 | + } |
| 75 | + |
| 76 | + public init(from decoder: Decoder) throws { |
| 77 | + let container = try decoder.singleValueContainer() |
| 78 | + let time = DispatchTime(uptimeNanoseconds: try container.decode(UInt64.self)) |
| 79 | + self.init(time) |
| 80 | + } |
| 81 | + |
| 82 | + public func encode(to encoder: Encoder) throws { |
| 83 | + var container = encoder.singleValueContainer() |
| 84 | + try container.encode(dispatchTime.uptimeNanoseconds) |
| 85 | + } |
| 86 | + |
| 87 | + /// Returns the distance to another dispatch queue time. |
| 88 | + /// |
| 89 | + /// - Parameter other: Another dispatch queue time. |
| 90 | + /// - Returns: The time interval between this time and the provided time. |
| 91 | + public func distance(to other: SchedulerTimeType) -> Stride { |
| 92 | + return Stride(self.dispatchTime.distance(to: other.dispatchTime)) |
| 93 | + } |
| 94 | + |
| 95 | + /// Returns a dispatch queue scheduler time calculated by advancing this instance’s time by the given interval. |
| 96 | + /// |
| 97 | + /// - Parameter n: A time interval to advance. |
| 98 | + /// - Returns: A dispatch queue time advanced by the given interval from this instance’s time. |
| 99 | + public func advanced(by n: Stride) -> SchedulerTimeType { |
| 100 | + return SchedulerTimeType(self.dispatchTime.advanced(by: n.timeInterval)) |
| 101 | + } |
| 102 | + |
| 103 | + public func hash(into hasher: inout Hasher) { |
| 104 | + hasher.combine(dispatchTime.rawValue) |
| 105 | + } |
| 106 | + |
| 107 | + public struct Stride: SchedulerTimeIntervalConvertible, Comparable, SignedNumeric, ExpressibleByFloatLiteral, Hashable, Codable { |
| 108 | + /// If created via floating point literal, the value is converted to nanoseconds via multiplication. |
| 109 | + public typealias FloatLiteralType = Double |
| 110 | + |
| 111 | + /// Nanoseconds, same as DispatchTimeInterval. |
| 112 | + public typealias IntegerLiteralType = Int |
| 113 | + public typealias Magnitude = Int |
| 114 | + |
| 115 | + /// The value of this time interval in nanoseconds. |
| 116 | + public var magnitude: Int |
| 117 | + |
| 118 | + /// A `DispatchTimeInterval` created with the value of this type in nanoseconds. |
| 119 | + public var timeInterval: DispatchTimeInterval { |
| 120 | + return .nanoseconds(magnitude) |
| 121 | + } |
| 122 | + |
| 123 | + /// Creates a dispatch queue time interval from the given dispatch time interval. |
| 124 | + /// |
| 125 | + /// - Parameter timeInterval: A dispatch time interval. |
| 126 | + public init(_ timeInterval: DispatchTimeInterval) { |
| 127 | + magnitude = Int(timeInterval.nanoseconds) |
| 128 | + } |
| 129 | + |
| 130 | + /// Creates a dispatch queue time interval from a floating-point seconds value. |
| 131 | + /// |
| 132 | + /// - Parameter value: The number of seconds, as a `Double`. |
| 133 | + public init(floatLiteral value: Double) { |
| 134 | + magnitude = Int(value * 1_000_000_000) |
| 135 | + } |
| 136 | + |
| 137 | + /// Creates a dispatch queue time interval from an integer seconds value. |
| 138 | + /// |
| 139 | + /// - Parameter value: The number of seconds, as an `Int`. |
| 140 | + public init(integerLiteral value: Int) { |
| 141 | + magnitude = value * 1_000_000_000 |
| 142 | + } |
| 143 | + |
| 144 | + /// Creates a dispatch queue time interval from a binary integer type. |
| 145 | + /// |
| 146 | + /// If `exactly` cannot convert to an `Int`, the resulting time interval is `nil`. |
| 147 | + /// - Parameter exactly: A binary integer representing a time interval. |
| 148 | + public init?<T>(exactly source: T) where T: BinaryInteger { |
| 149 | + if let v = Int(exactly: source) { |
| 150 | + magnitude = v |
| 151 | + } else { |
| 152 | + return nil |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + // --- |
| 157 | + |
| 158 | + public static func < (lhs: Stride, rhs: Stride) -> Bool { |
| 159 | + return lhs.magnitude < rhs.magnitude |
| 160 | + } |
| 161 | + |
| 162 | + // --- |
| 163 | + |
| 164 | + public static func * (lhs: Stride, rhs: Stride) -> Stride { |
| 165 | + return Stride(.nanoseconds(lhs.magnitude * rhs.magnitude)) |
| 166 | + } |
| 167 | + |
| 168 | + public static func + (lhs: Stride, rhs: Stride) -> Stride { |
| 169 | + return Stride(.nanoseconds(lhs.magnitude + rhs.magnitude)) |
| 170 | + } |
| 171 | + |
| 172 | + public static func - (lhs: Stride, rhs: Stride) -> Stride { |
| 173 | + return Stride(.nanoseconds(lhs.magnitude - rhs.magnitude)) |
| 174 | + } |
| 175 | + |
| 176 | + // --- |
| 177 | + |
| 178 | + public static func -= (lhs: inout Stride, rhs: Stride) { |
| 179 | + let result = lhs - rhs |
| 180 | + lhs = result |
| 181 | + } |
| 182 | + |
| 183 | + public static func *= (lhs: inout Stride, rhs: Stride) { |
| 184 | + let result = lhs * rhs |
| 185 | + lhs = result |
| 186 | + } |
| 187 | + |
| 188 | + public static func += (lhs: inout Stride, rhs: Stride) { |
| 189 | + let result = lhs + rhs |
| 190 | + lhs = result |
| 191 | + } |
| 192 | + |
| 193 | + // --- |
| 194 | + |
| 195 | + public static func seconds(_ s: Double) -> Stride { |
| 196 | + return Stride(.nanoseconds(Int(s * 1_000_000_000))) |
| 197 | + } |
| 198 | + |
| 199 | + public static func seconds(_ s: Int) -> Stride { |
| 200 | + return Stride(.seconds(s)) |
| 201 | + } |
| 202 | + |
| 203 | + public static func milliseconds(_ ms: Int) -> Stride { |
| 204 | + return Stride(.milliseconds(ms)) |
| 205 | + } |
| 206 | + |
| 207 | + public static func microseconds(_ us: Int) -> Stride { |
| 208 | + return Stride(.microseconds(us)) |
| 209 | + } |
| 210 | + |
| 211 | + public static func nanoseconds(_ ns: Int) -> Stride { |
| 212 | + return Stride(.nanoseconds(ns)) |
| 213 | + } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + /// Options that affect the operation of the dispatch queue scheduler. |
| 218 | + public struct SchedulerOptions { |
| 219 | + /// The dispatch queue quality of service. |
| 220 | + public var qos: DispatchQoS |
| 221 | + |
| 222 | + /// The dispatch queue work item flags. |
| 223 | + public var flags: DispatchWorkItemFlags |
| 224 | + |
| 225 | + /// The dispatch group, if any, that should be used for performing actions. |
| 226 | + public var group: DispatchGroup? |
| 227 | + |
| 228 | + public init(qos: DispatchQoS = .unspecified, flags: DispatchWorkItemFlags = [], group: DispatchGroup? = nil) { |
| 229 | + self.qos = qos |
| 230 | + self.flags = flags |
| 231 | + self.group = group |
| 232 | + } |
| 233 | + } |
| 234 | + |
| 235 | + public var minimumTolerance: SchedulerTimeType.Stride { |
| 236 | + return SchedulerTimeType.Stride(DispatchTimeInterval.seconds(0)) |
| 237 | + } |
| 238 | + |
| 239 | + public var now: DispatchQueue.SchedulerTimeType { |
| 240 | + return SchedulerTimeType(DispatchTime.now()) |
| 241 | + } |
| 242 | + |
| 243 | + public func schedule(options: SchedulerOptions?, _ action: @escaping () -> Void) { |
| 244 | + let qos = options?.qos ?? .unspecified |
| 245 | + let flags = options?.flags ?? [] |
| 246 | + |
| 247 | + if let group = options?.group { |
| 248 | + // Distinguish on the group because it appears to not be a call-through like the others. This may need to be adjusted. |
| 249 | + self.async(group: group, qos: qos, flags: flags, execute: action) |
| 250 | + } else { |
| 251 | + self.async(qos: qos, flags: flags, execute: action) |
| 252 | + } |
| 253 | + } |
| 254 | + |
| 255 | + public func schedule(after date: SchedulerTimeType, |
| 256 | + tolerance: SchedulerTimeType.Stride, |
| 257 | + options: SchedulerOptions?, |
| 258 | + _ action: @escaping () -> Void) { |
| 259 | + // TODO: Tolerance ignored |
| 260 | + let qos = options?.qos ?? .unspecified |
| 261 | + let flags = options?.flags ?? [] |
| 262 | + |
| 263 | + self.asyncAfter(deadline: date.dispatchTime, qos: qos, flags: flags, execute: action) |
| 264 | + } |
| 265 | + |
| 266 | + public func schedule(after date: SchedulerTimeType, |
| 267 | + interval: SchedulerTimeType.Stride, |
| 268 | + tolerance: SchedulerTimeType.Stride, |
| 269 | + options: SchedulerOptions?, |
| 270 | + _ action: @escaping () -> Void) -> Cancellable { |
| 271 | + let source = DispatchSource.makeTimerSource(flags: DispatchSource.TimerFlags(), queue: self) |
| 272 | + |
| 273 | + source.schedule(deadline: date.dispatchTime, |
| 274 | + repeating: interval.timeInterval, |
| 275 | + leeway: tolerance.timeInterval) |
| 276 | + source.setEventHandler(handler: action) |
| 277 | + source.resume() |
| 278 | + |
| 279 | + return AnyCancellable(source.cancel) |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +#endif /* !(os(iOS) && (arch(i386) || arch(arm))) */ |
0 commit comments