1 //===-- tsd.h ---------------------------------------------------*- 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 //===----------------------------------------------------------------------===//
12 #include "atomic_helpers.h"
16 #include <limits.h> // for PTHREAD_DESTRUCTOR_ITERATIONS
18 // With some build setups, this might still not be defined.
19 #ifndef PTHREAD_DESTRUCTOR_ITERATIONS
20 #define PTHREAD_DESTRUCTOR_ITERATIONS 4
25 template <class Allocator> struct ALIGNED(SCUDO_CACHE_LINE_SIZE) TSD {
26 typename Allocator::CacheT Cache;
27 typename Allocator::QuarantineCacheT QuarantineCache;
28 u8 DestructorIterations;
30 void initLinkerInitialized(Allocator *Instance) {
31 Instance->initCache(&Cache);
32 DestructorIterations = PTHREAD_DESTRUCTOR_ITERATIONS;
34 void init(Allocator *Instance) {
35 memset(this, 0, sizeof(*this));
36 initLinkerInitialized(Instance);
39 void commitBack(Allocator *Instance) { Instance->commitBack(this); }
41 INLINE bool tryLock() {
42 if (Mutex.tryLock()) {
43 atomic_store_relaxed(&Precedence, 0);
46 if (atomic_load_relaxed(&Precedence) == 0)
49 static_cast<uptr>(getMonotonicTime() >> FIRST_32_SECOND_64(16, 0)));
53 atomic_store_relaxed(&Precedence, 0);
56 INLINE void unlock() { Mutex.unlock(); }
57 INLINE uptr getPrecedence() { return atomic_load_relaxed(&Precedence); }
61 atomic_uptr Precedence;
66 #endif // SCUDO_TSD_H_