7
7
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
8
8
//
9
9
10
+ #if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
10
11
import CoreFoundation
12
+ #else
13
+ @_implementationOnly import CoreFoundation
14
+ #endif
11
15
12
16
internal let kCFRunLoopEntry = CFRunLoopActivity . entry. rawValue
13
17
internal let kCFRunLoopBeforeTimers = CFRunLoopActivity . beforeTimers. rawValue
@@ -83,10 +87,24 @@ open class RunLoop: NSObject {
83
87
}
84
88
}
85
89
90
+ // On platforms where it's available, getCFRunLoop() can be overridden and we use it below.
91
+ // Make sure we honor the override -- var currentCFRunLoop will do so on platforms where overrides are available.
92
+
93
+ #if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
94
+ internal var currentCFRunLoop : CFRunLoop { getCFRunLoop ( ) }
95
+
86
96
@available ( * , deprecated, message: " Directly accessing the run loop may cause your code to not become portable in the future. " )
87
97
open func getCFRunLoop( ) -> CFRunLoop {
88
98
return _cfRunLoop
89
99
}
100
+ #else
101
+ internal var currentCFRunLoop : CFRunLoop { _cfRunLoop }
102
+
103
+ @available ( * , unavailable, message: " Core Foundation is not available on your platform. " )
104
+ open func getCFRunLoop( ) -> Never {
105
+ fatalError ( )
106
+ }
107
+ #endif
90
108
91
109
open func add( _ timer: Timer , forMode mode: RunLoop . Mode ) {
92
110
CFRunLoopAddTimer ( _cfRunLoop, timer. _cfObject, mode. _cfStringUniquingKnown)
@@ -206,7 +224,7 @@ extension RunLoop {
206
224
}
207
225
208
226
public func perform( inModes modes: [ RunLoop . Mode ] , block: @escaping ( ) -> Void ) {
209
- CFRunLoopPerformBlock ( getCFRunLoop ( ) , ( modes. map { $0. _cfStringUniquingKnown } ) . _cfObject, block)
227
+ CFRunLoopPerformBlock ( currentCFRunLoop , ( modes. map { $0. _cfStringUniquingKnown } ) . _cfObject, block)
210
228
}
211
229
212
230
public func perform( _ block: @escaping ( ) -> Void ) {
@@ -219,13 +237,13 @@ extension RunLoop {
219
237
extension RunLoop {
220
238
@available ( * , deprecated, message: " For XCTest use only. " )
221
239
public func _stop( ) {
222
- CFRunLoopStop ( getCFRunLoop ( ) )
240
+ CFRunLoopStop ( currentCFRunLoop )
223
241
}
224
242
225
243
@available ( * , deprecated, message: " For XCTest use only. " )
226
244
public func _observe( _ activities: _Activities , in mode: RunLoop . Mode = . default, repeats: Bool = true , order: Int = 0 , handler: @escaping ( _Activity ) -> Void ) -> _Observer {
227
245
let observer = _Observer ( activities: activities, repeats: repeats, order: order, handler: handler)
228
- CFRunLoopAddObserver ( self . getCFRunLoop ( ) , observer. cfObserver, mode. _cfStringUniquingKnown)
246
+ CFRunLoopAddObserver ( self . currentCFRunLoop , observer. cfObserver, mode. _cfStringUniquingKnown)
229
247
return observer
230
248
}
231
249
0 commit comments