Skip to content

Commit

Permalink
give common blocking functions a clear name
Browse files Browse the repository at this point in the history
  • Loading branch information
weissi committed Nov 21, 2024
1 parent 64eb8bf commit 108fbdb
Show file tree
Hide file tree
Showing 14 changed files with 322 additions and 141 deletions.
7 changes: 4 additions & 3 deletions Sources/NIOCore/EventLoopFuture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2020 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2017-2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand Down Expand Up @@ -1048,11 +1048,12 @@ extension EventLoopFuture {
@preconcurrency
@inlinable
public func wait(file: StaticString = #file, line: UInt = #line) throws -> Value where Value: Sendable {
try self._wait(file: file, line: line)
try self._blockingWaitForFutureCompletion(file: file, line: line)
}

@inlinable
func _wait(file: StaticString, line: UInt) throws -> Value where Value: Sendable {
@inline(never)
func _blockingWaitForFutureCompletion(file: StaticString, line: UInt) throws -> Value where Value: Sendable {
self.eventLoop._preconditionSafeToWait(file: file, line: line)

let v: UnsafeMutableTransferBox<Result<Value, Error>?> = .init(nil)
Expand Down
15 changes: 12 additions & 3 deletions Sources/NIOCore/SystemCallHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2017-2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand Down Expand Up @@ -60,7 +60,8 @@ private let sysGetifaddrs: @convention(c) (UnsafeMutablePointer<UnsafeMutablePoi
#endif
#endif

private func isUnacceptableErrno(_ code: Int32) -> Bool {
@inlinable
internal func isUnacceptableErrno(_ code: Int32) -> Bool {
switch code {
case EFAULT, EBADF:
return true
Expand All @@ -69,7 +70,8 @@ private func isUnacceptableErrno(_ code: Int32) -> Bool {
}
}

private func preconditionIsNotUnacceptableErrno(err: CInt, where function: String) {
@inlinable
internal func preconditionIsNotUnacceptableErrno(err: CInt, where function: String) {
// strerror is documented to return "Unknown error: ..." for illegal value so it won't ever fail
precondition(
!isUnacceptableErrno(err),
Expand Down Expand Up @@ -126,6 +128,7 @@ enum SystemCalls {
#endif

@inline(never)
@usableFromInline
internal static func close(descriptor: CInt) throws {
let res = sysClose(descriptor)
if res == -1 {
Expand All @@ -150,6 +153,7 @@ enum SystemCalls {
}

@inline(never)
@usableFromInline
internal static func open(
file: UnsafePointer<CChar>,
oFlag: CInt,
Expand All @@ -170,6 +174,7 @@ enum SystemCalls {

@discardableResult
@inline(never)
@usableFromInline
internal static func lseek(descriptor: CInt, offset: off_t, whence: CInt) throws -> off_t {
try syscall(blocking: false) {
sysLseek(descriptor, offset, whence)
Expand All @@ -178,6 +183,7 @@ enum SystemCalls {

#if os(Windows)
@inline(never)
@usableFromInline
internal static func read(
descriptor: CInt,
pointer: UnsafeMutableRawPointer,
Expand All @@ -189,6 +195,7 @@ enum SystemCalls {
}
#elseif !os(WASI)
@inline(never)
@usableFromInline
internal static func read(
descriptor: CInt,
pointer: UnsafeMutableRawPointer,
Expand All @@ -202,6 +209,7 @@ enum SystemCalls {

#if !os(WASI)
@inline(never)
@usableFromInline
internal static func if_nametoindex(_ name: UnsafePointer<CChar>?) throws -> CUnsignedInt {
try syscall(blocking: false) {
sysIfNameToIndex(name!)
Expand All @@ -210,6 +218,7 @@ enum SystemCalls {

#if !os(Windows)
@inline(never)
@usableFromInline
internal static func getifaddrs(_ addrs: UnsafeMutablePointer<UnsafeMutablePointer<ifaddrs>?>) throws {
_ = try syscall(blocking: false) {
sysGetifaddrs(addrs)
Expand Down
4 changes: 3 additions & 1 deletion Sources/NIOPosix/BSDSocketAPICommon.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2020-2022 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2020-2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand Down Expand Up @@ -31,6 +31,7 @@ protocol _SocketShutdownProtocol {
var cValue: CInt { get }
}

@usableFromInline
internal enum Shutdown: _SocketShutdownProtocol {
case RD
case WR
Expand All @@ -47,6 +48,7 @@ extension NIOBSDSocket {

extension NIOBSDSocket {
/// Specifies the type of socket.
@usableFromInline
internal struct SocketType: RawRepresentable {
public typealias RawValue = CInt
public var rawValue: RawValue
Expand Down
3 changes: 2 additions & 1 deletion Sources/NIOPosix/BaseSocket.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2017-2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -22,6 +22,7 @@ import let WinSDK.EBADF
import struct WinSDK.socklen_t
#endif

@usableFromInline
protocol Registration {
/// The `SelectorEventSet` in which the `Registration` is interested.
var interested: SelectorEventSet { get set }
Expand Down
3 changes: 2 additions & 1 deletion Sources/NIOPosix/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2017-2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -24,6 +24,7 @@ extension IOResult where T: FixedWidthInteger {
}

/// An result for an IO operation that was done on a non-blocking resource.
@usableFromInline
enum IOResult<T: Equatable>: Equatable {

/// Signals that the IO operation could not be completed as otherwise we would need to block.
Expand Down
36 changes: 35 additions & 1 deletion Sources/NIOPosix/Linux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2017-2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -18,11 +18,13 @@
#if os(Linux) || os(Android)
import CNIOLinux

@usableFromInline
internal enum TimerFd {
internal static let TFD_CLOEXEC = CNIOLinux.TFD_CLOEXEC
internal static let TFD_NONBLOCK = CNIOLinux.TFD_NONBLOCK

@inline(never)
@usableFromInline
internal static func timerfd_settime(
fd: CInt,
flags: CInt,
Expand All @@ -35,16 +37,21 @@ internal enum TimerFd {
}

@inline(never)
@usableFromInline
internal static func timerfd_create(clockId: CInt, flags: CInt) throws -> CInt {
try syscall(blocking: false) {
CNIOLinux.timerfd_create(clockId, flags)
}.result
}
}

@usableFromInline
internal enum EventFd {
@usableFromInline
internal static let EFD_CLOEXEC = CNIOLinux.EFD_CLOEXEC
@usableFromInline
internal static let EFD_NONBLOCK = CNIOLinux.EFD_NONBLOCK
@usableFromInline
internal typealias eventfd_t = CNIOLinux.eventfd_t

@inline(never)
Expand All @@ -55,6 +62,7 @@ internal enum EventFd {
}

@inline(never)
@usableFromInline
internal static func eventfd_read(fd: CInt, value: UnsafeMutablePointer<UInt64>) throws -> CInt {
try syscall(blocking: false) {
CNIOLinux.eventfd_read(fd, value)
Expand All @@ -73,40 +81,65 @@ internal enum EventFd {
}
}

@usableFromInline
internal enum Epoll {
@usableFromInline
internal typealias epoll_event = CNIOLinux.epoll_event

@usableFromInline
internal static let EPOLL_CTL_ADD: CInt = numericCast(CNIOLinux.EPOLL_CTL_ADD)
@usableFromInline
internal static let EPOLL_CTL_MOD: CInt = numericCast(CNIOLinux.EPOLL_CTL_MOD)
@usableFromInline
internal static let EPOLL_CTL_DEL: CInt = numericCast(CNIOLinux.EPOLL_CTL_DEL)

#if canImport(Android) || canImport(Musl)
@usableFromInline
internal static let EPOLLIN: CUnsignedInt = numericCast(CNIOLinux.EPOLLIN)
@usableFromInline
internal static let EPOLLOUT: CUnsignedInt = numericCast(CNIOLinux.EPOLLOUT)
@usableFromInline
internal static let EPOLLERR: CUnsignedInt = numericCast(CNIOLinux.EPOLLERR)
@usableFromInline
internal static let EPOLLRDHUP: CUnsignedInt = numericCast(CNIOLinux.EPOLLRDHUP)
@usableFromInline
internal static let EPOLLHUP: CUnsignedInt = numericCast(CNIOLinux.EPOLLHUP)
#if canImport(Android)
@usableFromInline
internal static let EPOLLET: CUnsignedInt = 2_147_483_648 // C macro not imported by ClangImporter
#else
@usableFromInline
internal static let EPOLLET: CUnsignedInt = numericCast(CNIOLinux.EPOLLET)
#endif
#elseif os(Android)
@usableFromInline
internal static let EPOLLIN: CUnsignedInt = 1 //numericCast(CNIOLinux.EPOLLIN)
@usableFromInline
internal static let EPOLLOUT: CUnsignedInt = 4 //numericCast(CNIOLinux.EPOLLOUT)
@usableFromInline
internal static let EPOLLERR: CUnsignedInt = 8 // numericCast(CNIOLinux.EPOLLERR)
@usableFromInline
internal static let EPOLLRDHUP: CUnsignedInt = 8192 //numericCast(CNIOLinux.EPOLLRDHUP)
@usableFromInline
internal static let EPOLLHUP: CUnsignedInt = 16 //numericCast(CNIOLinux.EPOLLHUP)
@usableFromInline
internal static let EPOLLET: CUnsignedInt = 2_147_483_648 //numericCast(CNIOLinux.EPOLLET)
#else
@usableFromInline
internal static let EPOLLIN: CUnsignedInt = numericCast(CNIOLinux.EPOLLIN.rawValue)
@usableFromInline
internal static let EPOLLOUT: CUnsignedInt = numericCast(CNIOLinux.EPOLLOUT.rawValue)
@usableFromInline
internal static let EPOLLERR: CUnsignedInt = numericCast(CNIOLinux.EPOLLERR.rawValue)
@usableFromInline
internal static let EPOLLRDHUP: CUnsignedInt = numericCast(CNIOLinux.EPOLLRDHUP.rawValue)
@usableFromInline
internal static let EPOLLHUP: CUnsignedInt = numericCast(CNIOLinux.EPOLLHUP.rawValue)
@usableFromInline
internal static let EPOLLET: CUnsignedInt = numericCast(CNIOLinux.EPOLLET.rawValue)
#endif

@usableFromInline
internal static let ENOENT: CUnsignedInt = numericCast(CNIOLinux.ENOENT)

@inline(never)
Expand All @@ -130,6 +163,7 @@ internal enum Epoll {
}

@inline(never)
@usableFromInline
internal static func epoll_wait(
epfd: CInt,
events: UnsafeMutablePointer<epoll_event>,
Expand Down
6 changes: 5 additions & 1 deletion Sources/NIOPosix/MultiThreadedEventLoopGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Copyright (c) 2017-2024 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
Expand All @@ -20,6 +20,7 @@ import NIOCore
import Dispatch
#endif

@usableFromInline
struct NIORegistration: Registration {
enum ChannelType {
case serverSocketChannel(ServerSocketChannel)
Expand All @@ -31,9 +32,11 @@ struct NIORegistration: Registration {
var channel: ChannelType

/// The `SelectorEventSet` in which this `NIORegistration` is interested in.
@usableFromInline
var interested: SelectorEventSet

/// The registration ID for this `NIORegistration` used by the `Selector`.
@usableFromInline
var registrationID: SelectorRegistrationID
}

Expand Down Expand Up @@ -568,6 +571,7 @@ extension ScheduledTask: Comparable {
}

extension NIODeadline {
@inlinable
func readyIn(_ target: NIODeadline) -> TimeAmount {
if self < target {
return .nanoseconds(0)
Expand Down
Loading

0 comments on commit 108fbdb

Please sign in to comment.