Skip to content

Commit 5a9fa82

Browse files
drodriguezzayass
authored andcommitted
[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 ec6cb6e commit 5a9fa82

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

CoreFoundation/Base.subproj/CFPlatform.c

Lines changed: 16 additions & 2 deletions
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 DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI || DEPLOYMENT_TARGET_WINDOWS
3943
#define kCFPlatformInterfaceStringEncoding kCFStringEncodingUTF8
4044
#else
@@ -1416,7 +1420,18 @@ CF_CROSS_PLATFORM_EXPORT int _CFThreadSetName(_CFThreadRef thread, const char *_
14161420
CF_CROSS_PLATFORM_EXPORT int _CFThreadGetName(char *buf, int length) {
14171421
#if DEPLOYMENT_TARGET_MACOSX || DEPLOYMENT_TARGET_EMBEDDED || DEPLOYMENT_TARGET_EMBEDDED_MINI
14181422
return pthread_getname_np(pthread_self(), buf, length);
1419-
#elif DEPLOYMENT_TARGET_LINUX
1423+
#elif TARGET_OS_ANDROID
1424+
// Android did not get pthread_getname_np until API 26, but prctl seems to
1425+
// return at most 15 chars of the name + null terminator.
1426+
char *buffer[16] = {0};
1427+
if (prctl(PR_GET_NAME, buffer, 0, 0, 0) != 0) {
1428+
return -1;
1429+
}
1430+
size_t sz = MIN(strnlen(buffer, 15), length - 1);
1431+
memcpy(buf, buffer, sz);
1432+
buf[sz] = 0;
1433+
return 0;
1434+
#elif TARGET_OS_LINUX
14201435
return pthread_getname_np(pthread_self(), buf, length);
14211436
#endif
14221437
return -1;
@@ -1452,4 +1467,3 @@ CF_CROSS_PLATFORM_EXPORT void *_CFReallocf(void *ptr, size_t size) {
14521467
}
14531468

14541469
#endif
1455-

0 commit comments

Comments
 (0)