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 
7 namespace v8 {
8 namespace internal {
9 
TryLock()10 OperationsBarrier::Token OperationsBarrier::TryLock() {
11   base::MutexGuard guard(&mutex_);
12   if (cancelled_) return {};
13   ++operations_count_;
14   return Token(this);
15 }
16 
CancelAndWait()17 void 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 
Release()26 void 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