/root/doris/be/src/util/countdown_latch.h
Line | Count | Source (jump to first uncovered line) |
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 | | #pragma once |
19 | | |
20 | | // IWYU pragma: no_include <bits/chrono.h> |
21 | | #include <chrono> |
22 | | #include <condition_variable> |
23 | | #include <mutex> |
24 | | |
25 | | #include "common/logging.h" |
26 | | #include "olap/olap_define.h" |
27 | | |
28 | | namespace doris { |
29 | | |
30 | | // This is a C++ implementation of the Java CountDownLatch |
31 | | // class. |
32 | | // See http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CountDownLatch.html |
33 | | class CountDownLatch { |
34 | | public: |
35 | | // Initialize the latch with the given initial count. |
36 | 4.56k | explicit CountDownLatch(int count) : _count(count) {} |
37 | | |
38 | | // Decrement the count of this latch by 'amount' |
39 | | // If the new count is less than or equal to zero, then all waiting threads are woken up. |
40 | | // If the count is already zero, this has no effect. |
41 | 4.56k | void count_down(int amount) { |
42 | 4.56k | DCHECK_GE(amount, 0); |
43 | 4.56k | std::lock_guard<std::mutex> lock(_lock); |
44 | 4.56k | if (_count == 0) { |
45 | 2 | return; |
46 | 2 | } |
47 | | |
48 | 4.56k | if (amount >= _count) { |
49 | 4.56k | _count = 0; |
50 | 4.56k | } else { |
51 | 1 | _count -= amount; |
52 | 1 | } |
53 | | |
54 | 4.56k | if (_count == 0) { |
55 | | // Latch has triggered. |
56 | 4.56k | _cond.notify_all(); |
57 | 4.56k | } |
58 | 4.56k | } |
59 | | |
60 | | // Decrement the count of this latch. |
61 | | // If the new count is zero, then all waiting threads are woken up. |
62 | | // If the count is already zero, this has no effect. |
63 | 4.56k | void count_down() { count_down(1); } |
64 | | |
65 | | // Wait until the count on the latch reaches zero. |
66 | | // If the count is already zero, this returns immediately. |
67 | 566 | void wait() { |
68 | 566 | std::unique_lock<std::mutex> lock(_lock); |
69 | 1.09k | while (_count > 0) { |
70 | 530 | _cond.wait(lock); |
71 | 530 | } |
72 | 566 | } |
73 | | |
74 | | // Waits for the count on the latch to reach zero, or until 'delta' time elapses. |
75 | | // Returns true if the count became zero, false otherwise. |
76 | | template <class Rep, class Period> |
77 | 2.93k | bool wait_for(const std::chrono::duration<Rep, Period>& delta) { |
78 | 2.93k | std::unique_lock lock(_lock); |
79 | 4.67k | return _cond.wait_for(lock, delta, [&]() { return _count <= 0; }); _ZZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1000EEEEbRKNSt6chrono8durationIT_T0_EEENKUlvE_clEv Line | Count | Source | 79 | 4.34k | return _cond.wait_for(lock, delta, [&]() { return _count <= 0; }); |
_ZZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1EEEEbRKNSt6chrono8durationIT_T0_EEENKUlvE_clEv Line | Count | Source | 79 | 330 | return _cond.wait_for(lock, delta, [&]() { return _count <= 0; }); |
|
80 | 2.93k | } _ZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1000EEEEbRKNSt6chrono8durationIT_T0_EE Line | Count | Source | 77 | 2.75k | bool wait_for(const std::chrono::duration<Rep, Period>& delta) { | 78 | 2.75k | std::unique_lock lock(_lock); | 79 | 2.75k | return _cond.wait_for(lock, delta, [&]() { return _count <= 0; }); | 80 | 2.75k | } |
_ZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1EEEEbRKNSt6chrono8durationIT_T0_EE Line | Count | Source | 77 | 179 | bool wait_for(const std::chrono::duration<Rep, Period>& delta) { | 78 | 179 | std::unique_lock lock(_lock); | 79 | 179 | return _cond.wait_for(lock, delta, [&]() { return _count <= 0; }); | 80 | 179 | } |
|
81 | | |
82 | | // Reset the latch with the given count. This is equivalent to reconstructing |
83 | | // the latch. If 'count' is 0, and there are currently waiters, those waiters |
84 | | // will be triggered as if you counted down to 0. |
85 | 1 | void reset(uint64_t count) { |
86 | 1 | std::lock_guard<std::mutex> lock(_lock); |
87 | 1 | _count = count; |
88 | 1 | if (_count == 0) { |
89 | | // Awake any waiters if we reset to 0. |
90 | 1 | _cond.notify_all(); |
91 | 1 | } |
92 | 1 | } |
93 | | |
94 | 9.72k | uint64_t count() const { |
95 | 9.72k | std::lock_guard<std::mutex> lock(_lock); |
96 | 9.72k | return _count; |
97 | 9.72k | } |
98 | | |
99 | | private: |
100 | | mutable std::mutex _lock; |
101 | | mutable std::condition_variable _cond; |
102 | | |
103 | | uint64_t _count; |
104 | | |
105 | | CountDownLatch(const CountDownLatch&) = delete; |
106 | | void operator=(const CountDownLatch&) = delete; |
107 | | }; |
108 | | |
109 | | // Utility class which calls latch->CountDown() in its destructor. |
110 | | class CountDownOnScopeExit { |
111 | | public: |
112 | 0 | explicit CountDownOnScopeExit(CountDownLatch* latch) : _latch(latch) {} |
113 | 0 | ~CountDownOnScopeExit() { _latch->count_down(); } |
114 | | |
115 | | private: |
116 | | CountDownLatch* _latch = nullptr; |
117 | | |
118 | | CountDownOnScopeExit(const CountDownOnScopeExit&) = delete; |
119 | | void operator=(const CountDownOnScopeExit&) = delete; |
120 | | }; |
121 | | |
122 | | } // namespace doris |