From fd73235f6b2534aa63af41359d9f386ade43a5cc Mon Sep 17 00:00:00 2001 From: Austin Schuh Date: Mon, 8 Jun 2015 18:49:16 -0700 Subject: Refactored threadlocal logic. Refactored the threadlocal logic for Android and IOS into logic in platform_macro.h which computes a GOOGLE_PROTOBUF_NO_THREADLOCAL define which is then used elsewhere. This allows new platforms without THREADLOCAL to be easily defined. --- src/google/protobuf/arena.cc | 2 +- src/google/protobuf/arena.h | 3 ++- src/google/protobuf/stubs/atomicops.h | 1 + src/google/protobuf/stubs/common.h | 8 +++----- src/google/protobuf/stubs/platform_macros.h | 10 ++++++++-- 5 files changed, 15 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/arena.cc b/src/google/protobuf/arena.cc index 96009645..ed1c5ef2 100755 --- a/src/google/protobuf/arena.cc +++ b/src/google/protobuf/arena.cc @@ -43,7 +43,7 @@ Arena::ThreadCache& Arena::thread_cache() { static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_ = { -1, NULL }; return thread_cache_; } -#elif defined(GOOGLE_PROTOBUF_OS_ANDROID) || defined(GOOGLE_PROTOBUF_OS_IPHONE) +#elif defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) Arena::ThreadCache& Arena::thread_cache() { static internal::ThreadLocalStorage* thread_cache_ = new internal::ThreadLocalStorage(); diff --git a/src/google/protobuf/arena.h b/src/google/protobuf/arena.h index 6c3f606e..51149bae 100644 --- a/src/google/protobuf/arena.h +++ b/src/google/protobuf/arena.h @@ -39,6 +39,7 @@ #include #include #include +#include #include namespace google { @@ -528,7 +529,7 @@ class LIBPROTOBUF_EXPORT Arena { // Thread local variables cannot be exposed through DLL interface but we can // wrap them in static functions. static ThreadCache& thread_cache(); -#elif defined(GOOGLE_PROTOBUF_OS_ANDROID) || defined(GOOGLE_PROTOBUF_OS_IPHONE) +#elif defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) // Android ndk does not support GOOGLE_THREAD_LOCAL keyword so we use a custom thread // local storage class we implemented. // iOS also does not support the GOOGLE_THREAD_LOCAL keyword. diff --git a/src/google/protobuf/stubs/atomicops.h b/src/google/protobuf/stubs/atomicops.h index bbe2d845..cd20caac 100644 --- a/src/google/protobuf/stubs/atomicops.h +++ b/src/google/protobuf/stubs/atomicops.h @@ -56,6 +56,7 @@ // Don't include this file for people not concerned about thread safety. #ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY +#include #include namespace google { diff --git a/src/google/protobuf/stubs/common.h b/src/google/protobuf/stubs/common.h index e4c35a2f..49d16020 100644 --- a/src/google/protobuf/stubs/common.h +++ b/src/google/protobuf/stubs/common.h @@ -62,6 +62,8 @@ #include #endif +#include + #if defined(__APPLE__) #include // for TARGET_OS_IPHONE #endif @@ -1174,11 +1176,7 @@ class LIBPROTOBUF_EXPORT MutexLockMaybe { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe); }; -#if defined(__ANDROID__) || defined(GOOGLE_PROTOBUF_OS_ANDROID) || (defined(TARGET_OS_IPHONE) && TARGET_OS_IPHONE) || defined(GOOGLE_PROTOBUF_OS_IPHONE) -// Android ndk does not support the __thread keyword very well yet. Here -// we use pthread_key_create()/pthread_getspecific()/... methods for -// TLS support on android. -// iOS also does not support the __thread keyword. +#if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL) template class ThreadLocalStorage { public: diff --git a/src/google/protobuf/stubs/platform_macros.h b/src/google/protobuf/stubs/platform_macros.h index 2ce7fc8f..9e0344d8 100644 --- a/src/google/protobuf/stubs/platform_macros.h +++ b/src/google/protobuf/stubs/platform_macros.h @@ -31,8 +31,6 @@ #ifndef GOOGLE_PROTOBUF_PLATFORM_MACROS_H_ #define GOOGLE_PROTOBUF_PLATFORM_MACROS_H_ -#include - #define GOOGLE_PROTOBUF_PLATFORM_ERROR \ #error "Host platform was not detected as supported by protobuf" @@ -111,4 +109,12 @@ GOOGLE_PROTOBUF_PLATFORM_ERROR #undef GOOGLE_PROTOBUF_PLATFORM_ERROR +#if defined(GOOGLE_PROTOBUF_OS_ANDROID) || defined(GOOGLE_PROTOBUF_OS_IPHONE) +// Android ndk does not support the __thread keyword very well yet. Here +// we use pthread_key_create()/pthread_getspecific()/... methods for +// TLS support on android. +// iOS also does not support the __thread keyword. +#define GOOGLE_PROTOBUF_NO_THREADLOCAL +#endif + #endif // GOOGLE_PROTOBUF_PLATFORM_MACROS_H_ -- cgit v1.2.3