blob: 75da8ae68cb6559a9d1ab8df77a37302ebad5c22 [file] [log] [blame]
Kostya Serebryany019b76f2011-11-30 01:07:021//===-- 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).
Kostya Serebryanya82f0d42012-01-10 21:24:4022// We define the class using opaque storage to avoid including system headers.
Kostya Serebryany019b76f2011-11-30 01:07:0223
Kostya Serebryany019b76f2011-11-30 01:07:0224namespace __asan {
Kostya Serebryanya82f0d42012-01-10 21:24:4025
Kostya Serebryany019b76f2011-11-30 01:07:0226class AsanLock {
27 public:
Kostya Serebryanya82f0d42012-01-10 21:24:4028 explicit AsanLock(LinkerInitialized);
29 void Lock();
30 void Unlock();
31 bool IsLocked() { return owner_ != 0; }
Kostya Serebryany019b76f2011-11-30 01:07:0232 private:
Kostya Serebryanya82f0d42012-01-10 21:24:4033 uintptr_t opaque_storage_[10];
34 uintptr_t owner_; // for debugging and for malloc_introspection_t interface
Kostya Serebryany019b76f2011-11-30 01:07:0235};
Kostya Serebryany019b76f2011-11-30 01:07:0236
Kostya Serebryany019b76f2011-11-30 01:07:0237class 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