Skip to content

Commit 17789b5

Browse files
authored
Merge pull request #1279 from spevans/pr_thread_name
2 parents f23d554 + 0021cb6 commit 17789b5

File tree

4 files changed

+69
-26
lines changed

4 files changed

+69
-26
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,15 +1316,18 @@ _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (*
13161316
return thread;
13171317
}
13181318

1319-
CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
1319+
CF_SWIFT_EXPORT int _CFThreadSetName(pthread_t thread, const char *_Nonnull name) {
13201320
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
1321-
pthread_setname_np(name);
1321+
if (pthread_equal(pthread_self(), thread)) {
1322+
return pthread_setname_np(name);
1323+
}
1324+
return EINVAL;
13221325
#elif DEPLOYMENT_TARGET_LINUX
1323-
pthread_setname_np(pthread_self(), name);
1326+
return pthread_setname_np(thread, name);
13241327
#endif
13251328
}
13261329

1327-
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length) {
1330+
CF_SWIFT_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length) {
13281331
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
13291332
return pthread_getname_np(pthread_self(), buf, length);
13301333
#elif DEPLOYMENT_TARGET_ANDROID

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,8 +316,8 @@ typedef pthread_t _CFThreadRef;
316316

317317
CF_EXPORT _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (* _Nonnull startfn)(void *_Nullable), void *restrict _Nullable context);
318318

319-
CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name);
320-
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length);
319+
CF_SWIFT_EXPORT int _CFThreadSetName(pthread_t thread, const char *_Nonnull name);
320+
CF_SWIFT_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length);
321321

322322
CF_EXPORT Boolean _CFCharacterSetIsLongCharacterMember(CFCharacterSetRef theSet, UTF32Char theChar);
323323
CF_EXPORT CFCharacterSetRef _CFCharacterSetCreateCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet);

Foundation/Thread.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ internal enum _NSThreadStatus {
4343
private func NSThreadStart(_ context: UnsafeMutableRawPointer?) -> UnsafeMutableRawPointer? {
4444
let thread: Thread = NSObject.unretainedReference(context!)
4545
Thread._currentThread.set(thread)
46+
if let name = thread.name {
47+
_CFThreadSetName(pthread_self(), name)
48+
}
4649
thread._status = .executing
4750
thread.main()
4851
thread._status = .finished
@@ -141,11 +144,12 @@ open class Thread : NSObject {
141144
}
142145

143146
internal var _main: () -> Void = {}
144-
#if os(OSX) || os(iOS) || CYGWIN
145-
private var _thread: pthread_t? = nil
146-
#elseif os(Linux) || os(Android)
147+
#if os(Android)
147148
private var _thread = pthread_t()
149+
#else
150+
private var _thread: pthread_t? = nil
148151
#endif
152+
149153
#if CYGWIN
150154
internal var _attr : pthread_attr_t? = nil
151155
#else
@@ -202,8 +206,8 @@ open class Thread : NSObject {
202206

203207
open var name: String? {
204208
didSet {
205-
if _thread == Thread.current._thread {
206-
_CFThreadSetName(name)
209+
if let thread = _thread {
210+
_CFThreadSetName(thread, name ?? "" )
207211
}
208212
}
209213
}

TestFoundation/TestThread.swift

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,70 @@ class TestThread : XCTestCase {
5959
}
6060

6161
func test_threadName() {
62-
let thread = Thread()
63-
XCTAssertNil(thread.name)
6462

65-
func getPThreadName() -> String? {
66-
var buf = [Int8](repeating: 0, count: 16)
63+
// Compare the name set in pthreads()
64+
func compareThreadName(to name: String) {
65+
var buf = [Int8](repeating: 0, count: 128)
66+
#if os(OSX) || os(iOS)
67+
// Dont use _CF functions on macOS as it will break testing with Darwin's native Foundation.
68+
let r = pthread_getname_np(pthread_self(), &buf, buf.count)
69+
#else
6770
let r = _CFThreadGetName(&buf, Int32(buf.count))
68-
69-
guard r == 0 else {
70-
return nil
71+
#endif
72+
if r == 0 {
73+
XCTAssertEqual(String(cString: buf), name)
74+
} else {
75+
XCTFail("Cant get thread name")
7176
}
72-
return String(cString: buf)
7377
}
7478

79+
// No name is set initially
80+
XCTAssertNil(Thread.current.name)
81+
82+
#if os(Linux) // Linux sets the initial thread name to the process name.
83+
compareThreadName(to: "TestFoundation")
84+
#else
85+
compareThreadName(to: "")
86+
#endif
87+
Thread.current.name = "mainThread"
88+
XCTAssertEqual(Thread.mainThread.name, "mainThread")
89+
90+
let condition = NSCondition()
91+
condition.lock()
92+
7593
let thread2 = Thread() {
76-
Thread.current.name = "Thread2"
77-
XCTAssertEqual(Thread.current.name, "Thread2")
78-
XCTAssertEqual(Thread.current.name, getPThreadName())
94+
XCTAssertEqual(Thread.current.name, "Thread2-1")
95+
compareThreadName(to: "Thread2-1")
96+
97+
Thread.current.name = "Thread2-2"
98+
XCTAssertEqual(Thread.current.name, "Thread2-2")
99+
compareThreadName(to: "Thread2-2")
100+
101+
Thread.current.name = "12345678901234567890"
102+
XCTAssertEqual(Thread.current.name, "12345678901234567890")
103+
#if os(OSX) || os(iOS)
104+
compareThreadName(to: "12345678901234567890")
105+
#elseif os(Linux)
106+
// pthread_setname_np() only allows 15 characters on Linux, so setting it fails
107+
// and the previous name will still be there.
108+
compareThreadName(to: "Thread2-2")
109+
#endif
110+
condition.lock()
111+
condition.signal()
112+
condition.unlock()
79113
}
80-
114+
thread2.name = "Thread2-1"
81115
thread2.start()
82116

83-
Thread.current.name = "CurrentThread"
84-
XCTAssertEqual(Thread.current.name, getPThreadName())
117+
// Allow 1 second for thread2 to finish
118+
XCTAssertTrue(condition.wait(until: Date(timeIntervalSinceNow: 1)))
119+
condition.unlock()
85120

121+
XCTAssertEqual(Thread.current.name, "mainThread")
122+
XCTAssertEqual(Thread.mainThread.name, "mainThread")
86123
let thread3 = Thread()
87124
thread3.name = "Thread3"
88125
XCTAssertEqual(thread3.name, "Thread3")
89-
XCTAssertNotEqual(thread3.name, getPThreadName())
90126
}
91127

92128
func test_mainThread() {

0 commit comments

Comments
 (0)