Address review feedback

This commit is contained in:
Franz Busch committed 2026-09-22 10:07:05 +02:00
1 parent 6e3259aec3
commit 241fedcbbd
4 files changed
+82 -50

No files matched your search

+7 -7
View File
@@ -34,7 +34,7 @@ internal enum IORequestCore {
)
case pollAdd(
file: FileDescriptor,
pollEvents: IORing.Request.PollEvents,
events: IORing.Request.PollEvents,
isMultiShot: Bool = true,
context: UInt64 = 0
)
@@ -248,7 +248,7 @@ extension IORing.Request {
/// var ring = try IORing(queueDepth: 32)
/// let pollRequest = IORing.Request.pollAdd(
/// listenSocket,
/// pollEvents: .pollIn,
/// events: .readable,
/// isMultiShot: true,
/// context: 1
/// )
@@ -271,7 +271,7 @@ extension IORing.Request {
///
/// - Parameters:
/// - file: The file descriptor to monitor for I/O events.
/// - pollEvents: The I/O events to monitor on the file descriptor.
/// - events: The I/O events to monitor on the file descriptor.
/// - isMultiShot: If `true`, the poll operation automatically rearms
/// after each event, continuing to monitor the file descriptor. If
/// `false`, the operation completes after the first matching event.
@@ -289,11 +289,11 @@ extension IORing.Request {
/// - ``IORing/Request/cancel(_:matching:)``: Cancelling poll operations.
@inlinable public static func pollAdd(
_ file: FileDescriptor,
pollEvents: PollEvents,
events: PollEvents,
isMultiShot: Bool = false,
context: UInt64 = 0
) -> IORing.Request {
.init(core: .pollAdd(file: file, pollEvents: pollEvents, isMultiShot: isMultiShot, context: context))
.init(core: .pollAdd(file: file, events: events, isMultiShot: isMultiShot, context: context))
}
@inlinable public static func read(
@@ -611,14 +611,14 @@ extension IORing.Request {
case .cancel(let flags):
request.operation = .asyncCancel
request.cancel_flags = flags
case .pollAdd(let file, let pollEvents, let isMultiShot, let context):
case .pollAdd(let file, let events, let isMultiShot, let context):
request.operation = .pollAdd
request.fileDescriptor = file
request.rawValue.user_data = context
if isMultiShot {
request.rawValue.len = Self.SWIFT_IORING_POLL_ADD_MULTI
}
request.pollEvents = pollEvents
request.pollEvents = events
}
return request
+40 -17
View File
@@ -18,7 +18,7 @@ extension IORing.Request {
/// defined in the kernel's `poll.h` header.
///
/// Use `PollEvents` with
/// ``IORing/Request/pollAdd(_:pollEvents:isMultiShot:context:)`` to
/// ``IORing/Request/pollAdd(_:events:isMultiShot:context:)`` to
/// register interest in specific I/O events. The poll operation completes
/// when any of the specified events become active on the file descriptor.
///
@@ -28,12 +28,12 @@ extension IORing.Request {
/// // Monitor a socket for incoming data
/// let request = IORing.Request.pollAdd(
/// socketFD,
/// pollEvents: .pollIn,
/// events: .readable,
/// isMultiShot: true
/// )
/// ```
public struct PollEvents: OptionSet, Hashable, Codable, CaseIterable {
public var rawValue: UInt32
public struct PollEvents: OptionSet, Hashable, Codable {
public let rawValue: UInt32
@inlinable
public init(rawValue: UInt32) {
@@ -47,14 +47,16 @@ extension IORing.Request {
@usableFromInline
enum Event: UInt32, RawRepresentable, Hashable, CaseIterable {
case pollIn = 0x0001
case pollOut = 0x0004
case pollErr = 0x0008
case pollHup = 0x0010
case pollNval = 0x0020
case readable = 0x0001
case priorityData = 0x0002
case writable = 0x0004
case error = 0x0008
case hangUp = 0x0010
case invalidDescriptor = 0x0020
case peerClosed = 0x2000
}
public static var allCases: [PollEvents] {
public static var allEvents: [PollEvents] {
Event.allCases.map(PollEvents.init(_:))
}
@@ -65,7 +67,16 @@ extension IORing.Request {
/// a new connection is available on a listening socket. Corresponds
/// to the POSIX `POLLIN` event flag.
@inlinable
public static var pollIn: PollEvents { PollEvents(.pollIn) }
public static var readable: PollEvents { PollEvents(.readable) }
/// An event indicating out-of-band data is available for reading.
///
/// For sockets this signals urgent data; it is also used to report
/// exceptional conditions on descriptors that have no other way to
/// signal them, such as a `sysfs` attribute that has changed value.
/// Corresponds to the POSIX `POLLPRI` event flag.
@_alwaysEmitIntoClient
public static var priorityData: PollEvents { PollEvents(.priorityData) }
/// An event indicating the file descriptor is ready for writing.
///
@@ -73,16 +84,17 @@ extension IORing.Request {
/// not block. For sockets, this indicates that send buffer space is
/// available. Corresponds to the POSIX `POLLOUT` event flag.
@inlinable
public static var pollOut: PollEvents { PollEvents(.pollOut) }
public static var writable: PollEvents { PollEvents(.writable) }
/// An event indicating an error condition on the file descriptor.
///
/// The kernel reports this event whether or not it was requested, so
/// it can appear in a completion's result mask even when the poll
/// asked only for ``pollIn`` or ``pollOut``. Requesting it explicitly
/// has no effect. Corresponds to the POSIX `POLLERR` event flag.
/// asked only for ``readable`` or ``writable``. Requesting it
/// explicitly has no effect. Corresponds to the POSIX `POLLERR` event
/// flag.
@_alwaysEmitIntoClient
public static var pollErr: PollEvents { PollEvents(.pollErr) }
public static var error: PollEvents { PollEvents(.error) }
/// An event indicating the peer closed its end of the channel.
///
@@ -95,7 +107,7 @@ extension IORing.Request {
/// requesting it explicitly has no effect. Corresponds to the POSIX
/// `POLLHUP` event flag.
@_alwaysEmitIntoClient
public static var pollHup: PollEvents { PollEvents(.pollHup) }
public static var hangUp: PollEvents { PollEvents(.hangUp) }
/// An event indicating that the object a descriptor refers to is no
/// longer valid.
@@ -111,7 +123,18 @@ extension IORing.Request {
/// requesting it explicitly has no effect. Corresponds to the POSIX
/// `POLLNVAL` event flag.
@_alwaysEmitIntoClient
public static var pollNval: PollEvents { PollEvents(.pollNval) }
public static var invalidDescriptor: PollEvents {
PollEvents(.invalidDescriptor)
}
/// An event indicating the peer closed its writing end of a stream
/// socket, or shut it down for writing.
///
/// Unlike ``hangUp``, this event leaves the connection half-open: data
/// already in flight can still be read, and the local end can still
/// write. Corresponds to the Linux `POLLRDHUP` event flag.
@_alwaysEmitIntoClient
public static var peerClosed: PollEvents { PollEvents(.peerClosed) }
}
}
#endif
+3 -3
View File
@@ -89,15 +89,15 @@ extension RawIORequest {
/// Big-endian kernels swap the halfwords of this field before reading it.
/// Equivalent to liburing's `__io_uring_prep_poll_mask`.
@_alwaysEmitIntoClient var pollEvents: IORing.Request.PollEvents {
get { .init(rawValue: _applyPollMask(rawValue.poll32_events)) }
set { rawValue.poll32_events = _applyPollMask(newValue.rawValue) }
get { .init(rawValue: Self._applyPollMask(rawValue.poll32_events)) }
set { rawValue.poll32_events = Self._applyPollMask(newValue.rawValue) }
}
/// Converts a poll event mask between its in-memory and `sqe` encodings.
///
/// This is a halfword rotate (`swahw32` from `<linux/swab.h>`), which keeps
/// the byte order within each half.
@_alwaysEmitIntoClient func _applyPollMask(_ mask: UInt32) -> UInt32 {
@_alwaysEmitIntoClient static func _applyPollMask(_ mask: UInt32) -> UInt32 {
#if _endian(big)
return (mask &<< 16) | (mask &>> 16)
#else
+32 -23
View File
@@ -332,7 +332,7 @@ final class IORingTests: XCTestCase {
// Submit a pollAdd request to monitor for POLLIN events (data available for reading)
let enqueued = try ring.submit(linkedRequests:
.pollAdd(testEventFD, pollEvents: .pollIn, isMultiShot: false, context: pollInContext))
.pollAdd(testEventFD, events: .readable, isMultiShot: false, context: pollInContext))
XCTAssert(enqueued)
// Write to the eventfd to trigger the POLLIN event
@@ -346,9 +346,9 @@ final class IORingTests: XCTestCase {
timeout: .seconds(1)
)
XCTAssertEqual(completion.context, pollInContext)
let pollIn = Int32(IORing.Request.PollEvents.pollIn.rawValue)
let readable = Int32(IORing.Request.PollEvents.readable.rawValue)
XCTAssertNotEqual(
completion.result & pollIn, 0, "expected POLLIN in the result mask"
completion.result & readable, 0, "expected POLLIN in the result mask"
)
}
@@ -377,7 +377,7 @@ final class IORingTests: XCTestCase {
// Submit a pollAdd request to monitor for POLLOUT events (ready for writing)
// Pipes are typically ready for writing when empty
let enqueuedOut = try ring.submit(linkedRequests:
.pollAdd(writeFD, pollEvents: .pollOut, isMultiShot: false, context: pollOutContext))
.pollAdd(writeFD, events: .writable, isMultiShot: false, context: pollOutContext))
XCTAssert(enqueuedOut)
// Consume the completion from the poll operation
@@ -385,15 +385,15 @@ final class IORingTests: XCTestCase {
timeout: .seconds(1)
)
XCTAssertEqual(completionOut.context, pollOutContext)
let pollOut = Int32(IORing.Request.PollEvents.pollOut.rawValue)
let writable = Int32(IORing.Request.PollEvents.writable.rawValue)
XCTAssertNotEqual(
completionOut.result & pollOut, 0,
completionOut.result & writable, 0,
"expected POLLOUT in the result mask"
)
}
// Similar to the multishot example in the documentation for
// `pollAdd(_:pollEvents:isMultiShot:context:)`: arm a multishot poll,
// `pollAdd(_:events:isMultiShot:context:)`: arm a multishot poll,
// then consume completions for as long as they carry `.moreCompletions`.
func testPollAddMultiShotRearmsAcrossEvents() throws {
try XCTSkipIf(!uringEnabled, failureMessage)
@@ -429,10 +429,10 @@ final class IORingTests: XCTestCase {
}
let context: UInt64 = 44
let pollIn = Int32(IORing.Request.PollEvents.pollIn.rawValue)
let readable = Int32(IORing.Request.PollEvents.readable.rawValue)
let pollRequest = IORing.Request.pollAdd(
readFD, pollEvents: .pollIn, isMultiShot: true, context: context
readFD, events: .readable, isMultiShot: true, context: context
)
let enqueued = try ring.submit(linkedRequests: pollRequest)
XCTAssert(enqueued)
@@ -446,7 +446,7 @@ final class IORingTests: XCTestCase {
)
XCTAssertEqual(first.context, context)
XCTAssertNotEqual(
first.result & pollIn, 0, "expected POLLIN in the result mask"
first.result & readable, 0, "expected POLLIN in the result mask"
)
XCTAssert(first.flags.contains(.moreCompletions))
@@ -459,7 +459,7 @@ final class IORingTests: XCTestCase {
let second = try ring.blockingConsumeCompletion(timeout: .seconds(1))
XCTAssertEqual(second.context, context)
XCTAssertNotEqual(
second.result & pollIn, 0, "expected POLLIN in the result mask"
second.result & readable, 0, "expected POLLIN in the result mask"
)
// Drain again
@@ -508,9 +508,13 @@ final class IORingTests: XCTestCase {
try XCTSkipIf(!uringEnabled, failureMessage)
var ring = try IORing(queueDepth: 8)
let (readFD, writeFD) = try FileDescriptor.pipe()
// The hangup is produced by closing the write end mid-test, so the
// cleanup below must not close it a second time: the descriptor number
// may already have been reused elsewhere in the test process.
var writeEndClosed = false
defer {
try? readFD.close()
try? writeFD.close()
if !writeEndClosed { try? writeFD.close() }
}
// This test case requires timeout support
@@ -520,18 +524,19 @@ final class IORingTests: XCTestCase {
)
let request = IORing.Request.pollAdd(
readFD, pollEvents: .pollIn, isMultiShot: false, context: 97
readFD, events: .readable, isMultiShot: false, context: 97
)
let success = try ring.submit(linkedRequests: request)
XCTAssertEqual(success, true)
try writeFD.close()
writeEndClosed = true
let dt = Duration.seconds(1)
let completion = try ring.blockingConsumeCompletion(timeout: dt)
for event in IORing.Request.PollEvents.allCases {
for event in IORing.Request.PollEvents.allEvents {
if completion.result & Int32(event.rawValue) != 0 {
XCTAssertEqual(event, .pollHup)
XCTAssertEqual(event, .hangUp)
return
}
}
@@ -544,8 +549,11 @@ final class IORingTests: XCTestCase {
try XCTSkipIf(!uringEnabled, failureMessage)
var ring = try IORing(queueDepth: 8)
let (readFD, writeFD) = try FileDescriptor.pipe(options: .nonBlocking)
// POLLERR is produced by closing the read end mid-test; see the note in
// `testPollHangup` for why cleanup must not close it twice.
var readEndClosed = false
defer {
try? readFD.close()
if !readEndClosed { try? readFD.close() }
try? writeFD.close()
}
@@ -562,19 +570,20 @@ final class IORingTests: XCTestCase {
}
let request = IORing.Request.pollAdd(
writeFD, pollEvents: .pollOut, isMultiShot: false, context: 98
writeFD, events: .writable, isMultiShot: false, context: 98
)
let success = try ring.submit(linkedRequests: request)
XCTAssertEqual(success, true)
try readFD.close()
readEndClosed = true
let dt = Duration.seconds(1)
let completion = try ring.blockingConsumeCompletion(timeout: dt)
XCTAssertEqual(completion.context, 98)
let pollErr = IORing.Request.PollEvents.pollErr.rawValue
let result = completion.result & Int32(pollErr)
if result != pollErr {
let pollError = IORing.Request.PollEvents.error.rawValue
let result = completion.result & Int32(pollError)
if result != pollError {
XCTFail("expected POLLERR, got 0x\(String(result, radix: 16))")
}
}
@@ -588,7 +597,7 @@ final class IORingTests: XCTestCase {
var ring = try IORing(queueDepth: 8)
let request = IORing.Request.pollAdd(
FileDescriptor(rawValue: -1), pollEvents: .pollIn,
FileDescriptor(rawValue: -1), events: .readable,
isMultiShot: false, context: 99
)
let success = try ring.submit(linkedRequests: request)
@@ -607,8 +616,8 @@ final class IORingTests: XCTestCase {
// A negative result may look like another result code.
// Checking for the error must happen first.
let pollNval = Int32(IORing.Request.PollEvents.pollNval.rawValue)
XCTAssertNotEqual(completion.result & pollNval, 0)
let invalidDescriptor = Int32(IORing.Request.PollEvents.invalidDescriptor.rawValue)
XCTAssertNotEqual(completion.result & invalidDescriptor, 0)
}
}
#endif // os(Linux)