Files
swift-system/Sources/System/IORing/PollEvents.swift
Franz Busch e69d5c1f3c [IO_URING] Add support for pollAdd operation
## Motivation

To observe events on file descriptors IO_URING supports the `pollAdd` operation. This is useful when you want to observe a file descriptor becoming ready to read or write

## Modifications

This PR adds a new `IORing.Request.PollEvents` option set to model the poll masks. Furthermore, it adds a new `static func pollAdd` to the `IORing.Request`.

## Result

We can now use IO_URING to poll for events on file descriptors.
2025-12-08 12:05:48 +01:00

60 lines
2.2 KiB
Swift

/*
This source file is part of the Swift System open source project
Copyright (c) 2020 Apple Inc. and the Swift System project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
*/
#if compiler(>=6.2) && $Lifetimes
#if os(Linux)
extension IORing.Request {
/// A set of I/O events that can be monitored on a file descriptor.
///
/// `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 defined in the kernel's `poll.h` header.
///
/// Use `PollEvents` with ``IORing/Request/pollAdd(_:pollEvents: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.
///
/// ## Usage
///
/// ```swift
/// // Monitor a socket for incoming data
/// let request = IORing.Request.pollAdd(
/// socketFD,
/// pollEvents: .pollin,
/// isMultiShot: true
/// )
/// ```
public struct PollEvents: OptionSet, Hashable, Codable {
public var rawValue: UInt32
@inlinable
public init(rawValue: UInt32) {
self.rawValue = rawValue
}
/// 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.
@inlinable
public static var pollin: PollEvents { PollEvents(rawValue: 0x0001) }
/// 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.
@inlinable
public static var pollout: PollEvents { PollEvents(rawValue: 0x0004) }
}
}
#endif
#endif