Coverage Report

Created: 2026-04-15 17:52

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
72.0k
    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
269k
    void count_down(int64_t amount) {
41
269k
        DCHECK_GE(amount, 0);
42
269k
        std::lock_guard<std::mutex> lock(_lock);
43
269k
        if (_count == 0) {
44
237
            return;
45
237
        }
46
47
269k
        if (amount >= _count) {
48
65.8k
            _count = 0;
49
203k
        } else {
50
203k
            _count -= amount;
51
203k
        }
52
53
269k
        if (_count == 0) {
54
            // Latch has triggered.
55
65.8k
            _cond.notify_all();
56
65.8k
        }
57
269k
    }
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
249k
    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
22.0k
    void wait() {
67
22.0k
        std::unique_lock<std::mutex> lock(_lock);
68
43.8k
        while (_count > 0) {
69
21.7k
            _cond.wait(lock);
70
21.7k
        }
71
22.0k
    }
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
230k
    bool wait_for(const std::chrono::duration<Rep, Period>& delta) {
77
230k
        std::unique_lock lock(_lock);
78
462k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
_ZZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1000EEEEbRKNSt6chrono8durationIT_T0_EEENKUlvE_clEv
Line
Count
Source
78
429k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
_ZZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1EEEEbRKNSt6chrono8durationIT_T0_EEENKUlvE_clEv
Line
Count
Source
78
32.4k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
79
230k
    }
_ZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1000EEEEbRKNSt6chrono8durationIT_T0_EE
Line
Count
Source
76
214k
    bool wait_for(const std::chrono::duration<Rep, Period>& delta) {
77
214k
        std::unique_lock lock(_lock);
78
214k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
79
214k
    }
_ZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1EEEEbRKNSt6chrono8durationIT_T0_EE
Line
Count
Source
76
16.2k
    bool wait_for(const std::chrono::duration<Rep, Period>& delta) {
77
16.2k
        std::unique_lock lock(_lock);
78
16.2k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
79
16.2k
    }
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
21.4k
    void arrive_and_wait(uint64_t n) {
95
21.4k
        DCHECK_GE(n, 0);
96
21.4k
        count_down(n);
97
21.4k
        wait();
98
21.4k
    }
99
100
599k
    uint64_t count() const {
101
599k
        std::lock_guard<std::mutex> lock(_lock);
102
599k
        return _count;
103
599k
    }
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