xref: /llvm-project/compiler-rt/lib/rtsan/rtsan_context.h (revision 10d43061aa1c95facea397a900d9ce4f65fa03da)
11adb55b1SChris Apple //===--- rtsan_context.h - Realtime Sanitizer -------------------*- C++ -*-===//
21adb55b1SChris Apple //
31adb55b1SChris Apple // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
41adb55b1SChris Apple // See https://llvm.org/LICENSE.txt for license information.
51adb55b1SChris Apple // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61adb55b1SChris Apple //
71adb55b1SChris Apple //===----------------------------------------------------------------------===//
81adb55b1SChris Apple //
91adb55b1SChris Apple //===----------------------------------------------------------------------===//
101adb55b1SChris Apple 
111adb55b1SChris Apple #pragma once
121adb55b1SChris Apple 
131adb55b1SChris Apple namespace __rtsan {
141adb55b1SChris Apple 
151adb55b1SChris Apple class Context {
161adb55b1SChris Apple public:
171adb55b1SChris Apple   Context();
181adb55b1SChris Apple 
191adb55b1SChris Apple   void RealtimePush();
201adb55b1SChris Apple   void RealtimePop();
211adb55b1SChris Apple 
221adb55b1SChris Apple   void BypassPush();
231adb55b1SChris Apple   void BypassPop();
241adb55b1SChris Apple 
251adb55b1SChris Apple   bool InRealtimeContext() const;
261adb55b1SChris Apple   bool IsBypassed() const;
271adb55b1SChris Apple 
2830d56bedSChris Apple   Context(const Context &) = delete;
2930d56bedSChris Apple   Context(Context &&) = delete;
3030d56bedSChris Apple   Context &operator=(const Context &) = delete;
3130d56bedSChris Apple   Context &operator=(Context &&) = delete;
3230d56bedSChris Apple 
3330d56bedSChris Apple private:
3430d56bedSChris Apple   int realtime_depth_{0};
3530d56bedSChris Apple   int bypass_depth_{0};
361adb55b1SChris Apple };
371adb55b1SChris Apple 
38*10d43061SChris Apple class ScopedBypass {
39*10d43061SChris Apple public:
40*10d43061SChris Apple   [[nodiscard]] explicit ScopedBypass(Context &context) : context_(context) {
41*10d43061SChris Apple     context_.BypassPush();
42*10d43061SChris Apple   }
43*10d43061SChris Apple 
44*10d43061SChris Apple   ~ScopedBypass() { context_.BypassPop(); }
45*10d43061SChris Apple 
46*10d43061SChris Apple   ScopedBypass(const ScopedBypass &) = delete;
47*10d43061SChris Apple   ScopedBypass &operator=(const ScopedBypass &) = delete;
48*10d43061SChris Apple   ScopedBypass(ScopedBypass &&) = delete;
49*10d43061SChris Apple   ScopedBypass &operator=(ScopedBypass &&) = delete;
50*10d43061SChris Apple 
51*10d43061SChris Apple private:
52*10d43061SChris Apple   Context &context_;
53*10d43061SChris Apple };
54*10d43061SChris Apple 
551adb55b1SChris Apple Context &GetContextForThisThread();
561adb55b1SChris Apple } // namespace __rtsan
57