Skip to content

Commit b5e006c

Browse files
authored
Merge pull request #807 from TheCodez/implement-nsthread-name
2 parents 727e892 + 3b83376 commit b5e006c

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,23 @@ _CFThreadRef _CFThreadCreate(const _CFThreadAttributes attrs, void *_Nullable (*
13101310
return thread;
13111311
}
13121312

1313+
CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name) {
1314+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
1315+
pthread_setname_np(name);
1316+
#elif DEPLOYMENT_TARGET_LINUX
1317+
pthread_setname_np(pthread_self(), name);
1318+
#endif
1319+
}
1320+
1321+
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length) {
1322+
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
1323+
return pthread_getname_np(pthread_self(), buf, length);
1324+
#elif DEPLOYMENT_TARGET_LINUX
1325+
return pthread_getname_np(pthread_self(), buf, length);
1326+
#endif
1327+
return -1;
1328+
}
1329+
13131330
CF_EXPORT char **_CFEnviron(void) {
13141331
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED
13151332
return *_NSGetEnviron();

CoreFoundation/Base.subproj/ForSwiftFoundationOnly.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,9 @@ typedef pthread_t _CFThreadRef;
298298

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

301+
CF_SWIFT_EXPORT void _CFThreadSetName(const char *_Nullable name);
302+
CF_SWIFT_EXPORT int _CFThreadGetName(char *buf, int length);
303+
301304
CF_EXPORT Boolean _CFCharacterSetIsLongCharacterMember(CFCharacterSetRef theSet, UTF32Char theChar);
302305
CF_EXPORT CFCharacterSetRef _CFCharacterSetCreateCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet);
303306
CF_EXPORT CFMutableCharacterSetRef _CFCharacterSetCreateMutableCopy(CFAllocatorRef alloc, CFCharacterSetRef theSet);

Foundation/NSThread.swift

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ open class Thread : NSObject {
143143
#endif
144144
internal var _status = _NSThreadStatus.initialized
145145
internal var _cancelled = false
146+
internal var _name: String?
146147
/// - Note: this differs from the Darwin implementation in that the keys must be Strings
147148
open var threadDictionary = [String : Any]()
148149

@@ -191,7 +192,16 @@ open class Thread : NSObject {
191192
}
192193

193194
open var name: String? {
194-
NSUnimplemented()
195+
get {
196+
return _name
197+
}
198+
set {
199+
_name = newValue
200+
201+
if _thread == Thread.current._thread {
202+
_CFThreadSetName(_name)
203+
}
204+
}
195205
}
196206

197207
open var stackSize: Int {

TestFoundation/TestNSThread.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
import SwiftXCTest
1717
#endif
1818

19+
import CoreFoundation
1920

2021
class TestNSThread : XCTestCase {
2122
static var allTests: [(String, (TestNSThread) -> () throws -> Void)] {
2223
return [
2324
("test_currentThread", test_currentThread ),
2425
("test_threadStart", test_threadStart),
26+
("test_threadName", test_threadName),
2527
]
2628
}
2729

@@ -51,4 +53,35 @@ class TestNSThread : XCTestCase {
5153
condition.unlock()
5254
XCTAssertTrue(started)
5355
}
56+
57+
func test_threadName() {
58+
let thread = Thread()
59+
XCTAssertNil(thread.name)
60+
61+
func getPThreadName() -> String? {
62+
var buf = [Int8](repeating: 0, count: 16)
63+
let r = _CFThreadGetName(&buf, Int32(buf.count))
64+
65+
guard r == 0 else {
66+
return nil
67+
}
68+
return String(cString: buf)
69+
}
70+
71+
let thread2 = Thread() {
72+
Thread.current.name = "Thread2"
73+
XCTAssertEqual(Thread.current.name, "Thread2")
74+
XCTAssertEqual(Thread.current.name, getPThreadName())
75+
}
76+
77+
thread2.start()
78+
79+
Thread.current.name = "CurrentThread"
80+
XCTAssertEqual(Thread.current.name, getPThreadName())
81+
82+
let thread3 = Thread()
83+
thread3.name = "Thread3"
84+
XCTAssertEqual(thread3.name, "Thread3")
85+
XCTAssertNotEqual(thread3.name, getPThreadName())
86+
}
5487
}

0 commit comments

Comments
 (0)