1// Copyright 2020 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "src/tasks/operations-barrier.h"
6
7namespace v8 {
8namespace internal {
9
10OperationsBarrier::Token OperationsBarrier::TryLock() {
11  base::MutexGuard guard(&mutex_);
12  if (cancelled_) return {};
13  ++operations_count_;
14  return Token(this);
15}
16
17void OperationsBarrier::CancelAndWait() {
18  base::MutexGuard guard(&mutex_);
19  DCHECK(!cancelled_);
20  cancelled_ = true;
21  while (operations_count_ > 0) {
22    release_condition_.Wait(&mutex_);
23  }
24}
25
26void OperationsBarrier::Release() {
27  base::MutexGuard guard(&mutex_);
28  if (--operations_count_ == 0 && cancelled_) {
29    release_condition_.NotifyOne();
30  }
31}
32
33}  // namespace internal
34}  // namespace v8
35