8//===----------------------------------------------------------------------===// 9// 10// This file is a part of XRay, a dynamic runtime instrumentation system. 11// 12//===----------------------------------------------------------------------===// 13#ifndef XRAY_XRAY_RECURSION_GUARD_H 14#define XRAY_XRAY_RECURSION_GUARD_H 15 16#include "sanitizer_common/sanitizer_atomic.h" 17 18namespace __xray { 19 20/// The RecursionGuard is useful for guarding against signal handlers which are 21/// also potentially calling XRay-instrumented functions. To use the 22/// RecursionGuard, you'll typically need a thread_local atomic_uint8_t: 23/// 24/// thread_local atomic_uint8_t Guard{0}; 25/// 26/// // In a handler function: 27/// void handleArg0(int32_t F, XRayEntryType T) { 28/// RecursionGuard G(Guard); 29/// if (!G) 30/// return; // Failed to acquire the guard. 31/// ... 32/// } 33/// 34class RecursionGuard { 35 atomic_uint8_t &Running; 36 const bool Valid; 37 38public: 39 explicit inline RecursionGuard(atomic_uint8_t &R) 40 : Running(R), Valid(!atomic_exchange(&R, 1, memory_order_acq_rel)) {} 41 42 inline RecursionGuard(const RecursionGuard &) = delete; 43 inline RecursionGuard(RecursionGuard &&) = delete; 44 inline RecursionGuard &operator=(const RecursionGuard &) = delete; 45 inline RecursionGuard &operator=(RecursionGuard &&) = delete; 46 47 explicit inline operator bool() const { return Valid; } 48 49 inline ~RecursionGuard() noexcept { 50 if (Valid) 51 atomic_store(&Running, 0, memory_order_release); 52 } 53}; 54 55} // namespace __xray 56 57#endif // XRAY_XRAY_RECURSION_GUARD_H
| 7//===----------------------------------------------------------------------===// 8// 9// This file is a part of XRay, a dynamic runtime instrumentation system. 10// 11//===----------------------------------------------------------------------===// 12#ifndef XRAY_XRAY_RECURSION_GUARD_H 13#define XRAY_XRAY_RECURSION_GUARD_H 14 15#include "sanitizer_common/sanitizer_atomic.h" 16 17namespace __xray { 18 19/// The RecursionGuard is useful for guarding against signal handlers which are 20/// also potentially calling XRay-instrumented functions. To use the 21/// RecursionGuard, you'll typically need a thread_local atomic_uint8_t: 22/// 23/// thread_local atomic_uint8_t Guard{0}; 24/// 25/// // In a handler function: 26/// void handleArg0(int32_t F, XRayEntryType T) { 27/// RecursionGuard G(Guard); 28/// if (!G) 29/// return; // Failed to acquire the guard. 30/// ... 31/// } 32/// 33class RecursionGuard { 34 atomic_uint8_t &Running; 35 const bool Valid; 36 37public: 38 explicit inline RecursionGuard(atomic_uint8_t &R) 39 : Running(R), Valid(!atomic_exchange(&R, 1, memory_order_acq_rel)) {} 40 41 inline RecursionGuard(const RecursionGuard &) = delete; 42 inline RecursionGuard(RecursionGuard &&) = delete; 43 inline RecursionGuard &operator=(const RecursionGuard &) = delete; 44 inline RecursionGuard &operator=(RecursionGuard &&) = delete; 45 46 explicit inline operator bool() const { return Valid; } 47 48 inline ~RecursionGuard() noexcept { 49 if (Valid) 50 atomic_store(&Running, 0, memory_order_release); 51 } 52}; 53 54} // namespace __xray 55 56#endif // XRAY_XRAY_RECURSION_GUARD_H
|