summaryrefslogtreecommitdiff
path: root/lib/scudo/scudo_tls_context_android.inc
blob: 4787ec7b613b9cd9de2bc2ffde02bce05e2b4209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//===-- scudo_tls_context_android.inc ---------------------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// Android specific base thread context definition.
///
//===----------------------------------------------------------------------===//

#ifndef SCUDO_TLS_CONTEXT_ANDROID_INC_
#define SCUDO_TLS_CONTEXT_ANDROID_INC_

#ifndef SCUDO_TLS_H_
# error "This file must be included inside scudo_tls.h."
#endif  // SCUDO_TLS_H_

#if SANITIZER_LINUX && SANITIZER_ANDROID

struct ScudoTSDPlatform {
  INLINE bool tryLock() {
    if (Mutex.TryLock()) {
      atomic_store_relaxed(&Precedence, 0);
      return true;
    }
    if (atomic_load_relaxed(&Precedence) == 0)
      atomic_store_relaxed(&Precedence, NanoTime());
    return false;
  }

  INLINE void lock() {
    Mutex.Lock();
    atomic_store_relaxed(&Precedence, 0);
  }

  INLINE void unlock() {
    Mutex.Unlock();
  }

  INLINE u64 getPrecedence() {
    return atomic_load_relaxed(&Precedence);
  }

 private:
  StaticSpinMutex Mutex;
  atomic_uint64_t Precedence;
};

#endif  // SANITIZER_LINUX && SANITIZER_ANDROID

#endif  // SCUDO_TLS_CONTEXT_ANDROID_INC_