xref: /freebsd/contrib/llvm-project/compiler-rt/lib/xray/xray_recursion_guard.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 //===-- xray_recursion_guard.h ---------------------------------*- C++ -*-===//
2 //
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
6 //
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 
17 namespace __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 ///
33 class RecursionGuard {
34   atomic_uint8_t &Running;
35   const bool Valid;
36 
37 public:
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
57