Skip to content

Use a concurrent queue to wait for process completion #450

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Sources/TSCBasic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ add_library(TSCBasic
WritableByteStream.swift
Path.swift
PathShims.swift
Process.swift
ProcessEnv.swift
ProcessSet.swift
Process/Process.swift
Process/ProcessEnv.swift
Process/ProcessSet.swift
RegEx.swift
Result.swift
SortedArray.swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,15 @@ public struct ProcessResult: CustomStringConvertible, Sendable {
}
}

#if swift(<5.6)
extension Process: UnsafeSendable {}
#else
extension Process: @unchecked Sendable {}
#endif

extension DispatchQueue {
// a shared concurrent queue for running concurrent asynchronous operations
static let processConcurrent = DispatchQueue(
label: "swift.org.swift-tsc.process.concurrent",
attributes: .concurrent
)
}

/// Process allows spawning new subprocesses and working with them.
///
Expand Down Expand Up @@ -829,25 +833,15 @@ public final class Process {
@available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
@discardableResult
public func waitUntilExit() async throws -> ProcessResult {
#if compiler(>=5.6)
return try await withCheckedThrowingContinuation { continuation in
waitUntilExit(continuation.resume(with:))
}
#else
if #available(macOS 12.0, iOS 15.0, tvOS 15.0, watchOS 8.0, *) {
return try await withCheckedThrowingContinuation { continuation in
waitUntilExit(continuation.resume(with:))
try await withCheckedThrowingContinuation { continuation in
DispatchQueue.processConcurrent.async {
self.waitUntilExit(continuation.resume(with:))
}
} else {
preconditionFailure("Unsupported with Swift 5.5 on this OS version")
}
#endif
}

/// Blocks the calling process until the subprocess finishes execution.
// #if compiler(>=5.8)
// @available(*, noasync)
// #endif
@available(*, noasync)
@discardableResult
public func waitUntilExit() throws -> ProcessResult {
let group = DispatchGroup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import Foundation
import TSCLibc

/// Provides functionality related a process's enviorment.
/// Provides functionality related a process's environment.
public enum ProcessEnv {

/// Returns a dictionary containing the current environment.
Expand Down