condition_variable_base.h 1.4 KB

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