Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(824)

Side by Side Diff: base/threading/thread_local_storage.h

Issue 60743004: Implement chromium's TLS. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/base.gypi ('k') | base/threading/thread_local_storage.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 // WARNING: You should *NOT* be using this class directly.
22 // PlatformThreadLocalStorage is low-level abstraction to the OS's TLS
23 // interface, you should instead be using ThreadLocalStorage::StaticSlot/Slot.
24 class BASE_EXPORT PlatformThreadLocalStorage {
25 public:
26
27 #if defined(OS_WIN)
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;
32 // The following is a "reserved key" which is used in our generic Chromium
33 // 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
35 // will just request another key.
36 enum { TLS_KEY_OUT_OF_INDEXES = 0x7FFFFFFF };
37 #endif
38 // The following methods need to be supported on each OS platform, so that
brettw 2013/12/16 17:56:37 Can you put a blank line above this to help separa
michaelbai 2013/12/16 22:09:20 Done.
39 // the Chromium ThreadLocalStore functionality can be constructed.
40 // Chromium will use these methods to acquire a single OS slot, and then use
41 // that to support a much larger number of Chromium slots (independent of the
42 // OS restrictions).
43 // The following returns true if it successfully is able to return an OS
44 // key in |key|.
45 static bool AllocTLS(TLSKey* key);
46 // Note: FreeTLS() doesn't have to be called, it is fine with this leak, OS
47 // might not reuse released slot, you might just reset the TLS value with
48 // SetTLSValue().
49 static void FreeTLS(TLSKey key);
50 static void SetTLSValue(TLSKey key, void* value);
51 static void* GetTLSValue(TLSKey key);
52
53 // Each platform (OS implementation) is required to call this method on each
54 // terminating thread when the thread is about to terminate. This method
55 // will then call all registered destructors for slots in Chromium
56 // ThreadLocalStorage, until there are no slot values remaining as having
57 // been set on this thread.
58 // Destructors may end up being called multiple times on a terminating
59 // thread, as other destructors may re-set slots that were previously
60 // destroyed.
61 #if defined(OS_WIN)
62 // Since Windows which doesn't support TLS destructor, the implementation
63 // should use GetTLSValue() to retrieve the value of TLS slot.
64 static void OnThreadExit();
65 #elif defined(OS_POSIX)
66 // |Value| is the data stored in TLS slot, The implementation can't use
67 // GetTLSValue() to retrieve the value of slot as it has already been reset
68 // in Posix.
69 static void OnThreadExit(void* value);
70 #endif
71 };
72
73 } // namespace internal
74
17 // Wrapper for thread local storage. This class doesn't do much except provide 75 // Wrapper for thread local storage. This class doesn't do much except provide
18 // an API for portability. 76 // an API for portability.
19 class BASE_EXPORT ThreadLocalStorage { 77 class BASE_EXPORT ThreadLocalStorage {
20 public: 78 public:
21 79
22 // Prototype for the TLS destructor function, which can be optionally used to 80 // 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 81 // cleanup thread local storage on thread exit. 'value' is the data that is
24 // stored in thread local storage. 82 // stored in thread local storage.
25 typedef void (*TLSDestructorFunc)(void* value); 83 typedef void (*TLSDestructorFunc)(void* value);
26 84
(...skipping 26 matching lines...) Expand all
53 void* Get() const; 111 void* Get() const;
54 112
55 // Set the thread-local value stored in slot 'slot' to 113 // Set the thread-local value stored in slot 'slot' to
56 // value 'value'. 114 // value 'value'.
57 void Set(void* value); 115 void Set(void* value);
58 116
59 bool initialized() const { return initialized_; } 117 bool initialized() const { return initialized_; }
60 118
61 // The internals of this struct should be considered private. 119 // The internals of this struct should be considered private.
62 bool initialized_; 120 bool initialized_;
63 #if defined(OS_WIN)
64 int slot_; 121 int slot_;
65 #elif defined(OS_POSIX)
66 pthread_key_t key_;
67 #endif
68
69 }; 122 };
70 123
71 // A convenience wrapper around StaticSlot with a constructor. Can be used 124 // A convenience wrapper around StaticSlot with a constructor. Can be used
72 // as a member variable. 125 // as a member variable.
73 class BASE_EXPORT Slot : public StaticSlot { 126 class BASE_EXPORT Slot : public StaticSlot {
74 public: 127 public:
75 // Calls StaticSlot::Initialize(). 128 // Calls StaticSlot::Initialize().
76 explicit Slot(TLSDestructorFunc destructor = NULL); 129 explicit Slot(TLSDestructorFunc destructor = NULL);
77 130
78 private: 131 private:
79 using StaticSlot::initialized_; 132 using StaticSlot::initialized_;
80 #if defined(OS_WIN)
81 using StaticSlot::slot_; 133 using StaticSlot::slot_;
82 #elif defined(OS_POSIX) 134
83 using StaticSlot::key_;
84 #endif
85 DISALLOW_COPY_AND_ASSIGN(Slot); 135 DISALLOW_COPY_AND_ASSIGN(Slot);
86 }; 136 };
87 137
88 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); 138 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
89 }; 139 };
90 140
91 } // namespace base 141 } // namespace base
92 142
93 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_ 143 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_
OLDNEW
« no previous file with comments | « base/base.gypi ('k') | base/threading/thread_local_storage.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698