Path: blob/main/contrib/llvm-project/compiler-rt/lib/tsan/rtl/tsan_mutexset.h
35269 views
//===-- tsan_mutexset.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//===----------------------------------------------------------------------===//7//8// This file is a part of ThreadSanitizer (TSan), a race detector.9//10// MutexSet holds the set of mutexes currently held by a thread.11//===----------------------------------------------------------------------===//12#ifndef TSAN_MUTEXSET_H13#define TSAN_MUTEXSET_H1415#include "tsan_defs.h"1617namespace __tsan {1819class MutexSet {20public:21// Holds limited number of mutexes.22// The oldest mutexes are discarded on overflow.23static constexpr uptr kMaxSize = 16;24struct Desc {25uptr addr;26StackID stack_id;27u32 seq;28u32 count;29bool write;3031Desc() { internal_memset(this, 0, sizeof(*this)); }32Desc(const Desc& other) { *this = other; }33Desc& operator=(const MutexSet::Desc& other) {34internal_memcpy(this, &other, sizeof(*this));35return *this;36}37};3839MutexSet();40void Reset();41void AddAddr(uptr addr, StackID stack_id, bool write);42void DelAddr(uptr addr, bool destroy = false);43uptr Size() const;44Desc Get(uptr i) const;4546private:47#if !SANITIZER_GO48u32 seq_ = 0;49uptr size_ = 0;50Desc descs_[kMaxSize];5152void RemovePos(uptr i);53#endif54};5556// MutexSet is too large to live on stack.57// DynamicMutexSet can be use used to create local MutexSet's.58class DynamicMutexSet {59public:60DynamicMutexSet();61~DynamicMutexSet();62MutexSet* operator->() { return ptr_; }63operator MutexSet*() { return ptr_; }64DynamicMutexSet(const DynamicMutexSet&) = delete;65DynamicMutexSet& operator=(const DynamicMutexSet&) = delete;6667private:68MutexSet* ptr_;69#if SANITIZER_GO70MutexSet set_;71#endif72};7374// Go does not have mutexes, so do not spend memory and time.75// (Go sync.Mutex is actually a semaphore -- can be unlocked76// in different goroutine).77#if SANITIZER_GO78MutexSet::MutexSet() {}79void MutexSet::Reset() {}80void MutexSet::AddAddr(uptr addr, StackID stack_id, bool write) {}81void MutexSet::DelAddr(uptr addr, bool destroy) {}82uptr MutexSet::Size() const { return 0; }83MutexSet::Desc MutexSet::Get(uptr i) const { return Desc(); }84DynamicMutexSet::DynamicMutexSet() : ptr_(&set_) {}85DynamicMutexSet::~DynamicMutexSet() {}86#endif8788} // namespace __tsan8990#endif // TSAN_MUTEXSET_H919293