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..9ed8dd0b63107f31a068cd8910172cc0370fb2e2 100644 |
--- a/base/threading/thread_local_storage.h |
+++ b/base/threading/thread_local_storage.h |
@@ -8,12 +8,67 @@ |
#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 { |
+ |
+// WARNING: You should *NOT* be using this class directly. |
+// PlatformThreadLocalStorage is low-level abstraction to the OS's TLS |
+// interface, you should instead be using ThreadLocalStorage::StaticSlot/Slot, |
+// see thread_local_storage.h. |
+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; |
+ // The following is a "reserved key" which is used in our generic Chromium |
+ // ThreadLocalStorage implementation. We expect that an OS will not return |
+ // such a key, but if it is returned (i.e., the OS tries to allocate it) we |
+ // will just request another key. |
+ enum { TLS_KEY_OUT_OF_INDEXES = 0x7FFFFFFF }; |
+#endif |
+ // The following methods need to be supported on each OS platform, so that |
+ // the Chromium ThreadLocalStore functionality can be constructed. |
+ // Chromium will use these methods to acquire a single OS slot, and then use |
+ // that to support a much larger number of Chromium slots (independent of the |
+ // OS restrictions). |
+ // The following returns true IFF it successfully is able to return an OS |
+ // key in |key|. |
+ static bool AllocTLS(TLSKey* key); |
+ // Note: FreeTLS() doesn't have to be called, it is fine with this leak, OS |
+ // might not reuse released slot, you might just reset the TLS value with |
+ // SetTLSValue(). |
+ static void FreeTLS(TLSKey key); |
+ static void SetTLSValue(TLSKey key, void* value); |
+ static void* GetTLSValue(TLSKey key); |
+ |
+ // Each platform (OS implementation) is required to call this method on each |
+ // terminating thread when the thread is about to terminate. This method |
+ // will then call all registered destructors for slots in Chromium |
+ // ThreadLocalStorage, until there are no slot values remaining as having |
+ // been set on this thread. |
+ // Destructors may end up being called multiple times on a terminating |
+ // thread, as other destructors may re-set slots that were previously |
+ // destroyed. |
+ // |Value| is the data stored in TLS slot, and will be NULL in Windows which |
+ // doesn't support TLS destructor. The Windows implementation should use |
+ // GetTLSValue() to retrieve the vale of TLS slot. Posix implementation can't |
+ // use the same way because the value was already reset before calling |
+ // destructor. |
+ static void OnThreadExit(void* value); |
jar (doing other things)
2013/12/05 19:53:32
It sure would have been nice if the Window's imple
michaelbai
2013/12/05 22:56:15
In order to do this, we need to know the TLS key i
|
+}; |
+ |
+} // 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 +115,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 +127,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); |
}; |