Skip to content

Commit 6214772

Browse files
committed
Foundation: correctly read thread name
Foundation was correctly setting the thread name, but it would only ever read the name that was stored in the thread object. This meant that if the thread name was set elsewhere (e.g. by Linux, or by SwiftNIO) that name would not be reflected in Thread's name property.
1 parent 34016a9 commit 6214772

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

Diff for: Foundation/Thread.swift

+5-2
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,12 @@ open class Thread : NSObject {
251251
}
252252

253253
open var name: String? {
254-
didSet {
254+
get {
255+
return _name
256+
}
257+
set {
255258
if let thread = _thread {
256-
_CFThreadSetName(thread, name ?? "" )
259+
_CFThreadSetName(thread, newValue ?? "" )
257260
}
258261
}
259262
}

Diff for: TestFoundation/TestThread.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,17 @@ class TestThread : XCTestCase {
6868

6969
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
7070
func test_threadName() {
71-
// No name is set initially
72-
XCTAssertNil(Thread.current.name)
73-
7471
#if os(Linux) // Linux sets the initial thread name to the process name.
72+
XCTAssertEqual(Thread.current.name, "TestFoundation")
7573
XCTAssertEqual(Thread.current._name, "TestFoundation")
7674
#else
75+
// No name is set initially
76+
XCTAssertEqual(Thread.current.name, "")
7777
XCTAssertEqual(Thread.current._name, "")
7878
#endif
7979
Thread.current.name = "mainThread"
8080
XCTAssertEqual(Thread.mainThread.name, "mainThread")
81+
XCTAssertEqual(Thread.mainThread._name, "mainThread")
8182

8283
let condition = NSCondition()
8384
condition.lock()

0 commit comments

Comments
 (0)