Skip to content

Remove all uses of CF from the framework's interface outside of Linux or Darwin. #2878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Foundation/Port.swift
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ open class SocketPort : Port {
}

open override func schedule(in runLoop: RunLoop, forMode mode: RunLoop.Mode) {
let loop = runLoop.getCFRunLoop()
let loop = runLoop.currentCFRunLoop
let loopKey = ObjectIdentifier(loop)

core.lock.synchronized {
Expand All @@ -685,7 +685,7 @@ open class SocketPort : Port {
}

open override func remove(from runLoop: RunLoop, forMode mode: RunLoop.Mode) {
let loop = runLoop.getCFRunLoop()
let loop = runLoop.currentCFRunLoop
let loopKey = ObjectIdentifier(loop)

core.lock.synchronized {
Expand Down
24 changes: 21 additions & 3 deletions Sources/Foundation/RunLoop.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//

#if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
import CoreFoundation
#else
@_implementationOnly import CoreFoundation
#endif

internal let kCFRunLoopEntry = CFRunLoopActivity.entry.rawValue
internal let kCFRunLoopBeforeTimers = CFRunLoopActivity.beforeTimers.rawValue
Expand Down Expand Up @@ -83,10 +87,24 @@ open class RunLoop: NSObject {
}
}

// On platforms where it's available, getCFRunLoop() can be overridden and we use it below.
// Make sure we honor the override -- var currentCFRunLoop will do so on platforms where overrides are available.

#if os(Linux) || os(macOS) || os(iOS) || os(tvOS) || os(watchOS)
internal var currentCFRunLoop: CFRunLoop { getCFRunLoop() }

@available(*, deprecated, message: "Directly accessing the run loop may cause your code to not become portable in the future.")
open func getCFRunLoop() -> CFRunLoop {
return _cfRunLoop
}
#else
internal var currentCFRunLoop: CFRunLoop { _cfRunLoop }

@available(*, unavailable, message: "Core Foundation is not available on your platform.")
open func getCFRunLoop() -> Never {
fatalError()
}
#endif

open func add(_ timer: Timer, forMode mode: RunLoop.Mode) {
CFRunLoopAddTimer(_cfRunLoop, timer._cfObject, mode._cfStringUniquingKnown)
Expand Down Expand Up @@ -206,7 +224,7 @@ extension RunLoop {
}

public func perform(inModes modes: [RunLoop.Mode], block: @escaping () -> Void) {
CFRunLoopPerformBlock(getCFRunLoop(), (modes.map { $0._cfStringUniquingKnown })._cfObject, block)
CFRunLoopPerformBlock(currentCFRunLoop, (modes.map { $0._cfStringUniquingKnown })._cfObject, block)
}

public func perform(_ block: @escaping () -> Void) {
Expand All @@ -219,13 +237,13 @@ extension RunLoop {
extension RunLoop {
@available(*, deprecated, message: "For XCTest use only.")
public func _stop() {
CFRunLoopStop(getCFRunLoop())
CFRunLoopStop(currentCFRunLoop)
}

@available(*, deprecated, message: "For XCTest use only.")
public func _observe(_ activities: _Activities, in mode: RunLoop.Mode = .default, repeats: Bool = true, order: Int = 0, handler: @escaping (_Activity) -> Void) -> _Observer {
let observer = _Observer(activities: activities, repeats: repeats, order: order, handler: handler)
CFRunLoopAddObserver(self.getCFRunLoop(), observer.cfObserver, mode._cfStringUniquingKnown)
CFRunLoopAddObserver(self.currentCFRunLoop, observer.cfObserver, mode._cfStringUniquingKnown)
return observer
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/Foundation/Stream.swift
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,11 @@ open class InputStream: Stream {
}

open override func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
CFReadStreamScheduleWithRunLoop(_stream, aRunLoop.getCFRunLoop(), mode.rawValue._cfObject)
CFReadStreamScheduleWithRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
}

open override func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
CFReadStreamUnscheduleFromRunLoop(_stream, aRunLoop.getCFRunLoop(), mode.rawValue._cfObject)
CFReadStreamUnscheduleFromRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
}
}

Expand Down Expand Up @@ -251,11 +251,11 @@ open class OutputStream : Stream {
}

open override func schedule(in aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
CFWriteStreamScheduleWithRunLoop(_stream, aRunLoop.getCFRunLoop(), mode.rawValue._cfObject)
CFWriteStreamScheduleWithRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
}

open override func remove(from aRunLoop: RunLoop, forMode mode: RunLoop.Mode) {
CFWriteStreamUnscheduleFromRunLoop(_stream, aRunLoop.getCFRunLoop(), mode.rawValue._cfObject)
CFWriteStreamUnscheduleFromRunLoop(_stream, aRunLoop.currentCFRunLoop, mode.rawValue._cfObject)
}
}

Expand Down