summaryrefslogtreecommitdiffstats
path: root/chromium/v8/src/tasks/operations-barrier.cc
blob: c6f6a51f4f8aa61c04f6145e197065186a52c8d3 (plain)
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
// Copyright 2020 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/tasks/operations-barrier.h"

namespace v8 {
namespace internal {

OperationsBarrier::Token OperationsBarrier::TryLock() {
  base::MutexGuard guard(&mutex_);
  if (cancelled_) return {};
  ++operations_count_;
  return Token(this);
}

void OperationsBarrier::CancelAndWait() {
  base::MutexGuard guard(&mutex_);
  DCHECK(!cancelled_);
  cancelled_ = true;
  while (operations_count_ > 0) {
    release_condition_.Wait(&mutex_);
  }
}

void OperationsBarrier::Release() {
  base::MutexGuard guard(&mutex_);
  if (--operations_count_ == 0 && cancelled_) {
    release_condition_.NotifyOne();
  }
}

}  // namespace internal
}  // namespace v8