diff options
author | Chris Dickens <christopher.a.dickens@gmail.com> | 2020-03-27 00:03:41 -0700 |
---|---|---|
committer | Chris Dickens <christopher.a.dickens@gmail.com> | 2020-03-27 00:03:41 -0700 |
commit | 30b56baec51b70d5b10f1f14ff47327ecc8ebbd0 (patch) | |
tree | 1e5fbc312cff509037a75c81a24bea5708394df5 /libusb | |
parent | 1d67425879c3ae515e97b73df94bf2e7336178b0 (diff) | |
download | libusb-30b56baec51b70d5b10f1f14ff47327ecc8ebbd0.tar.gz |
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 <christopher.a.dickens@gmail.com>
Diffstat (limited to 'libusb')
-rw-r--r-- | libusb/os/threads_posix.c | 52 | ||||
-rw-r--r-- | libusb/version_nano.h | 2 |
2 files changed, 42 insertions, 12 deletions
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 <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; } 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 |