diff --git a/Sources/System/IORing/PollEvents.swift b/Sources/System/IORing/PollEvents.swift index e24f816..d832597 100644 --- a/Sources/System/IORing/PollEvents.swift +++ b/Sources/System/IORing/PollEvents.swift @@ -14,7 +14,7 @@ extension IORing.Request { /// /// `PollEvents` represents the event mask used with io_uring poll /// operations to specify which I/O conditions to monitor on a file - /// descriptor. These events correspond to the standard Posix poll events + /// descriptor. These events correspond to the standard POSIX poll events /// defined in the kernel's `poll.h` header. /// /// Use `PollEvents` with @@ -32,7 +32,7 @@ extension IORing.Request { /// isMultiShot: true /// ) /// ``` - public struct PollEvents: OptionSet, Hashable, Codable { + public struct PollEvents: OptionSet, Hashable, Codable, CaseIterable { public var rawValue: UInt32 @inlinable @@ -40,22 +40,72 @@ extension IORing.Request { self.rawValue = rawValue } + @usableFromInline + init(_ event: Event) { + self.rawValue = event.rawValue + } + + @usableFromInline + enum Event: UInt32, RawRepresentable, Hashable, CaseIterable { + case pollIn = 0x0001 + case pollOut = 0x0004 + case pollErr = 0x0008 + case pollHup = 0x0010 + case pollNval = 0x0020 + } + + public static var allCases: [PollEvents] { + Event.allCases.map(PollEvents.init(_:)) + } + /// An event indicating data is available for reading. /// /// This event becomes active when data arrives on the file descriptor /// and can be read without blocking. For sockets, this includes when /// a new connection is available on a listening socket. Corresponds - /// to the Posix `POLLIN` event flag. + /// to the POSIX `POLLIN` event flag. @inlinable - public static var pollIn: PollEvents { PollEvents(rawValue: 0x0001) } + public static var pollIn: PollEvents { PollEvents(.pollIn) } /// An event indicating the file descriptor is ready for writing. /// /// This event becomes active when writing to the file descriptor will /// not block. For sockets, this indicates that send buffer space is - /// available. Corresponds to the Posix `POLLOUT` event flag. + /// available. Corresponds to the POSIX `POLLOUT` event flag. @inlinable - public static var pollOut: PollEvents { PollEvents(rawValue: 0x0004) } + public static var pollOut: PollEvents { PollEvents(.pollOut) } + + /// 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. + @_alwaysEmitIntoClient + public static var pollErr: PollEvents { PollEvents(.pollErr) } + + /// An event indicating the peer closed its end of the channel. + /// + /// For a pipe this means the writing end was closed; for a socket, that + /// the connection was shut down. A descriptor reporting this event will + /// never become readable again, so treating it as "not ready yet" and + /// polling again will not make progress. + /// + /// The kernel reports this event whether or not it was requested, and + /// requesting it explicitly has no effect. Corresponds to the POSIX + /// `POLLHUP` event flag. + @_alwaysEmitIntoClient + public static var pollHup: PollEvents { PollEvents(.pollHup) } + + /// An event indicating the file descriptor is not open. + /// + /// This usually means the descriptor was closed, or was never valid. + /// + /// The kernel reports this event whether or not it was requested, and + /// requesting it explicitly has no effect. Corresponds to the POSIX + /// `POLLNVAL` event flag. + @_alwaysEmitIntoClient + public static var pollNval: PollEvents { PollEvents(.pollNval) } } } #endif diff --git a/Tests/SystemTests/IORingTests.swift b/Tests/SystemTests/IORingTests.swift index f9cedca..ab974b9 100644 --- a/Tests/SystemTests/IORingTests.swift +++ b/Tests/SystemTests/IORingTests.swift @@ -505,7 +505,7 @@ final class IORingTests: XCTestCase { } } - func testUnexpectedPollValue() throws { + func testPollHangup() throws { try XCTSkipIf(!uringEnabled, failureMessage) var ring = try IORing(queueDepth: 8) let (readFD, writeFD) = try FileDescriptor.pipe() @@ -529,21 +529,15 @@ final class IORingTests: XCTestCase { let dt = Duration.seconds(1) let completion = try ring.blockingConsumeCompletion(timeout: dt) - let values = [ // This should be caseIterable. - IORing.Request.PollEvents.pollIn.rawValue, - IORing.Request.PollEvents.pollOut.rawValue - ] - - for value in values { - if completion.result & Int32(value) != 0 { - // success! + for event in IORing.Request.PollEvents.allCases { + if completion.result & Int32(event.rawValue) != 0 { + XCTAssertEqual(event, .pollHup) return } } - let pollValue = completion.result & 0x10 - XCTAssertEqual(pollValue, 0x10) - XCTFail("Unexpected poll event: 0x\(String(pollValue, radix: 16))") + let unexpected = completion.result + XCTFail("Unexpected poll event: 0x\(String(unexpected, radix: 16))") } } #endif // os(Linux)