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.
4 :
5 : #ifndef BASE_THREAD_LOCAL_STORAGE_H_
6 : #define BASE_THREAD_LOCAL_STORAGE_H_
7 :
8 : #include "base/basictypes.h"
9 :
10 : #if defined(OS_POSIX)
11 : #include <pthread.h>
12 : #endif
13 :
14 : // Wrapper for thread local storage. This class doesn't do much except provide
15 : // an API for portability.
16 : class ThreadLocalStorage {
17 : public:
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.
22 : typedef void (*TLSDestructorFunc)(void* value);
23 :
24 : // A key representing one value stored in TLS.
25 : class Slot {
26 : public:
27 : Slot(TLSDestructorFunc destructor = NULL);
28 :
29 : // This constructor should be used for statics.
30 : // It returns an uninitialized Slot.
31 1464 : explicit Slot(base::LinkerInitialized x) {}
32 :
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);
38 :
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 0 : 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 : };
66 :
67 : #if defined(OS_WIN)
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];
85 : #endif // OS_WIN
86 :
87 : DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
88 : };
89 :
90 : // Temporary backwards-compatible name.
91 : // TODO(evanm): replace all usage of TLSSlot.
92 : typedef ThreadLocalStorage::Slot TLSSlot;
93 :
94 : #endif // BASE_THREAD_LOCAL_STORAGE_H_
|