Skip to content

Commit 2efa9e9

Browse files
committed
[android] Enable Thread tests were possible.
Enable most of the Thread test for Android. The only tests that cannot pass right now are the ones related to backtraces, which do not use the Linux API backtrace and would need an specific implementation. And while Android did not get pthread_getname_np until API 26, the low level prctl(PR_GET_NAME) works with the same restriction as in Linux (only 15 characters). With that change, the test about thread names also passes in Android.
1 parent 97796f5 commit 2efa9e9

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535

3636
#endif
3737

38+
#if DEPLOYMENT_TARGET_ANDROID
39+
#include <sys/prctl.h>
40+
#endif
41+
3842
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_WINDOWS
3943
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
4044
#else
@@ -1427,6 +1431,15 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
14271431
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
14281432
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
14291433
return pthread_getname_np(pthread_self(), buf, length);
1434+
#elif DEPLOYMENT_TARGET_ANDROID
1435+
// Android did not get pthread_getname_np until API 26, but prctl seems to
1436+
// return at least 15 chars of the name.
1437+
char *buffer[16] = {0};
1438+
if (prctl(PR_GET_NAME, buffer, 0, 0, 0) != 0) {
1439+
return -1;
1440+
}
1441+
memcpy(buf, buffer, MIN(strnlen(buffer, 16), length));
1442+
return 0;
14301443
#elif DEPLOYMENT_TARGET_LINUX
14311444
return pthread_getname_np(pthread_self(), buf, length);
14321445
#endif

TestFoundation/TestThread.swift

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,20 @@
2121

2222
class TestThread : XCTestCase {
2323
static var allTests: [(String, (TestThread) -> () throws -> Void)] {
24-
#if os(Android)
25-
return []
26-
#endif
27-
2824
var tests: [(String, (TestThread) -> () throws -> Void)] = [
29-
("test_currentThread", test_currentThread ),
25+
("test_currentThread", test_currentThread),
3026
("test_threadStart", test_threadStart),
3127
("test_mainThread", test_mainThread),
32-
("test_callStackSymbols", test_callStackSymbols),
33-
("test_callStackReurnAddresses", test_callStackReturnAddresses),
3428
]
3529

30+
#if !os(Android)
31+
// Android doesn't support backtraces at the moment.
32+
tests.append(contentsOf: [
33+
("test_callStackSymbols", test_callStackSymbols),
34+
("test_callStackReturnAddresses", test_callStackReturnAddresses),
35+
])
36+
#endif
37+
3638
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
3739
tests.append(contentsOf: [
3840
("test_threadName", test_threadName),
@@ -71,7 +73,7 @@ class TestThread : XCTestCase {
7173
// No name is set initially
7274
XCTAssertNil(Thread.current.name)
7375

74-
#if os(Linux) // Linux sets the initial thread name to the process name.
76+
#if os(Linux) || os(Android) // Linux sets the initial thread name to the process name.
7577
XCTAssertEqual(Thread.current._name, "TestFoundation")
7678
#else
7779
XCTAssertEqual(Thread.current._name, "")
@@ -93,7 +95,7 @@ class TestThread : XCTestCase {
9395
XCTAssertEqual(Thread.current.name, "12345678901234567890")
9496
#if os(macOS) || os(iOS)
9597
XCTAssertEqual(Thread.current._name, Thread.current.name)
96-
#elseif os(Linux)
98+
#elseif os(Linux) || os(Android)
9799
// pthread_setname_np() only allows 15 characters on Linux, so setting it fails
98100
// and the previous name will still be there.
99101
XCTAssertEqual(Thread.current._name, "Thread2-2")

0 commit comments

Comments
 (0)