1 | /* |
2 | * Copyright (C) 2008, 2016 Apple Inc. All rights reserved. |
3 | * Copyright (C) 2009 Jian Li <[email protected]> |
4 | * Copyright (C) 2012 Patrick Gansterer <[email protected]> |
5 | * |
6 | * Redistribution and use in source and binary forms, with or without |
7 | * modification, are permitted provided that the following conditions |
8 | * are met: |
9 | * |
10 | * 1. Redistributions of source code must retain the above copyright |
11 | * notice, this list of conditions and the following disclaimer. |
12 | * 2. Redistributions in binary form must reproduce the above copyright |
13 | * notice, this list of conditions and the following disclaimer in the |
14 | * documentation and/or other materials provided with the distribution. |
15 | * 3. Neither the name of Apple Inc. ("Apple") nor the names of |
16 | * its contributors may be used to endorse or promote products derived |
17 | * from this software without specific prior written permission. |
18 | * |
19 | * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY |
20 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
21 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
22 | * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
23 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
24 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
25 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
26 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
28 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | */ |
30 | |
31 | /* Thread local storage is implemented by using either pthread API or Windows |
32 | * native API. There is subtle semantic discrepancy for the cleanup function |
33 | * implementation as noted below: |
34 | * @ In pthread implementation, the destructor function will be called |
35 | * repeatedly if there is still non-NULL value associated with the function. |
36 | * @ In Windows native implementation, the destructor function will be called |
37 | * only once. |
38 | * This semantic discrepancy does not impose any problem because nowhere in |
39 | * WebKit the repeated call bahavior is utilized. |
40 | */ |
41 | |
42 | #pragma once |
43 | |
44 | #include <wtf/MainThread.h> |
45 | #include <wtf/Noncopyable.h> |
46 | #include <wtf/StdLibExtras.h> |
47 | #include <wtf/Threading.h> |
48 | |
49 | namespace WTF { |
50 | |
51 | enum class CanBeGCThread { |
52 | False, |
53 | True |
54 | }; |
55 | |
56 | template<typename T, CanBeGCThread canBeGCThread = CanBeGCThread::False> class ThreadSpecific { |
57 | WTF_MAKE_NONCOPYABLE(ThreadSpecific); |
58 | WTF_MAKE_FAST_ALLOCATED; |
59 | public: |
60 | ThreadSpecific(); |
61 | bool isSet(); // Useful as a fast check to see if this thread has set this value. |
62 | T* operator->(); |
63 | operator T*(); |
64 | T& operator*(); |
65 | |
66 | private: |
67 | // Not implemented. It's technically possible to destroy a thread specific key, but one would need |
68 | // to make sure that all values have been destroyed already (usually, that all threads that used it |
69 | // have exited). It's unlikely that any user of this call will be in that situation - and having |
70 | // a destructor defined can be confusing, given that it has such strong pre-requisites to work correctly. |
71 | ~ThreadSpecific(); |
72 | |
73 | struct Data { |
74 | WTF_MAKE_NONCOPYABLE(Data); |
75 | WTF_MAKE_FAST_ALLOCATED; |
76 | public: |
77 | using PointerType = typename std::remove_const<T>::type*; |
78 | |
79 | Data(ThreadSpecific<T, canBeGCThread>* owner) |
80 | : owner(owner) |
81 | { |
82 | // Set up thread-specific value's memory pointer before invoking constructor, in case any function it calls |
83 | // needs to access the value, to avoid recursion. |
84 | owner->setInTLS(this); |
85 | new (NotNull, storagePointer()) T(); |
86 | } |
87 | |
88 | ~Data() |
89 | { |
90 | storagePointer()->~T(); |
91 | owner->setInTLS(nullptr); |
92 | } |
93 | |
94 | PointerType storagePointer() const { return const_cast<PointerType>(reinterpret_cast<const T*>(&m_storage)); } |
95 | |
96 | typename std::aligned_storage<sizeof(T), std::alignment_of<T>::value>::type m_storage; |
97 | ThreadSpecific<T, canBeGCThread>* owner; |
98 | }; |
99 | |
100 | T* get(); |
101 | T* set(); |
102 | void setInTLS(Data*); |
103 | void static destroy(void* ptr); |
104 | |
105 | #if USE(PTHREADS) |
106 | pthread_key_t m_key { }; |
107 | #elif OS(WINDOWS) |
108 | int m_key; |
109 | #endif |
110 | }; |
111 | |
112 | #if USE(PTHREADS) |
113 | |
114 | template<typename T, CanBeGCThread canBeGCThread> |
115 | inline ThreadSpecific<T, canBeGCThread>::ThreadSpecific() |
116 | { |
117 | int error = pthread_key_create(&m_key, destroy); |
118 | if (error) |
119 | CRASH(); |
120 | } |
121 | |
122 | template<typename T, CanBeGCThread canBeGCThread> |
123 | inline T* ThreadSpecific<T, canBeGCThread>::get() |
124 | { |
125 | Data* data = static_cast<Data*>(pthread_getspecific(m_key)); |
126 | if (data) |
127 | return data->storagePointer(); |
128 | return nullptr; |
129 | } |
130 | |
131 | template<typename T, CanBeGCThread canBeGCThread> |
132 | inline void ThreadSpecific<T, canBeGCThread>::setInTLS(Data* data) |
133 | { |
134 | pthread_setspecific(m_key, data); |
135 | } |
136 | |
137 | #elif OS(WINDOWS) |
138 | |
139 | template<typename T, CanBeGCThread canBeGCThread> |
140 | inline ThreadSpecific<T, canBeGCThread>::ThreadSpecific() |
141 | : m_key(-1) |
142 | { |
143 | bool ok = Thread::SpecificStorage::allocateKey(m_key, destroy); |
144 | if (!ok) |
145 | CRASH(); |
146 | } |
147 | |
148 | template<typename T, CanBeGCThread canBeGCThread> |
149 | inline T* ThreadSpecific<T, canBeGCThread>::get() |
150 | { |
151 | auto data = static_cast<Data*>(Thread::current().specificStorage().get(m_key)); |
152 | if (!data) |
153 | return nullptr; |
154 | return data->storagePointer(); |
155 | } |
156 | |
157 | template<typename T, CanBeGCThread canBeGCThread> |
158 | inline void ThreadSpecific<T, canBeGCThread>::setInTLS(Data* data) |
159 | { |
160 | return Thread::current().specificStorage().set(m_key, data); |
161 | } |
162 | |
163 | #else |
164 | #error ThreadSpecific is not implemented for this platform. |
165 | #endif |
166 | |
167 | template<typename T, CanBeGCThread canBeGCThread> |
168 | inline void ThreadSpecific<T, canBeGCThread>::destroy(void* ptr) |
169 | { |
170 | Data* data = static_cast<Data*>(ptr); |
171 | |
172 | #if USE(PTHREADS) |
173 | // We want get() to keep working while data destructor works, because it can be called indirectly by the destructor. |
174 | // Some pthreads implementations zero out the pointer before calling destroy(), so we temporarily reset it. |
175 | pthread_setspecific(data->owner->m_key, ptr); |
176 | #endif |
177 | |
178 | delete data; |
179 | } |
180 | |
181 | template<typename T, CanBeGCThread canBeGCThread> |
182 | inline T* ThreadSpecific<T, canBeGCThread>::set() |
183 | { |
184 | RELEASE_ASSERT(canBeGCThread == CanBeGCThread::True || !Thread::mayBeGCThread()); |
185 | ASSERT(!get()); |
186 | Data* data = new Data(this); // Data will set itself into TLS. |
187 | ASSERT(get() == data->storagePointer()); |
188 | return data->storagePointer(); |
189 | } |
190 | |
191 | template<typename T, CanBeGCThread canBeGCThread> |
192 | inline bool ThreadSpecific<T, canBeGCThread>::isSet() |
193 | { |
194 | return !!get(); |
195 | } |
196 | |
197 | template<typename T, CanBeGCThread canBeGCThread> |
198 | inline ThreadSpecific<T, canBeGCThread>::operator T*() |
199 | { |
200 | if (T* ptr = get()) |
201 | return ptr; |
202 | return set(); |
203 | } |
204 | |
205 | template<typename T, CanBeGCThread canBeGCThread> |
206 | inline T* ThreadSpecific<T, canBeGCThread>::operator->() |
207 | { |
208 | return operator T*(); |
209 | } |
210 | |
211 | template<typename T, CanBeGCThread canBeGCThread> |
212 | inline T& ThreadSpecific<T, canBeGCThread>::operator*() |
213 | { |
214 | return *operator T*(); |
215 | } |
216 | |
217 | } // namespace WTF |
218 | |
219 | using WTF::ThreadSpecific; |
220 | |