From be2cc9f93077ad024aa2aee31dd9ad5fa5873ecc Mon Sep 17 00:00:00 2001 From: Aditya Kumar Date: Thu, 5 Sep 2024 14:03:59 -0700 Subject: [PATCH] Fix type of android_thread_func typedef Found the error with latest clang: https://android-build.corp.google.com/artifact/pending/P80448506/art-host-x86_64/latest/view/logs%2Fbuild_error.log ``` out/soong/.intermediates/system/core/libutils/libutils/linux_glibc_x86_64_static/obj/system/core/libutils/Threads.o system/core/libutils/Threads.cpp system/core/libutils/Threads.cpp:149:21: error: cast from 'android_thread_func_t' (aka 'int (*)(void *)') to 'android_pthread_entry' (aka 'void *(*)(void *)') converts to incompatible function type [-Werror,-Wcast-function-type-mismatch] 149 | (android_pthread_entry)entryFunction, userData); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1 error generated. ``` Change-Id: Ie2423a568483c975a36a1fa0b06a3f93a27be5aa --- libutils/Threads.cpp | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/libutils/Threads.cpp b/libutils/Threads.cpp index 0b96ab0bb..d8d75acf4 100644 --- a/libutils/Threads.cpp +++ b/libutils/Threads.cpp @@ -60,7 +60,7 @@ using namespace android; * We create it "detached", so it cleans up after itself. */ -typedef void* (*android_pthread_entry)(void*); +typedef int (*android_pthread_entry)(void*); #if defined(__ANDROID__) struct thread_data_t { @@ -88,6 +88,20 @@ struct thread_data_t { }; #endif +// Adapted from bionic's implmenetation of trampoline to make C11 thrd_create +// work with pthread_create. +struct libutil_thread_data { + android_pthread_entry _Nonnull entry_func; + void* _Nullable entry_func_arg; +}; + +static void* _Nonnull libutil_thread_trampoline(void* _Nonnull arg) { + libutil_thread_data *data_ptr = static_cast(arg); + int result = data_ptr->entry_func(data_ptr->entry_func_arg); + delete data_ptr; + return reinterpret_cast(static_cast(result)); +} + void androidSetThreadName(const char* name) { #if defined(__linux__) // Mac OS doesn't have this, and we build libutil for the host too @@ -145,8 +159,13 @@ int androidCreateRawThreadEtc(android_thread_func_t entryFunction, errno = 0; pthread_t thread; + + libutil_thread_data* pthread_arg = new libutil_thread_data; + pthread_arg->entry_func = entryFunction; + pthread_arg->entry_func_arg = userData; + int result = pthread_create(&thread, &attr, - (android_pthread_entry)entryFunction, userData); + libutil_thread_trampoline, pthread_arg); pthread_attr_destroy(&attr); if (result != 0) { ALOGE("androidCreateRawThreadEtc failed (entry=%p, res=%d, %s)\n"