Chromium Code Reviews| 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_POSIX) | 11 #if defined(OS_WIN) |
| 12 #include <windows.h> | |
| 13 #elif defined(OS_POSIX) | |
| 12 #include <pthread.h> | 14 #include <pthread.h> |
| 13 #endif | 15 #endif |
| 14 | 16 |
| 15 namespace base { | 17 namespace base { |
| 16 | 18 |
| 19 namespace internal { | |
| 20 | |
| 21 // Helper functions that abstract the cross-platform APIs. Do not use directly | |
| 22 class BASE_EXPORT PlatformThreadLocalStorage { | |
| 23 public: | |
| 24 | |
| 25 #if defined(OS_WIN) | |
| 26 typedef unsigned long TLSKey; | |
| 27 enum { TLS_KEY_OUT_OF_INDEXES = TLS_OUT_OF_INDEXES }; | |
| 28 #elif defined(OS_POSIX) | |
| 29 typedef pthread_key_t TLSKey; | |
| 30 // In theory, there is no invalid key in Posix, It is critical to define | |
| 31 // one, so we can use minimal system service to implement TLS. | |
| 32 // It is almost imposible to get the defined invalid key, if we do get it, | |
| 33 // just alloc another one. | |
| 34 enum { TLS_KEY_OUT_OF_INDEXES = 0x7FFFFFFF }; | |
| 35 #endif | |
| 36 // Cross-platform APIs | |
| 37 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
| |
| 38 static void FreeTLS(TLSKey key); | |
| 39 static void SetTLSValue(TLSKey key, void* value); | |
| 40 static void* GetTLSValue(TLSKey key); | |
| 41 | |
| 42 // 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
| |
| 43 // 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
| |
| 44 // |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
| |
| 45 static void OnThreadExit(void* value); | |
| 46 }; | |
| 47 | |
| 48 } // namespace internal | |
| 49 | |
| 17 // Wrapper for thread local storage. This class doesn't do much except provide | 50 // Wrapper for thread local storage. This class doesn't do much except provide |
| 18 // an API for portability. | 51 // an API for portability. |
| 19 class BASE_EXPORT ThreadLocalStorage { | 52 class BASE_EXPORT ThreadLocalStorage { |
| 20 public: | 53 public: |
| 21 | 54 |
| 22 // Prototype for the TLS destructor function, which can be optionally used to | 55 // Prototype for the TLS destructor function, which can be optionally used to |
| 23 // cleanup thread local storage on thread exit. 'value' is the data that is | 56 // cleanup thread local storage on thread exit. 'value' is the data that is |
| 24 // stored in thread local storage. | 57 // stored in thread local storage. |
| 25 typedef void (*TLSDestructorFunc)(void* value); | 58 typedef void (*TLSDestructorFunc)(void* value); |
| 26 | 59 |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 53 void* Get() const; | 86 void* Get() const; |
| 54 | 87 |
| 55 // Set the thread-local value stored in slot 'slot' to | 88 // Set the thread-local value stored in slot 'slot' to |
| 56 // value 'value'. | 89 // value 'value'. |
| 57 void Set(void* value); | 90 void Set(void* value); |
| 58 | 91 |
| 59 bool initialized() const { return initialized_; } | 92 bool initialized() const { return initialized_; } |
| 60 | 93 |
| 61 // The internals of this struct should be considered private. | 94 // The internals of this struct should be considered private. |
| 62 bool initialized_; | 95 bool initialized_; |
| 63 #if defined(OS_WIN) | |
| 64 int slot_; | 96 int slot_; |
| 65 #elif defined(OS_POSIX) | |
| 66 pthread_key_t key_; | |
| 67 #endif | |
| 68 | |
| 69 }; | 97 }; |
| 70 | 98 |
| 71 // A convenience wrapper around StaticSlot with a constructor. Can be used | 99 // A convenience wrapper around StaticSlot with a constructor. Can be used |
| 72 // as a member variable. | 100 // as a member variable. |
| 73 class BASE_EXPORT Slot : public StaticSlot { | 101 class BASE_EXPORT Slot : public StaticSlot { |
| 74 public: | 102 public: |
| 75 // Calls StaticSlot::Initialize(). | 103 // Calls StaticSlot::Initialize(). |
| 76 explicit Slot(TLSDestructorFunc destructor = NULL); | 104 explicit Slot(TLSDestructorFunc destructor = NULL); |
| 77 | 105 |
| 78 private: | 106 private: |
| 79 using StaticSlot::initialized_; | 107 using StaticSlot::initialized_; |
| 80 #if defined(OS_WIN) | |
| 81 using StaticSlot::slot_; | 108 using StaticSlot::slot_; |
| 82 #elif defined(OS_POSIX) | 109 |
| 83 using StaticSlot::key_; | |
| 84 #endif | |
| 85 DISALLOW_COPY_AND_ASSIGN(Slot); | 110 DISALLOW_COPY_AND_ASSIGN(Slot); |
| 86 }; | 111 }; |
| 87 | 112 |
| 88 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); | 113 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); |
| 89 }; | 114 }; |
| 90 | 115 |
| 91 } // namespace base | 116 } // namespace base |
| 92 | 117 |
| 93 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_ | 118 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_ |
| OLD | NEW |