diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index b6d023e..53dd59c 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -18,7 +18,7 @@ jobs: linux_swift_versions: "[\"6.4\"]" linux_os_versions: "[\"resolute\"]" linux_host_archs: "[\"aarch64\"]" - linux_pre_build_command: "apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y curl && curl -fsSL -o /tmp/toolchain.tar.gz https://download.swift.org/tmp/pull-request/91981/9/ubuntu2604/PR-ubuntu2604.tar.gz && tar -xzf /tmp/toolchain.tar.gz -C / && rm /tmp/toolchain.tar.gz" + linux_pre_build_command: "apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y curl && curl -fsSL -o /tmp/toolchain.tar.gz https://download.swift.org/tmp/pull-request/91981/15/ubuntu2604/PR-ubuntu2604.tar.gz && tar -xzf /tmp/toolchain.tar.gz -C / && rm /tmp/toolchain.tar.gz" windows_swift_versions: "[\"nightly\"]" linux_build_command: swift test --traits ExperimentalIO @@ -44,6 +44,6 @@ jobs: persist-credentials: false - name: Pre-build # The same toolchain of https://github.com/swiftlang/swift/pull/91981 as the shared Linux job. - run: apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y curl && curl -fsSL -o /tmp/toolchain.tar.gz https://download.swift.org/tmp/pull-request/91981/9/ubuntu2604/PR-ubuntu2604.tar.gz && tar -xzf /tmp/toolchain.tar.gz -C / && rm /tmp/toolchain.tar.gz + run: apt-get update && DEBIAN_FRONTEND=noninteractive apt-get install -y curl && curl -fsSL -o /tmp/toolchain.tar.gz https://download.swift.org/tmp/pull-request/91981/15/ubuntu2604/PR-ubuntu2604.tar.gz && tar -xzf /tmp/toolchain.tar.gz -C / && rm /tmp/toolchain.tar.gz - name: Build / Test run: swift test --traits ExperimentalIOUring diff --git a/Package.swift b/Package.swift index 1b4ec87..bd1f159 100644 --- a/Package.swift +++ b/Package.swift @@ -32,7 +32,7 @@ let package = Package( // The I/O interfaces require a toolchain that contains the split continuation of // https://github.com/swiftlang/swift/pull/91981, which wires through cancellation and priority // escalation of the operations. The toolchains can be downloaded from: - // - Linux: https://download.swift.org/tmp/pull-request/91981/9/ubuntu2604/PR-ubuntu2604.tar.gz + // - Linux: https://download.swift.org/tmp/pull-request/91981/15/ubuntu2604/PR-ubuntu2604.tar.gz // - macOS: https://download.swift.org/tmp/pull-request/91981/2475/xcode/swift-PR-91981-2475-osx.tar.gz // On macOS, processes have to run against the Concurrency runtime of the toolchain, for example with // `DYLD_LIBRARY_PATH=/usr/lib/swift/macosx`. diff --git a/Sources/PlatformExecutors/PThread/Internal/IO/PThreadTaskExecutor+FileOperations.swift b/Sources/PlatformExecutors/PThread/Internal/IO/PThreadTaskExecutor+FileOperations.swift index 66c6bc1..6a3f4e9 100644 --- a/Sources/PlatformExecutors/PThread/Internal/IO/PThreadTaskExecutor+FileOperations.swift +++ b/Sources/PlatformExecutors/PThread/Internal/IO/PThreadTaskExecutor+FileOperations.swift @@ -101,15 +101,20 @@ extension PThreadTaskExecutor: FileOperationScheduler { private func performBlocking(_ operation: IOOperation) async throws(IOError) -> Int { let queue = self.blockingQueue() return try await withContinuation(of: Int.self, throwing: IOError.self) { - (continuation, awaiter) async throws(IOError) -> Int in + (continuation, waiter) async throws(IOError) -> Int in + let priority = Task.currentPriority let id = unsafe queue.submit( operation: operation, - priority: Task.currentPriority, + priority: priority, continuation: continuation ) - return try await awaiter.wait( - onCancel: { queue.cancel(id) }, - onEscalate: { newPriority in queue.escalate(id, to: newPriority) } + return try await waiter.wait( + onCancel: { _ in queue.cancel(id) }, + onPriorityEscalated: { _, newPriority in + // The handler also runs once when the wait starts, with the priority that the task has then. + guard newPriority > priority else { return } + queue.escalate(id, to: newPriority) + } ) } } diff --git a/Sources/PlatformExecutors/PThread/PThreadExecutor.swift b/Sources/PlatformExecutors/PThread/PThreadExecutor.swift index 1a1466d..49b2ecc 100644 --- a/Sources/PlatformExecutors/PThread/PThreadExecutor.swift +++ b/Sources/PlatformExecutors/PThread/PThreadExecutor.swift @@ -1021,26 +1021,26 @@ extension PThreadExecutor { /// Creates a resumer whose resumed tasks run inline if they run on the given task executor. init(taskExecutor: UnownedTaskExecutor) { self.resumeConnection = { continuation, result in - unsafe continuation.resume(with: result, on: taskExecutor) + unsafe continuation.resumeSynchronously(with: result, on: taskExecutor) } self.resumeByteCount = { continuation, result in - unsafe continuation.resume(with: result, on: taskExecutor) + unsafe continuation.resumeSynchronously(with: result, on: taskExecutor) } self.resumeVoid = { continuation, result in - unsafe continuation.resume(with: result, on: taskExecutor) + unsafe continuation.resumeSynchronously(with: result, on: taskExecutor) } } /// Creates a resumer whose resumed tasks run inline if they are isolated to the given serial executor. init(serialExecutor: UnownedSerialExecutor) { self.resumeConnection = { continuation, result in - unsafe continuation.resume(with: result, isolatedTo: serialExecutor) + unsafe continuation.resumeSynchronously(with: result, isolatedTo: serialExecutor) } self.resumeByteCount = { continuation, result in - unsafe continuation.resume(with: result, isolatedTo: serialExecutor) + unsafe continuation.resumeSynchronously(with: result, isolatedTo: serialExecutor) } self.resumeVoid = { continuation, result in - unsafe continuation.resume(with: result, isolatedTo: serialExecutor) + unsafe continuation.resumeSynchronously(with: result, isolatedTo: serialExecutor) } } } @@ -1077,15 +1077,20 @@ extension PThreadExecutor { try await withContinuation( of: Success.self, throwing: IOError.self - ) { (continuation, awaiter) async throws(IOError) -> Success in + ) { (continuation, waiter) async throws(IOError) -> Success in + let priority = Task.currentPriority guard let id = unsafe self.submit(operation, resuming: resumption(continuation)) else { // The operation completed synchronously, so there is nothing // to cancel and no reason to install the handlers. - return try await awaiter.wait() + return try await waiter.wait() } - return try await awaiter.wait( - onCancel: { self.cancel(id) }, - onEscalate: { newPriority in self.escalatePriority(of: id, to: newPriority) } + return try await waiter.wait( + onCancel: { _ in self.cancel(id) }, + onPriorityEscalated: { _, newPriority in + // The handler also runs once when the wait starts, with the priority that the task has then. + guard newPriority > priority else { return } + self.escalatePriority(of: id, to: newPriority) + } ) } }