Path: blob/main/contrib/llvm-project/compiler-rt/lib/xray/xray_recursion_guard.h
35265 views
//===-- xray_recursion_guard.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 XRay, a dynamic runtime instrumentation system.9//10//===----------------------------------------------------------------------===//11#ifndef XRAY_XRAY_RECURSION_GUARD_H12#define XRAY_XRAY_RECURSION_GUARD_H1314#include "sanitizer_common/sanitizer_atomic.h"1516namespace __xray {1718/// The RecursionGuard is useful for guarding against signal handlers which are19/// also potentially calling XRay-instrumented functions. To use the20/// RecursionGuard, you'll typically need a thread_local atomic_uint8_t:21///22/// thread_local atomic_uint8_t Guard{0};23///24/// // In a handler function:25/// void handleArg0(int32_t F, XRayEntryType T) {26/// RecursionGuard G(Guard);27/// if (!G)28/// return; // Failed to acquire the guard.29/// ...30/// }31///32class RecursionGuard {33atomic_uint8_t &Running;34const bool Valid;3536public:37explicit inline RecursionGuard(atomic_uint8_t &R)38: Running(R), Valid(!atomic_exchange(&R, 1, memory_order_acq_rel)) {}3940inline RecursionGuard(const RecursionGuard &) = delete;41inline RecursionGuard(RecursionGuard &&) = delete;42inline RecursionGuard &operator=(const RecursionGuard &) = delete;43inline RecursionGuard &operator=(RecursionGuard &&) = delete;4445explicit inline operator bool() const { return Valid; }4647inline ~RecursionGuard() noexcept {48if (Valid)49atomic_store(&Running, 0, memory_order_release);50}51};5253} // namespace __xray5455#endif // XRAY_XRAY_RECURSION_GUARD_H565758