]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/compiler-rt/lib/scudo/scudo_tsd.h
Merge clang 7.0.1 and several follow-up changes
[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(SANITIZER_CACHE_LINE_SIZE) ScudoTSD {
27   AllocatorCacheT Cache;
28   uptr QuarantineCachePlaceHolder[4];
29
30   void init();
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, static_cast<uptr>(
40           MonotonicNanoTime() >> FIRST_32_SECOND_64(16, 0)));
41     return false;
42   }
43
44   INLINE void lock() {
45     atomic_store_relaxed(&Precedence, 0);
46     Mutex.Lock();
47   }
48
49   INLINE void unlock() { Mutex.Unlock(); }
50
51   INLINE uptr getPrecedence() { return atomic_load_relaxed(&Precedence); }
52
53  private:
54   StaticSpinMutex Mutex;
55   atomic_uintptr_t Precedence;
56 };
57
58 void initThread(bool MinimalInit);
59
60 // TSD model specific fastpath functions definitions.
61 #include "scudo_tsd_exclusive.inc"
62 #include "scudo_tsd_shared.inc"
63
64 }  // namespace __scudo
65
66 #endif  // SCUDO_TSD_H_