|
| 1 | +//===--------------- ProcessSet.swift - Swift Subprocesses ---------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import struct TSCBasic.Condition |
| 14 | +import class TSCBasic.Process |
| 15 | +import struct TSCBasic.ProcessResult |
| 16 | +import class TSCBasic.Thread |
| 17 | +import Dispatch |
| 18 | +import Foundation |
| 19 | + |
| 20 | +public enum ProcessSetError: Swift.Error { |
| 21 | + /// The process group was cancelled and doesn't allow adding more processes. |
| 22 | + case cancelled |
| 23 | +} |
| 24 | + |
| 25 | +/// A process set is a small wrapper for collection of processes. |
| 26 | +/// |
| 27 | +/// This class is thread safe. |
| 28 | +public final class ProcessSet { |
| 29 | + |
| 30 | + /// Array to hold the processes. |
| 31 | + private var processes: Set<Process> = [] |
| 32 | + |
| 33 | + /// Queue to mutate internal states of the process group. |
| 34 | + private let serialQueue = DispatchQueue(label: "org.swift.swiftpm.process-set") |
| 35 | + |
| 36 | + /// If the process group was asked to cancel all active processes. |
| 37 | + private var cancelled = false |
| 38 | + |
| 39 | + /// The timeout (in seconds) after which the processes should be killed if they don't respond to SIGINT. |
| 40 | + public let killTimeout: Double |
| 41 | + |
| 42 | + /// Condition to block kill thread until timeout. |
| 43 | + private var killingCondition = Condition() |
| 44 | + |
| 45 | + /// Boolean predicate for killing condition. |
| 46 | + private var shouldKill = false |
| 47 | + |
| 48 | + /// Create a process set. |
| 49 | + public init(killTimeout: Double = 5) { |
| 50 | + self.killTimeout = killTimeout |
| 51 | + } |
| 52 | + |
| 53 | + /// Add a process to the process set. This method will throw if the process set is terminated using the terminate() |
| 54 | + /// method. |
| 55 | + /// |
| 56 | + /// Call remove() method to remove the process from set once it has terminated. |
| 57 | + /// |
| 58 | + /// - Parameters: |
| 59 | + /// - process: The process to add. |
| 60 | + /// - Throws: ProcessGroupError |
| 61 | + public func add(_ process: TSCBasic.Process) throws { |
| 62 | + return try serialQueue.sync { |
| 63 | + guard !cancelled else { |
| 64 | + throw ProcessSetError.cancelled |
| 65 | + } |
| 66 | + self.processes.insert(process) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /// Terminate all the processes. This method blocks until all processes in the set are terminated. |
| 71 | + /// |
| 72 | + /// A process set cannot be used once it has been asked to terminate. |
| 73 | + public func terminate() { |
| 74 | + // Mark a process set as cancelled. |
| 75 | + serialQueue.sync { |
| 76 | + cancelled = true |
| 77 | + } |
| 78 | + |
| 79 | + // Interrupt all processes. |
| 80 | + signalAll(SIGINT) |
| 81 | + |
| 82 | + // Create a thread that will kill all processes after a timeout. |
| 83 | + let thread = TSCBasic.Thread { |
| 84 | + // Compute the timeout date. |
| 85 | + let timeout = Date() + self.killTimeout |
| 86 | + // Block until we timeout or notification. |
| 87 | + self.killingCondition.whileLocked { |
| 88 | + while !self.shouldKill { |
| 89 | + // Block until timeout expires. |
| 90 | + let timeLimitReached = !self.killingCondition.wait(until: timeout) |
| 91 | + // Set should kill to true if time limit was reached. |
| 92 | + if timeLimitReached { |
| 93 | + self.shouldKill = true |
| 94 | + } |
| 95 | + } |
| 96 | + } |
| 97 | + // Send kill signal to all processes. |
| 98 | + #if os(Windows) |
| 99 | + self.signalAll(SIGTERM) |
| 100 | + #else |
| 101 | + self.signalAll(SIGKILL) |
| 102 | + #endif |
| 103 | + } |
| 104 | + |
| 105 | + thread.start() |
| 106 | + |
| 107 | + // Wait until all processes terminate and notify the kill thread |
| 108 | + // if everyone exited to avoid waiting till timeout. |
| 109 | + for process in self.processes { |
| 110 | + _ = try? process.waitUntilExit() |
| 111 | + } |
| 112 | + killingCondition.whileLocked { |
| 113 | + shouldKill = true |
| 114 | + killingCondition.signal() |
| 115 | + } |
| 116 | + |
| 117 | + // Join the kill thread so we don't exit before everything terminates. |
| 118 | + thread.join() |
| 119 | + } |
| 120 | + |
| 121 | + /// Sends signal to all processes in the set. |
| 122 | + private func signalAll(_ signal: Int32) { |
| 123 | + serialQueue.sync { |
| 124 | + // Signal all active processes. |
| 125 | + for process in self.processes { |
| 126 | + process.signal(signal) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments