Skip to content

Commit 227a38f

Browse files
committed
Remove blocking enqueue
1 parent bbe2f1c commit 227a38f

2 files changed

Lines changed: 14 additions & 41 deletions

File tree

Sources/System/IORing.swift

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -724,23 +724,15 @@ public struct IORing: ~Copyable {
724724

725725
/// Attempts to prepare an IO request for submission to the kernel. Returns false if no space is available to enqueue the request
726726
@inlinable
727-
public mutating func tryPrepare(request: __owned Request) -> Bool {
727+
public mutating func prepare(request: __owned Request) -> Bool {
728728
var raw: RawIORequest? = request.makeRawRequest()
729729
return _tryWriteRequest(
730730
raw.take()!, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
731731
}
732732

733-
/// Attempts to prepare an IO request for submission to the kernel. Blocks if needed until space becomes available to enqueue the request
734-
@inlinable
735-
public mutating func prepare(request: __owned Request) {
736-
var raw: RawIORequest? = request.makeRawRequest()
737-
_writeRequest(
738-
raw.take()!, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
739-
}
740-
741733
/// Attempts to prepare a chain of linked IO requests for submission to the kernel. Returns false if not enough space is available to enqueue the request. If any linked operation fails, subsequent operations will be canceled. Linked operations always execute in order.
742734
@inlinable
743-
mutating func tryPrepare(linkedRequests: some BidirectionalCollection<Request>) -> Bool {
735+
mutating func prepare(linkedRequests: some BidirectionalCollection<Request>) -> Bool {
744736
guard linkedRequests.count > 0 else {
745737
return true
746738
}
@@ -763,41 +755,20 @@ public struct IORing: ~Copyable {
763755
return true
764756
}
765757

766-
/// Prepares a chain of linked IO requests for submission to the kernel. Blocks if needed until space becomes available to enqueue the requests. If any linked operation fails, subsequent operations will be canceled. Linked operations always execute in order.
767-
@inlinable
768-
mutating func prepare(linkedRequests: some BidirectionalCollection<Request>) {
769-
guard linkedRequests.count > 0 else {
770-
return
771-
}
772-
let last = linkedRequests.last!
773-
for req in linkedRequests.dropLast() {
774-
var raw = req.makeRawRequest()
775-
raw.linkToNextRequest()
776-
_writeRequest(
777-
raw, ring: &submissionRing, submissionQueueEntries: submissionQueueEntries)
778-
}
779-
_writeRequest(
780-
last.makeRawRequest(), ring: &submissionRing,
781-
submissionQueueEntries: submissionQueueEntries)
782-
}
783-
784758
/// Prepares a sequence of requests for submission to the ring. Returns false if the submission queue doesn't have enough available space.
785759
@inlinable
786-
public mutating func tryPrepare(linkedRequests: Request...) -> Bool {
787-
tryPrepare(linkedRequests: linkedRequests)
788-
}
789-
790-
/// Prepares a sequence of requests for submission to the ring. Blocks if the submission queue doesn't have enough available space.
791-
@inlinable
792-
public mutating func prepare(linkedRequests: Request...) {
760+
public mutating func prepare(linkedRequests: Request...) -> Bool {
793761
prepare(linkedRequests: linkedRequests)
794762
}
795763

796-
/// Prepares and submits a sequence of requests to the ring. Blocks if the submission queue doesn't have enough available space.
764+
/// Prepares and submits a sequence of requests to the ring. Returns false if the submission queue doesn't have enough available space.
797765
@inlinable
798-
public mutating func submit(linkedRequests: Request...) throws(Errno) {
799-
prepare(linkedRequests: linkedRequests)
766+
public mutating func submit(linkedRequests: Request...) throws(Errno) -> Bool {
767+
if !prepare(linkedRequests: linkedRequests) {
768+
return false
769+
}
800770
try submitPreparedRequests()
771+
return true
801772
}
802773

803774
/// Describes which io_uring features are supported by the kernel this program is running on

Tests/SystemTests/IORingTests.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ final class IORingTests: XCTestCase {
1515

1616
func testNop() throws {
1717
var ring = try IORing(queueDepth: 32, flags: [])
18-
try ring.submit(linkedRequests: .nop())
18+
_ = try ring.submit(linkedRequests: .nop())
1919
let completion = try ring.blockingConsumeCompletion()
2020
XCTAssertEqual(completion.result, 0)
2121
}
@@ -57,10 +57,11 @@ final class IORingTests: XCTestCase {
5757
try ring.registerEventFD(eventFD)
5858

5959
//Part 1: read the file we just created, and make sure the eventfd fires
60-
try ring.submit(linkedRequests:
60+
let enqueued = try ring.submit(linkedRequests:
6161
.open(path, in: parent, into: ring.registeredFileSlots[0], mode: .readOnly),
6262
.read(ring.registeredFileSlots[0], into: ring.registeredBuffers[0]),
6363
.close(ring.registeredFileSlots[0]))
64+
XCTAssert(enqueued)
6465
let efdBuf = UnsafeMutableRawBufferPointer.allocate(byteCount: 8, alignment: 0)
6566
_ = try eventFD.read(into: efdBuf)
6667
_ = try ring.blockingConsumeCompletion() //open
@@ -76,10 +77,11 @@ final class IORingTests: XCTestCase {
7677
remove($0)
7778
}
7879
XCTAssertEqual(rmResult, 0)
79-
try ring.submit(linkedRequests:
80+
let enqueued2 = try ring.submit(linkedRequests:
8081
.open(path, in: parent, into: ring.registeredFileSlots[0], mode: .readWrite, options: .create, permissions: .ownerReadWrite),
8182
.write(ring.registeredBuffers[0], into: ring.registeredFileSlots[0]),
8283
.close(ring.registeredFileSlots[0]))
84+
XCTAssert(enqueued2)
8385
_ = try eventFD.read(into: efdBuf)
8486
_ = try ring.blockingConsumeCompletion() //open
8587
_ = try eventFD.read(into: efdBuf)

0 commit comments

Comments
 (0)