OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "base/base_export.h" | 9 #include "base/base_export.h" |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
11 | 11 |
12 #if defined(OS_POSIX) | 12 #if defined(OS_POSIX) |
13 #include <pthread.h> | 13 #include <pthread.h> |
14 #endif | 14 #endif |
15 | 15 |
16 namespace base { | 16 namespace base { |
17 | 17 |
18 // Wrapper for thread local storage. This class doesn't do much except provide | 18 // Wrapper for thread local storage. This class doesn't do much except provide |
19 // an API for portability. | 19 // an API for portability. |
20 class BASE_EXPORT ThreadLocalStorage { | 20 class BASE_EXPORT ThreadLocalStorage { |
21 public: | 21 public: |
22 | 22 |
23 // Prototype for the TLS destructor function, which can be optionally used to | 23 // Prototype for the TLS destructor function, which can be optionally used to |
24 // cleanup thread local storage on thread exit. 'value' is the data that is | 24 // cleanup thread local storage on thread exit. 'value' is the data that is |
25 // stored in thread local storage. | 25 // stored in thread local storage. |
26 typedef void (*TLSDestructorFunc)(void* value); | 26 typedef void (*TLSDestructorFunc)(void* value); |
27 | 27 |
| 28 // StaticSlot uses its own struct initializer-list style static |
| 29 // initialization, as base's LINKER_INITIALIZED requires a constructor and on |
| 30 // some compilers (notably gcc 4.4) this still ends up needing runtime |
| 31 // initialization. |
| 32 #define TLS_INITIALIZER {0} |
| 33 |
28 // A key representing one value stored in TLS. | 34 // A key representing one value stored in TLS. |
29 class BASE_EXPORT Slot { | 35 // Initialize like |
30 public: | 36 // ThreadLocalStorage::StaticSlot my_slot = TLS_INITIALIZER; |
31 explicit Slot(TLSDestructorFunc destructor = NULL); | 37 // If you're not using a static variable, use the convenience class |
32 | 38 // ThreadLocalStorage::Slot (below) instead. |
33 // This constructor should be used for statics. | 39 struct BASE_EXPORT StaticSlot { |
34 // It returns an uninitialized Slot. | |
35 explicit Slot(base::LinkerInitialized x) {} | |
36 | |
37 // Set up the TLS slot. Called by the constructor. | 40 // Set up the TLS slot. Called by the constructor. |
38 // 'destructor' is a pointer to a function to perform per-thread cleanup of | 41 // 'destructor' is a pointer to a function to perform per-thread cleanup of |
39 // this object. If set to NULL, no cleanup is done for this TLS slot. | 42 // this object. If set to NULL, no cleanup is done for this TLS slot. |
40 // Returns false on error. | 43 // Returns false on error. |
41 bool Initialize(TLSDestructorFunc destructor); | 44 bool Initialize(TLSDestructorFunc destructor); |
42 | 45 |
43 // Free a previously allocated TLS 'slot'. | 46 // Free a previously allocated TLS 'slot'. |
44 // If a destructor was set for this slot, removes | 47 // If a destructor was set for this slot, removes |
45 // the destructor so that remaining threads exiting | 48 // the destructor so that remaining threads exiting |
46 // will not free data. | 49 // will not free data. |
47 void Free(); | 50 void Free(); |
48 | 51 |
49 // Get the thread-local value stored in slot 'slot'. | 52 // Get the thread-local value stored in slot 'slot'. |
50 // Values are guaranteed to initially be zero. | 53 // Values are guaranteed to initially be zero. |
51 void* Get() const; | 54 void* Get() const; |
52 | 55 |
53 // Set the thread-local value stored in slot 'slot' to | 56 // Set the thread-local value stored in slot 'slot' to |
54 // value 'value'. | 57 // value 'value'. |
55 void Set(void* value); | 58 void Set(void* value); |
56 | 59 |
57 bool initialized() const { return initialized_; } | 60 bool initialized() const { return initialized_; } |
58 | 61 |
59 private: | |
60 // The internals of this struct should be considered private. | 62 // The internals of this struct should be considered private. |
61 bool initialized_; | 63 bool initialized_; |
62 #if defined(OS_WIN) | 64 #if defined(OS_WIN) |
63 int slot_; | 65 int slot_; |
64 #elif defined(OS_POSIX) | 66 #elif defined(OS_POSIX) |
65 pthread_key_t key_; | 67 pthread_key_t key_; |
66 #endif | 68 #endif |
67 | 69 |
| 70 }; |
| 71 |
| 72 // A convenience wrapper around StaticSlot with a constructor. Can be used |
| 73 // as a member variable. |
| 74 class BASE_EXPORT Slot : public StaticSlot { |
| 75 public: |
| 76 // Calls StaticSlot::Initialize(). |
| 77 explicit Slot(TLSDestructorFunc destructor = NULL); |
| 78 |
| 79 private: |
| 80 using StaticSlot::initialized_; |
| 81 #if defined(OS_WIN) |
| 82 using StaticSlot::slot_; |
| 83 #elif defined(OS_POSIX) |
| 84 using StaticSlot::key_; |
| 85 #endif |
68 DISALLOW_COPY_AND_ASSIGN(Slot); | 86 DISALLOW_COPY_AND_ASSIGN(Slot); |
69 }; | 87 }; |
70 | 88 |
71 #if defined(OS_WIN) | 89 #if defined(OS_WIN) |
72 // Function called when on thread exit to call TLS | 90 // Function called when on thread exit to call TLS |
73 // destructor functions. This function is used internally. | 91 // destructor functions. This function is used internally. |
74 static void ThreadExit(); | 92 static void ThreadExit(); |
75 | 93 |
76 private: | 94 private: |
77 // Function to lazily initialize our thread local storage. | 95 // Function to lazily initialize our thread local storage. |
78 static void **Initialize(); | 96 static void **Initialize(); |
79 | 97 |
80 static long tls_key_; | 98 static long tls_key_; |
81 static long tls_max_; | 99 static long tls_max_; |
82 #endif // OS_WIN | 100 #endif // OS_WIN |
83 | 101 |
84 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); | 102 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); |
85 }; | 103 }; |
86 | 104 |
87 } // namespace base | 105 } // namespace base |
88 | 106 |
89 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_ | 107 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_ |
OLD | NEW |