aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang18-rt/lib/scudo/standalone/condition_variable_linux.cpp
blob: e6d9bd1771a4b8bf3c20bf149f2d3c913e8d9d3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//===-- condition_variable_linux.cpp ----------------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "platform.h"

#if SCUDO_LINUX

#include "condition_variable_linux.h"

#include "atomic_helpers.h"

#include <limits.h>
#include <linux/futex.h>
#include <sys/syscall.h>
#include <unistd.h>

namespace scudo {

void ConditionVariableLinux::notifyAllImpl(UNUSED HybridMutex &M) {
  const u32 V = atomic_load_relaxed(&Counter);
  atomic_store_relaxed(&Counter, V + 1);

  // TODO(chiahungduan): Move the waiters from the futex waiting queue
  // `Counter` to futex waiting queue `M` so that the awoken threads won't be
  // blocked again due to locked `M` by current thread.
  if (LastNotifyAll != V) {
    syscall(SYS_futex, reinterpret_cast<uptr>(&Counter), FUTEX_WAKE_PRIVATE,
            INT_MAX, nullptr, nullptr, 0);
  }

  LastNotifyAll = V + 1;
}

void ConditionVariableLinux::waitImpl(HybridMutex &M) {
  const u32 V = atomic_load_relaxed(&Counter) + 1;
  atomic_store_relaxed(&Counter, V);

  // TODO: Use ScopedUnlock when it's supported.
  M.unlock();
  syscall(SYS_futex, reinterpret_cast<uptr>(&Counter), FUTEX_WAIT_PRIVATE, V,
          nullptr, nullptr, 0);
  M.lock();
}

} // namespace scudo

#endif // SCUDO_LINUX