Skip to content

[android] Enable Thread tests were possible. #2181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion CoreFoundation/Base.subproj/CFPlatform.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@

#endif

#if TARGET_OS_ANDROID
#include <sys/prctl.h>
#endif

#if TARGET_OS_MAC || TARGET_OS_WIN32
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
#else
Expand Down Expand Up @@ -1495,6 +1499,17 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
#if TARGET_OS_MAC
return pthread_getname_np(pthread_self(), buf, length);
#elif TARGET_OS_ANDROID
// Android did not get pthread_getname_np until API 26, but prctl seems to
// return at most 15 chars of the name + null terminator.
char *buffer[16] = {0};
if (prctl(PR_GET_NAME, buffer, 0, 0, 0) != 0) {
return -1;
}
size_t sz = MIN(strnlen(buffer, 15), length - 1);
memcpy(buf, buffer, sz);
buf[sz] = 0;
return 0;
#elif TARGET_OS_LINUX
return pthread_getname_np(pthread_self(), buf, length);
#endif
Expand Down Expand Up @@ -1532,4 +1547,3 @@ CF_CROSS_PLATFORM_EXPORT void *_CFReallocf(void *ptr, size_t size) {
}

#endif

20 changes: 11 additions & 9 deletions TestFoundation/TestThread.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,20 @@

class TestThread : XCTestCase {
static var allTests: [(String, (TestThread) -> () throws -> Void)] {
#if os(Android)
return []
#endif

var tests: [(String, (TestThread) -> () throws -> Void)] = [
("test_currentThread", test_currentThread ),
("test_currentThread", test_currentThread),
("test_threadStart", test_threadStart),
("test_mainThread", test_mainThread),
("test_callStackSymbols", test_callStackSymbols),
("test_callStackReurnAddresses", test_callStackReturnAddresses),
]

#if !os(Android)
// Android doesn't support backtraces at the moment.
tests.append(contentsOf: [
("test_callStackSymbols", test_callStackSymbols),
("test_callStackReturnAddresses", test_callStackReturnAddresses),
])
#endif

#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
tests.append(contentsOf: [
("test_threadName", test_threadName),
Expand Down Expand Up @@ -68,7 +70,7 @@ class TestThread : XCTestCase {

#if NS_FOUNDATION_ALLOWS_TESTABLE_IMPORT
func test_threadName() {
#if os(Linux) // Linux sets the initial thread name to the process name.
#if os(Linux) || os(Android) // Linux sets the initial thread name to the process name.
XCTAssertEqual(Thread.current.name, "TestFoundation")
XCTAssertEqual(Thread.current._name, "TestFoundation")
#else
Expand All @@ -94,7 +96,7 @@ class TestThread : XCTestCase {
XCTAssertEqual(Thread.current.name, "12345678901234567890")
#if os(macOS) || os(iOS)
XCTAssertEqual(Thread.current._name, Thread.current.name)
#elseif os(Linux)
#elseif os(Linux) || os(Android)
// pthread_setname_np() only allows 15 characters on Linux, so setting it fails
// and the previous name will still be there.
XCTAssertEqual(Thread.current._name, "Thread2-2")
Expand Down