threads_posix: Improve usbi_get_tid() for various platforms
authorChris Dickens <christopher.a.dickens@gmail.com>
Fri, 27 Mar 2020 07:03:41 +0000 (00:03 -0700)
committerChris Dickens <christopher.a.dickens@gmail.com>
Fri, 27 Mar 2020 07:03:41 +0000 (00:03 -0700)
Add support for real thread IDs on macOS 10.6 and later using the new
pthread_threadid_np() function.

Add support for thread IDs on Haiku, NetBSD and Solaris.

Provide a fallback value other than -1 when direct support is not
available. This should suffice as a unique identifier since pthread_t,
while opaque, is still distinct amongst active threads.

Signed-off-by: Chris Dickens <christopher.a.dickens@gmail.com>
Xcode/config.h
android/config.h
configure.ac
libusb/os/threads_posix.c
libusb/version_nano.h

index 1815715..49aabfd 100644 (file)
@@ -1,11 +1,6 @@
 /* config.h.  Manually generated for Xcode.  */
 
-/* On 10.12 and later, use newly available clock_*() functions */
 #include <AvailabilityMacros.h>
-#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
-/* Define to 1 if you have the `clock_gettime' function. */
-#define HAVE_CLOCK_GETTIME 1
-#endif
 
 /* Default visibility */
 #define DEFAULT_VISIBILITY /**/
@@ -13,6 +8,21 @@
 /* Message logging */
 #define ENABLE_LOGGING 1
 
+/* Define to 1 if the compiler supports _Thread_local. */
+#define HAVE_CC_THREAD_LOCAL 1
+
+/* On 10.12 and later, use newly available clock_*() functions */
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 101200
+/* Define to 1 if you have the `clock_gettime' function. */
+#define HAVE_CLOCK_GETTIME 1
+#endif
+
+/* On 10.6 and later, use newly available pthread_threadid_np() function */
+#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
+/* Define to 1 if you have the 'pthread_threadid_np' function. */
+#define HAVE_PTHREAD_THREADID_NP 1
+#endif
+
 /* Define to 1 if the system has the type `nfds_t'. */
 #define HAVE_NFDS_T 1
 
index 9b95bb2..060940f 100644 (file)
@@ -29,6 +29,9 @@
 /* Define to 1 if you have the <asm/types.h> header file. */
 #define HAVE_ASM_TYPES_H 1
 
+/* Define to 1 if the compiler supports _Thread_local. */
+#define HAVE_CC_THREAD_LOCAL 1
+
 /* Define to 1 if the system has the type `nfds_t'. */
 #define HAVE_NFDS_T 1
 
index e8c0023..fb2369a 100644 (file)
@@ -117,6 +117,7 @@ esac
 case $backend in
 darwin)
        AC_DEFINE([OS_DARWIN], [1], [Darwin backend])
+       AC_CHECK_FUNCS([pthread_threadid_np])
        LIBS="-lobjc -Wl,-framework,IOKit -Wl,-framework,CoreFoundation"
        LTLDFLAGS="${LTLDFLAGS} -Wl,-prebind"
        ;;
index c18bc7a..763cd84 100644 (file)
 
 #if defined(__ANDROID__)
 # include <unistd.h>
-#elif defined(__linux__) || defined(__OpenBSD__)
-# if defined(__OpenBSD__)
-#  define _BSD_SOURCE
-# endif
+#elif defined(__HAIKU__)
+# include <os/kernel/OS.h>
+#elif defined(__linux__)
+# include <sys/syscall.h>
+# include <unistd.h>
+#elif defined(__NetBSD__)
+# include <lwp.h>
+#elif defined(__OpenBSD__)
+# define _BSD_SOURCE
 # include <sys/syscall.h>
+# include <unistd.h>
+#elif defined(__sun__)
+# include <sys/lwp.h>
 #endif
 
 int usbi_cond_timedwait(pthread_cond_t *cond,
@@ -63,19 +71,41 @@ int usbi_get_tid(void)
 
 #if defined(__ANDROID__)
        tid = gettid();
+#elif defined(__APPLE__)
+#ifdef HAVE_PTHREAD_THREADID_NP
+       uint64_t thread_id;
+
+       if (pthread_threadid_np(NULL, &thread_id) == 0)
+               tid = (int)thread_id;
+       else
+               tid = -1;
+#else
+       tid = (int)pthread_mach_thread_np(pthread_self());
+#endif
+#elif defined(__HAIKU__)
+       tid = get_pthread_thread_id(pthread_self());
 #elif defined(__linux__)
-       tid = syscall(SYS_gettid);
+       tid = (int)syscall(SYS_gettid);
+#elif defined(__NetBSD__)
+       tid = _lwp_self();
 #elif defined(__OpenBSD__)
        /* The following only works with OpenBSD > 5.1 as it requires
-          real thread support. For 5.1 and earlier, -1 is returned. */
+        * real thread support. For 5.1 and earlier, -1 is returned. */
        tid = syscall(SYS_getthrid);
-#elif defined(__APPLE__)
-       tid = (int)pthread_mach_thread_np(pthread_self());
-#elif defined(__CYGWIN__)
-       tid = GetCurrentThreadId();
+#elif defined(__sun__)
+       tid = _lwp_self();
+#elif defined(_WIN32)
+       tid = (int)GetCurrentThreadId();
 #else
        tid = -1;
 #endif
-/* TODO: NetBSD thread ID support */
+
+       if (tid == -1) {
+               /* If we don't have a thread ID, at least return a unique
+                * value that can be used to distinguish individual
+                * threads. */
+               tid = (int)(intptr_t)pthread_self();
+       }
+
        return tid;
 }
index b362d2c..ea2459a 100644 (file)
@@ -1 +1 @@
-#define LIBUSB_NANO 11475
+#define LIBUSB_NANO 11476