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.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_UTIL_COUNTDOWN_LATCH_H
33
#define YB_UTIL_COUNTDOWN_LATCH_H
34
35
#include <atomic>
36
37
#include "yb/util/condition_variable.h"
38
#include "yb/util/monotime.h"
39
#include "yb/util/mutex.h"
40
41
namespace yb {
42
43
// This is a C++ implementation of the Java CountDownLatch
44
// class.
45
// See http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CountDownLatch.html
46
class CountDownLatch {
47
 public:
48
  // Initialize the latch with the given initial count.
49
  explicit CountDownLatch(uint64_t count);
50
  ~CountDownLatch();
51
52
  // Decrement the count of this latch by 'amount'
53
  // If the new count is less than or equal to zero, then all waiting threads are woken up.
54
  // If the count is already zero, this has no effect.
55
  void CountDown(uint64_t amount);
56
57
  // Decrement the count of this latch.
58
  // If the new count is zero, then all waiting threads are woken up.
59
  // If the count is already zero, this has no effect.
60
1.91M
  void CountDown() {
61
1.91M
    CountDown(1);
62
1.91M
  }
63
64
  // Wait until the count on the latch reaches zero.
65
  // If the count is already zero, this returns immediately.
66
  void Wait() const;
67
68
  // Waits for the count on the latch to reach zero, or until 'when' time is reached.
69
  // Returns true if the count became zero, false otherwise.
70
  bool WaitUntil(MonoTime when) const;
71
  bool WaitUntil(CoarseTimePoint when) const;
72
73
  // Waits for the count on the latch to reach zero, or until 'delta' time elapses.
74
  // Returns true if the count became zero, false otherwise.
75
  bool WaitFor(MonoDelta delta) const;
76
77
  // Reset the latch with the given count. This is equivalent to reconstructing
78
  // the latch. If 'count' is 0, and there are currently waiters, those waiters
79
  // will be triggered as if you counted down to 0.
80
  void Reset(uint64_t count);
81
  uint64_t count() const;
82
83
1.05M
  auto CountDownCallback() {
84
1.05M
    return [this] {
85
1.05M
      this->CountDown();
86
1.05M
    };
87
1.05M
  }
88
89
 private:
90
  mutable Mutex lock_;
91
  ConditionVariable cond_;
92
93
  std::atomic<uint64_t> count_;
94
95
  DISALLOW_COPY_AND_ASSIGN(CountDownLatch);
96
};
97
98
// Utility class which calls latch->CountDown() in its destructor.
99
class CountDownOnScopeExit {
100
 public:
101
6
  explicit CountDownOnScopeExit(CountDownLatch *latch) : latch_(latch) {}
102
6
  ~CountDownOnScopeExit() {
103
6
    latch_->CountDown();
104
6
  }
105
106
 private:
107
  CountDownLatch *latch_;
108
109
  DISALLOW_COPY_AND_ASSIGN(CountDownOnScopeExit);
110
};
111
112
} // namespace yb
113
114
#endif // YB_UTIL_COUNTDOWN_LATCH_H