Skip to content

Commit 9e4d00c

Browse files
committed
Get/set thread name for BSD.
Add the relevant routines for getting and setting thread name in CFPlatform. While we are doing this, change an #if branch in CFStream to set thread name to using the actual routines declared in CFPlatform to do this.
1 parent 702dc5d commit 9e4d00c

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

+6
Original file line numberDiff line numberDiff line change
@@ -1616,6 +1616,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
16161616
return 0;
16171617
#elif TARGET_OS_LINUX
16181618
return pthread_setname_np(thread, name);
1619+
#elif TARGET_OS_BSD
1620+
pthread_set_name_np(thread, name);
1621+
return 0;
16191622
#endif
16201623
}
16211624

@@ -1635,6 +1638,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
16351638
return 0;
16361639
#elif TARGET_OS_LINUX
16371640
return pthread_getname_np(pthread_self(), buf, length);
1641+
#elif TARGET_OS_BSD
1642+
pthread_get_name_np(pthread_self(), buf, length);
1643+
return 0;
16381644
#elif TARGET_OS_WIN32
16391645
*buf = '\0';
16401646

CoreFoundation/Stream.subproj/CFStream.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -1675,11 +1675,7 @@ static void _perform(void* info)
16751675

16761676
static void* _legacyStreamRunLoop_workThread(void* arg)
16771677
{
1678-
#if TARGET_OS_LINUX
1679-
pthread_setname_np(pthread_self(), "com.apple.CFStream.LegacyThread");
1680-
#else
1681-
pthread_setname_np("com.apple.CFStream.LegacyThread");
1682-
#endif
1678+
_CFThreadSetName(pthread_self(), "com.apple.CFStream.LegacyThread");
16831679
sLegacyRL = CFRunLoopGetCurrent();
16841680

16851681
#if defined(LOG_STREAM)

Tests/Foundation/Tests/TestThread.swift

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class TestThread : XCTestCase {
5656
#if os(Linux) || os(Android) // Linux sets the initial thread name to the process name.
5757
XCTAssertEqual(Thread.current.name, "TestFoundation")
5858
testInternalThreadName("TestFoundation")
59+
#elseif os(OpenBSD) // OpenBSD sets the initial thread name to this.
60+
XCTAssertEqual(Thread.current.name, "Original thread")
61+
testInternalThreadName("Original thread")
5962
#else
6063
// No name is set initially
6164
XCTAssertEqual(Thread.current.name, "")

0 commit comments

Comments
 (0)