condition_variable.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //===-- condition_variable.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_H_
  9. #define SCUDO_CONDITION_VARIABLE_H_
  10. #include "condition_variable_base.h"
  11. #include "common.h"
  12. #include "platform.h"
  13. #include "condition_variable_linux.h"
  14. namespace scudo {
  15. // A default implementation of default condition variable. It doesn't do a real
  16. // `wait`, instead it spins a short amount of time only.
  17. class ConditionVariableDummy
  18. : public ConditionVariableBase<ConditionVariableDummy> {
  19. public:
  20. void notifyAllImpl(UNUSED HybridMutex &M) REQUIRES(M) {}
  21. void waitImpl(UNUSED HybridMutex &M) REQUIRES(M) {
  22. M.unlock();
  23. constexpr u32 SpinTimes = 64;
  24. volatile u32 V = 0;
  25. for (u32 I = 0; I < SpinTimes; ++I) {
  26. u32 Tmp = V + 1;
  27. V = Tmp;
  28. }
  29. M.lock();
  30. }
  31. };
  32. template <typename Config, typename = const bool>
  33. struct ConditionVariableState {
  34. static constexpr bool enabled() { return false; }
  35. // This is only used for compilation purpose so that we won't end up having
  36. // many conditional compilations. If you want to use `ConditionVariableDummy`,
  37. // define `ConditionVariableT` in your allocator configuration. See
  38. // allocator_config.h for more details.
  39. using ConditionVariableT = ConditionVariableDummy;
  40. };
  41. template <typename Config>
  42. struct ConditionVariableState<Config, decltype(Config::UseConditionVariable)> {
  43. static constexpr bool enabled() { return Config::UseConditionVariable; }
  44. using ConditionVariableT = typename Config::ConditionVariableT;
  45. };
  46. } // namespace scudo
  47. #endif // SCUDO_CONDITION_VARIABLE_H_