Skip to content

Commit e84d955

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 990c791 commit e84d955

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
@@ -35,6 +35,10 @@
3535

3636
#endif
3737

38+
#if TARGET_OS_ANDROID
39+
#include <sys/prctl.h>
40+
#endif
41+
3842
#if TARGET_OS_MAC || TARGET_OS_WIN32
3943
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
4044
#else
@@ -1491,6 +1495,15 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
14911495
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
14921496
#if TARGET_OS_MAC
14931497
return pthread_getname_np(pthread_self(), buf, length);
1498+
#elif TARGET_OS_ANDROID
1499+
// Android did not get pthread_getname_np until API 26, but prctl seems to
1500+
// return at least 15 chars of the name.
1501+
char *buffer[16] = {0};
1502+
if (prctl(PR_GET_NAME, buffer, 0, 0, 0) != 0) {
1503+
return -1;
1504+
}
1505+
memcpy(buf, buffer, MIN(strnlen(buffer, 16), length));
1506+
return 0;
14941507
#elif TARGET_OS_LINUX
14951508
return pthread_getname_np(pthread_self(), buf, length);
14961509
#endif
@@ -1528,4 +1541,3 @@ CF_CROSS_PLATFORM_EXPORT void *_CFReallocf(void *ptr, size_t size) {
15281541
}
15291542

15301543
#endif
1531-

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)