license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 4 | |
avi@google.com | 4be2f9b | 2008-08-08 05:12:28 +0900 | [diff] [blame] | 5 | #ifndef BASE_THREAD_LOCAL_STORAGE_H_ |
| 6 | #define BASE_THREAD_LOCAL_STORAGE_H_ |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 7 | |
| 8 | #include "base/basictypes.h" |
| 9 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 10 | #if defined(OS_POSIX) |
avi@google.com | 4be2f9b | 2008-08-08 05:12:28 +0900 | [diff] [blame] | 11 | #include <pthread.h> |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 12 | #endif |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 13 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 14 | // Wrapper for thread local storage. This class doesn't do much except provide |
| 15 | // an API for portability. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 16 | class ThreadLocalStorage { |
| 17 | public: |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 18 | |
| 19 | // Prototype for the TLS destructor function, which can be optionally used to |
| 20 | // cleanup thread local storage on thread exit. 'value' is the data that is |
| 21 | // stored in thread local storage. |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 22 | typedef void (*TLSDestructorFunc)(void* value); |
| 23 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 24 | // A key representing one value stored in TLS. |
| 25 | class Slot { |
| 26 | public: |
| 27 | Slot(TLSDestructorFunc destructor = NULL); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 28 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 29 | // This constructor should be used for statics. |
| 30 | // It returns an uninitialized Slot. |
| 31 | explicit Slot(base::LinkerInitialized x) {} |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 32 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 33 | // Set up the TLS slot. Called by the constructor. |
| 34 | // 'destructor' is a pointer to a function to perform per-thread cleanup of |
| 35 | // this object. If set to NULL, no cleanup is done for this TLS slot. |
| 36 | // Returns false on error. |
| 37 | bool Initialize(TLSDestructorFunc destructor); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 38 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 39 | // Free a previously allocated TLS 'slot'. |
| 40 | // If a destructor was set for this slot, removes |
| 41 | // the destructor so that remaining threads exiting |
| 42 | // will not free data. |
| 43 | void Free(); |
| 44 | |
| 45 | // Get the thread-local value stored in slot 'slot'. |
| 46 | // Values are guaranteed to initially be zero. |
| 47 | void* Get() const; |
| 48 | |
| 49 | // Set the thread-local value stored in slot 'slot' to |
| 50 | // value 'value'. |
| 51 | void Set(void* value); |
| 52 | |
| 53 | bool initialized() const { return initialized_; } |
| 54 | |
| 55 | private: |
| 56 | // The internals of this struct should be considered private. |
| 57 | bool initialized_; |
| 58 | #if defined(OS_WIN) |
| 59 | int slot_; |
| 60 | #elif defined(OS_POSIX) |
| 61 | pthread_key_t key_; |
| 62 | #endif |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(Slot); |
| 65 | }; |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 66 | |
darin@google.com | 3810032 | 2008-08-07 06:27:02 +0900 | [diff] [blame] | 67 | #if defined(OS_WIN) |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 68 | // Function called when on thread exit to call TLS |
| 69 | // destructor functions. This function is used internally. |
| 70 | static void ThreadExit(); |
| 71 | |
| 72 | private: |
| 73 | // Function to lazily initialize our thread local storage. |
| 74 | static void **Initialize(); |
| 75 | |
| 76 | private: |
| 77 | // The maximum number of 'slots' in our thread local storage stack. |
| 78 | // For now, this is fixed. We could either increase statically, or |
| 79 | // we could make it dynamic in the future. |
| 80 | static const int kThreadLocalStorageSize = 64; |
| 81 | |
| 82 | static long tls_key_; |
| 83 | static long tls_max_; |
| 84 | static TLSDestructorFunc tls_destructors_[kThreadLocalStorageSize]; |
darin@google.com | 3810032 | 2008-08-07 06:27:02 +0900 | [diff] [blame] | 85 | #endif // OS_WIN |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 86 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 87 | DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); |
initial.commit | 3f4a732 | 2008-07-27 06:49:38 +0900 | [diff] [blame] | 88 | }; |
| 89 | |
evanm@google.com | f26fd3a | 2008-08-21 07:54:52 +0900 | [diff] [blame] | 90 | // Temporary backwards-compatible name. |
| 91 | // TODO(evanm): replace all usage of TLSSlot. |
| 92 | typedef ThreadLocalStorage::Slot TLSSlot; |
| 93 | |
avi@google.com | 4be2f9b | 2008-08-08 05:12:28 +0900 | [diff] [blame] | 94 | #endif // BASE_THREAD_LOCAL_STORAGE_H_ |
license.bot | f003cfe | 2008-08-24 09:55:55 +0900 | [diff] [blame^] | 95 | |