1*0b57cec5SDimitry Andric //===-- tsd.h ---------------------------------------------------*- C++ -*-===// 2*0b57cec5SDimitry Andric // 3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6*0b57cec5SDimitry Andric // 7*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 8*0b57cec5SDimitry Andric 9*0b57cec5SDimitry Andric #ifndef SCUDO_TSD_H_ 10*0b57cec5SDimitry Andric #define SCUDO_TSD_H_ 11*0b57cec5SDimitry Andric 12*0b57cec5SDimitry Andric #include "atomic_helpers.h" 13*0b57cec5SDimitry Andric #include "common.h" 14*0b57cec5SDimitry Andric #include "mutex.h" 15*0b57cec5SDimitry Andric 16*0b57cec5SDimitry Andric #include <limits.h> // for PTHREAD_DESTRUCTOR_ITERATIONS 17*0b57cec5SDimitry Andric 18*0b57cec5SDimitry Andric // With some build setups, this might still not be defined. 19*0b57cec5SDimitry Andric #ifndef PTHREAD_DESTRUCTOR_ITERATIONS 20*0b57cec5SDimitry Andric #define PTHREAD_DESTRUCTOR_ITERATIONS 4 21*0b57cec5SDimitry Andric #endif 22*0b57cec5SDimitry Andric 23*0b57cec5SDimitry Andric namespace scudo { 24*0b57cec5SDimitry Andric 25*0b57cec5SDimitry Andric template <class Allocator> struct ALIGNED(SCUDO_CACHE_LINE_SIZE) TSD { 26*0b57cec5SDimitry Andric typename Allocator::CacheT Cache; 27*0b57cec5SDimitry Andric typename Allocator::QuarantineCacheT QuarantineCache; 28*0b57cec5SDimitry Andric u8 DestructorIterations; 29*0b57cec5SDimitry Andric 30*0b57cec5SDimitry Andric void initLinkerInitialized(Allocator *Instance) { 31*0b57cec5SDimitry Andric Instance->initCache(&Cache); 32*0b57cec5SDimitry Andric DestructorIterations = PTHREAD_DESTRUCTOR_ITERATIONS; 33*0b57cec5SDimitry Andric } 34*0b57cec5SDimitry Andric void init(Allocator *Instance) { 35*0b57cec5SDimitry Andric memset(this, 0, sizeof(*this)); 36*0b57cec5SDimitry Andric initLinkerInitialized(Instance); 37*0b57cec5SDimitry Andric } 38*0b57cec5SDimitry Andric 39*0b57cec5SDimitry Andric void commitBack(Allocator *Instance) { Instance->commitBack(this); } 40*0b57cec5SDimitry Andric 41*0b57cec5SDimitry Andric INLINE bool tryLock() { 42*0b57cec5SDimitry Andric if (Mutex.tryLock()) { 43*0b57cec5SDimitry Andric atomic_store_relaxed(&Precedence, 0); 44*0b57cec5SDimitry Andric return true; 45*0b57cec5SDimitry Andric } 46*0b57cec5SDimitry Andric if (atomic_load_relaxed(&Precedence) == 0) 47*0b57cec5SDimitry Andric atomic_store_relaxed( 48*0b57cec5SDimitry Andric &Precedence, 49*0b57cec5SDimitry Andric static_cast<uptr>(getMonotonicTime() >> FIRST_32_SECOND_64(16, 0))); 50*0b57cec5SDimitry Andric return false; 51*0b57cec5SDimitry Andric } 52*0b57cec5SDimitry Andric INLINE void lock() { 53*0b57cec5SDimitry Andric atomic_store_relaxed(&Precedence, 0); 54*0b57cec5SDimitry Andric Mutex.lock(); 55*0b57cec5SDimitry Andric } 56*0b57cec5SDimitry Andric INLINE void unlock() { Mutex.unlock(); } 57*0b57cec5SDimitry Andric INLINE uptr getPrecedence() { return atomic_load_relaxed(&Precedence); } 58*0b57cec5SDimitry Andric 59*0b57cec5SDimitry Andric private: 60*0b57cec5SDimitry Andric HybridMutex Mutex; 61*0b57cec5SDimitry Andric atomic_uptr Precedence; 62*0b57cec5SDimitry Andric }; 63*0b57cec5SDimitry Andric 64*0b57cec5SDimitry Andric } // namespace scudo 65*0b57cec5SDimitry Andric 66*0b57cec5SDimitry Andric #endif // SCUDO_TSD_H_ 67