Skip to content

Commit 7c2585f

Browse files
committed
Thread: Fixes to thread name
- _CFThreadSetName(): Take the thread ID, as Linux allows setting the name of another thread. On macOS just check that it is the same as pthread_self(). - On Linux, trim the name to 15 characters as that is the maximum that can be set. - When starting a thread, set it's name in pthreads if the name is non-nil. - On Linux, make _thread an Optional initialised to nil instead of setting it to pthread_t(), which gives it the value 0. This matches macOS behaviour. - TestThread.swift: Add some more tests.
1 parent f34b11f commit 7c2585f

File tree

4 files changed

+72
-26
lines changed

4 files changed

+72
-26
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1316,15 +1316,22 @@ _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+
// pthread_setname_np will fail if name >= 16 characters
1327+
char short_name[16];
1328+
strncpy(short_name, name, 15);
1329+
short_name[15] = '\0';
1330+
return pthread_setname_np(thread, short_name);
13241331
#endif
13251332
}
13261333

1327-
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length) {
1334+
CF_SWIFT_EXPORT int _CFThreadGetName(char *_Nonnull buf, int length) {
13281335
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
13291336
return pthread_getname_np(pthread_self(), buf, length);
13301337
#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, 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: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,34 +59,69 @@ 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
107+
compareThreadName(to: "123456789012345")
108+
#endif
109+
condition.lock()
110+
condition.signal()
111+
condition.unlock()
79112
}
80-
113+
thread2.name = "Thread2-1"
81114
thread2.start()
82115

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

120+
XCTAssertEqual(Thread.current.name, "mainThread")
121+
XCTAssertEqual(Thread.mainThread.name, "mainThread")
86122
let thread3 = Thread()
87123
thread3.name = "Thread3"
88124
XCTAssertEqual(thread3.name, "Thread3")
89-
XCTAssertNotEqual(thread3.name, getPThreadName())
90125
}
91126

92127
func test_mainThread() {

0 commit comments

Comments
 (0)