Index: base/threading/thread_local_storage.h |
diff --git a/base/threading/thread_local_storage.h b/base/threading/thread_local_storage.h |
index eb5648f76c9b693a6ac041d5b6a41ee35a1fcad9..a90b55285f3ed5d44948eb764ceba39a6755e2a6 100644 |
--- a/base/threading/thread_local_storage.h |
+++ b/base/threading/thread_local_storage.h |
@@ -8,12 +8,45 @@ |
#include "base/base_export.h" |
#include "base/basictypes.h" |
-#if defined(OS_POSIX) |
+#if defined(OS_WIN) |
+#include <windows.h> |
+#elif defined(OS_POSIX) |
#include <pthread.h> |
#endif |
namespace base { |
+namespace internal { |
+ |
+// Helper functions that abstract the cross-platform APIs. Do not use directly |
+class BASE_EXPORT PlatformThreadLocalStorage { |
+ public: |
+ |
+#if defined(OS_WIN) |
+ typedef unsigned long TLSKey; |
+ enum { TLS_KEY_OUT_OF_INDEXES = TLS_OUT_OF_INDEXES }; |
+#elif defined(OS_POSIX) |
+ typedef pthread_key_t TLSKey; |
+ // In theory, there is no invalid key in Posix, It is critical to define |
+ // one, so we can use minimal system service to implement TLS. |
+ // It is almost imposible to get the defined invalid key, if we do get it, |
+ // just alloc another one. |
+ enum { TLS_KEY_OUT_OF_INDEXES = 0x7FFFFFFF }; |
+#endif |
+ // Cross-platform APIs |
+ static bool AllocTLS(TLSKey* key); |
jar (doing other things)
2013/11/20 01:46:15
The cross platform API must include destructors (c
michaelbai
2013/11/20 05:27:30
That because Windows doesn't support destructor ke
|
+ static void FreeTLS(TLSKey key); |
+ static void SetTLSValue(TLSKey key, void* value); |
+ static void* GetTLSValue(TLSKey key); |
+ |
+ // This is the TLS destructor and not a cross-platform API. The implementation |
jar (doing other things)
2013/11/20 01:46:15
Please describe what the method is for, or what it
|
+ // of cross-platform APIs should call this method when thread exit. The |
jar (doing other things)
2013/11/20 01:46:15
I couldn't grok:
...call this method when thread
|
+ // |value| is always NULL in Windows. |
jar (doing other things)
2013/11/20 01:46:15
I couldn't understand this comment, from start to
|
+ static void OnThreadExit(void* value); |
+}; |
+ |
+} // namespace internal |
+ |
// Wrapper for thread local storage. This class doesn't do much except provide |
// an API for portability. |
class BASE_EXPORT ThreadLocalStorage { |
@@ -60,12 +93,7 @@ class BASE_EXPORT ThreadLocalStorage { |
// The internals of this struct should be considered private. |
bool initialized_; |
-#if defined(OS_WIN) |
int slot_; |
-#elif defined(OS_POSIX) |
- pthread_key_t key_; |
-#endif |
- |
}; |
// A convenience wrapper around StaticSlot with a constructor. Can be used |
@@ -77,11 +105,8 @@ class BASE_EXPORT ThreadLocalStorage { |
private: |
using StaticSlot::initialized_; |
-#if defined(OS_WIN) |
using StaticSlot::slot_; |
-#elif defined(OS_POSIX) |
- using StaticSlot::key_; |
-#endif |
+ |
DISALLOW_COPY_AND_ASSIGN(Slot); |
}; |