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

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
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 // see thread_local_storage.h.
25 class BASE_EXPORT PlatformThreadLocalStorage {
26 public:
27
28 #if defined(OS_WIN)
29 typedef unsigned long TLSKey;
30 enum { TLS_KEY_OUT_OF_INDEXES = TLS_OUT_OF_INDEXES };
31 #elif defined(OS_POSIX)
32 typedef pthread_key_t TLSKey;
33 // In theory, there is no invalid key in Posix, It is critical to define
34 // one, so we can use minimal system service to implement TLS.
35 // It is almost imposible to get the defined invalid key, if we do get it,
36 // just alloc another one.
jar (doing other things) 2013/11/26 20:05:24 nit: I don't understand the comment. Perhaps you
michaelbai 2013/11/27 00:29:26 Correct, update the comment. On 2013/11/26 20:05:
37 enum { TLS_KEY_OUT_OF_INDEXES = 0x7FFFFFFF };
38 #endif
39 // Cross-platform APIs
jar (doing other things) 2013/11/26 20:05:24 nit: Better comment is: The following methods nee
michaelbai 2013/11/27 00:29:26 Done.
40 static bool AllocTLS(TLSKey* key);
jar (doing other things) 2013/11/26 20:05:24 You need a comment above line 40 this describing t
michaelbai 2013/11/27 00:29:26 Done.
41 static void FreeTLS(TLSKey key);
jar (doing other things) 2013/11/26 20:05:24 Why do we need to Free our OS slot? Why do we car
michaelbai 2013/11/27 00:29:26 I think it just here for code looks clean. On 201
42 static void SetTLSValue(TLSKey key, void* value);
43 static void* GetTLSValue(TLSKey key);
44
45 // This is the TLS destructor, the implementation of cross-platform APIs
jar (doing other things) 2013/11/26 20:05:24 Nit: Delete the phrase "This is the TLS destructor
michaelbai 2013/11/27 00:29:26 Done.
46 // should call this method on the terminating thread when a thread exits,
47 // this function calls any TLS destructors that were passed in from
48 // ThreadLocalStorage::StaticSlot::Initialize() and are pending for the
49 // thread.
50 static void OnThreadExit();
51 };
52
53 } // namespace internal
54
17 // Wrapper for thread local storage. This class doesn't do much except provide 55 // Wrapper for thread local storage. This class doesn't do much except provide
18 // an API for portability. 56 // an API for portability.
19 class BASE_EXPORT ThreadLocalStorage { 57 class BASE_EXPORT ThreadLocalStorage {
20 public: 58 public:
21 59
22 // Prototype for the TLS destructor function, which can be optionally used to 60 // 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 61 // cleanup thread local storage on thread exit. 'value' is the data that is
24 // stored in thread local storage. 62 // stored in thread local storage.
25 typedef void (*TLSDestructorFunc)(void* value); 63 typedef void (*TLSDestructorFunc)(void* value);
26 64
(...skipping 26 matching lines...) Expand all
53 void* Get() const; 91 void* Get() const;
54 92
55 // Set the thread-local value stored in slot 'slot' to 93 // Set the thread-local value stored in slot 'slot' to
56 // value 'value'. 94 // value 'value'.
57 void Set(void* value); 95 void Set(void* value);
58 96
59 bool initialized() const { return initialized_; } 97 bool initialized() const { return initialized_; }
60 98
61 // The internals of this struct should be considered private. 99 // The internals of this struct should be considered private.
62 bool initialized_; 100 bool initialized_;
63 #if defined(OS_WIN)
64 int slot_; 101 int slot_;
65 #elif defined(OS_POSIX)
66 pthread_key_t key_;
67 #endif
68
69 }; 102 };
70 103
71 // A convenience wrapper around StaticSlot with a constructor. Can be used 104 // A convenience wrapper around StaticSlot with a constructor. Can be used
72 // as a member variable. 105 // as a member variable.
73 class BASE_EXPORT Slot : public StaticSlot { 106 class BASE_EXPORT Slot : public StaticSlot {
74 public: 107 public:
75 // Calls StaticSlot::Initialize(). 108 // Calls StaticSlot::Initialize().
76 explicit Slot(TLSDestructorFunc destructor = NULL); 109 explicit Slot(TLSDestructorFunc destructor = NULL);
77 110
78 private: 111 private:
79 using StaticSlot::initialized_; 112 using StaticSlot::initialized_;
80 #if defined(OS_WIN)
81 using StaticSlot::slot_; 113 using StaticSlot::slot_;
82 #elif defined(OS_POSIX) 114
83 using StaticSlot::key_;
84 #endif
85 DISALLOW_COPY_AND_ASSIGN(Slot); 115 DISALLOW_COPY_AND_ASSIGN(Slot);
86 }; 116 };
87 117
88 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage); 118 DISALLOW_COPY_AND_ASSIGN(ThreadLocalStorage);
89 }; 119 };
90 120
91 } // namespace base 121 } // namespace base
92 122
93 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_ 123 #endif // BASE_THREADING_THREAD_LOCAL_STORAGE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698