qemu/thread.h: Update to 2.0.0

Change-Id: I72cc746271af3b9da04d7e0b43f8efb5c80cc840
diff --git a/android-configure.sh b/android-configure.sh
index 0b8d31a..41edc36 100755
--- a/android-configure.sh
+++ b/android-configure.sh
@@ -519,6 +519,24 @@
 feature_check_header HAVE_MACHINE_BSWAP_H "<machine/bswap.h>"
 feature_check_header HAVE_FNMATCH_H       "<fnmatch.h>"
 
+# Check whether we have pthread_set_name_np() on Linux
+case $TARGET_OS in
+    linux-*)
+        OLD_LDFLAGS=$LDFLAGS
+        LDFLAGS="$LDFLAGS -lpthread"
+        cat > $TMPC <<EOF
+#define _GNU_SOURCE 1
+#include <pthread.h>
+
+int main(void) {
+    pthread_setname_np(pthread_self(), "dummy");
+    return 0;
+}
+EOF
+        feature_run_exec HAVE_PTHREAD_SETNAME_NP
+        LDFLAGS=$OLD_LDFLAGS
+        ;;
+esac
 # check for Mingw version.
 MINGW_VERSION=
 if [ "$TARGET_OS" = "windows" ]; then
@@ -652,6 +670,9 @@
 if [ "$HAVE_FNMATCH_H" = "yes" ] ; then
   echo "#define CONFIG_FNMATCH  1" >> $config_h
 fi
+if [ "$HAVE_PTHREAD_SETNAME_NP" = "yes" ]; then
+  echo "#define CONFIG_PTHREAD_SETNAME_NP 1" >> $config_h
+fi
 echo "#define CONFIG_GDBSTUB  1" >> $config_h
 echo "#define CONFIG_SLIRP    1" >> $config_h
 echo "#define CONFIG_SKINS    1" >> $config_h
diff --git a/android/build/common.sh b/android/build/common.sh
index b859862..45f1096 100644
--- a/android/build/common.sh
+++ b/android/build/common.sh
@@ -366,16 +366,14 @@
 feature_check_compile ()
 {
     local result_cc=yes
-    local OLD_CFLAGS
-    OLD_CFLAGS="$CFLAGS"
-    CFLAGS="$CFLAGS $EXTRA_CFLAGS"
-    compile
-    if [ $? != 0 ] ; then
-        result_cc=no
-    fi
+    (
+      set -e
+      CFLAGS="$CFLAGS $EXTRA_CFLAGS"
+      compile
+    ) || result_cc=no
     eval $1=$result_cc
     EXTRA_CFLAGS=
-    CFLAGS=$OLD_CFLAGS
+    clean_temp
 }
 
 # check that a given C program $TMPC can be linked on the host system
@@ -387,23 +385,16 @@
 feature_check_link ()
 {
     local result_cl=yes
-    local OLD_CFLAGS OLD_LDFLAGS
-    OLD_CFLAGS=$CFLAGS
-    OLD_LDFLAGS=$LDFLAGS
-    CFLAGS="$CFLAGS $EXTRA_CFLAGS"
-    LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
-    compile
-    if [ $? != 0 ] ; then
-        result_cl=no
-    else
-        link
-        if [ $? != 0 ] ; then
-            result_cl=no
-        fi
-    fi
-    CFLAGS=$OLD_CFLAGS
-    LDFLAGS=$OLD_LDFLAGS
+    (
+      set -e
+      CFLAGS="$CFLAGS $EXTRA_CFLAGS"
+      LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
+      compile
+      link
+    ) || result_cl=no
     eval $1=$result_cl
+    EXTRA_CFLAGS=
+    EXTRA_LDFLAGS=
     clean_temp
 }
 
@@ -424,9 +415,6 @@
 EOF
     feature_check_compile result_ch
     eval $1=$result_ch
-    #eval result=$`echo $1`
-    #log  "Host       : $1=$result_ch"
-    clean_temp
 }
 
 # run the test program that is in $TMPC and set its exit status
@@ -435,17 +423,16 @@
 #
 feature_run_exec ()
 {
-    local run_exec_result
-    local OLD_CFLAGS="$CFLAGS"
-    local OLD_LDFLAGS="$LDFLAGS"
-    CFLAGS="$CFLAGS $EXTRA_CFLAGS"
-    LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
-    compile_exec_run
-    run_exec_result=$?
-    CFLAGS="$OLD_CFLAGS"
-    LDFLAGS="$OLD_LDFLAGS"
+    local run_exec_result=yes
+    (
+        CFLAGS="$CFLAGS $EXTRA_CFLAGS"
+        LDFLAGS="$LDFLAGS $EXTRA_LDFLAGS"
+        compile_exec_run >/dev/null 2>&1
+    ) || run_exec_result=no
     eval $1=$run_exec_result
     log "Host       : $1=$run_exec_result"
+    EXTRA_CFLAGS=
+    EXTRA_LDFLAGS=
     clean_temp
 }
 
diff --git a/include/qemu/thread.h b/include/qemu/thread.h
index 6ff266a..b0692de 100644
--- a/include/qemu/thread.h
+++ b/include/qemu/thread.h
@@ -54,12 +54,13 @@
 void qemu_event_wait(QemuEvent *ev);
 void qemu_event_destroy(QemuEvent *ev);
 
-void qemu_thread_create(QemuThread *thread,
+void qemu_thread_create(QemuThread *thread, const char *name,
                         void *(*start_routine)(void *),
                         void *arg, int mode);
 void *qemu_thread_join(QemuThread *thread);
 void qemu_thread_get_self(QemuThread *thread);
 bool qemu_thread_is_self(QemuThread *thread);
 void qemu_thread_exit(void *retval);
+void qemu_thread_naming(bool enable);
 
 #endif
diff --git a/util/compatfd.c b/util/compatfd.c
index e2ba454..3fefe18 100644
--- a/util/compatfd.c
+++ b/util/compatfd.c
@@ -93,7 +93,8 @@
     memcpy(&info->mask, mask, sizeof(*mask));
     info->fd = fds[1];
 
-    qemu_thread_create(&thread, sigwait_compat, info, QEMU_THREAD_DETACHED);
+    qemu_thread_create(&thread, "signalfd_compat", sigwait_compat, info,
+                       QEMU_THREAD_DETACHED);
 
     return fds[0];
 }
diff --git a/util/qemu-thread-posix.c b/util/qemu-thread-posix.c
index 37dd298..3de6908 100644
--- a/util/qemu-thread-posix.c
+++ b/util/qemu-thread-posix.c
@@ -27,6 +27,19 @@
 #include "qemu/thread.h"
 #include "qemu/atomic.h"
 
+static bool name_threads;
+
+void qemu_thread_naming(bool enable)
+{
+    name_threads = enable;
+
+#ifndef CONFIG_THREAD_SETNAME_BYTHREAD
+    /* This is a debugging option, not fatal */
+    if (enable) {
+        fprintf(stderr, "qemu: thread naming not supported on this host\n");
+    }
+#endif
+}
 static void error_exit(int err, const char *msg)
 {
     fprintf(stderr, "qemu: %s: %s\n", msg, strerror(err));
@@ -387,8 +400,17 @@
     }
 }
 
+/* Attempt to set the threads name; note that this is for debug, so
+ * we're not going to fail if we can't set it.
+ */
+static void qemu_thread_set_name(QemuThread *thread, const char *name)
+{
+#ifdef CONFIG_PTHREAD_SETNAME_NP
+    pthread_setname_np(thread->thread, name);
+#endif
+}
 
-void qemu_thread_create(QemuThread *thread,
+void qemu_thread_create(QemuThread *thread, const char *name,
                        void *(*start_routine)(void*),
                        void *arg, int mode)
 {
@@ -413,6 +435,9 @@
     err = pthread_create(&thread->thread, &attr, start_routine, arg);
     if (err)
         error_exit(err, __func__);
+    if (name_threads) {
+        qemu_thread_set_name(thread, name);
+    }
 
     pthread_sigmask(SIG_SETMASK, &oldset, NULL);
 
diff --git a/util/qemu-thread-win32.c b/util/qemu-thread-win32.c
index 27a5217..8a0379a 100644
--- a/util/qemu-thread-win32.c
+++ b/util/qemu-thread-win32.c
@@ -16,6 +16,15 @@
 #include <assert.h>
 #include <limits.h>
 
+static bool name_threads;
+
+void qemu_thread_naming(bool enable)
+{
+    /* But note we don't actually name them on Windows yet */
+    name_threads = enable;
+
+    fprintf(stderr, "qemu: thread naming not supported on this host\n");
+}
 static void error_exit(int err, const char *msg)
 {
     char *pstr;
@@ -325,7 +334,7 @@
     return ret;
 }
 
-void qemu_thread_create(QemuThread *thread,
+void qemu_thread_create(QemuThread *thread, const char *name,
                        void *(*start_routine)(void *),
                        void *arg, int mode)
 {