Path: blob/main/contrib/llvm-project/compiler-rt/lib/scudo/standalone/mutex.h
35291 views
//===-- mutex.h -------------------------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef SCUDO_MUTEX_H_9#define SCUDO_MUTEX_H_1011#include "atomic_helpers.h"12#include "common.h"13#include "thread_annotations.h"1415#include <string.h>1617#if SCUDO_FUCHSIA18#include <lib/sync/mutex.h> // for sync_mutex_t19#endif2021namespace scudo {2223class CAPABILITY("mutex") HybridMutex {24public:25bool tryLock() TRY_ACQUIRE(true);26NOINLINE void lock() ACQUIRE() {27if (LIKELY(tryLock()))28return;29// The compiler may try to fully unroll the loop, ending up in a30// NumberOfTries*NumberOfYields block of pauses mixed with tryLocks. This31// is large, ugly and unneeded, a compact loop is better for our purpose32// here. Use a pragma to tell the compiler not to unroll the loop.33#ifdef __clang__34#pragma nounroll35#endif36for (u8 I = 0U; I < NumberOfTries; I++) {37delayLoop();38if (tryLock())39return;40}41lockSlow();42}43void unlock() RELEASE();4445// TODO(chiahungduan): In general, we may want to assert the owner of lock as46// well. Given the current uses of HybridMutex, it's acceptable without47// asserting the owner. Re-evaluate this when we have certain scenarios which48// requires a more fine-grained lock granularity.49ALWAYS_INLINE void assertHeld() ASSERT_CAPABILITY(this) {50if (SCUDO_DEBUG)51assertHeldImpl();52}5354private:55void delayLoop() {56// The value comes from the average time spent in accessing caches (which57// are the fastest operations) so that we are unlikely to wait too long for58// fast operations.59constexpr u32 SpinTimes = 16;60volatile u32 V = 0;61for (u32 I = 0; I < SpinTimes; ++I) {62u32 Tmp = V + 1;63V = Tmp;64}65}6667void assertHeldImpl();6869// TODO(chiahungduan): Adapt this value based on scenarios. E.g., primary and70// secondary allocator have different allocation times.71static constexpr u8 NumberOfTries = 32U;7273#if SCUDO_LINUX74atomic_u32 M = {};75#elif SCUDO_FUCHSIA76sync_mutex_t M = {};77#endif7879void lockSlow() ACQUIRE();80};8182class SCOPED_CAPABILITY ScopedLock {83public:84explicit ScopedLock(HybridMutex &M) ACQUIRE(M) : Mutex(M) { Mutex.lock(); }85~ScopedLock() RELEASE() { Mutex.unlock(); }8687private:88HybridMutex &Mutex;8990ScopedLock(const ScopedLock &) = delete;91void operator=(const ScopedLock &) = delete;92};9394} // namespace scudo9596#endif // SCUDO_MUTEX_H_979899