forked from ReactiveCocoa/ReactiveSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScheduler.swift
613 lines (534 loc) · 19.1 KB
/
Scheduler.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//
// Scheduler.swift
// ReactiveSwift
//
// Created by Justin Spahr-Summers on 2014-06-02.
// Copyright (c) 2014 GitHub. All rights reserved.
//
import Dispatch
import Foundation
#if os(Linux)
import let CDispatch.NSEC_PER_SEC
#endif
/// Represents a serial queue of work items.
public protocol Scheduler: class {
/// Enqueues an action on the scheduler.
///
/// When the work is executed depends on the scheduler in use.
///
/// - parameters:
/// - action: The action to be scheduled.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
@discardableResult
func schedule(_ action: @escaping () -> Void) -> Disposable?
}
/// A particular kind of scheduler that supports enqueuing actions at future
/// dates.
public protocol DateScheduler: Scheduler {
/// The current date, as determined by this scheduler.
///
/// This can be implemented to deterministically return a known date (e.g.,
/// for testing purposes).
var currentDate: Date { get }
/// Schedules an action for execution at or after the given date.
///
/// - parameters:
/// - date: The start date.
/// - action: A closure of the action to be performed.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
@discardableResult
func schedule(after date: Date, action: @escaping () -> Void) -> Disposable?
/// Schedules a recurring action at the given interval, beginning at the
/// given date.
///
/// - parameters:
/// - date: The start date.
/// - interval: A repetition interval.
/// - leeway: Some delta for repetition.
/// - action: A closure of the action to be performed.
///
/// - note: If you plan to specify an `interval` value greater than 200,000
/// seconds, use `schedule(after:interval:leeway:action)` instead
/// and specify your own `leeway` value to avoid potential overflow.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
@discardableResult
func schedule(after date: Date, interval: DispatchTimeInterval, leeway: DispatchTimeInterval, action: @escaping () -> Void) -> Disposable?
}
/// A scheduler that performs all work synchronously.
public final class ImmediateScheduler: Scheduler {
public init() {}
/// Immediately calls passed in `action`.
///
/// - parameters:
/// - action: A closure of the action to be performed.
///
/// - returns: `nil`.
@discardableResult
public func schedule(_ action: @escaping () -> Void) -> Disposable? {
action()
return nil
}
}
/// A scheduler that performs all work on the main queue, as soon as possible.
///
/// If the caller is already running on the main queue when an action is
/// scheduled, it may be run synchronously. However, ordering between actions
/// will always be preserved.
public final class UIScheduler: Scheduler {
private static let dispatchSpecificKey = DispatchSpecificKey<UInt8>()
private static let dispatchSpecificValue = UInt8.max
private static var __once: () = {
DispatchQueue.main.setSpecific(key: UIScheduler.dispatchSpecificKey,
value: dispatchSpecificValue)
}()
#if os(Linux)
private var queueLength: Atomic<Int32> = Atomic(0)
#else
// `inout` references do not guarantee atomicity. Use `UnsafeMutablePointer`
// instead.
//
// https://lists.swift.org/pipermail/swift-users/Week-of-Mon-20161205/004147.html
private let queueLength: UnsafeMutablePointer<Int32> = {
let memory = UnsafeMutablePointer<Int32>.allocate(capacity: 1)
memory.initialize(to: 0)
return memory
}()
deinit {
queueLength.deinitialize(count: 1)
queueLength.deallocate()
}
#endif
/// Initializes `UIScheduler`
public init() {
/// This call is to ensure the main queue has been setup appropriately
/// for `UIScheduler`. It is only called once during the application
/// lifetime, since Swift has a `dispatch_once` like mechanism to
/// lazily initialize global variables and static variables.
_ = UIScheduler.__once
}
/// Queues an action to be performed on main queue. If the action is called
/// on the main thread and no work is queued, no scheduling takes place and
/// the action is called instantly.
///
/// - parameters:
/// - action: A closure of the action to be performed on the main thread.
///
/// - returns: `Disposable` that can be used to cancel the work before it
/// begins.
@discardableResult
public func schedule(_ action: @escaping () -> Void) -> Disposable? {
let positionInQueue = enqueue()
// If we're already running on the main queue, and there isn't work
// already enqueued, we can skip scheduling and just execute directly.
if positionInQueue == 1 && DispatchQueue.getSpecific(key: UIScheduler.dispatchSpecificKey) == UIScheduler.dispatchSpecificValue {
action()
dequeue()
return nil
} else {
let disposable = AnyDisposable()
DispatchQueue.main.async {
defer { self.dequeue() }
guard !disposable.isDisposed else { return }
action()
}
return disposable
}
}
private func dequeue() {
#if os(Linux)
queueLength.modify { $0 -= 1 }
#else
OSAtomicDecrement32(queueLength)
#endif
}
private func enqueue() -> Int32 {
#if os(Linux)
return queueLength.modify { value -> Int32 in
value += 1
return value
}
#else
return OSAtomicIncrement32(queueLength)
#endif
}
}
/// A `Hashable` wrapper for `DispatchSourceTimer`. `Hashable` conformance is
/// based on the identity of the wrapper object rather than the wrapped
/// `DispatchSourceTimer`, so two wrappers wrapping the same timer will *not*
/// be equal.
private final class DispatchSourceTimerWrapper: Hashable {
private let value: DispatchSourceTimer
#if swift(>=4.1.50)
fileprivate func hash(into hasher: inout Hasher) {
hasher.combine(ObjectIdentifier(self))
}
#else
fileprivate var hashValue: Int {
return ObjectIdentifier(self).hashValue
}
#endif
fileprivate init(_ value: DispatchSourceTimer) {
self.value = value
}
fileprivate static func ==(lhs: DispatchSourceTimerWrapper, rhs: DispatchSourceTimerWrapper) -> Bool {
// Note that this isn't infinite recursion thanks to `===`.
return lhs === rhs
}
}
/// A scheduler backed by a serial GCD queue.
public final class QueueScheduler: DateScheduler {
/// A singleton `QueueScheduler` that always targets the main thread's GCD
/// queue.
///
/// - note: Unlike `UIScheduler`, this scheduler supports scheduling for a
/// future date, and will always schedule asynchronously (even if
/// already running on the main thread).
public static let main = QueueScheduler(internalQueue: DispatchQueue.main)
public var currentDate: Date {
return Date()
}
public let queue: DispatchQueue
private var timers: Atomic<Set<DispatchSourceTimerWrapper>>
internal init(internalQueue: DispatchQueue) {
queue = internalQueue
timers = Atomic(Set())
}
/// Initializes a scheduler that will target the given queue with its
/// work.
///
/// - note: Even if the queue is concurrent, all work items enqueued with
/// the `QueueScheduler` will be serial with respect to each other.
///
/// - warning: Obsoleted in OS X 10.11
@available(OSX, deprecated:10.10, obsoleted:10.11, message:"Use init(qos:name:targeting:) instead")
@available(iOS, deprecated:8.0, obsoleted:9.0, message:"Use init(qos:name:targeting:) instead.")
public convenience init(queue: DispatchQueue, name: String = "org.reactivecocoa.ReactiveSwift.QueueScheduler") {
self.init(internalQueue: DispatchQueue(label: name, target: queue))
}
/// Initializes a scheduler that creates a new serial queue with the
/// given quality of service class.
///
/// - parameters:
/// - qos: Dispatch queue's QoS value.
/// - name: A name for the queue in the form of reverse domain.
/// - targeting: (Optional) The queue on which this scheduler's work is
/// targeted
@available(OSX 10.10, *)
public convenience init(
qos: DispatchQoS = .default,
name: String = "org.reactivecocoa.ReactiveSwift.QueueScheduler",
targeting targetQueue: DispatchQueue? = nil
) {
self.init(internalQueue: DispatchQueue(
label: name,
qos: qos,
target: targetQueue
))
}
/// Schedules action for dispatch on internal queue
///
/// - parameters:
/// - action: A closure of the action to be scheduled.
///
/// - returns: `Disposable` that can be used to cancel the work before it
/// begins.
@discardableResult
public func schedule(_ action: @escaping () -> Void) -> Disposable? {
let d = AnyDisposable()
queue.async {
if !d.isDisposed {
action()
}
}
return d
}
private func wallTime(with date: Date) -> DispatchWallTime {
let (seconds, frac) = modf(date.timeIntervalSince1970)
let nsec: Double = frac * Double(NSEC_PER_SEC)
let walltime = timespec(tv_sec: Int(seconds), tv_nsec: Int(nsec))
return DispatchWallTime(timespec: walltime)
}
/// Schedules an action for execution at or after the given date.
///
/// - parameters:
/// - date: The start date.
/// - action: A closure of the action to be performed.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
@discardableResult
public func schedule(after date: Date, action: @escaping () -> Void) -> Disposable? {
let d = AnyDisposable()
queue.asyncAfter(wallDeadline: wallTime(with: date)) {
if !d.isDisposed {
action()
}
}
return d
}
/// Schedules a recurring action at the given interval and beginning at the
/// given start date. A reasonable default timer interval leeway is
/// provided.
///
/// - parameters:
/// - date: A date to schedule the first action for.
/// - interval: A repetition interval.
/// - action: Closure of the action to repeat.
///
/// - note: If you plan to specify an `interval` value greater than 200,000
/// seconds, use `schedule(after:interval:leeway:action)` instead
/// and specify your own `leeway` value to avoid potential overflow.
///
/// - returns: Optional disposable that can be used to cancel the work
/// before it begins.
@discardableResult
public func schedule(after date: Date, interval: DispatchTimeInterval, action: @escaping () -> Void) -> Disposable? {
// Apple's "Power Efficiency Guide for Mac Apps" recommends a leeway of
// at least 10% of the timer interval.
return schedule(after: date, interval: interval, leeway: interval * 0.1, action: action)
}
/// Schedules a recurring action at the given interval with provided leeway,
/// beginning at the given start time.
///
/// - precondition: `interval` must be non-negative number.
/// - precondition: `leeway` must be non-negative number.
///
/// - parameters:
/// - date: A date to schedule the first action for.
/// - interval: A repetition interval.
/// - leeway: Some delta for repetition interval.
/// - action: A closure of the action to repeat.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
@discardableResult
public func schedule(after date: Date, interval: DispatchTimeInterval, leeway: DispatchTimeInterval, action: @escaping () -> Void) -> Disposable? {
precondition(interval.timeInterval >= 0)
precondition(leeway.timeInterval >= 0)
let timer = DispatchSource.makeTimerSource(
flags: DispatchSource.TimerFlags(rawValue: UInt(0)),
queue: queue
)
#if swift(>=4.0)
timer.schedule(wallDeadline: wallTime(with: date),
repeating: interval,
leeway: leeway)
#else
timer.scheduleRepeating(wallDeadline: wallTime(with: date),
interval: interval,
leeway: leeway)
#endif
timer.setEventHandler(handler: action)
timer.resume()
let wrappedTimer = DispatchSourceTimerWrapper(timer)
timers.modify { timers in
timers.insert(wrappedTimer)
}
return AnyDisposable { [weak self] in
timer.cancel()
if let scheduler = self {
scheduler.timers.modify { timers in
timers.remove(wrappedTimer)
}
}
}
}
}
/// A scheduler that implements virtualized time, for use in testing.
public final class TestScheduler: DateScheduler {
private final class ScheduledAction {
let date: Date
let action: () -> Void
init(date: Date, action: @escaping () -> Void) {
self.date = date
self.action = action
}
func less(_ rhs: ScheduledAction) -> Bool {
return date < rhs.date
}
}
private let lock = NSRecursiveLock()
private var _currentDate: Date
/// The virtual date that the scheduler is currently at.
public var currentDate: Date {
let d: Date
lock.lock()
d = _currentDate
lock.unlock()
return d
}
private var scheduledActions: [ScheduledAction] = []
/// Initializes a TestScheduler with the given start date.
///
/// - parameters:
/// - startDate: The start date of the scheduler.
public init(startDate: Date = Date(timeIntervalSinceReferenceDate: 0)) {
lock.name = "org.reactivecocoa.ReactiveSwift.TestScheduler"
_currentDate = startDate
}
private func schedule(_ action: ScheduledAction) -> Disposable {
lock.lock()
scheduledActions.append(action)
scheduledActions.sort { $0.less($1) }
lock.unlock()
return AnyDisposable {
self.lock.lock()
self.scheduledActions = self.scheduledActions.filter { $0 !== action }
self.lock.unlock()
}
}
/// Enqueues an action on the scheduler.
///
/// - note: The work is executed on `currentDate` as it is understood by the
/// scheduler.
///
/// - parameters:
/// - action: An action that will be performed on scheduler's
/// `currentDate`.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
@discardableResult
public func schedule(_ action: @escaping () -> Void) -> Disposable? {
return schedule(ScheduledAction(date: currentDate, action: action))
}
/// Schedules an action for execution after some delay.
///
/// - parameters:
/// - delay: A delay for execution.
/// - action: A closure of the action to perform.
///
/// - returns: Optional disposable that can be used to cancel the work
/// before it begins.
@discardableResult
public func schedule(after delay: DispatchTimeInterval, action: @escaping () -> Void) -> Disposable? {
return schedule(after: currentDate.addingTimeInterval(delay), action: action)
}
/// Schedules an action for execution at or after the given date.
///
/// - parameters:
/// - date: A starting date.
/// - action: A closure of the action to perform.
///
/// - returns: Optional disposable that can be used to cancel the work
/// before it begins.
@discardableResult
public func schedule(after date: Date, action: @escaping () -> Void) -> Disposable? {
return schedule(ScheduledAction(date: date, action: action))
}
/// Schedules a recurring action at the given interval, beginning at the
/// given start date.
///
/// - precondition: `interval` must be non-negative.
///
/// - parameters:
/// - date: A date to schedule the first action for.
/// - interval: A repetition interval.
/// - disposable: A disposable.
/// - action: A closure of the action to repeat.
///
/// - note: If you plan to specify an `interval` value greater than 200,000
/// seconds, use `schedule(after:interval:leeway:action)` instead
/// and specify your own `leeway` value to avoid potential overflow.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
private func schedule(after date: Date, interval: DispatchTimeInterval, disposable: SerialDisposable, action: @escaping () -> Void) {
precondition(interval.timeInterval >= 0)
disposable.inner = schedule(after: date) { [unowned self] in
action()
self.schedule(after: date.addingTimeInterval(interval), interval: interval, disposable: disposable, action: action)
}
}
/// Schedules a recurring action after given delay repeated at the given,
/// interval, beginning at the given interval counted from `currentDate`.
///
/// - parameters:
/// - delay: A delay for action's dispatch.
/// - interval: A repetition interval.
/// - leeway: Some delta for repetition interval.
/// - action: A closure of the action to repeat.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
@discardableResult
public func schedule(after delay: DispatchTimeInterval, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .seconds(0), action: @escaping () -> Void) -> Disposable? {
return schedule(after: currentDate.addingTimeInterval(delay), interval: interval, leeway: leeway, action: action)
}
/// Schedules a recurring action at the given interval with
/// provided leeway, beginning at the given start date.
///
/// - parameters:
/// - date: A date to schedule the first action for.
/// - interval: A repetition interval.
/// - leeway: Some delta for repetition interval.
/// - action: A closure of the action to repeat.
///
/// - returns: Optional `Disposable` that can be used to cancel the work
/// before it begins.
public func schedule(after date: Date, interval: DispatchTimeInterval, leeway: DispatchTimeInterval = .seconds(0), action: @escaping () -> Void) -> Disposable? {
let disposable = SerialDisposable()
schedule(after: date, interval: interval, disposable: disposable, action: action)
return disposable
}
/// Advances the virtualized clock by an extremely tiny interval, dequeuing
/// and executing any actions along the way.
///
/// This is intended to be used as a way to execute actions that have been
/// scheduled to run as soon as possible.
public func advance() {
advance(by: .nanoseconds(1))
}
/// Advances the virtualized clock by the given interval, dequeuing and
/// executing any actions along the way.
///
/// - parameters:
/// - interval: Interval by which the current date will be advanced.
public func advance(by interval: DispatchTimeInterval) {
lock.lock()
advance(to: currentDate.addingTimeInterval(interval))
lock.unlock()
}
/// Advances the virtualized clock to the given future date, dequeuing and
/// executing any actions up until that point.
///
/// - parameters:
/// - newDate: Future date to which the virtual clock will be advanced.
public func advance(to newDate: Date) {
lock.lock()
assert(currentDate <= newDate)
while scheduledActions.count > 0 {
if newDate < scheduledActions[0].date {
break
}
_currentDate = scheduledActions[0].date
let scheduledAction = scheduledActions.remove(at: 0)
scheduledAction.action()
}
_currentDate = newDate
lock.unlock()
}
/// Dequeues and executes all scheduled actions, leaving the scheduler's
/// date at `Date.distantFuture()`.
public func run() {
advance(to: Date.distantFuture)
}
/// Rewinds the virtualized clock by the given interval.
/// This simulates that user changes device date.
///
/// - parameters:
/// - interval: An interval by which the current date will be retreated.
public func rewind(by interval: DispatchTimeInterval) {
lock.lock()
let newDate = currentDate.addingTimeInterval(-interval)
assert(currentDate >= newDate)
_currentDate = newDate
lock.unlock()
}
}