From 3760dc265ac2ebce5dfa87d030148edc2e7b0d30 Mon Sep 17 00:00:00 2001 From: David Smith Date: Mon, 21 Apr 2025 16:35:08 -0700 Subject: [PATCH] Nest IOResource, and update proposal to match --- NNNN-swift-system-io-uring.md | 70 ++++++++++++++++------------------- Sources/System/IORing.swift | 34 ++++++++--------- 2 files changed, 48 insertions(+), 56 deletions(-) diff --git a/NNNN-swift-system-io-uring.md b/NNNN-swift-system-io-uring.md index 0d8cf8e..2bd981d 100644 --- a/NNNN-swift-system-io-uring.md +++ b/NNNN-swift-system-io-uring.md @@ -33,7 +33,7 @@ We propose a *low level, unopinionated* Swift interface for io_uring on Linux (s * Enqueueing IO requests * Dequeueing IO completions -`struct IOResource` represents, via its two typealiases `IORingFileSlot` and `IORingBuffer`, registered file descriptors and buffers. +`struct RegisteredResource` represents, via its two typealiases `IORing.RegisteredFile` and `IORing.RegisteredBuffer`, registered file descriptors and buffers. `struct IORequest: ~Copyable` represents an IO operation that can be enqueued for the kernel to execute. It supports a wide variety of operations matching traditional unix file and socket operations. @@ -43,7 +43,7 @@ IORequest operations are expressed as overloaded static methods on `IORequest`, public static func open( _ path: FilePath, in directory: FileDescriptor, - into slot: IORingFileSlot, + into slot: IORing.RegisteredFile, mode: FileDescriptor.AccessMode, options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(), permissions: FilePermissions? = nil, @@ -84,14 +84,6 @@ Unfortunately the underlying kernel API makes it relatively difficult to determi ## Detailed design ```swift -public struct IOResource { } -public typealias IORingFileSlot = IOResource -public typealias IORingBuffer = IOResource - -extension IORingBuffer { - public var unsafeBuffer: UnsafeMutableRawBufferPointer -} - // IORing is intentionally not Sendable, to avoid internal locking overhead public struct IORing: ~Copyable { @@ -111,6 +103,10 @@ public struct IORing: ~Copyable { public mutating func registerEventFD(_ descriptor: FileDescriptor) throws(Errno) public mutating func unregisterEventFD(_ descriptor: FileDescriptor) throws(Errno) + + public struct RegisteredResource { } + public typealias RegisteredFile = RegisteredResource + public typealias RegisteredBuffer = RegisteredResource // An IORing.RegisteredResources is a view into the buffers or files registered with the ring, if any public struct RegisteredResources: RandomAccessCollection { @@ -118,23 +114,23 @@ public struct IORing: ~Copyable { public subscript(position: UInt16) -> IOResource // This is useful because io_uring likes to use UInt16s as indexes } - public mutating func registerFileSlots(count: Int) throws(Errno) -> RegisteredResources + public mutating func registerFileSlots(count: Int) throws(Errno) -> RegisteredResources public func unregisterFiles() - public var registeredFileSlots: RegisteredResources + public var registeredFileSlots: RegisteredResources public mutating func registerBuffers( _ buffers: some Collection - ) throws(Errno) -> RegisteredResources + ) throws(Errno) -> RegisteredResources public mutating func registerBuffers( _ buffers: UnsafeMutableRawBufferPointer... - ) throws(Errno) -> RegisteredResources + ) throws(Errno) -> RegisteredResources public func unregisterBuffers() - public var registeredBuffers: RegisteredResources + public var registeredBuffers: RegisteredResources public func prepare(requests: IORequest...) public func prepare(linkedRequests: IORequest...) @@ -189,27 +185,31 @@ public struct IORing: ~Copyable { public var supportedFeatures: Features } +extension IORing.RegisteredBuffer { + public var unsafeBuffer: UnsafeMutableRawBufferPointer +} + public struct IORequest: ~Copyable { public static func nop(context: UInt64 = 0) -> IORequest // overloads for each combination of registered vs unregistered buffer/descriptor // Read public static func read( - _ file: IORingFileSlot, - into buffer: IORingBuffer, + _ file: IORing.RegisteredFile, + into buffer: IORing.RegisteredBuffer, at offset: UInt64 = 0, context: UInt64 = 0 ) -> IORequest public static func read( _ file: FileDescriptor, - into buffer: IORingBuffer, + into buffer: IORing.RegisteredBuffer, at offset: UInt64 = 0, context: UInt64 = 0 ) -> IORequest public static func read( - _ file: IORingFileSlot, + _ file: IORing.RegisteredFile, into buffer: UnsafeMutableRawBufferPointer, at offset: UInt64 = 0, context: UInt64 = 0 @@ -224,14 +224,14 @@ public struct IORequest: ~Copyable { // Write public static func write( - _ buffer: IORingBuffer, - into file: IORingFileSlot, + _ buffer: IORing.RegisteredBuffer, + into file: IORing.RegisteredFile, at offset: UInt64 = 0, context: UInt64 = 0 ) -> IORequest public static func write( - _ buffer: IORingBuffer, + _ buffer: IORing.RegisteredBuffer, into file: FileDescriptor, at offset: UInt64 = 0, context: UInt64 = 0 @@ -239,7 +239,7 @@ public struct IORequest: ~Copyable { public static func write( _ buffer: UnsafeMutableRawBufferPointer, - into file: IORingFileSlot, + into file: IORing.RegisteredFile, at offset: UInt64 = 0, context: UInt64 = 0 ) -> IORequest @@ -258,7 +258,7 @@ public struct IORequest: ~Copyable { ) -> IORequest public static func close( - _ file: IORingFileSlot, + _ file: IORing.RegisteredFile, context: UInt64 = 0 ) -> IORequest @@ -266,7 +266,7 @@ public struct IORequest: ~Copyable { public static func open( _ path: FilePath, in directory: FileDescriptor, - into slot: IORingFileSlot, + into slot: IORing.RegisteredFile, mode: FileDescriptor.AccessMode, options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(), permissions: FilePermissions? = nil, @@ -296,26 +296,18 @@ public struct IORequest: ~Copyable { } public static func cancel( - _ matchAll: CancellationMatch, - matchingContext: UInt64, - context: UInt64 + _ matchAll: CancellationMatch, + matchingContext: UInt64, ) -> IORequest public static func cancel( - _ matchAll: CancellationMatch, - matchingFileDescriptor: FileDescriptor, - context: UInt64 + _ matchAll: CancellationMatch, + matching: FileDescriptor, ) -> IORequest public static func cancel( - _ matchAll: CancellationMatch, - matchingRegisteredFileDescriptorAtIndex: Int, - context: UInt64 - ) -> IORequest - - public static func cancel( - _ matchAll: CancellationMatch, - context: UInt64 + _ matchAll: CancellationMatch, + matching: IORing.RegisteredFile, ) -> IORequest // Other operations follow in the same pattern diff --git a/Sources/System/IORing.swift b/Sources/System/IORing.swift index 54e3b10..7d69aa3 100644 --- a/Sources/System/IORing.swift +++ b/Sources/System/IORing.swift @@ -52,20 +52,6 @@ struct CQRing: ~Copyable { let cqes: UnsafeBufferPointer } -public struct IOResource { - public typealias Resource = T - @usableFromInline let resource: T - @usableFromInline let index: Int - - internal init( - resource: T, - index: Int - ) { - self.resource = resource - self.index = index - } -} - @inline(__always) internal func _writeRequest( _ request: __owned RawIORequest, ring: inout SQRing, @@ -302,9 +288,23 @@ public struct IORing: ~Copyable { var _registeredBuffers: [iovec] var features = Features(rawValue: 0) + + public struct RegisteredResource { + public typealias Resource = T + @usableFromInline let resource: T + @usableFromInline let index: Int - public typealias RegisteredFile = IOResource - public typealias RegisteredBuffer = IOResource + internal init( + resource: T, + index: Int + ) { + self.resource = resource + self.index = index + } + } + + public typealias RegisteredFile = RegisteredResource + public typealias RegisteredBuffer = RegisteredResource @frozen public struct SetupFlags: OptionSet, RawRepresentable, Hashable { @@ -747,4 +747,4 @@ extension IORing.RegisteredBuffer { public var unsafeBuffer: UnsafeMutableRawBufferPointer { return .init(start: resource.iov_base, count: resource.iov_len) } -} \ No newline at end of file +}