mirror of
https://github.com/apple/swift-system
synced 2026-11-29 05:24:25 +00:00
Cleanup, implement buffer unregistering, inline almost everything because we're not ABI stable on Linux
This commit is contained in:
1 parent
27ccce55db
commit
a6149c215e
4 files changed
+144
-124
No files matched your search
@@ -1,9 +1,12 @@
|
||||
#if os(Linux)
|
||||
@_implementationOnly import CSystem
|
||||
import CSystem
|
||||
|
||||
public extension IORing {
|
||||
struct Completion: ~Copyable {
|
||||
let rawValue: io_uring_cqe
|
||||
@inlinable init(rawValue inRawValue: io_uring_cqe) {
|
||||
rawValue = inRawValue
|
||||
}
|
||||
@usableFromInline let rawValue: io_uring_cqe
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +14,7 @@ public extension IORing.Completion {
|
||||
struct Flags: OptionSet, Hashable, Codable {
|
||||
public let rawValue: UInt32
|
||||
|
||||
public init(rawValue: UInt32) {
|
||||
@inlinable public init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
@@ -23,31 +26,31 @@ public extension IORing.Completion {
|
||||
}
|
||||
|
||||
public extension IORing.Completion {
|
||||
var context: UInt64 {
|
||||
@inlinable var context: UInt64 {
|
||||
get {
|
||||
rawValue.user_data
|
||||
}
|
||||
}
|
||||
|
||||
var userPointer: UnsafeRawPointer? {
|
||||
@inlinable var userPointer: UnsafeRawPointer? {
|
||||
get {
|
||||
UnsafeRawPointer(bitPattern: UInt(rawValue.user_data))
|
||||
}
|
||||
}
|
||||
|
||||
var result: Int32 {
|
||||
@inlinable var result: Int32 {
|
||||
get {
|
||||
rawValue.res
|
||||
}
|
||||
}
|
||||
|
||||
var flags: IORing.Completion.Flags {
|
||||
@inlinable var flags: IORing.Completion.Flags {
|
||||
get {
|
||||
Flags(rawValue: rawValue.flags & 0x0000FFFF)
|
||||
}
|
||||
}
|
||||
|
||||
var bufferIndex: UInt16? {
|
||||
@inlinable var bufferIndex: UInt16? {
|
||||
get {
|
||||
if self.flags.contains(.allocatedBuffer) {
|
||||
return UInt16(rawValue.flags >> 16)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#if os(Linux)
|
||||
@_implementationOnly import struct CSystem.io_uring_sqe
|
||||
import CSystem
|
||||
|
||||
@usableFromInline
|
||||
internal enum IORequestCore {
|
||||
@@ -96,7 +96,7 @@ internal enum IORequestCore {
|
||||
)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@inline(__always) @inlinable
|
||||
internal func makeRawRequest_readWrite_registered(
|
||||
file: FileDescriptor,
|
||||
buffer: IORing.RegisteredBuffer,
|
||||
@@ -112,7 +112,7 @@ internal func makeRawRequest_readWrite_registered(
|
||||
return request
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@inline(__always) @inlinable
|
||||
internal func makeRawRequest_readWrite_registered_slot(
|
||||
file: IORing.RegisteredFile,
|
||||
buffer: IORing.RegisteredBuffer,
|
||||
@@ -129,7 +129,7 @@ internal func makeRawRequest_readWrite_registered_slot(
|
||||
return request
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@inline(__always) @inlinable
|
||||
internal func makeRawRequest_readWrite_unregistered(
|
||||
file: FileDescriptor,
|
||||
buffer: UnsafeMutableRawBufferPointer,
|
||||
@@ -144,7 +144,7 @@ internal func makeRawRequest_readWrite_unregistered(
|
||||
return request
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@inline(__always) @inlinable
|
||||
internal func makeRawRequest_readWrite_unregistered_slot(
|
||||
file: IORing.RegisteredFile,
|
||||
buffer: UnsafeMutableRawBufferPointer,
|
||||
@@ -164,6 +164,10 @@ extension IORing {
|
||||
public struct Request {
|
||||
@usableFromInline var core: IORequestCore
|
||||
|
||||
@inlinable internal init(core inCore: IORequestCore) {
|
||||
core = inCore
|
||||
}
|
||||
|
||||
@inlinable internal consuming func extractCore() -> IORequestCore {
|
||||
return core
|
||||
}
|
||||
@@ -173,11 +177,11 @@ extension IORing {
|
||||
|
||||
|
||||
extension IORing.Request {
|
||||
public static func nop(context: UInt64 = 0) -> IORing.Request {
|
||||
@inlinable public static func nop(context: UInt64 = 0) -> IORing.Request {
|
||||
.init(core: .nop)
|
||||
}
|
||||
|
||||
public static func read(
|
||||
@inlinable public static func read(
|
||||
_ file: IORing.RegisteredFile,
|
||||
into buffer: IORing.RegisteredBuffer,
|
||||
at offset: UInt64 = 0,
|
||||
@@ -186,7 +190,7 @@ extension IORing.Request {
|
||||
.init(core: .readSlot(file: file, buffer: buffer, offset: offset, context: context))
|
||||
}
|
||||
|
||||
public static func read(
|
||||
@inlinable public static func read(
|
||||
_ file: FileDescriptor,
|
||||
into buffer: IORing.RegisteredBuffer,
|
||||
at offset: UInt64 = 0,
|
||||
@@ -195,7 +199,7 @@ extension IORing.Request {
|
||||
.init(core: .read(file: file, buffer: buffer, offset: offset, context: context))
|
||||
}
|
||||
|
||||
public static func read(
|
||||
@inlinable public static func read(
|
||||
_ file: IORing.RegisteredFile,
|
||||
into buffer: UnsafeMutableRawBufferPointer,
|
||||
at offset: UInt64 = 0,
|
||||
@@ -204,7 +208,7 @@ extension IORing.Request {
|
||||
.init(core: .readUnregisteredSlot(file: file, buffer: buffer, offset: offset, context: context))
|
||||
}
|
||||
|
||||
public static func read(
|
||||
@inlinable public static func read(
|
||||
_ file: FileDescriptor,
|
||||
into buffer: UnsafeMutableRawBufferPointer,
|
||||
at offset: UInt64 = 0,
|
||||
@@ -213,7 +217,7 @@ extension IORing.Request {
|
||||
.init(core: .readUnregistered(file: file, buffer: buffer, offset: offset, context: context))
|
||||
}
|
||||
|
||||
public static func write(
|
||||
@inlinable public static func write(
|
||||
_ buffer: IORing.RegisteredBuffer,
|
||||
into file: IORing.RegisteredFile,
|
||||
at offset: UInt64 = 0,
|
||||
@@ -222,7 +226,7 @@ extension IORing.Request {
|
||||
.init(core: .writeSlot(file: file, buffer: buffer, offset: offset, context: context))
|
||||
}
|
||||
|
||||
public static func write(
|
||||
@inlinable public static func write(
|
||||
_ buffer: IORing.RegisteredBuffer,
|
||||
into file: FileDescriptor,
|
||||
at offset: UInt64 = 0,
|
||||
@@ -231,7 +235,7 @@ extension IORing.Request {
|
||||
.init(core: .write(file: file, buffer: buffer, offset: offset, context: context))
|
||||
}
|
||||
|
||||
public static func write(
|
||||
@inlinable public static func write(
|
||||
_ buffer: UnsafeMutableRawBufferPointer,
|
||||
into file: IORing.RegisteredFile,
|
||||
at offset: UInt64 = 0,
|
||||
@@ -241,7 +245,7 @@ extension IORing.Request {
|
||||
file: file, buffer: buffer, offset: offset, context: context))
|
||||
}
|
||||
|
||||
public static func write(
|
||||
@inlinable public static func write(
|
||||
_ buffer: UnsafeMutableRawBufferPointer,
|
||||
into file: FileDescriptor,
|
||||
at offset: UInt64 = 0,
|
||||
@@ -252,21 +256,21 @@ extension IORing.Request {
|
||||
)
|
||||
}
|
||||
|
||||
public static func close(
|
||||
@inlinable public static func close(
|
||||
_ file: FileDescriptor,
|
||||
context: UInt64 = 0
|
||||
) -> IORing.Request {
|
||||
.init(core: .close(file, context: context))
|
||||
}
|
||||
|
||||
public static func close(
|
||||
@inlinable public static func close(
|
||||
_ file: IORing.RegisteredFile,
|
||||
context: UInt64 = 0
|
||||
) -> IORing.Request {
|
||||
.init(core: .closeSlot(file, context: context))
|
||||
}
|
||||
|
||||
public static func open(
|
||||
@inlinable public static func open(
|
||||
_ path: FilePath,
|
||||
in directory: FileDescriptor,
|
||||
into slot: IORing.RegisteredFile,
|
||||
@@ -281,7 +285,7 @@ extension IORing.Request {
|
||||
permissions: permissions, intoSlot: slot, context: context))
|
||||
}
|
||||
|
||||
public static func open(
|
||||
@inlinable public static func open(
|
||||
_ path: FilePath,
|
||||
in directory: FileDescriptor,
|
||||
mode: FileDescriptor.AccessMode,
|
||||
@@ -296,7 +300,7 @@ extension IORing.Request {
|
||||
))
|
||||
}
|
||||
|
||||
public static func unlink(
|
||||
@inlinable public static func unlink(
|
||||
_ path: FilePath,
|
||||
in directory: FileDescriptor,
|
||||
context: UInt64 = 0
|
||||
@@ -307,31 +311,22 @@ extension IORing.Request {
|
||||
// Cancel
|
||||
|
||||
/*
|
||||
* ASYNC_CANCEL flags.
|
||||
*
|
||||
* IORING_ASYNC_CANCEL_ALL Cancel all requests that match the given key
|
||||
* IORING_ASYNC_CANCEL_FD Key off 'fd' for cancelation rather than the
|
||||
* request 'user_data'
|
||||
* IORING_ASYNC_CANCEL_ANY Match any request
|
||||
* IORING_ASYNC_CANCEL_FD_FIXED 'fd' passed in is a fixed descriptor
|
||||
* IORING_ASYNC_CANCEL_USERDATA Match on user_data, default for no other key
|
||||
* IORING_ASYNC_CANCEL_OP Match request based on opcode
|
||||
*/
|
||||
//TODO: why aren't these showing up from the header import?
|
||||
private static var IORING_ASYNC_CANCEL_ALL: UInt32 { (1 as UInt32) << 0 }
|
||||
private static var IORING_ASYNC_CANCEL_FD: UInt32 { (1 as UInt32) << 1 }
|
||||
private static var IORING_ASYNC_CANCEL_ANY: UInt32 { (1 as UInt32) << 2 }
|
||||
private static var IORING_ASYNC_CANCEL_FD_FIXED: UInt32 { (1 as UInt32) << 3 }
|
||||
private static var IORING_ASYNC_CANCEL_USERDATA: UInt32 { (1 as UInt32) << 4 }
|
||||
private static var IORING_ASYNC_CANCEL_OP: UInt32 { (1 as UInt32) << 5 }
|
||||
|
||||
|
||||
* ASYNC_CANCEL flags.
|
||||
*
|
||||
* IORING_ASYNC_CANCEL_ALL Cancel all requests that match the given key
|
||||
* IORING_ASYNC_CANCEL_FD Key off 'fd' for cancelation rather than the
|
||||
* request 'user_data'
|
||||
* IORING_ASYNC_CANCEL_ANY Match any request
|
||||
* IORING_ASYNC_CANCEL_FD_FIXED 'fd' passed in is a fixed descriptor
|
||||
* IORING_ASYNC_CANCEL_USERDATA Match on user_data, default for no other key
|
||||
* IORING_ASYNC_CANCEL_OP Match request based on opcode
|
||||
*/
|
||||
public enum CancellationMatch {
|
||||
case all
|
||||
case first
|
||||
}
|
||||
|
||||
public static func cancel(
|
||||
@inlinable public static func cancel(
|
||||
_ matchAll: CancellationMatch,
|
||||
matchingContext: UInt64,
|
||||
) -> IORing.Request {
|
||||
@@ -343,7 +338,7 @@ extension IORing.Request {
|
||||
}
|
||||
}
|
||||
|
||||
public static func cancel(
|
||||
@inlinable public static func cancel(
|
||||
_ matchAll: CancellationMatch,
|
||||
matching: FileDescriptor,
|
||||
) -> IORing.Request {
|
||||
@@ -355,7 +350,7 @@ extension IORing.Request {
|
||||
}
|
||||
}
|
||||
|
||||
public static func cancel(
|
||||
@inlinable public static func cancel(
|
||||
_ matchAll: CancellationMatch,
|
||||
matching: IORing.RegisteredFile,
|
||||
) -> IORing.Request {
|
||||
@@ -369,8 +364,8 @@ extension IORing.Request {
|
||||
|
||||
//TODO: add support for CANCEL_OP
|
||||
|
||||
@inline(__always)
|
||||
public consuming func makeRawRequest() -> RawIORequest {
|
||||
@inline(__always) @inlinable
|
||||
internal consuming func makeRawRequest() -> RawIORequest {
|
||||
var request = RawIORequest()
|
||||
switch extractCore() {
|
||||
case .nop:
|
||||
|
||||
+60
-39
@@ -1,11 +1,8 @@
|
||||
#if os(Linux)
|
||||
@_implementationOnly import CSystem
|
||||
import CSystem
|
||||
import Glibc // needed for mmap
|
||||
import Synchronization
|
||||
|
||||
@_implementationOnly import struct CSystem.io_uring_sqe
|
||||
|
||||
// XXX: this *really* shouldn't be here. oh well.
|
||||
extension UnsafeMutableRawPointer {
|
||||
func advanced(by offset: UInt32) -> UnsafeMutableRawPointer {
|
||||
return advanced(by: Int(offset))
|
||||
@@ -20,40 +17,40 @@ extension UnsafeMutableRawBufferPointer {
|
||||
|
||||
// all pointers in this struct reference kernel-visible memory
|
||||
@usableFromInline struct SQRing: ~Copyable {
|
||||
let kernelHead: UnsafePointer<Atomic<UInt32>>
|
||||
let kernelTail: UnsafePointer<Atomic<UInt32>>
|
||||
var userTail: UInt32
|
||||
@usableFromInline let kernelHead: UnsafePointer<Atomic<UInt32>>
|
||||
@usableFromInline let kernelTail: UnsafePointer<Atomic<UInt32>>
|
||||
@usableFromInline var userTail: UInt32
|
||||
|
||||
// from liburing: the kernel should never change these
|
||||
// might change in the future with resizable rings?
|
||||
let ringMask: UInt32
|
||||
@usableFromInline let ringMask: UInt32
|
||||
// let ringEntries: UInt32 - absorbed into array.count
|
||||
|
||||
// ring flags bitfield
|
||||
// currently used by the kernel only in SQPOLL mode to indicate
|
||||
// when the polling thread needs to be woken up
|
||||
let flags: UnsafePointer<Atomic<UInt32>>
|
||||
@usableFromInline let flags: UnsafePointer<Atomic<UInt32>>
|
||||
|
||||
// ring array
|
||||
// maps indexes between the actual ring and the submissionQueueEntries list,
|
||||
// allowing the latter to be used as a kind of freelist with enough work?
|
||||
// currently, just 1:1 mapping (0..<n)
|
||||
let array: UnsafeMutableBufferPointer<UInt32>
|
||||
@usableFromInline let array: UnsafeMutableBufferPointer<UInt32>
|
||||
}
|
||||
|
||||
struct CQRing: ~Copyable {
|
||||
let kernelHead: UnsafePointer<Atomic<UInt32>>
|
||||
let kernelTail: UnsafePointer<Atomic<UInt32>>
|
||||
@usableFromInline struct CQRing: ~Copyable {
|
||||
@usableFromInline let kernelHead: UnsafePointer<Atomic<UInt32>>
|
||||
@usableFromInline let kernelTail: UnsafePointer<Atomic<UInt32>>
|
||||
|
||||
// TODO: determine if this is actually used
|
||||
var userHead: UInt32
|
||||
|
||||
let ringMask: UInt32
|
||||
@usableFromInline let ringMask: UInt32
|
||||
|
||||
let cqes: UnsafeBufferPointer<io_uring_cqe>
|
||||
@usableFromInline let cqes: UnsafeBufferPointer<io_uring_cqe>
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@inline(__always) @inlinable
|
||||
internal func _writeRequest(
|
||||
_ request: __owned RawIORequest, ring: inout SQRing,
|
||||
submissionQueueEntries: UnsafeMutableBufferPointer<io_uring_sqe>
|
||||
@@ -66,7 +63,7 @@ internal func _writeRequest(
|
||||
return true
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@inline(__always) @inlinable
|
||||
internal func _blockingGetSubmissionEntry(
|
||||
ring: inout SQRing, submissionQueueEntries: UnsafeMutableBufferPointer<io_uring_sqe>
|
||||
) -> UnsafeMutablePointer<
|
||||
@@ -86,6 +83,7 @@ internal func _blockingGetSubmissionEntry(
|
||||
|
||||
//TODO: omitting signal mask for now
|
||||
//Tell the kernel that we've submitted requests and/or are waiting for completions
|
||||
@inlinable
|
||||
internal func _enter(
|
||||
ringDescriptor: Int32,
|
||||
numEvents: UInt32,
|
||||
@@ -113,22 +111,26 @@ internal func _enter(
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal func _submitRequests(ring: borrowing SQRing, ringDescriptor: Int32) throws(Errno) {
|
||||
let flushedEvents = _flushQueue(ring: ring)
|
||||
_ = try _enter(
|
||||
ringDescriptor: ringDescriptor, numEvents: flushedEvents, minCompletions: 0, flags: 0)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal func _getUnconsumedSubmissionCount(ring: borrowing SQRing) -> UInt32 {
|
||||
return ring.userTail - ring.kernelHead.pointee.load(ordering: .acquiring)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal func _getUnconsumedCompletionCount(ring: borrowing CQRing) -> UInt32 {
|
||||
return ring.kernelTail.pointee.load(ordering: .acquiring)
|
||||
- ring.kernelHead.pointee.load(ordering: .acquiring)
|
||||
}
|
||||
|
||||
//TODO: pretty sure this is supposed to do more than it does
|
||||
@inlinable
|
||||
internal func _flushQueue(ring: borrowing SQRing) -> UInt32 {
|
||||
ring.kernelTail.pointee.store(
|
||||
ring.userTail, ordering: .releasing
|
||||
@@ -136,7 +138,7 @@ internal func _flushQueue(ring: borrowing SQRing) -> UInt32 {
|
||||
return _getUnconsumedSubmissionCount(ring: ring)
|
||||
}
|
||||
|
||||
@inline(__always)
|
||||
@inlinable
|
||||
internal func _getSubmissionEntry(
|
||||
ring: inout SQRing, submissionQueueEntries: UnsafeMutableBufferPointer<io_uring_sqe>
|
||||
) -> UnsafeMutablePointer<
|
||||
@@ -188,7 +190,6 @@ private func setUpRing(
|
||||
|| params.features & IORING_FEAT_NODROP == 0
|
||||
{
|
||||
close(ringDescriptor)
|
||||
// TODO: error handling
|
||||
throw Errno.invalidArgument
|
||||
}
|
||||
|
||||
@@ -241,22 +242,22 @@ private func setUpRing(
|
||||
|
||||
public struct IORing: ~Copyable {
|
||||
let ringFlags: UInt32
|
||||
let ringDescriptor: Int32
|
||||
@usableFromInline let ringDescriptor: Int32
|
||||
|
||||
@usableFromInline var submissionRing: SQRing
|
||||
// FEAT: set this eventually
|
||||
let submissionPolling: Bool = false
|
||||
|
||||
let completionRing: CQRing
|
||||
@usableFromInline let completionRing: CQRing
|
||||
|
||||
let submissionQueueEntries: UnsafeMutableBufferPointer<io_uring_sqe>
|
||||
@usableFromInline let submissionQueueEntries: UnsafeMutableBufferPointer<io_uring_sqe>
|
||||
|
||||
// kept around for unmap / cleanup
|
||||
let ringSize: Int
|
||||
let ringPtr: UnsafeMutableRawPointer
|
||||
|
||||
var _registeredFiles: [UInt32]
|
||||
var _registeredBuffers: [iovec]
|
||||
@usableFromInline var _registeredFiles: [UInt32]
|
||||
@usableFromInline var _registeredBuffers: [iovec]
|
||||
|
||||
var features = Features(rawValue: 0)
|
||||
|
||||
@@ -265,7 +266,7 @@ public struct IORing: ~Copyable {
|
||||
@usableFromInline let resource: T
|
||||
@usableFromInline let index: Int
|
||||
|
||||
internal init(
|
||||
@inlinable internal init(
|
||||
resource: T,
|
||||
index: Int
|
||||
) {
|
||||
@@ -277,7 +278,6 @@ public struct IORing: ~Copyable {
|
||||
public typealias RegisteredFile = RegisteredResource<UInt32>
|
||||
public typealias RegisteredBuffer = RegisteredResource<iovec>
|
||||
|
||||
@frozen
|
||||
public struct SetupFlags: OptionSet, RawRepresentable, Hashable {
|
||||
public var rawValue: UInt32
|
||||
|
||||
@@ -370,7 +370,8 @@ public struct IORing: ~Copyable {
|
||||
self.ringFlags = params.flags
|
||||
}
|
||||
|
||||
private func _blockingConsumeCompletionGuts<Err: Error>(
|
||||
@inlinable
|
||||
internal func _blockingConsumeCompletionGuts<Err: Error>(
|
||||
minimumCount: UInt32,
|
||||
maximumCount: UInt32,
|
||||
extraArgs: UnsafeMutablePointer<swift_io_uring_getevents_arg>? = nil,
|
||||
@@ -438,6 +439,7 @@ public struct IORing: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
internal func _blockingConsumeOneCompletion(
|
||||
extraArgs: UnsafeMutablePointer<swift_io_uring_getevents_arg>? = nil
|
||||
) throws(Errno) -> Completion {
|
||||
@@ -454,6 +456,7 @@ public struct IORing: ~Copyable {
|
||||
return result.take()!
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func blockingConsumeCompletion(
|
||||
timeout: Duration? = nil
|
||||
) throws(Errno) -> Completion {
|
||||
@@ -476,6 +479,7 @@ public struct IORing: ~Copyable {
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func blockingConsumeCompletions<Err: Error>(
|
||||
minimumCount: UInt32 = 1,
|
||||
timeout: Duration? = nil,
|
||||
@@ -507,10 +511,12 @@ public struct IORing: ~Copyable {
|
||||
|
||||
// }
|
||||
|
||||
@inlinable
|
||||
public func tryConsumeCompletion() -> Completion? {
|
||||
return _tryConsumeCompletion(ring: completionRing)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
func _tryConsumeCompletion(ring: borrowing CQRing) -> Completion? {
|
||||
let tail = ring.kernelTail.pointee.load(ordering: .acquiring)
|
||||
let head = ring.kernelHead.pointee.load(ordering: .acquiring)
|
||||
@@ -580,6 +586,7 @@ public struct IORing: ~Copyable {
|
||||
fatalError("failed to unregister files")
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var registeredFileSlots: RegisteredResources<RegisteredFile.Resource> {
|
||||
RegisteredResources(resources: _registeredFiles)
|
||||
}
|
||||
@@ -610,6 +617,7 @@ public struct IORing: ~Copyable {
|
||||
return registeredBuffers
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func registerBuffers(_ buffers: UnsafeMutableRawBufferPointer...) throws(Errno)
|
||||
-> RegisteredResources<RegisteredBuffer.Resource>
|
||||
{
|
||||
@@ -617,33 +625,44 @@ public struct IORing: ~Copyable {
|
||||
}
|
||||
|
||||
public struct RegisteredResources<T>: RandomAccessCollection {
|
||||
let resources: [T]
|
||||
@usableFromInline let resources: [T]
|
||||
|
||||
public var startIndex: Int { 0 }
|
||||
public var endIndex: Int { resources.endIndex }
|
||||
init(resources: [T]) {
|
||||
@inlinable public var startIndex: Int { 0 }
|
||||
@inlinable public var endIndex: Int { resources.endIndex }
|
||||
@inlinable init(resources: [T]) {
|
||||
self.resources = resources
|
||||
}
|
||||
public subscript(position: Int) -> RegisteredResource<T> {
|
||||
@inlinable public subscript(position: Int) -> RegisteredResource<T> {
|
||||
RegisteredResource(resource: resources[position], index: position)
|
||||
}
|
||||
public subscript(position: UInt16) -> RegisteredResource<T> {
|
||||
@inlinable public subscript(position: UInt16) -> RegisteredResource<T> {
|
||||
RegisteredResource(resource: resources[Int(position)], index: Int(position))
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public var registeredBuffers: RegisteredResources<RegisteredBuffer.Resource> {
|
||||
RegisteredResources(resources: _registeredBuffers)
|
||||
}
|
||||
|
||||
public func unregisterBuffers() {
|
||||
fatalError("failed to unregister buffers: TODO")
|
||||
public func unregisterBuffers() throws {
|
||||
let result = io_uring_register(
|
||||
self.ringDescriptor,
|
||||
IORING_UNREGISTER_BUFFERS.rawValue,
|
||||
nil,
|
||||
0
|
||||
)
|
||||
guard result >= 0 else {
|
||||
throw Errno(rawValue: -result)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func submitPreparedRequests() throws(Errno) {
|
||||
try _submitRequests(ring: submissionRing, ringDescriptor: ringDescriptor)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public func submitPreparedRequestsAndConsumeCompletions<Err: Error>(
|
||||
minimumCount: UInt32 = 1,
|
||||
timeout: Duration? = nil,
|
||||
@@ -662,12 +681,14 @@ public struct IORing: ~Copyable {
|
||||
)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func prepare(request: __owned Request) -> Bool {
|
||||
var raw: RawIORequest? = request.makeRawRequest()
|
||||
return _writeRequest(
|
||||
raw.take()!, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
mutating func prepare(linkedRequests: some BidirectionalCollection<Request>) {
|
||||
guard linkedRequests.count > 0 else {
|
||||
return
|
||||
@@ -684,17 +705,17 @@ public struct IORing: ~Copyable {
|
||||
submissionQueueEntries: submissionQueueEntries)
|
||||
}
|
||||
|
||||
//@inlinable //TODO: make sure the array allocation gets optimized out...
|
||||
@inlinable
|
||||
public mutating func prepare(linkedRequests: Request...) {
|
||||
prepare(linkedRequests: linkedRequests)
|
||||
}
|
||||
|
||||
@inlinable
|
||||
public mutating func submit(linkedRequests: Request...) throws(Errno) {
|
||||
prepare(linkedRequests: linkedRequests)
|
||||
try submitPreparedRequests()
|
||||
}
|
||||
|
||||
@frozen
|
||||
public struct Features: OptionSet, RawRepresentable, Hashable {
|
||||
public let rawValue: UInt32
|
||||
|
||||
@@ -734,12 +755,12 @@ public struct IORing: ~Copyable {
|
||||
}
|
||||
|
||||
extension IORing.RegisteredFile {
|
||||
public var unsafeFileSlot: Int {
|
||||
@inlinable public var unsafeFileSlot: Int {
|
||||
return index
|
||||
}
|
||||
}
|
||||
extension IORing.RegisteredBuffer {
|
||||
public var unsafeBuffer: UnsafeMutableRawBufferPointer {
|
||||
@inlinable public var unsafeBuffer: UnsafeMutableRawBufferPointer {
|
||||
return .init(start: resource.iov_base, count: resource.iov_len)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
#if os(Linux)
|
||||
// TODO: investigate @usableFromInline / @_implementationOnly dichotomy
|
||||
@_implementationOnly import CSystem
|
||||
@_implementationOnly import struct CSystem.io_uring_sqe
|
||||
import CSystem
|
||||
import struct CSystem.io_uring_sqe
|
||||
|
||||
//TODO: make this internal
|
||||
public struct RawIORequest: ~Copyable {
|
||||
var rawValue: io_uring_sqe
|
||||
var path: FilePath? //buffer owner for the path pointer that the sqe may have
|
||||
@usableFromInline
|
||||
internal struct RawIORequest: ~Copyable {
|
||||
@usableFromInline var rawValue: io_uring_sqe
|
||||
@usableFromInline var path: FilePath? //buffer owner for the path pointer that the sqe may have
|
||||
|
||||
public init() {
|
||||
@inlinable public init() {
|
||||
self.rawValue = io_uring_sqe()
|
||||
}
|
||||
}
|
||||
|
||||
extension RawIORequest {
|
||||
@usableFromInline
|
||||
enum Operation: UInt8 {
|
||||
case nop = 0
|
||||
case readv = 1
|
||||
@@ -45,49 +45,49 @@ extension RawIORequest {
|
||||
public struct Flags: OptionSet, Hashable, Codable {
|
||||
public let rawValue: UInt8
|
||||
|
||||
public init(rawValue: UInt8) {
|
||||
@inlinable public init(rawValue: UInt8) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let fixedFile = Flags(rawValue: 1 << 0)
|
||||
public static let drainQueue = Flags(rawValue: 1 << 1)
|
||||
public static let linkRequest = Flags(rawValue: 1 << 2)
|
||||
public static let hardlinkRequest = Flags(rawValue: 1 << 3)
|
||||
public static let asynchronous = Flags(rawValue: 1 << 4)
|
||||
public static let selectBuffer = Flags(rawValue: 1 << 5)
|
||||
public static let skipSuccess = Flags(rawValue: 1 << 6)
|
||||
@inlinable public static var fixedFile: RawIORequest.Flags { Flags(rawValue: 1 << 0) }
|
||||
@inlinable public static var drainQueue: RawIORequest.Flags { Flags(rawValue: 1 << 1) }
|
||||
@inlinable public static var linkRequest: RawIORequest.Flags { Flags(rawValue: 1 << 2) }
|
||||
@inlinable public static var hardlinkRequest: RawIORequest.Flags { Flags(rawValue: 1 << 3) }
|
||||
@inlinable public static var asynchronous: RawIORequest.Flags { Flags(rawValue: 1 << 4) }
|
||||
@inlinable public static var selectBuffer: RawIORequest.Flags { Flags(rawValue: 1 << 5) }
|
||||
@inlinable public static var skipSuccess: RawIORequest.Flags { Flags(rawValue: 1 << 6) }
|
||||
}
|
||||
|
||||
var operation: Operation {
|
||||
@inlinable var operation: Operation {
|
||||
get { Operation(rawValue: rawValue.opcode)! }
|
||||
set { rawValue.opcode = newValue.rawValue }
|
||||
}
|
||||
|
||||
var cancel_flags: UInt32 {
|
||||
@inlinable var cancel_flags: UInt32 {
|
||||
get { rawValue.cancel_flags }
|
||||
set { rawValue.cancel_flags = newValue }
|
||||
}
|
||||
|
||||
var addr: UInt64 {
|
||||
@inlinable var addr: UInt64 {
|
||||
get { rawValue.addr }
|
||||
set { rawValue.addr = newValue }
|
||||
}
|
||||
|
||||
public var flags: Flags {
|
||||
@inlinable public var flags: Flags {
|
||||
get { Flags(rawValue: rawValue.flags) }
|
||||
set { rawValue.flags = newValue.rawValue }
|
||||
}
|
||||
|
||||
public mutating func linkToNextRequest() {
|
||||
@inlinable public mutating func linkToNextRequest() {
|
||||
flags = Flags(rawValue: flags.rawValue | Flags.linkRequest.rawValue)
|
||||
}
|
||||
|
||||
public var fileDescriptor: FileDescriptor {
|
||||
@inlinable public var fileDescriptor: FileDescriptor {
|
||||
get { FileDescriptor(rawValue: rawValue.fd) }
|
||||
set { rawValue.fd = newValue.rawValue }
|
||||
}
|
||||
|
||||
public var offset: UInt64? {
|
||||
@inlinable public var offset: UInt64? {
|
||||
get {
|
||||
if (rawValue.off == UInt64.max) {
|
||||
return nil
|
||||
@@ -104,7 +104,7 @@ extension RawIORequest {
|
||||
}
|
||||
}
|
||||
|
||||
public var buffer: UnsafeMutableRawBufferPointer {
|
||||
@inlinable public var buffer: UnsafeMutableRawBufferPointer {
|
||||
get {
|
||||
let ptr = UnsafeMutableRawPointer(bitPattern: UInt(exactly: rawValue.addr)!)
|
||||
return UnsafeMutableRawBufferPointer(start: ptr, count: Int(rawValue.len))
|
||||
@@ -135,45 +135,46 @@ extension RawIORequest {
|
||||
|
||||
public struct ReadWriteFlags: OptionSet, Hashable, Codable {
|
||||
public var rawValue: UInt32
|
||||
public init(rawValue: UInt32) {
|
||||
@inlinable public init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let highPriority = ReadWriteFlags(rawValue: 1 << 0)
|
||||
@inlinable public static var highPriority: RawIORequest.ReadWriteFlags { ReadWriteFlags(rawValue: 1 << 0) }
|
||||
|
||||
// sync with only data integrity
|
||||
public static let dataSync = ReadWriteFlags(rawValue: 1 << 1)
|
||||
@inlinable public static var dataSync: RawIORequest.ReadWriteFlags { ReadWriteFlags(rawValue: 1 << 1) }
|
||||
|
||||
// sync with full data + file integrity
|
||||
public static let fileSync = ReadWriteFlags(rawValue: 1 << 2)
|
||||
@inlinable public static var fileSync: RawIORequest.ReadWriteFlags { ReadWriteFlags(rawValue: 1 << 2) }
|
||||
|
||||
// return -EAGAIN if operation blocks
|
||||
public static let noWait = ReadWriteFlags(rawValue: 1 << 3)
|
||||
@inlinable public static var noWait: RawIORequest.ReadWriteFlags { ReadWriteFlags(rawValue: 1 << 3) }
|
||||
|
||||
// append to end of the file
|
||||
public static let append = ReadWriteFlags(rawValue: 1 << 4)
|
||||
@inlinable public static var append: RawIORequest.ReadWriteFlags { ReadWriteFlags(rawValue: 1 << 4) }
|
||||
}
|
||||
|
||||
public struct TimeOutFlags: OptionSet, Hashable, Codable {
|
||||
public var rawValue: UInt32
|
||||
|
||||
public init(rawValue: UInt32) {
|
||||
@inlinable public init(rawValue: UInt32) {
|
||||
self.rawValue = rawValue
|
||||
}
|
||||
|
||||
public static let relativeTime: RawIORequest.TimeOutFlags = TimeOutFlags(rawValue: 0)
|
||||
public static let absoluteTime: RawIORequest.TimeOutFlags = TimeOutFlags(rawValue: 1 << 0)
|
||||
@inlinable public static var relativeTime: RawIORequest.TimeOutFlags { TimeOutFlags(rawValue: 0) }
|
||||
@inlinable public static var absoluteTime: RawIORequest.TimeOutFlags { TimeOutFlags(rawValue: 1 << 0) }
|
||||
}
|
||||
}
|
||||
|
||||
extension RawIORequest {
|
||||
@inlinable
|
||||
static func nop() -> RawIORequest {
|
||||
var req: RawIORequest = RawIORequest()
|
||||
req.operation = .nop
|
||||
return req
|
||||
}
|
||||
|
||||
//TODO: typed errors
|
||||
@inlinable
|
||||
static func withTimeoutRequest<R>(
|
||||
linkedTo opEntry: UnsafeMutablePointer<io_uring_sqe>,
|
||||
in timeoutEntry: UnsafeMutablePointer<io_uring_sqe>,
|
||||
|
||||
Reference in new issue
Block a user