mutex.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //===-- mutex.h -------------------------------------------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #ifndef GWP_ASAN_MUTEX_H_
  9. #define GWP_ASAN_MUTEX_H_
  10. #include "gwp_asan/platform_specific/mutex_fuchsia.h" // IWYU pragma: keep
  11. #include "gwp_asan/platform_specific/mutex_posix.h" // IWYU pragma: keep
  12. namespace gwp_asan {
  13. class Mutex final : PlatformMutex {
  14. public:
  15. constexpr Mutex() = default;
  16. ~Mutex() = default;
  17. Mutex(const Mutex &) = delete;
  18. Mutex &operator=(const Mutex &) = delete;
  19. // Lock the mutex.
  20. void lock();
  21. // Nonblocking trylock of the mutex. Returns true if the lock was acquired.
  22. bool tryLock();
  23. // Unlock the mutex.
  24. void unlock();
  25. };
  26. class ScopedLock {
  27. public:
  28. explicit ScopedLock(Mutex &Mx) : Mu(Mx) { Mu.lock(); }
  29. ~ScopedLock() { Mu.unlock(); }
  30. ScopedLock(const ScopedLock &) = delete;
  31. ScopedLock &operator=(const ScopedLock &) = delete;
  32. private:
  33. Mutex Μ
  34. };
  35. } // namespace gwp_asan
  36. #endif // GWP_ASAN_MUTEX_H_