Skip to content

Swift: correct dispatch source construction on Win32 #479

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
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
19 changes: 17 additions & 2 deletions src/swift/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@

import CDispatch
import _SwiftDispatchOverlayShims
#if os(Windows)
import WinSDK
#endif

extension DispatchSourceProtocol {

Expand Down Expand Up @@ -179,7 +182,13 @@ extension DispatchSource {
#endif

public class func makeReadSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceRead {
let source = dispatch_source_create(_swift_dispatch_source_type_READ(), UInt(fileDescriptor), 0, queue?.__wrapped)
#if os(Windows)
let handle: UInt = UInt(_get_osfhandle(fileDescriptor))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strictly speaking you don't need to declare the type here.

if handle == UInt(bitPattern: INVALID_HANDLE_VALUE) { fatalError("unable to get underlying handle from file descriptor") }
#else
let handle: UInt = UInt(fileDescriptor)
#endif
let source = dispatch_source_create(_swift_dispatch_source_type_READ(), handle, 0, queue?.__wrapped)
return DispatchSource(source: source) as DispatchSourceRead
}

Expand Down Expand Up @@ -216,7 +225,13 @@ extension DispatchSource {
#endif

public class func makeWriteSource(fileDescriptor: Int32, queue: DispatchQueue? = nil) -> DispatchSourceWrite {
let source = dispatch_source_create(_swift_dispatch_source_type_WRITE(), UInt(fileDescriptor), 0, queue?.__wrapped)
#if os(Windows)
let handle: UInt = UInt(_get_osfhandle(fileDescriptor))
if handle == UInt(bitPattern: INVALID_HANDLE_VALUE) { fatalError("unable to get underlying handle from file descriptor") }
#else
let handle: UInt = UInt(fileDescriptor)
#endif
let source = dispatch_source_create(_swift_dispatch_source_type_WRITE(), handle, 0, queue?.__wrapped)
return DispatchSource(source: source) as DispatchSourceWrite
}
}
Expand Down