Skip to content

Commit f28d8da

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 f28d8da

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

Foundation/Thread.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@ open class Thread : NSObject {
255255
}
256256
}
257257

258+
public var _name: String? {
259+
var buffer: [Int8] = Array<Int8>(repeating: 0, count: 128)
260+
if _CFThreadGetName(&buffer, Int32(buffer.count)) == 0 {
261+
return nil
262+
}
263+
return String(cString: buffer)
264+
}
265+
258266
#if os(Windows)
259267
open var stackSize: Int {
260268
get {

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)