From cd206b3900639c44767cd892bccfbffabf222f67 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Wed, 29 Apr 2015 17:13:32 -0700 Subject: [PATCH] Fix gettid() on Windows. Accidentally had this all hidden by an #ifndef _WIN32 when I wrote it. Bug: 19517541 (cherry picked from commit b3a36ca5ee79411c18984b85b224d9285b4468af) Change-Id: Ifbd5d19e506e7313700e2e29a2dae5736e049844 --- include/cutils/threads.h | 17 +++++++++------- libcutils/threads.c | 42 ++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/include/cutils/threads.h b/include/cutils/threads.h index 3133cdb04..572749407 100644 --- a/include/cutils/threads.h +++ b/include/cutils/threads.h @@ -17,6 +17,14 @@ #ifndef _LIBS_CUTILS_THREADS_H #define _LIBS_CUTILS_THREADS_H +#include + +#if !defined(_WIN32) +#include +#else +#include +#endif + #ifdef __cplusplus extern "C" { #endif @@ -29,10 +37,9 @@ extern "C" { /***********************************************************************/ /***********************************************************************/ -#if !defined(_WIN32) +extern pid_t gettid(); -#include -#include +#if !defined(_WIN32) typedef struct { pthread_mutex_t lock; @@ -40,14 +47,10 @@ typedef struct { pthread_key_t tls; } thread_store_t; -extern pid_t gettid(); - #define THREAD_STORE_INITIALIZER { PTHREAD_MUTEX_INITIALIZER, 0, 0 } #else // !defined(_WIN32) -#include - typedef struct { int lock_init; int has_tls; diff --git a/libcutils/threads.c b/libcutils/threads.c index 5f5577b42..3d8dd4848 100644 --- a/libcutils/threads.c +++ b/libcutils/threads.c @@ -16,8 +16,6 @@ #include "cutils/threads.h" -#if !defined(_WIN32) - // For gettid. #if defined(__APPLE__) #include "AvailabilityMacros.h" // For MAC_OS_X_VERSION_MAX_ALLOWED @@ -30,9 +28,29 @@ #include #include #elif defined(_WIN32) -#include +#include #endif +// No definition needed for Android because we'll just pick up bionic's copy. +#ifndef __ANDROID__ +pid_t gettid() { +#if defined(__APPLE__) + uint64_t owner; + int rc = pthread_threadid_np(NULL, &owner); + if (rc != 0) { + abort(); + } + return owner; +#elif defined(__linux__) + return syscall(__NR_gettid); +#elif defined(_WIN32) + return GetCurrentThreadId(); +#endif +} +#endif // __ANDROID__ + +#if !defined(_WIN32) + void* thread_store_get( thread_store_t* store ) { if (!store->has_tls) @@ -58,24 +76,6 @@ extern void thread_store_set( thread_store_t* store, pthread_setspecific( store->tls, value ); } -// No definition needed for Android because we'll just pick up bionic's copy. -#ifndef __ANDROID__ -pid_t gettid() { -#if defined(__APPLE__) - uint64_t owner; - int rc = pthread_threadid_np(NULL, &owner); - if (rc != 0) { - abort(); - } - return owner; -#elif defined(__linux__) - return syscall(__NR_gettid); -#elif defined(_WIN32) - return (pid_t)GetCurrentThreadId(); -#endif -} -#endif // __ANDROID__ - #else /* !defined(_WIN32) */ void* thread_store_get( thread_store_t* store ) {