asan_lock.h revision 3f4c3875c42078e22c7e5356c5746fd18756d958
1//===-- asan_lock.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// This file is a part of AddressSanitizer, an address sanity checker.
11//
12// A wrapper for a simple lock.
13//===----------------------------------------------------------------------===//
14#ifndef ASAN_LOCK_H
15#define ASAN_LOCK_H
16
17#include "asan_internal.h"
18
19// The locks in ASan are global objects and they are never destroyed to avoid
20// at-exit races (that is, a lock is being used by other threads while the main
21// thread is doing atexit destructors).
22// We define the class using opaque storage to avoid including system headers.
23
24namespace __asan {
25
26class AsanLock {
27 public:
28  explicit AsanLock(LinkerInitialized);
29  void Lock();
30  void Unlock();
31  bool IsLocked() { return owner_ != 0; }
32 private:
33  uptr opaque_storage_[10];
34  uptr owner_;  // for debugging and for malloc_introspection_t interface
35};
36
37class ScopedLock {
38 public:
39  explicit ScopedLock(AsanLock *mu) : mu_(mu) {
40    mu_->Lock();
41  }
42  ~ScopedLock() {
43    mu_->Unlock();
44  }
45 private:
46  AsanLock *mu_;
47};
48
49}  // namespace __asan
50
51#endif  // ASAN_LOCK_H
52