Skip to content

Commit e1749e2

Browse files
committed
Thread: make Thread.name return the current thread name
This makes the `name` property use the CoreFoundation SPI to query the thread name to avoid having to have this interface be used directly in the tests.
1 parent 30ca0a3 commit e1749e2

File tree

2 files changed

+8
-12
lines changed

2 files changed

+8
-12
lines changed

Foundation/Thread.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,13 @@ open class Thread : NSObject {
253253
_CFThreadSetName(thread, name ?? "" )
254254
}
255255
}
256+
get {
257+
var buffer: [Int8] = Array<Int8>(repeating: 0, count: 128)
258+
if _CFThreadGetName(&buf, Int32(buf.count)) {
259+
return String(cString: buffer)
260+
}
261+
return nil
262+
}
256263
}
257264

258265
#if os(Windows)

TestFoundation/TestThread.swift

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,7 @@ class TestThread : XCTestCase {
5454

5555
// Compare the name set in pthreads()
5656
func compareThreadName(to name: String) {
57-
var buf = [Int8](repeating: 0, count: 128)
58-
#if os(macOS) || os(iOS)
59-
// Dont use _CF functions on macOS as it will break testing with Darwin's native Foundation.
60-
let r = pthread_getname_np(pthread_self(), &buf, buf.count)
61-
#else
62-
let r = _CFThreadGetName(&buf, Int32(buf.count))
63-
#endif
64-
if r == 0 {
65-
XCTAssertEqual(String(cString: buf), name)
66-
} else {
67-
XCTFail("Cant get thread name")
68-
}
57+
XCTAssertEqual(Thread.current.name, name)
6958
}
7059

7160
// No name is set initially

0 commit comments

Comments
 (0)