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"