Coverage Report

Created: 2026-04-10 16:11

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/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
#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 "storage/olap_define.h"
27
28
namespace doris {
29
// This is a C++ implementation of the Java CountDownLatch
30
// class.
31
// See http://docs.oracle.com/javase/6/docs/api/java/util/concurrent/CountDownLatch.html
32
class CountDownLatch {
33
public:
34
    // Initialize the latch with the given initial count.
35
70.7k
    explicit CountDownLatch(int count) : _count(count) {}
36
37
    // Decrement the count of this latch by 'amount'
38
    // If the new count is less than or equal to zero, then all waiting threads are woken up.
39
    // If the count is already zero, this has no effect.
40
260k
    void count_down(int64_t amount) {
41
260k
        DCHECK_GE(amount, 0);
42
260k
        std::lock_guard<std::mutex> lock(_lock);
43
260k
        if (_count == 0) {
44
193
            return;
45
193
        }
46
47
259k
        if (amount >= _count) {
48
64.5k
            _count = 0;
49
195k
        } else {
50
195k
            _count -= amount;
51
195k
        }
52
53
259k
        if (_count == 0) {
54
            // Latch has triggered.
55
64.5k
            _cond.notify_all();
56
64.5k
        }
57
259k
    }
58
59
    // Decrement the count of this latch.
60
    // If the new count is zero, then all waiting threads are woken up.
61
    // If the count is already zero, this has no effect.
62
241k
    void count_down() { count_down(1); }
63
64
    // Wait until the count on the latch reaches zero.
65
    // If the count is already zero, this returns immediately.
66
20.6k
    void wait() {
67
20.6k
        std::unique_lock<std::mutex> lock(_lock);
68
41.1k
        while (_count > 0) {
69
20.4k
            _cond.wait(lock);
70
20.4k
        }
71
20.6k
    }
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
    template <class Rep, class Period>
76
672k
    bool wait_for(const std::chrono::duration<Rep, Period>& delta) {
77
672k
        std::unique_lock lock(_lock);
78
1.34M
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
_ZZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1000EEEEbRKNSt6chrono8durationIT_T0_EEENKUlvE_clEv
Line
Count
Source
78
1.26M
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
_ZZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1EEEEbRKNSt6chrono8durationIT_T0_EEENKUlvE_clEv
Line
Count
Source
78
78.1k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
79
672k
    }
_ZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1000EEEEbRKNSt6chrono8durationIT_T0_EE
Line
Count
Source
76
633k
    bool wait_for(const std::chrono::duration<Rep, Period>& delta) {
77
633k
        std::unique_lock lock(_lock);
78
633k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
79
633k
    }
_ZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1EEEEbRKNSt6chrono8durationIT_T0_EE
Line
Count
Source
76
39.1k
    bool wait_for(const std::chrono::duration<Rep, Period>& delta) {
77
39.1k
        std::unique_lock lock(_lock);
78
39.1k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
79
39.1k
    }
80
81
    // Reset the latch with the given count. This is equivalent to reconstructing
82
    // the latch. If 'count' is 0, and there are currently waiters, those waiters
83
    // will be triggered as if you counted down to 0.
84
8
    void reset(int64_t count) {
85
8
        std::lock_guard<std::mutex> lock(_lock);
86
8
        _count = count;
87
8
        if (_count == 0) {
88
            // Awake any waiters if we reset to 0.
89
1
            _cond.notify_all();
90
1
        }
91
8
    }
92
93
    // decrements the internal counter by n and blocks the calling thread until the counter reaches zero.
94
20.0k
    void arrive_and_wait(uint64_t n) {
95
20.0k
        DCHECK_GE(n, 0);
96
20.0k
        count_down(n);
97
20.0k
        wait();
98
20.0k
    }
99
100
1.19M
    uint64_t count() const {
101
1.19M
        std::lock_guard<std::mutex> lock(_lock);
102
1.19M
        return _count;
103
1.19M
    }
104
105
private:
106
    mutable std::mutex _lock;
107
    mutable std::condition_variable _cond;
108
109
    uint64_t _count;
110
111
    CountDownLatch(const CountDownLatch&) = delete;
112
    void operator=(const CountDownLatch&) = delete;
113
};
114
115
// Utility class which calls latch->CountDown() in its destructor.
116
class CountDownOnScopeExit {
117
public:
118
0
    explicit CountDownOnScopeExit(CountDownLatch* latch) : _latch(latch) {}
119
0
    ~CountDownOnScopeExit() { _latch->count_down(); }
120
121
private:
122
    CountDownLatch* _latch = nullptr;
123
124
    CountDownOnScopeExit(const CountDownOnScopeExit&) = delete;
125
    void operator=(const CountDownOnScopeExit&) = delete;
126
};
127
} // namespace doris