Snap for 12342105 from a168df35dea93612cc63f41f0b5d3e1d02377b56 to 24Q4-release

Change-Id: Ic363cd80e6036e6a9798a84d1bc58d1e3f68e8b7
diff --git a/libutils/Threads.cpp b/libutils/Threads.cpp
index 0b96ab0..d8d75ac 100644
--- a/libutils/Threads.cpp
+++ b/libutils/Threads.cpp
@@ -60,7 +60,7 @@
  * 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 @@
 };
 #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<libutil_thread_data*>(arg);
+  int result = data_ptr->entry_func(data_ptr->entry_func_arg);
+  delete data_ptr;
+  return reinterpret_cast<void*>(static_cast<uintptr_t>(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 @@
 
     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"