Coverage Report

Created: 2025-07-24 01:10

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/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 "olap/olap_define.h"
27
28
namespace doris {
29
#include "common/compile_check_begin.h"
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
3.75k
    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
3.71k
    void count_down(int64_t amount) {
42
3.71k
        DCHECK_GE(amount, 0);
43
3.71k
        std::lock_guard<std::mutex> lock(_lock);
44
3.71k
        if (_count == 0) {
45
11
            return;
46
11
        }
47
48
3.70k
        if (amount >= _count) {
49
3.70k
            _count = 0;
50
3.70k
        } else {
51
1
            _count -= amount;
52
1
        }
53
54
3.70k
        if (_count == 0) {
55
            // Latch has triggered.
56
3.70k
            _cond.notify_all();
57
3.70k
        }
58
3.70k
    }
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
3.65k
    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
587
    void wait() {
68
587
        std::unique_lock<std::mutex> lock(_lock);
69
1.13k
        while (_count > 0) {
70
549
            _cond.wait(lock);
71
549
        }
72
587
    }
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
23.3k
    bool wait_for(const std::chrono::duration<Rep, Period>& delta) {
78
23.3k
        std::unique_lock lock(_lock);
79
45.7k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
80
23.3k
    }
Unexecuted instantiation: _ZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1EEEEbRKNSt6chrono8durationIT_T0_EE
_ZN5doris14CountDownLatch8wait_forIlSt5ratioILl1ELl1000EEEEbRKNSt6chrono8durationIT_T0_EE
Line
Count
Source
77
23.3k
    bool wait_for(const std::chrono::duration<Rep, Period>& delta) {
78
23.3k
        std::unique_lock lock(_lock);
79
23.3k
        return _cond.wait_for(lock, delta, [&]() { return _count <= 0; });
80
23.3k
    }
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(int64_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.2k
    uint64_t count() const {
102
10.2k
        std::lock_guard<std::mutex> lock(_lock);
103
10.2k
        return _count;
104
10.2k
    }
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
#include "common/compile_check_end.h"
129
} // namespace doris