| 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 | |
| 9 | #ifndef GWP_ASAN_MUTEX_H_ |
| 10 | #define GWP_ASAN_MUTEX_H_ |
| 11 | |
| 12 | #include "gwp_asan/platform_specific/mutex_fuchsia.h" // IWYU pragma: keep |
| 13 | #include "gwp_asan/platform_specific/mutex_posix.h" // IWYU pragma: keep |
| 14 | |
| 15 | namespace gwp_asan { |
| 16 | class Mutex final : PlatformMutex { |
| 17 | public: |
| 18 | constexpr Mutex() = default; |
| 19 | ~Mutex() = default; |
| 20 | Mutex(const Mutex &) = delete; |
| 21 | Mutex &operator=(const Mutex &) = delete; |
| 22 | // Lock the mutex. |
| 23 | void lock(); |
| 24 | // Nonblocking trylock of the mutex. Returns true if the lock was acquired. |
| 25 | bool tryLock(); |
| 26 | // Unlock the mutex. |
| 27 | void unlock(); |
| 28 | }; |
| 29 | |
| 30 | class ScopedLock { |
| 31 | public: |
| 32 | explicit ScopedLock(Mutex &Mx) : Mu(Mx) { Mu.lock(); } |
| 33 | ~ScopedLock() { Mu.unlock(); } |
| 34 | ScopedLock(const ScopedLock &) = delete; |
| 35 | ScopedLock &operator=(const ScopedLock &) = delete; |
| 36 | |
| 37 | private: |
| 38 | Mutex Μ |
| 39 | }; |
| 40 | } // namespace gwp_asan |
| 41 | |
| 42 | #endif // GWP_ASAN_MUTEX_H_ |
| 43 | |