Skip to content

Foundation: correctly read thread name #2309

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
May 28, 2019
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
7 changes: 5 additions & 2 deletions Foundation/Thread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,12 @@ open class Thread : NSObject {
}

open var name: String? {
didSet {
get {
return _name
}
set {
if let thread = _thread {
_CFThreadSetName(thread, name ?? "" )
_CFThreadSetName(thread, newValue ?? "" )
}
}
}
Expand Down
7 changes: 4 additions & 3 deletions TestFoundation/TestThread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,17 @@ class TestThread : XCTestCase {

#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
func test_threadName() {
// No name is set initially
XCTAssertNil(Thread.current.name)

#if os(Linux) // Linux sets the initial thread name to the process name.
XCTAssertEqual(Thread.current.name, "TestFoundation")
XCTAssertEqual(Thread.current._name, "TestFoundation")
#else
// No name is set initially
XCTAssertEqual(Thread.current.name, "")
XCTAssertEqual(Thread.current._name, "")
#endif
Thread.current.name = "mainThread"
XCTAssertEqual(Thread.mainThread.name, "mainThread")
XCTAssertEqual(Thread.mainThread._name, "mainThread")

let condition = NSCondition()
condition.lock()
Expand Down