Commit fd73235f authored by Austin Schuh's avatar Austin Schuh

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.
parent 9cbdaedb
...@@ -43,7 +43,7 @@ Arena::ThreadCache& Arena::thread_cache() { ...@@ -43,7 +43,7 @@ Arena::ThreadCache& Arena::thread_cache() {
static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_ = { -1, NULL }; static GOOGLE_THREAD_LOCAL ThreadCache thread_cache_ = { -1, NULL };
return thread_cache_; 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() { Arena::ThreadCache& Arena::thread_cache() {
static internal::ThreadLocalStorage<ThreadCache>* thread_cache_ = static internal::ThreadLocalStorage<ThreadCache>* thread_cache_ =
new internal::ThreadLocalStorage<ThreadCache>(); new internal::ThreadLocalStorage<ThreadCache>();
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <google/protobuf/stubs/atomic_sequence_num.h> #include <google/protobuf/stubs/atomic_sequence_num.h>
#include <google/protobuf/stubs/atomicops.h> #include <google/protobuf/stubs/atomicops.h>
#include <google/protobuf/stubs/common.h> #include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/platform_macros.h>
#include <google/protobuf/stubs/type_traits.h> #include <google/protobuf/stubs/type_traits.h>
namespace google { namespace google {
...@@ -528,7 +529,7 @@ class LIBPROTOBUF_EXPORT Arena { ...@@ -528,7 +529,7 @@ class LIBPROTOBUF_EXPORT Arena {
// Thread local variables cannot be exposed through DLL interface but we can // Thread local variables cannot be exposed through DLL interface but we can
// wrap them in static functions. // wrap them in static functions.
static ThreadCache& thread_cache(); 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 // Android ndk does not support GOOGLE_THREAD_LOCAL keyword so we use a custom thread
// local storage class we implemented. // local storage class we implemented.
// iOS also does not support the GOOGLE_THREAD_LOCAL keyword. // iOS also does not support the GOOGLE_THREAD_LOCAL keyword.
......
...@@ -56,6 +56,7 @@ ...@@ -56,6 +56,7 @@
// Don't include this file for people not concerned about thread safety. // Don't include this file for people not concerned about thread safety.
#ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY #ifndef GOOGLE_PROTOBUF_NO_THREAD_SAFETY
#include <google/protobuf/stubs/common.h>
#include <google/protobuf/stubs/platform_macros.h> #include <google/protobuf/stubs/platform_macros.h>
namespace google { namespace google {
......
...@@ -62,6 +62,8 @@ ...@@ -62,6 +62,8 @@
#include <exception> #include <exception>
#endif #endif
#include <google/protobuf/stubs/platform_macros.h>
#if defined(__APPLE__) #if defined(__APPLE__)
#include <TargetConditionals.h> // for TARGET_OS_IPHONE #include <TargetConditionals.h> // for TARGET_OS_IPHONE
#endif #endif
...@@ -1174,11 +1176,7 @@ class LIBPROTOBUF_EXPORT MutexLockMaybe { ...@@ -1174,11 +1176,7 @@ class LIBPROTOBUF_EXPORT MutexLockMaybe {
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(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) #if defined(GOOGLE_PROTOBUF_NO_THREADLOCAL)
// 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.
template<typename T> template<typename T>
class ThreadLocalStorage { class ThreadLocalStorage {
public: public:
......
...@@ -31,8 +31,6 @@ ...@@ -31,8 +31,6 @@
#ifndef GOOGLE_PROTOBUF_PLATFORM_MACROS_H_ #ifndef GOOGLE_PROTOBUF_PLATFORM_MACROS_H_
#define GOOGLE_PROTOBUF_PLATFORM_MACROS_H_ #define GOOGLE_PROTOBUF_PLATFORM_MACROS_H_
#include <google/protobuf/stubs/common.h>
#define GOOGLE_PROTOBUF_PLATFORM_ERROR \ #define GOOGLE_PROTOBUF_PLATFORM_ERROR \
#error "Host platform was not detected as supported by protobuf" #error "Host platform was not detected as supported by protobuf"
...@@ -111,4 +109,12 @@ GOOGLE_PROTOBUF_PLATFORM_ERROR ...@@ -111,4 +109,12 @@ GOOGLE_PROTOBUF_PLATFORM_ERROR
#undef 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_ #endif // GOOGLE_PROTOBUF_PLATFORM_MACROS_H_
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment