2 * Copyright 2017 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
27 #include <glog/logging.h>
29 #include <folly/Exception.h>
30 #include <folly/Foreach.h>
31 #include <folly/Function.h>
32 #include <folly/Malloc.h>
33 #include <folly/MicroSpinLock.h>
34 #include <folly/Portability.h>
35 #include <folly/ScopeGuard.h>
36 #include <folly/portability/PThread.h>
38 #include <folly/detail/StaticSingletonManager.h>
40 // In general, emutls cleanup is not guaranteed to play nice with the way
41 // StaticMeta mixes direct pthread calls and the use of __thread. This has
42 // caused problems on multiple platforms so don't use __thread there.
44 // XXX: Ideally we would instead determine if emutls is in use at runtime as it
45 // is possible to configure glibc on Linux to use emutls regardless.
46 #if !FOLLY_MOBILE && !defined(__APPLE__) && !defined(_MSC_VER)
47 #define FOLLY_TLD_USE_FOLLY_TLS 1
49 #undef FOLLY_TLD_USE_FOLLY_TLS
53 namespace threadlocal_detail {
56 * POD wrapper around an element (a void*) and an associated deleter.
57 * This must be POD, as we memset() it to 0 and memcpy() it around.
59 struct ElementWrapper {
60 using DeleterFunType = void(void*, TLPDestructionMode);
62 bool dispose(TLPDestructionMode mode) {
67 DCHECK(deleter1 != nullptr);
68 ownsDeleter ? (*deleter2)(ptr, mode) : (*deleter1)(ptr, mode);
85 auto guard = makeGuard([&] { delete p; });
86 DCHECK(ptr == nullptr);
87 DCHECK(deleter1 == nullptr);
91 deleter1 = [](void* pt, TLPDestructionMode) {
92 delete static_cast<Ptr>(pt);
99 template <class Ptr, class Deleter>
100 void set(Ptr p, const Deleter& d) {
101 auto guard = makeGuard([&] {
103 d(p, TLPDestructionMode::THIS_THREAD);
106 DCHECK(ptr == nullptr);
107 DCHECK(deleter2 == nullptr);
110 auto d2 = d; // gcc-4.8 doesn't decay types correctly in lambda captures
111 deleter2 = new std::function<DeleterFunType>(
112 [d2](void* pt, TLPDestructionMode mode) {
113 d2(static_cast<Ptr>(pt), mode);
131 DeleterFunType* deleter1;
132 std::function<DeleterFunType>* deleter2;
137 struct StaticMetaBase;
140 * Per-thread entry. Each thread using a StaticMeta object has one.
141 * This is written from the owning thread only (under the lock), read
142 * from the owning thread (no lock necessary), and read from other threads
146 ElementWrapper* elements{nullptr};
147 size_t elementsCapacity{0};
148 ThreadEntry* next{nullptr};
149 ThreadEntry* prev{nullptr};
150 StaticMetaBase* meta{nullptr};
153 constexpr uint32_t kEntryIDInvalid = std::numeric_limits<uint32_t>::max();
155 struct PthreadKeyUnregisterTester;
158 * We want to disable onThreadExit call at the end of shutdown, we don't care
159 * about leaking memory at that point.
161 * Otherwise if ThreadLocal is used in a shared library, onThreadExit may be
162 * called after dlclose().
164 * This class has one single static instance; however since it's so widely used,
165 * directly or indirectly, by so many classes, we need to take care to avoid
166 * problems stemming from the Static Initialization/Destruction Order Fiascos.
167 * Therefore this class needs to be constexpr-constructible, so as to avoid
168 * the need for this to participate in init/destruction order.
170 class PthreadKeyUnregister {
172 static constexpr size_t kMaxKeys = 1UL << 16;
174 ~PthreadKeyUnregister() {
175 // If static constructor priorities are not supported then
176 // ~PthreadKeyUnregister logic is not safe.
177 #if !defined(__APPLE__) && !defined(_MSC_VER)
180 pthread_key_delete(keys_[--size_]);
185 static void registerKey(pthread_key_t key) {
186 instance_.registerKeyImpl(key);
191 * Only one global instance should exist, hence this is private.
192 * See also the important note at the top of this class about `constexpr`
195 constexpr PthreadKeyUnregister() : lock_(), size_(0), keys_() { }
196 friend struct folly::threadlocal_detail::PthreadKeyUnregisterTester;
198 void registerKeyImpl(pthread_key_t key) {
200 if (size_ == kMaxKeys) {
201 throw std::logic_error("pthread_key limit has already been reached");
203 keys_[size_++] = key;
208 pthread_key_t keys_[kMaxKeys];
210 static PthreadKeyUnregister instance_;
213 struct StaticMetaBase {
214 // Represents an ID of a thread local object. Initially set to the maximum
215 // uint. This representation allows us to avoid a branch in accessing TLS data
216 // (because if you test capacity > id if id = maxint then the test will always
217 // fail). It allows us to keep a constexpr constructor and avoid SIOF.
220 std::atomic<uint32_t> value;
222 constexpr EntryID() : value(kEntryIDInvalid) {
225 EntryID(EntryID&& other) noexcept : value(other.value.load()) {
226 other.value = kEntryIDInvalid;
229 EntryID& operator=(EntryID&& other) {
230 assert(this != &other);
231 value = other.value.load();
232 other.value = kEntryIDInvalid;
236 EntryID(const EntryID& other) = delete;
237 EntryID& operator=(const EntryID& other) = delete;
239 uint32_t getOrInvalid() {
240 // It's OK for this to be relaxed, even though we're effectively doing
241 // double checked locking in using this value. We only care about the
242 // uniqueness of IDs, getOrAllocate does not modify any other memory
243 // this thread will use.
244 return value.load(std::memory_order_relaxed);
247 uint32_t getOrAllocate(StaticMetaBase& meta) {
248 uint32_t id = getOrInvalid();
249 if (id != kEntryIDInvalid) {
252 // The lock inside allocate ensures that a single value is allocated
253 return meta.allocate(this);
257 StaticMetaBase(ThreadEntry* (*threadEntry)(), bool strict);
259 [[noreturn]] ~StaticMetaBase() {
260 folly::assume_unreachable();
263 void push_back(ThreadEntry* t) {
265 t->prev = head_.prev;
266 head_.prev->next = t;
270 void erase(ThreadEntry* t) {
271 t->next->prev = t->prev;
272 t->prev->next = t->next;
273 t->next = t->prev = t;
276 static void onThreadExit(void* ptr);
278 uint32_t allocate(EntryID* ent);
280 void destroy(EntryID* ent);
283 * Reserve enough space in the ThreadEntry::elements for the item
286 void reserve(EntryID* id);
288 ElementWrapper& get(EntryID* ent);
290 static void initAtFork();
291 static void registerAtFork(
292 folly::Function<void()> prepare,
293 folly::Function<void()> parent,
294 folly::Function<void()> child);
297 std::vector<uint32_t> freeIds_;
299 SharedMutex accessAllThreadsLock_;
300 pthread_key_t pthreadKey_;
302 ThreadEntry* (*threadEntry_)();
306 // Held in a singleton to track our global instances.
307 // We have one of these per "Tag", by default one for the whole system
310 // Creating and destroying ThreadLocalPtr objects, as well as thread exit
311 // for threads that use ThreadLocalPtr objects collide on a lock inside
312 // StaticMeta; you can specify multiple Tag types to break that lock.
313 template <class Tag, class AccessMode>
314 struct StaticMeta : StaticMetaBase {
317 &StaticMeta::getThreadEntrySlow,
318 std::is_same<AccessMode, AccessModeStrict>::value) {
320 /*prepare*/ &StaticMeta::preFork,
321 /*parent*/ &StaticMeta::onForkParent,
322 /*child*/ &StaticMeta::onForkChild);
325 static StaticMeta<Tag, AccessMode>& instance() {
326 // Leak it on exit, there's only one per process and we don't have to
327 // worry about synchronization with exiting threads.
328 /* library-local */ static auto instance =
329 detail::createGlobal<StaticMeta<Tag, AccessMode>, void>();
333 ElementWrapper& get(EntryID* ent) {
334 ThreadEntry* threadEntry = getThreadEntry();
335 uint32_t id = ent->getOrInvalid();
336 // if id is invalid, it is equal to uint32_t's max value.
337 // x <= max value is always true
338 if (UNLIKELY(threadEntry->elementsCapacity <= id)) {
340 id = ent->getOrInvalid();
341 assert(threadEntry->elementsCapacity > id);
343 return threadEntry->elements[id];
346 static ThreadEntry* getThreadEntrySlow() {
347 auto& meta = instance();
348 auto key = meta.pthreadKey_;
349 ThreadEntry* threadEntry =
350 static_cast<ThreadEntry*>(pthread_getspecific(key));
352 #ifdef FOLLY_TLD_USE_FOLLY_TLS
353 static FOLLY_TLS ThreadEntry threadEntrySingleton;
354 threadEntry = &threadEntrySingleton;
356 threadEntry = new ThreadEntry();
358 threadEntry->meta = &meta;
359 int ret = pthread_setspecific(key, threadEntry);
360 checkPosixError(ret, "pthread_setspecific failed");
365 inline static ThreadEntry* getThreadEntry() {
366 #ifdef FOLLY_TLD_USE_FOLLY_TLS
367 static FOLLY_TLS ThreadEntry* threadEntryCache{nullptr};
368 if (UNLIKELY(threadEntryCache == nullptr)) {
369 threadEntryCache = instance().threadEntry_();
371 return threadEntryCache;
373 return instance().threadEntry_();
377 static void preFork(void) {
378 instance().lock_.lock(); // Make sure it's created
381 static void onForkParent(void) { instance().lock_.unlock(); }
383 static void onForkChild(void) {
384 // only the current thread survives
385 instance().head_.next = instance().head_.prev = &instance().head_;
386 ThreadEntry* threadEntry = getThreadEntry();
387 // If this thread was in the list before the fork, add it back.
388 if (threadEntry->elementsCapacity != 0) {
389 instance().push_back(threadEntry);
391 instance().lock_.unlock();
395 } // namespace threadlocal_detail