Coverage Report

Created: 2025-07-27 00:06

/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.61k
    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.61k
    void count_down(int amount) {
42
4.61k
        DCHECK_GE(amount, 0);
43
4.61k
        std::lock_guard<std::mutex> lock(_lock);
44
4.61k
        if (_count == 0) {
45
2
            return;
46
2
        }
47
48
4.61k
        if (amount >= _count) {
49
4.60k
            _count = 0;
50
4.60k
        } else {
51
1
            _count -= amount;
52
1
        }
53
54
4.61k
        if (_count == 0) {
55
            // Latch has triggered.
56
4.60k
            _cond.notify_all();
57
4.60k
        }
58
4.61k
    }
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.61k
    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
586
    void wait() {
68
586
        std::unique_lock<std::mutex> lock(_lock);
69
1.13k
        while (_count > 0) {
70
545
            _cond.wait(lock);
71
545
        }
72
586
    }
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.33k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
_ZZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1EEEEbRKNSt6chrono8durationIT_T0_EEENKUlvE_clEv
Line
Count
Source
79
332
        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
    // decrements the internal counter by n and blocks the calling thread until the counter reaches zero.
95
0
    void arrive_and_wait(uint64_t n) {
96
0
        DCHECK_GE(n, 0);
97
0
        count_down(n);
98
0
        wait();
99
0
    }
100
101
10.9k
    uint64_t count() const {
102
10.9k
        std::lock_guard<std::mutex> lock(_lock);
103
10.9k
        return _count;
104
10.9k
    }
105
106
private:
107
    mutable std::mutex _lock;
108
    mutable std::condition_variable _cond;
109
110
    uint64_t _count;
111
112
    CountDownLatch(const CountDownLatch&) = delete;
113
    void operator=(const CountDownLatch&) = delete;
114
};
115
116
// Utility class which calls latch->CountDown() in its destructor.
117
class CountDownOnScopeExit {
118
public:
119
0
    explicit CountDownOnScopeExit(CountDownLatch* latch) : _latch(latch) {}
120
0
    ~CountDownOnScopeExit() { _latch->count_down(); }
121
122
private:
123
    CountDownLatch* _latch = nullptr;
124
125
    CountDownOnScopeExit(const CountDownOnScopeExit&) = delete;
126
    void operator=(const CountDownOnScopeExit&) = delete;
127
};
128
129
} // namespace doris