Skip to content

Commit 418ebb8

Browse files
damuellenparkera
authored andcommitted
[SR-3279] Renaming Task to Process (#724)
* Renaming Task to Process * Rename property to match Darwin version * Comments implemented. Build.py updated. * make notification a static property * Must be public
1 parent b190994 commit 418ebb8

File tree

5 files changed

+143
-140
lines changed

5 files changed

+143
-140
lines changed

Foundation.xcodeproj/project.pbxproj

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
5B13B3431C582D4C00651CE2 /* TestNSScanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = 844DC3321C17584F005611F9 /* TestNSScanner.swift */; };
5050
5B13B3441C582D4C00651CE2 /* TestNSSet.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA66F6411BF1619600136161 /* TestNSSet.swift */; };
5151
5B13B3451C582D4C00651CE2 /* TestNSString.swift in Sources */ = {isa = PBXBuildFile; fileRef = EA66F6421BF1619600136161 /* TestNSString.swift */; };
52-
5B13B3461C582D4C00651CE2 /* TestNSTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B6F17941C48631C00935030 /* TestNSTask.swift */; };
52+
5B13B3461C582D4C00651CE2 /* TestProcess.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B6F17941C48631C00935030 /* TestProcess.swift */; };
5353
5B13B3471C582D4C00651CE2 /* TestNSThread.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E5835F31C20C9B500C81317 /* TestNSThread.swift */; };
5454
5B13B3481C582D4C00651CE2 /* TestNSTimer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 61D6C9EE1C1DFE9500DEF583 /* TestNSTimer.swift */; };
5555
5B13B3491C582D4C00651CE2 /* TestNSTimeZone.swift in Sources */ = {isa = PBXBuildFile; fileRef = 84BA558D1C16F90900F48C54 /* TestNSTimeZone.swift */; };
@@ -383,7 +383,7 @@
383383
EADE0BB51BD15E0000C49C64 /* NSScanner.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B771BD15DFF00C49C64 /* NSScanner.swift */; };
384384
EADE0BB61BD15E0000C49C64 /* NSSortDescriptor.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B781BD15DFF00C49C64 /* NSSortDescriptor.swift */; };
385385
EADE0BB71BD15E0000C49C64 /* NSStream.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B791BD15DFF00C49C64 /* NSStream.swift */; };
386-
EADE0BB81BD15E0000C49C64 /* NSTask.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B7A1BD15DFF00C49C64 /* NSTask.swift */; };
386+
EADE0BB81BD15E0000C49C64 /* Process.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B7A1BD15DFF00C49C64 /* Process.swift */; };
387387
EADE0BB91BD15E0000C49C64 /* NSTextCheckingResult.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B7B1BD15DFF00C49C64 /* NSTextCheckingResult.swift */; };
388388
EADE0BBB1BD15E0000C49C64 /* NSURLAuthenticationChallenge.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B7D1BD15DFF00C49C64 /* NSURLAuthenticationChallenge.swift */; };
389389
EADE0BBC1BD15E0000C49C64 /* NSURLCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = EADE0B7E1BD15DFF00C49C64 /* NSURLCache.swift */; };
@@ -657,7 +657,7 @@
657657
5B628EDE1D1C995C00CA9570 /* NSMeasurementFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSMeasurementFormatter.swift; sourceTree = "<group>"; };
658658
5B6F17921C48631C00935030 /* TestNSNull.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSNull.swift; sourceTree = "<group>"; };
659659
5B6F17931C48631C00935030 /* TestNSNumberFormatter.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSNumberFormatter.swift; sourceTree = "<group>"; };
660-
5B6F17941C48631C00935030 /* TestNSTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSTask.swift; sourceTree = "<group>"; };
660+
5B6F17941C48631C00935030 /* TestProcess.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = TestProcess.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
661661
5B6F17951C48631C00935030 /* TestNSXMLDocument.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestNSXMLDocument.swift; sourceTree = "<group>"; };
662662
5B6F17961C48631C00935030 /* TestUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TestUtils.swift; sourceTree = "<group>"; };
663663
5B7818591D6CB5CD004A01F2 /* CGFloat.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CGFloat.swift; sourceTree = "<group>"; };
@@ -848,7 +848,7 @@
848848
EADE0B771BD15DFF00C49C64 /* NSScanner.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSScanner.swift; sourceTree = "<group>"; };
849849
EADE0B781BD15DFF00C49C64 /* NSSortDescriptor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSSortDescriptor.swift; sourceTree = "<group>"; };
850850
EADE0B791BD15DFF00C49C64 /* NSStream.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSStream.swift; sourceTree = "<group>"; };
851-
EADE0B7A1BD15DFF00C49C64 /* NSTask.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTask.swift; sourceTree = "<group>"; };
851+
EADE0B7A1BD15DFF00C49C64 /* Process.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; lineEnding = 0; path = Process.swift; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.swift; };
852852
EADE0B7B1BD15DFF00C49C64 /* NSTextCheckingResult.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSTextCheckingResult.swift; sourceTree = "<group>"; };
853853
EADE0B7D1BD15DFF00C49C64 /* NSURLAuthenticationChallenge.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSURLAuthenticationChallenge.swift; sourceTree = "<group>"; };
854854
EADE0B7E1BD15DFF00C49C64 /* NSURLCache.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NSURLCache.swift; sourceTree = "<group>"; };
@@ -1361,7 +1361,7 @@
13611361
844DC3321C17584F005611F9 /* TestNSScanner.swift */,
13621362
EA66F6411BF1619600136161 /* TestNSSet.swift */,
13631363
EA66F6421BF1619600136161 /* TestNSString.swift */,
1364-
5B6F17941C48631C00935030 /* TestNSTask.swift */,
1364+
5B6F17941C48631C00935030 /* TestProcess.swift */,
13651365
5FE52C941D147D1C00F7D270 /* TestNSTextCheckingResult.swift */,
13661366
5E5835F31C20C9B500C81317 /* TestNSThread.swift */,
13671367
61D6C9EE1C1DFE9500DEF583 /* TestNSTimer.swift */,
@@ -1635,7 +1635,7 @@
16351635
children = (
16361636
EADE0B5D1BD15DFF00C49C64 /* NSFileHandle.swift */,
16371637
EADE0B5E1BD15DFF00C49C64 /* NSFileManager.swift */,
1638-
EADE0B7A1BD15DFF00C49C64 /* NSTask.swift */,
1638+
EADE0B7A1BD15DFF00C49C64 /* Process.swift */,
16391639
5BDC3F2F1BCC5DCB00ED97BB /* NSBundle.swift */,
16401640
5BDC3F411BCC5DCB00ED97BB /* NSProcessInfo.swift */,
16411641
5BDC3F471BCC5DCB00ED97BB /* NSThread.swift */,
@@ -2054,7 +2054,7 @@
20542054
6EB768281D18C12C00D4B719 /* UUID.swift in Sources */,
20552055
5B1FD9D41D6D16580080E83C /* Configuration.swift in Sources */,
20562056
5BF7AEA51BCD51F9008F214A /* NSCalendar.swift in Sources */,
2057-
EADE0BB81BD15E0000C49C64 /* NSTask.swift in Sources */,
2057+
EADE0BB81BD15E0000C49C64 /* Process.swift in Sources */,
20582058
5BF7AEB31BCD51F9008F214A /* NSObjCRuntime.swift in Sources */,
20592059
5BD31D3F1D5D19D600563814 /* Dictionary.swift in Sources */,
20602060
5B94E8821C430DE70055C035 /* NSStringAPI.swift in Sources */,
@@ -2267,7 +2267,7 @@
22672267
5B13B3521C582D4C00651CE2 /* TestNSValue.swift in Sources */,
22682268
5B13B3311C582D4C00651CE2 /* TestNSIndexPath.swift in Sources */,
22692269
5B13B3271C582D4C00651CE2 /* TestNSArray.swift in Sources */,
2270-
5B13B3461C582D4C00651CE2 /* TestNSTask.swift in Sources */,
2270+
5B13B3461C582D4C00651CE2 /* TestProcess.swift in Sources */,
22712271
555683BD1C1250E70041D4C6 /* TestNSUserDefaults.swift in Sources */,
22722272
7900433B1CACD33E00ECCBF1 /* TestNSCompoundPredicate.swift in Sources */,
22732273
);

Foundation/NSTask.swift renamed to Foundation/Process.swift

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import CoreFoundation
1616
import Glibc
1717
#endif
1818

19-
extension Task {
19+
extension Process {
2020
public enum TerminationReason : Int {
2121
case exit
2222
case uncaughtSignal
@@ -72,27 +72,27 @@ private func runloopIsEqual(_ a : UnsafeRawPointer?, _ b : UnsafeRawPointer?) ->
7272
}
7373

7474

75-
// Equal method for task in run loop source
76-
private func nstaskIsEqual(_ a : UnsafeRawPointer?, _ b : UnsafeRawPointer?) -> _DarwinCompatibleBoolean {
75+
// Equal method for process in run loop source
76+
private func processIsEqual(_ a : UnsafeRawPointer?, _ b : UnsafeRawPointer?) -> _DarwinCompatibleBoolean {
7777

78-
let unmanagedTaskA = Unmanaged<AnyObject>.fromOpaque(a!)
79-
guard let taskA = unmanagedTaskA.takeUnretainedValue() as? Task else {
78+
let unmanagedProcessA = Unmanaged<AnyObject>.fromOpaque(a!)
79+
guard let processA = unmanagedProcessA.takeUnretainedValue() as? Process else {
8080
return false
8181
}
8282

83-
let unmanagedTaskB = Unmanaged<AnyObject>.fromOpaque(a!)
84-
guard let taskB = unmanagedTaskB.takeUnretainedValue() as? Task else {
83+
let unmanagedProcessB = Unmanaged<AnyObject>.fromOpaque(a!)
84+
guard let processB = unmanagedProcessB.takeUnretainedValue() as? Process else {
8585
return false
8686
}
8787

88-
guard taskA == taskB else {
88+
guard processA == processB else {
8989
return false
9090
}
9191

9292
return true
9393
}
9494

95-
open class Task: NSObject {
95+
open class Process: NSObject {
9696
private static func setup() {
9797
struct Once {
9898
static var done = false
@@ -124,7 +124,7 @@ open class Task: NSObject {
124124
}
125125

126126
managerThreadRunLoop?.run()
127-
fatalError("NSTask manager run loop exited unexpectedly; it should run forever once initialized")
127+
fatalError("Process manager run loop exited unexpectedly; it should run forever once initialized")
128128
}
129129
thread.start()
130130
managerThreadRunLoopIsRunningCondition.lock()
@@ -137,11 +137,11 @@ open class Task: NSObject {
137137
}
138138
}
139139

140-
// Create an NSTask which can be run at a later time
141-
// An NSTask can only be run once. Subsequent attempts to
142-
// run an NSTask will raise.
143-
// Upon task death a notification will be sent
144-
// { Name = NSTaskDidTerminateNotification; object = task; }
140+
// Create an Process which can be run at a later time
141+
// An Process can only be run once. Subsequent attempts to
142+
// run an Process will raise.
143+
// Upon process death a notification will be sent
144+
// { Name = ProcessDidTerminateNotification; object = process; }
145145
//
146146

147147
public override init() {
@@ -156,19 +156,19 @@ open class Task: NSObject {
156156
open var currentDirectoryPath: String = FileManager.default.currentDirectoryPath
157157

158158
// standard I/O channels; could be either an NSFileHandle or an NSPipe
159-
open var standardInput: AnyObject? {
159+
open var standardInput: Any? {
160160
willSet {
161161
precondition(newValue is Pipe || newValue is FileHandle,
162162
"standardInput must be either NSPipe or NSFileHandle")
163163
}
164164
}
165-
open var standardOutput: AnyObject? {
165+
open var standardOutput: Any? {
166166
willSet {
167167
precondition(newValue is Pipe || newValue is FileHandle,
168168
"standardOutput must be either NSPipe or NSFileHandle")
169169
}
170170
}
171-
open var standardError: AnyObject? {
171+
open var standardError: Any? {
172172
willSet {
173173
precondition(newValue is Pipe || newValue is FileHandle,
174174
"standardError must be either NSPipe or NSFileHandle")
@@ -189,7 +189,7 @@ open class Task: NSObject {
189189

190190
// Dispatch the manager thread if it isn't already running
191191

192-
Task.setup()
192+
Process.setup()
193193

194194
// Ensure that the launch path is set
195195

@@ -251,14 +251,14 @@ open class Task: NSObject {
251251
let socket = CFSocketCreateWithNative( nil, taskSocketPair[0], CFOptionFlags(kCFSocketDataCallBack), {
252252
(socket, type, address, data, info ) in
253253

254-
let task: Task = NSObject.unretainedReference(info!)
254+
let process: Process = NSObject.unretainedReference(info!)
255255

256-
task.processLaunchedCondition.lock()
257-
while task.running == false {
258-
task.processLaunchedCondition.wait()
256+
process.processLaunchedCondition.lock()
257+
while process.isRunning == false {
258+
process.processLaunchedCondition.wait()
259259
}
260260

261-
task.processLaunchedCondition.unlock()
261+
process.processLaunchedCondition.unlock()
262262

263263
var exitCode : Int32 = 0
264264
#if CYGWIN
@@ -271,31 +271,31 @@ open class Task: NSObject {
271271

272272
repeat {
273273
#if CYGWIN
274-
waitResult = waitpid( task.processIdentifier, exitCodePtrWrapper, 0)
274+
waitResult = waitpid( process.processIdentifier, exitCodePtrWrapper, 0)
275275
#else
276-
waitResult = waitpid( task.processIdentifier, &exitCode, 0)
276+
waitResult = waitpid( process.processIdentifier, &exitCode, 0)
277277
#endif
278278
} while ( (waitResult == -1) && (errno == EINTR) )
279279

280-
task.terminationStatus = WEXITSTATUS( exitCode )
280+
process.terminationStatus = WEXITSTATUS( exitCode )
281281

282282
// If a termination handler has been set, invoke it on a background thread
283283

284-
if task.terminationHandler != nil {
284+
if process.terminationHandler != nil {
285285
let thread = Thread {
286-
task.terminationHandler!(task)
286+
process.terminationHandler!(process)
287287
}
288288
thread.start()
289289
}
290290

291291
// Set the running flag to false
292292

293-
task.running = false
293+
process.isRunning = false
294294

295295
// Invalidate the source and wake up the run loop, if it's available
296296

297-
CFRunLoopSourceInvalidate(task.runLoopSource)
298-
if let runLoop = task.runLoop {
297+
CFRunLoopSourceInvalidate(process.runLoopSource)
298+
if let runLoop = process.runLoop {
299299
CFRunLoopWakeUp(runLoop._cfRunLoop)
300300
}
301301

@@ -384,7 +384,7 @@ open class Task: NSObject {
384384
retain: { return runLoopSourceRetain($0) },
385385
release: { runLoopSourceRelease($0) },
386386
copyDescription: nil,
387-
equal: { return nstaskIsEqual($0, $1) },
387+
equal: { return processIsEqual($0, $1) },
388388
hash: nil,
389389
schedule: nil,
390390
cancel: nil,
@@ -395,7 +395,7 @@ open class Task: NSObject {
395395
runLoopContext.version = 0
396396
runLoopContext.retain = runLoopSourceRetain
397397
runLoopContext.release = runLoopSourceRelease
398-
runLoopContext.equal = nstaskIsEqual
398+
runLoopContext.equal = processIsEqual
399399
runLoopContext.perform = emptyRunLoopCallback
400400
self.withUnretainedReference {
401401
(refPtr: UnsafeMutablePointer<UInt8>) in
@@ -406,7 +406,7 @@ open class Task: NSObject {
406406
self.runLoopSource = CFRunLoopSourceCreate(kCFAllocatorDefault, 0, &runLoopSourceContext!)
407407
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode)
408408

409-
running = true
409+
isRunning = true
410410

411411
self.processIdentifier = pid
412412

@@ -422,43 +422,46 @@ open class Task: NSObject {
422422

423423
// status
424424
open private(set) var processIdentifier: Int32 = -1
425-
open private(set) var running: Bool = false
425+
open private(set) var isRunning: Bool = false
426426

427427
open private(set) var terminationStatus: Int32 = 0
428428
open var terminationReason: TerminationReason { NSUnimplemented() }
429429

430430
/*
431-
A block to be invoked when the process underlying the NSTask terminates. Setting the block to nil is valid, and stops the previous block from being invoked, as long as it hasn't started in any way. The NSTask is passed as the argument to the block so the block does not have to capture, and thus retain, it. The block is copied when set. Only one termination handler block can be set at any time. The execution context in which the block is invoked is undefined. If the NSTask has already finished, the block is executed immediately/soon (not necessarily on the current thread). If a terminationHandler is set on an NSTask, the NSTaskDidTerminateNotification notification is not posted for that task. Also note that -waitUntilExit won't wait until the terminationHandler has been fully executed. You cannot use this property in a concrete subclass of NSTask which hasn't been updated to include an implementation of the storage and use of it.
431+
A block to be invoked when the process underlying the Process terminates. Setting the block to nil is valid, and stops the previous block from being invoked, as long as it hasn't started in any way. The Process is passed as the argument to the block so the block does not have to capture, and thus retain, it. The block is copied when set. Only one termination handler block can be set at any time. The execution context in which the block is invoked is undefined. If the Process has already finished, the block is executed immediately/soon (not necessarily on the current thread). If a terminationHandler is set on an Process, the ProcessDidTerminateNotification notification is not posted for that process. Also note that -waitUntilExit won't wait until the terminationHandler has been fully executed. You cannot use this property in a concrete subclass of Process which hasn't been updated to include an implementation of the storage and use of it.
432432
*/
433-
open var terminationHandler: ((Task) -> Void)?
434-
open var qualityOfService: NSQualityOfService = .default // read-only after the task is launched
433+
open var terminationHandler: ((Process) -> Void)?
434+
open var qualityOfService: NSQualityOfService = .default // read-only after the process is launched
435435
}
436436

437-
extension Task {
437+
extension Process {
438438

439439
// convenience; create and launch
440-
open class func launchedTaskWithLaunchPath(_ path: String, arguments: [String]) -> Task {
441-
let task = Task()
442-
task.launchPath = path
443-
task.arguments = arguments
444-
task.launch()
440+
open class func launchedProcess(launchPath path: String, arguments: [String]) -> Process {
441+
let process = Process()
442+
process.launchPath = path
443+
process.arguments = arguments
444+
process.launch()
445445

446-
return task
446+
return process
447447
}
448448

449-
// poll the runLoop in defaultMode until task completes
449+
// poll the runLoop in defaultMode until process completes
450450
open func waitUntilExit() {
451451

452452
repeat {
453453

454-
} while( self.running == true && RunLoop.current.run(mode: .defaultRunLoopMode, before: Date(timeIntervalSinceNow: 0.05)) )
454+
} while( self.isRunning == true && RunLoop.current.run(mode: .defaultRunLoopMode, before: Date(timeIntervalSinceNow: 0.05)) )
455455

456456
self.runLoop = nil
457457
}
458458
}
459459

460-
public let NSTaskDidTerminateNotification: String = "NSTaskDidTerminateNotification"
461-
460+
extension Process {
461+
462+
public static let didTerminateNotification = NSNotification.Name(rawValue: "NSTaskDidTerminateNotification")
463+
}
464+
462465
private func posix(_ code: Int32) {
463466
switch code {
464467
case 0: return

0 commit comments

Comments
 (0)