Skip to content

Commit 51fccb9

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 3034436 commit 51fccb9

File tree

3 files changed

+10
-5
lines changed

3 files changed

+10
-5
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1624,6 +1624,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
16241624
return 0;
16251625
#elif TARGET_OS_LINUX
16261626
return pthread_setname_np(thread, name);
1627+
#elif TARGET_OS_BSD
1628+
pthread_set_name_np(thread, name);
1629+
return 0;
16271630
#endif
16281631
}
16291632

@@ -1643,6 +1646,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
16431646
return 0;
16441647
#elif TARGET_OS_LINUX
16451648
return pthread_getname_np(pthread_self(), buf, length);
1649+
#elif TARGET_OS_BSD
1650+
pthread_get_name_np(pthread_self(), buf, length);
1651+
return 0;
16461652
#elif TARGET_OS_WIN32
16471653
*buf = '\0';
16481654

CoreFoundation/Stream.subproj/CFStream.c

Lines changed: 1 addition & 5 deletions
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

Lines changed: 3 additions & 0 deletions
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)