Skip to content

Commit 830d552

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 2b3a0af commit 830d552

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636

3737
#endif
3838

39+
#if TARGET_OS_ANDROID
40+
#include <sys/prctl.h>
41+
#endif
42+
3943
#if TARGET_OS_MAC || TARGET_OS_WIN32
4044
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
4145
#else
@@ -1495,6 +1499,15 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
14951499
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
14961500
#if TARGET_OS_MAC
14971501
return pthread_getname_np(pthread_self(), buf, length);
1502+
#elif TARGET_OS_ANDROID
1503+
// Android did not get pthread_getname_np until API 26, but prctl seems to
1504+
// return at least 15 chars of the name.
1505+
char *buffer[16] = {0};
1506+
if (prctl(PR_GET_NAME, buffer, 0, 0, 0) != 0) {
1507+
return -1;
1508+
}
1509+
memcpy(buf, buffer, MIN(strnlen(buffer, 16), length));
1510+
return 0;
14981511
#elif TARGET_OS_LINUX
14991512
return pthread_getname_np(pthread_self(), buf, length);
15001513
#endif
@@ -1532,4 +1545,3 @@ CF_CROSS_PLATFORM_EXPORT void *_CFReallocf(void *ptr, size_t size) {
15321545
}
15331546

15341547
#endif
1535-

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),
@@ -68,7 +70,7 @@ class TestThread : XCTestCase {
6870

6971
#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
7072
func test_threadName() {
71-
#if os(Linux) // Linux sets the initial thread name to the process name.
73+
#if os(Linux) || os(Android) // Linux sets the initial thread name to the process name.
7274
XCTAssertEqual(Thread.current.name, "TestFoundation")
7375
XCTAssertEqual(Thread.current._name, "TestFoundation")
7476
#else
@@ -94,7 +96,7 @@ class TestThread : XCTestCase {
9496
XCTAssertEqual(Thread.current.name, "12345678901234567890")
9597
#if os(macOS) || os(iOS)
9698
XCTAssertEqual(Thread.current._name, Thread.current.name)
97-
#elseif os(Linux)
99+
#elseif os(Linux) || os(Android)
98100
// pthread_setname_np() only allows 15 characters on Linux, so setting it fails
99101
// and the previous name will still be there.
100102
XCTAssertEqual(Thread.current._name, "Thread2-2")

0 commit comments

Comments
 (0)