YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/countdown_latch.cc
Line
Count
Source
1
// Copyright (c) YugaByte, Inc.
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
4
// in compliance with the License.  You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software distributed under the License
9
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
10
// or implied.  See the License for the specific language governing permissions and limitations
11
// under the License.
12
//
13
14
#include "yb/util/countdown_latch.h"
15
16
#include "yb/util/thread_restrictions.h"
17
18
namespace yb {
19
20
CountDownLatch::CountDownLatch(uint64_t count)
21
  : cond_(&lock_),
22
2.50M
    count_(count) {
23
2.50M
}
24
25
1.65M
CountDownLatch::~CountDownLatch() {
26
  // Lock mutex to synchronize deletion.
27
  // All locks of this mutex are short, so we don't wait.
28
1.65M
  MutexLock lock(lock_);
29
1.65M
}
30
31
1.95M
void CountDownLatch::CountDown(uint64_t amount) {
32
1.95M
  MutexLock lock(lock_);
33
1.95M
  auto existing_value = count_.load(std::memory_order_relaxed);
34
1.95M
  if (existing_value == 0) {
35
43
    return;
36
43
  }
37
38
1.95M
  if (amount >= existing_value) {
39
1.72M
    count_.store(0, std::memory_order_release);
40
    // Latch has triggered.
41
1.72M
    cond_.Broadcast();
42
228k
  } else {
43
228k
    count_.store(existing_value - amount, std::memory_order_release);
44
228k
  }
45
1.95M
}
46
47
1.28M
void CountDownLatch::Wait() const {
48
1.28M
  if (count_.load(std::memory_order_acquire) == 0) {
49
49.1k
    return;
50
49.1k
  }
51
1.23M
  ThreadRestrictions::AssertWaitAllowed();
52
1.23M
  MutexLock lock(lock_);
53
2.46M
  while (count_.load(std::memory_order_relaxed) > 0) {
54
1.23M
    cond_.Wait();
55
1.23M
  }
56
1.23M
}
57
58
164k
bool CountDownLatch::WaitUntil(CoarseTimePoint when) const {
59
164k
  return WaitUntil(ToSteady(when));
60
164k
}
61
62
242k
bool CountDownLatch::WaitUntil(MonoTime deadline) const {
63
242k
  if (count_.load(std::memory_order_acquire) == 0) {
64
29.6k
    return true;
65
29.6k
  }
66
212k
  ThreadRestrictions::AssertWaitAllowed();
67
212k
  MutexLock lock(lock_);
68
399k
  while (count_.load(std::memory_order_relaxed) > 0) {
69
212k
    if (!cond_.WaitUntil(deadline)) {
70
26.0k
      return false;
71
26.0k
    }
72
212k
  }
73
186k
  return true;
74
212k
}
75
76
78.1k
bool CountDownLatch::WaitFor(MonoDelta delta) const {
77
78.1k
  return WaitUntil(MonoTime::Now() + delta);
78
78.1k
}
79
80
130k
void CountDownLatch::Reset(uint64_t count) {
81
130k
  MutexLock lock(lock_);
82
130k
  count_.store(count, std::memory_order_release);
83
130k
  if (count != 0) {
84
130k
    return;
85
130k
  }
86
  // Awake any waiters if we reset to 0.
87
117
  cond_.Broadcast();
88
117
}
89
90
4.30M
uint64_t CountDownLatch::count() const {
91
4.30M
  return count_.load(std::memory_order_acquire);
92
4.30M
}
93
94
} // namespace yb