10b57cec5SDimitry Andric //===-- mutex.h -------------------------------------------------*- C++ -*-===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric 90b57cec5SDimitry Andric #ifndef GWP_ASAN_MUTEX_H_ 100b57cec5SDimitry Andric #define GWP_ASAN_MUTEX_H_ 110b57cec5SDimitry Andric 12*e8d8bef9SDimitry Andric #include "gwp_asan/platform_specific/mutex_fuchsia.h" // IWYU pragma: keep 13*e8d8bef9SDimitry Andric #include "gwp_asan/platform_specific/mutex_posix.h" // IWYU pragma: keep 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric namespace gwp_asan { 16*e8d8bef9SDimitry Andric class Mutex final : PlatformMutex { 170b57cec5SDimitry Andric public: 180b57cec5SDimitry Andric constexpr Mutex() = default; 190b57cec5SDimitry Andric ~Mutex() = default; 200b57cec5SDimitry Andric Mutex(const Mutex &) = delete; 210b57cec5SDimitry Andric Mutex &operator=(const Mutex &) = delete; 220b57cec5SDimitry Andric // Lock the mutex. 230b57cec5SDimitry Andric void lock(); 240b57cec5SDimitry Andric // Nonblocking trylock of the mutex. Returns true if the lock was acquired. 250b57cec5SDimitry Andric bool tryLock(); 260b57cec5SDimitry Andric // Unlock the mutex. 270b57cec5SDimitry Andric void unlock(); 280b57cec5SDimitry Andric }; 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric class ScopedLock { 310b57cec5SDimitry Andric public: ScopedLock(Mutex & Mx)320b57cec5SDimitry Andric explicit ScopedLock(Mutex &Mx) : Mu(Mx) { Mu.lock(); } ~ScopedLock()330b57cec5SDimitry Andric ~ScopedLock() { Mu.unlock(); } 340b57cec5SDimitry Andric ScopedLock(const ScopedLock &) = delete; 350b57cec5SDimitry Andric ScopedLock &operator=(const ScopedLock &) = delete; 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric private: 380b57cec5SDimitry Andric Mutex Μ 390b57cec5SDimitry Andric }; 400b57cec5SDimitry Andric } // namespace gwp_asan 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric #endif // GWP_ASAN_MUTEX_H_ 43