1 //===-- scudo_tsd_exclusive.cpp ---------------------------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 /// Scudo exclusive TSD implementation.
11 //===----------------------------------------------------------------------===//
13 #include "scudo_tsd.h"
15 #if SCUDO_TSD_EXCLUSIVE
19 static pthread_once_t GlobalInitialized = PTHREAD_ONCE_INIT;
20 static pthread_key_t PThreadKey;
22 __attribute__((tls_model("initial-exec")))
23 THREADLOCAL ThreadState ScudoThreadState = ThreadNotInitialized;
24 __attribute__((tls_model("initial-exec")))
25 THREADLOCAL ScudoTSD TSD;
27 // Fallback TSD for when the thread isn't initialized yet or is torn down. It
28 // can be shared between multiple threads and as such must be locked.
31 static void teardownThread(void *Ptr) {
32 uptr I = reinterpret_cast<uptr>(Ptr);
33 // The glibc POSIX thread-local-storage deallocation routine calls user
34 // provided destructors in a loop of PTHREAD_DESTRUCTOR_ITERATIONS.
35 // We want to be called last since other destructors might call free and the
36 // like, so we wait until PTHREAD_DESTRUCTOR_ITERATIONS before draining the
37 // quarantine and swallowing the cache.
39 // If pthread_setspecific fails, we will go ahead with the teardown.
40 if (LIKELY(pthread_setspecific(PThreadKey,
41 reinterpret_cast<void *>(I - 1)) == 0))
45 ScudoThreadState = ThreadTornDown;
49 static void initOnce() {
50 CHECK_EQ(pthread_key_create(&PThreadKey, teardownThread), 0);
55 void initThread(bool MinimalInit) {
56 CHECK_EQ(pthread_once(&GlobalInitialized, initOnce), 0);
57 if (UNLIKELY(MinimalInit))
59 CHECK_EQ(pthread_setspecific(PThreadKey, reinterpret_cast<void *>(
60 GetPthreadDestructorIterations())), 0);
62 ScudoThreadState = ThreadInitialized;
65 } // namespace __scudo
67 #endif // SCUDO_TSD_EXCLUSIVE