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