xref: /freebsd/contrib/llvm-project/compiler-rt/lib/scudo/standalone/condition_variable_base.h (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 //===-- condition_variable_base.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 #ifndef SCUDO_CONDITION_VARIABLE_BASE_H_
10 #define SCUDO_CONDITION_VARIABLE_BASE_H_
11 
12 #include "mutex.h"
13 #include "thread_annotations.h"
14 
15 namespace scudo {
16 
17 template <typename Derived> class ConditionVariableBase {
18 public:
19   constexpr ConditionVariableBase() = default;
20 
21   void bindTestOnly(HybridMutex &Mutex) {
22 #if SCUDO_DEBUG
23     boundMutex = &Mutex;
24 #else
25     (void)Mutex;
26 #endif
27   }
28 
29   void notifyAll(HybridMutex &M) REQUIRES(M) {
30 #if SCUDO_DEBUG
31     CHECK_EQ(&M, boundMutex);
32 #endif
33     getDerived()->notifyAllImpl(M);
34   }
35 
36   void wait(HybridMutex &M) REQUIRES(M) {
37 #if SCUDO_DEBUG
38     CHECK_EQ(&M, boundMutex);
39 #endif
40     getDerived()->waitImpl(M);
41   }
42 
43 protected:
44   Derived *getDerived() { return static_cast<Derived *>(this); }
45 
46 #if SCUDO_DEBUG
47   // Because thread-safety analysis doesn't support pointer aliasing, we are not
48   // able to mark the proper annotations without false positive. Instead, we
49   // pass the lock and do the same-lock check separately.
50   HybridMutex *boundMutex = nullptr;
51 #endif
52 };
53 
54 } // namespace scudo
55 
56 #endif // SCUDO_CONDITION_VARIABLE_BASE_H_
57