mirror of
https://github.com/apple/swift-system
synced 2026-11-28 05:14:33 +00:00
Remove managed abstractions for now
This commit is contained in:
1 parent
55fd6e7150
commit
e5fdf9ea96
4 files changed
-364
No files matched your search
@@ -1,161 +0,0 @@
|
||||
@_implementationOnly import CSystem
|
||||
|
||||
public struct AsyncFileDescriptor: ~Copyable {
|
||||
@usableFromInline var open: Bool = true
|
||||
@usableFromInline let fileSlot: IORingFileSlot
|
||||
@usableFromInline let ring: ManagedIORing
|
||||
|
||||
public static func open(
|
||||
path: FilePath,
|
||||
in directory: FileDescriptor = FileDescriptor(rawValue: -100),
|
||||
on ring: ManagedIORing,
|
||||
mode: FileDescriptor.AccessMode,
|
||||
options: FileDescriptor.OpenOptions = FileDescriptor.OpenOptions(),
|
||||
permissions: FilePermissions? = nil
|
||||
) async throws -> AsyncFileDescriptor {
|
||||
// todo; real error type
|
||||
guard let fileSlot = ring.getFileSlot() else {
|
||||
throw IORingError.missingRequiredFeatures
|
||||
}
|
||||
//TODO: need an async-friendly withCString
|
||||
let cstr = path.withCString {
|
||||
return $0 // bad
|
||||
}
|
||||
let res = try await ring.submit(request: IORequest(
|
||||
opening: cstr,
|
||||
in: directory,
|
||||
into: fileSlot,
|
||||
mode: mode,
|
||||
options: options,
|
||||
permissions: permissions
|
||||
))
|
||||
if res.result < 0 {
|
||||
throw Errno(rawValue: -res.result)
|
||||
}
|
||||
|
||||
return AsyncFileDescriptor(
|
||||
fileSlot, ring: ring
|
||||
)
|
||||
}
|
||||
|
||||
internal init(_ fileSlot: consuming IORingFileSlot, ring: ManagedIORing) {
|
||||
self.fileSlot = consume fileSlot
|
||||
self.ring = ring
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
public consuming func close(isolation actor: isolated (any Actor)? = #isolation) async throws {
|
||||
let res = try await ring.submit(request: IORequest(closing: fileSlot))
|
||||
if res.result < 0 {
|
||||
throw Errno(rawValue: -res.result)
|
||||
}
|
||||
self.open = false
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
public func read(
|
||||
into buffer: inout UnsafeMutableRawBufferPointer,
|
||||
atAbsoluteOffset offset: UInt64 = UInt64.max,
|
||||
isolation actor: isolated (any Actor)? = #isolation
|
||||
) async throws -> UInt32 {
|
||||
let res = try await ring.submit(request: IORequest(
|
||||
reading: fileSlot,
|
||||
into: buffer,
|
||||
at: offset
|
||||
))
|
||||
if res.result < 0 {
|
||||
throw Errno(rawValue: -res.result)
|
||||
} else {
|
||||
return UInt32(bitPattern: res.result)
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
public func read(
|
||||
into buffer: IORingBuffer, //TODO: should be inout?
|
||||
atAbsoluteOffset offset: UInt64 = UInt64.max,
|
||||
isolation actor: isolated (any Actor)? = #isolation
|
||||
) async throws -> UInt32 {
|
||||
let res = try await ring.submit(request: IORequest(
|
||||
reading: fileSlot,
|
||||
into: buffer,
|
||||
at: offset
|
||||
))
|
||||
if res.result < 0 {
|
||||
throw Errno(rawValue: -res.result)
|
||||
} else {
|
||||
return UInt32(bitPattern: res.result)
|
||||
}
|
||||
}
|
||||
|
||||
//TODO: temporary workaround until AsyncSequence supports ~Copyable
|
||||
public consuming func toBytes() -> AsyncFileDescriptorSequence {
|
||||
AsyncFileDescriptorSequence(self)
|
||||
}
|
||||
|
||||
//TODO: can we do the linear types thing and error if they don't consume it manually?
|
||||
// deinit {
|
||||
// if self.open {
|
||||
// close()
|
||||
// // TODO: close or error? TBD
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
||||
public class AsyncFileDescriptorSequence: AsyncSequence {
|
||||
var descriptor: AsyncFileDescriptor?
|
||||
|
||||
public func makeAsyncIterator() -> FileIterator {
|
||||
return .init(descriptor.take()!)
|
||||
}
|
||||
|
||||
internal init(_ descriptor: consuming AsyncFileDescriptor) {
|
||||
self.descriptor = consume descriptor
|
||||
}
|
||||
|
||||
public typealias AsyncIterator = FileIterator
|
||||
public typealias Element = UInt8
|
||||
}
|
||||
|
||||
//TODO: only a class due to ~Copyable limitations
|
||||
public class FileIterator: AsyncIteratorProtocol {
|
||||
@usableFromInline let file: AsyncFileDescriptor
|
||||
@usableFromInline var buffer: IORingBuffer
|
||||
@usableFromInline var done: Bool
|
||||
|
||||
@usableFromInline internal var currentByte: UnsafeRawPointer?
|
||||
@usableFromInline internal var lastByte: UnsafeRawPointer?
|
||||
|
||||
init(_ file: consuming AsyncFileDescriptor) {
|
||||
self.buffer = file.ring.getBuffer()!
|
||||
self.file = file
|
||||
self.done = false
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
public func nextBuffer() async throws {
|
||||
let bytesRead = Int(try await file.read(into: buffer))
|
||||
if _fastPath(bytesRead != 0) {
|
||||
let unsafeBuffer = buffer.unsafeBuffer
|
||||
let bufPointer = unsafeBuffer.baseAddress.unsafelyUnwrapped
|
||||
self.currentByte = UnsafeRawPointer(bufPointer)
|
||||
self.lastByte = UnsafeRawPointer(bufPointer.advanced(by: bytesRead))
|
||||
} else {
|
||||
done = true
|
||||
}
|
||||
}
|
||||
|
||||
@inlinable @inline(__always)
|
||||
public func next() async throws -> UInt8? {
|
||||
if _fastPath(currentByte != lastByte) {
|
||||
// SAFETY: both pointers should be non-nil if they're not equal
|
||||
let byte = currentByte.unsafelyUnwrapped.load(as: UInt8.self)
|
||||
currentByte = currentByte.unsafelyUnwrapped + 1
|
||||
return byte
|
||||
} else if done {
|
||||
return nil
|
||||
}
|
||||
try await nextBuffer()
|
||||
return try await next()
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
fileprivate func handleCompletionError(
|
||||
_ result: Int32,
|
||||
for continuation: UnsafeContinuation<IOCompletion, any Error>) {
|
||||
var error: IORingError = .unknown
|
||||
switch result {
|
||||
case -(_ECANCELED):
|
||||
error = .operationCanceled
|
||||
default:
|
||||
error = .unknown
|
||||
}
|
||||
continuation.resume(throwing: error)
|
||||
}
|
||||
|
||||
final public class ManagedIORing: @unchecked Sendable {
|
||||
var internalRing: IORing
|
||||
|
||||
public init(queueDepth: UInt32) throws {
|
||||
self.internalRing = try IORing(queueDepth: queueDepth)
|
||||
self.internalRing.registerBuffers(bufSize: 655336, count: 4)
|
||||
self.internalRing.registerFiles(count: 32)
|
||||
self.startWaiter()
|
||||
}
|
||||
|
||||
private func startWaiter() {
|
||||
Task.detached {
|
||||
while !Task.isCancelled {
|
||||
//TODO: should timeout handling be sunk into IORing?
|
||||
//TODO: sort out the error handling here
|
||||
let cqe = try! self.internalRing.blockingConsumeCompletion()
|
||||
|
||||
if cqe.userData == 0 {
|
||||
continue
|
||||
}
|
||||
let cont = unsafeBitCast(
|
||||
cqe.userData, to: UnsafeContinuation<IOCompletion, any Error>.self)
|
||||
|
||||
if cqe.result < 0 {
|
||||
handleCompletionError(cqe.result, for: cont)
|
||||
} else {
|
||||
cont.resume(returning: cqe)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public func submit(
|
||||
request: __owned IORequest,
|
||||
timeout: Duration? = nil,
|
||||
isolation actor: isolated (any Actor)? = #isolation
|
||||
) async throws -> IOCompletion {
|
||||
var consumeOnceWorkaround: IORequest? = request
|
||||
return try await withUnsafeThrowingContinuation { cont in
|
||||
do {
|
||||
try internalRing.submissionMutex.withLock { ring in
|
||||
let request = consumeOnceWorkaround.take()!
|
||||
let entry = _blockingGetSubmissionEntry(
|
||||
ring: &ring, submissionQueueEntries: internalRing.submissionQueueEntries)
|
||||
entry.pointee = request.makeRawRequest().rawValue
|
||||
entry.pointee.user_data = unsafeBitCast(cont, to: UInt64.self)
|
||||
if let timeout {
|
||||
//TODO: if IORING_FEAT_MIN_TIMEOUT is supported we can do this more efficiently
|
||||
if true { //replace with IORING_FEAT_MIN_TIMEOUT feature check
|
||||
try _submitRequests(ring: &ring, ringDescriptor: internalRing.ringDescriptor)
|
||||
} else {
|
||||
let timeoutEntry = _blockingGetSubmissionEntry(
|
||||
ring: &ring,
|
||||
submissionQueueEntries: internalRing.submissionQueueEntries
|
||||
)
|
||||
try RawIORequest.withTimeoutRequest(
|
||||
linkedTo: entry,
|
||||
in: timeoutEntry,
|
||||
duration: timeout,
|
||||
flags: .relativeTime
|
||||
) {
|
||||
try _submitRequests(ring: &ring, ringDescriptor: internalRing.ringDescriptor)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try _submitRequests(ring: &ring, ringDescriptor: internalRing.ringDescriptor)
|
||||
}
|
||||
}
|
||||
} catch (let e) {
|
||||
cont.resume(throwing: e)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal func getFileSlot() -> IORingFileSlot? {
|
||||
internalRing.getFile()
|
||||
}
|
||||
|
||||
internal func getBuffer() -> IORingBuffer? {
|
||||
internalRing.getBuffer()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,58 +0,0 @@
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
#else
|
||||
import System
|
||||
#endif
|
||||
|
||||
final class AsyncFileDescriptorTests: XCTestCase {
|
||||
func testOpen() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
_ = try await AsyncFileDescriptor.open(
|
||||
path: "/dev/zero",
|
||||
on: ring,
|
||||
mode: .readOnly
|
||||
)
|
||||
}
|
||||
|
||||
func testOpenClose() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
let file = try await AsyncFileDescriptor.open(
|
||||
path: "/dev/zero",
|
||||
on: ring,
|
||||
mode: .readOnly
|
||||
)
|
||||
try await file.close()
|
||||
}
|
||||
|
||||
func testDevNullEmpty() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
let file = try await AsyncFileDescriptor.open(
|
||||
path: "/dev/null",
|
||||
on: ring,
|
||||
mode: .readOnly
|
||||
)
|
||||
for try await _ in file.toBytes() {
|
||||
XCTFail("/dev/null should be empty")
|
||||
}
|
||||
}
|
||||
|
||||
func testRead() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
let file = try await AsyncFileDescriptor.open(
|
||||
path: "/dev/zero",
|
||||
on: ring,
|
||||
mode: .readOnly
|
||||
)
|
||||
let bytes = file.toBytes()
|
||||
var counter = 0
|
||||
for try await byte in bytes {
|
||||
XCTAssert(byte == 0)
|
||||
counter &+= 1
|
||||
if counter > 16384 {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
import XCTest
|
||||
|
||||
#if SYSTEM_PACKAGE
|
||||
import SystemPackage
|
||||
#else
|
||||
import System
|
||||
#endif
|
||||
|
||||
final class ManagedIORingTests: XCTestCase {
|
||||
func testInit() throws {
|
||||
_ = try ManagedIORing(queueDepth: 32)
|
||||
}
|
||||
|
||||
func testNop() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
let completion = try await ring.submit(request: IORequest())
|
||||
XCTAssertEqual(completion.result, 0)
|
||||
}
|
||||
|
||||
func testSubmitTimeout() async throws {
|
||||
let ring = try ManagedIORing(queueDepth: 32)
|
||||
var pipes: (Int32, Int32) = (0, 0)
|
||||
withUnsafeMutableBytes(of: &pipes) { ptr in
|
||||
ptr.withMemoryRebound(to: UInt32.self) { tptr in
|
||||
let res = pipe(tptr.baseAddress!)
|
||||
XCTAssertEqual(res, 0)
|
||||
}
|
||||
}
|
||||
let buffer = UnsafeMutableRawBufferPointer.allocate(byteCount: 128, alignment: 16)
|
||||
do {
|
||||
let completion = try await ring.submit(
|
||||
request: IORequest(reading: FileDescriptor(rawValue: pipes.0), into: buffer),
|
||||
timeout: .seconds(0.1)
|
||||
)
|
||||
print("\(completion)")
|
||||
XCTFail("An error should be thrown")
|
||||
} catch (let e) {
|
||||
if let err = e as? IORingError {
|
||||
XCTAssertEqual(err, .operationCanceled)
|
||||
} else {
|
||||
XCTFail()
|
||||
}
|
||||
buffer.deallocate()
|
||||
close(pipes.0)
|
||||
close(pipes.1)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user