| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef BASE_THREADING_THREAD_LOCAL_STORAGE_H_ | 5 #ifndef BASE_THREADING_THREAD_LOCAL_STORAGE_H_ |
| 6 #define BASE_THREADING_THREAD_LOCAL_STORAGE_H_ | 6 #define BASE_THREADING_THREAD_LOCAL_STORAGE_H_ |
| 7 | 7 |
| 8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
| 9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
| 10 | 10 |
| 11 #if defined(OS_WIN) | 11 #if defined(OS_POSIX) |
| 12 #include <windows.h> | |
| 13 #elif defined(OS_POSIX) | |
| 14 #include <pthread.h> | 12 #include <pthread.h> |
| 15 #endif | 13 #endif |
| 16 | 14 |
| 17 namespace base { | 15 namespace base { |
| 18 | 16 |
| 19 namespace internal { | 17 namespace internal { |
| 20 | 18 |
| 21 // WARNING: You should *NOT* be using this class directly. | 19 // WARNING: You should *NOT* be using this class directly. |
| 22 // PlatformThreadLocalStorage is low-level abstraction to the OS's TLS | 20 // PlatformThreadLocalStorage is low-level abstraction to the OS's TLS |
| 23 // interface, you should instead be using ThreadLocalStorage::StaticSlot/Slot. | 21 // interface, you should instead be using ThreadLocalStorage::StaticSlot/Slot. |
| 24 class BASE_EXPORT PlatformThreadLocalStorage { | 22 class BASE_EXPORT PlatformThreadLocalStorage { |
| 25 public: | 23 public: |
| 26 | 24 |
| 27 #if defined(OS_WIN) | 25 #if defined(OS_POSIX) |
| 28 typedef unsigned long TLSKey; | |
| 29 enum { TLS_KEY_OUT_OF_INDEXES = TLS_OUT_OF_INDEXES }; | |
| 30 #elif defined(OS_POSIX) | |
| 31 typedef pthread_key_t TLSKey; | 26 typedef pthread_key_t TLSKey; |
| 32 // The following is a "reserved key" which is used in our generic Chromium | 27 // The following is a "reserved key" which is used in our generic Chromium |
| 33 // ThreadLocalStorage implementation. We expect that an OS will not return | 28 // ThreadLocalStorage implementation. We expect that an OS will not return |
| 34 // such a key, but if it is returned (i.e., the OS tries to allocate it) we | 29 // such a key, but if it is returned (i.e., the OS tries to allocate it) we |
| 35 // will just request another key. | 30 // will just request another key. |
| 36 enum { TLS_KEY_OUT_OF_INDEXES = 0x7FFFFFFF }; | 31 enum { TLS_KEY_OUT_OF_INDEXES = 0x7FFFFFFF }; |
| 37 #endif | 32 #endif |
| 38 | 33 |
| 39 // The following methods need to be supported on each OS platform, so that | 34 // The following methods need to be supported on each OS platform, so that |
| 40 // the Chromium ThreadLocalStore functionality can be constructed. | 35 // the Chromium ThreadLocalStore functionality can be constructed. |
| (...skipping 11 matching lines...) Expand all Loading... |
| 52 static void* GetTLSValue(TLSKey key); | 47 static void* GetTLSValue(TLSKey key); |
| 53 | 48 |
| 54 // Each platform (OS implementation) is required to call this method on each | 49 // Each platform (OS implementation) is required to call this method on each |
| 55 // terminating thread when the thread is about to terminate. This method | 50 // terminating thread when the thread is about to terminate. This method |
| 56 // will then call all registered destructors for slots in Chromium | 51 // will then call all registered destructors for slots in Chromium |
| 57 // ThreadLocalStorage, until there are no slot values remaining as having | 52 // ThreadLocalStorage, until there are no slot values remaining as having |
| 58 // been set on this thread. | 53 // been set on this thread. |
| 59 // Destructors may end up being called multiple times on a terminating | 54 // Destructors may end up being called multiple times on a terminating |
| 60 // thread, as other destructors may re-set slots that were previously | 55 // thread, as other destructors may re-set slots that were previously |
| 61 // destroyed. | 56 // destroyed. |
| 62 #if defined(OS_WIN) | 57 #if defined(OS_POSIX) |
| 63 // Since Windows which doesn't support TLS destructor, the implementation | |
| 64 // should use GetTLSValue() to retrieve the value of TLS slot. | |
| 65 static void OnThreadExit(); | |
| 66 #elif defined(OS_POSIX) | |
| 67 // |Value| is the data stored in TLS slot, The implementation can't use | 58 // |Value| is the data stored in TLS slot, The implementation can't use |
| 68 // GetTLSValue() to retrieve the value of slot as it has already been reset | 59 // GetTLSValue() to retrieve the value of slot as it has already been reset |
| 69 // in Posix. | 60 // in Posix. |
| 70 static void OnThreadExit(void* value); | 61 static void OnThreadExit(void* value); |
| 71 #endif | 62 #endif |
| 72 }; | 63 }; |
| 73 | 64 |
| 74 } // namespace internal | 65 } // namespace internal |
| 75 | 66 |
| 76 // Wrapper for thread local storage. This class doesn't do much except provide | 67 // Wrapper for thread local storage. This class doesn't do much except provide |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 135 DISALLOW_COPY_AND_ASSIGN(Slot); | 126 DISALLOW_COPY_AND_ASSIGN(Slot); |
| 136 }; | 127 }; |
| 137 | 128 |
| 138 private: | 129 private: |
| 139 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); | 130 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); |
| 140 }; | 131 }; |
| 141 | 132 |
| 142 } // namespace base | 133 } // namespace base |
| 143 | 134 |
| 144 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_ | 135 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_ |
| OLD | NEW |