Skip to content

Commit 1f0a55f

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 a515417 commit 1f0a55f

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
@@ -1627,6 +1627,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
16271627
return 0;
16281628
#elif TARGET_OS_LINUX
16291629
return pthread_setname_np(thread, name);
1630+
#elif TARGET_OS_BSD
1631+
pthread_set_name_np(thread, name);
1632+
return 0;
16301633
#endif
16311634
}
16321635

@@ -1646,6 +1649,9 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
16461649
return 0;
16471650
#elif TARGET_OS_LINUX
16481651
return pthread_getname_np(pthread_self(), buf, length);
1652+
#elif TARGET_OS_BSD
1653+
pthread_get_name_np(pthread_self(), buf, length);
1654+
return 0;
16491655
#elif TARGET_OS_WIN32
16501656
*buf = '\0';
16511657

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)