From 30b56baec51b70d5b10f1f14ff47327ecc8ebbd0 Mon Sep 17 00:00:00 2001 From: Chris Dickens Date: Fri, 27 Mar 2020 00:03:41 -0700 Subject: threads_posix: Improve usbi_get_tid() for various platforms 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 --- Xcode/config.h | 20 +++++++++++++----- android/config.h | 3 +++ configure.ac | 1 + libusb/os/threads_posix.c | 52 +++++++++++++++++++++++++++++++++++++---------- libusb/version_nano.h | 2 +- 5 files changed, 61 insertions(+), 17 deletions(-) diff --git a/Xcode/config.h b/Xcode/config.h index 1815715..49aabfd 100644 --- a/Xcode/config.h +++ b/Xcode/config.h @@ -1,11 +1,6 @@ /* config.h. Manually generated for Xcode. */ -/* On 10.12 and later, use newly available clock_*() functions */ #include -#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 diff --git a/android/config.h b/android/config.h index 9b95bb2..060940f 100644 --- a/android/config.h +++ b/android/config.h @@ -29,6 +29,9 @@ /* Define to 1 if you have the 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 diff --git a/configure.ac b/configure.ac index e8c0023..fb2369a 100644 --- a/configure.ac +++ b/configure.ac @@ -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" ;; diff --git a/libusb/os/threads_posix.c b/libusb/os/threads_posix.c index c18bc7a..763cd84 100644 --- a/libusb/os/threads_posix.c +++ b/libusb/os/threads_posix.c @@ -23,11 +23,19 @@ #if defined(__ANDROID__) # include -#elif defined(__linux__) || defined(__OpenBSD__) -# if defined(__OpenBSD__) -# define _BSD_SOURCE -# endif +#elif defined(__HAIKU__) +# include +#elif defined(__linux__) +# include +# include +#elif defined(__NetBSD__) +# include +#elif defined(__OpenBSD__) +# define _BSD_SOURCE # include +# include +#elif defined(__sun__) +# include #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; } diff --git a/libusb/version_nano.h b/libusb/version_nano.h index b362d2c..ea2459a 100644 --- a/libusb/version_nano.h +++ b/libusb/version_nano.h @@ -1 +1 @@ -#define LIBUSB_NANO 11475 +#define LIBUSB_NANO 11476 -- cgit v1.2.1