1 //===-- mutex.h -------------------------------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 #define SCUDO_MUTEX_H_
12 #include "atomic_helpers.h"
18 #include <lib/sync/mutex.h> // for sync_mutex_t
25 void init() { memset(this, 0, sizeof(*this)); }
27 NOINLINE void lock() {
28 if (LIKELY(tryLock()))
30 // The compiler may try to fully unroll the loop, ending up in a
31 // NumberOfTries*NumberOfYields block of pauses mixed with tryLocks. This
32 // is large, ugly and unneeded, a compact loop is better for our purpose
33 // here. Use a pragma to tell the compiler not to unroll the loop.
37 for (u8 I = 0U; I < NumberOfTries; I++) {
38 yieldProcessor(NumberOfYields);
47 static constexpr u8 NumberOfTries = 8U;
48 static constexpr u8 NumberOfYields = 8U;
61 explicit ScopedLock(HybridMutex &M) : Mutex(M) { Mutex.lock(); }
62 ~ScopedLock() { Mutex.unlock(); }
67 ScopedLock(const ScopedLock &) = delete;
68 void operator=(const ScopedLock &) = delete;
73 #endif // SCUDO_MUTEX_H_