]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/scudo/scudo_tsd.h
Upgrade our copies of clang, llvm, lld, lldb, compiler-rt and libc++ to
[FreeBSD/FreeBSD.git] / contrib / compiler-rt / lib / scudo / scudo_tsd.h
1 //===-- scudo_tsd.h ---------------------------------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// Scudo thread specific data definition.
11 /// Implementation will differ based on the thread local storage primitives
12 /// offered by the underlying platform.
13 ///
14 //===----------------------------------------------------------------------===//
15
16 #ifndef SCUDO_TSD_H_
17 #define SCUDO_TSD_H_
18
19 #include "scudo_allocator.h"
20 #include "scudo_utils.h"
21
22 #include <pthread.h>
23
24 namespace __scudo {
25
26 struct ALIGNED(64) ScudoTSD {
27   AllocatorCache Cache;
28   uptr QuarantineCachePlaceHolder[4];
29
30   void init(bool Shared);
31   void commitBack();
32
33   INLINE bool tryLock() {
34     if (Mutex.TryLock()) {
35       atomic_store_relaxed(&Precedence, 0);
36       return true;
37     }
38     if (atomic_load_relaxed(&Precedence) == 0)
39       atomic_store_relaxed(&Precedence, MonotonicNanoTime());
40     return false;
41   }
42
43   INLINE void lock() {
44     Mutex.Lock();
45     atomic_store_relaxed(&Precedence, 0);
46   }
47
48   INLINE void unlock() {
49     if (!UnlockRequired)
50       return;
51     Mutex.Unlock();
52   }
53
54   INLINE u64 getPrecedence() {
55     return atomic_load_relaxed(&Precedence);
56   }
57
58  private:
59   bool UnlockRequired;
60   StaticSpinMutex Mutex;
61   atomic_uint64_t Precedence;
62 };
63
64 void initThread(bool MinimalInit);
65
66 // TSD model specific fastpath functions definitions.
67 #include "scudo_tsd_exclusive.inc"
68 #include "scudo_tsd_shared.inc"
69
70 }  // namespace __scudo
71
72 #endif  // SCUDO_TSD_H_