Coverage Report

Created: 2026-03-19 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/src/bthread/condition_variable.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
// bthread - An M:N threading library to make applications more concurrent.
19
20
// Date: 2015/12/14 21:26:26
21
22
#ifndef  BTHREAD_CONDITION_VARIABLE_H
23
#define  BTHREAD_CONDITION_VARIABLE_H
24
25
#include "butil/time.h"
26
#include "bthread/mutex.h"
27
28
__BEGIN_DECLS
29
extern int bthread_cond_init(bthread_cond_t* __restrict cond,
30
                             const bthread_condattr_t* __restrict cond_attr);
31
extern int bthread_cond_destroy(bthread_cond_t* cond);
32
extern int bthread_cond_signal(bthread_cond_t* cond);
33
extern int bthread_cond_broadcast(bthread_cond_t* cond);
34
extern int bthread_cond_wait(bthread_cond_t* __restrict cond,
35
                             bthread_mutex_t* __restrict mutex);
36
extern int bthread_cond_timedwait(
37
    bthread_cond_t* __restrict cond,
38
    bthread_mutex_t* __restrict mutex,
39
    const struct timespec* __restrict abstime);
40
__END_DECLS
41
42
namespace bthread {
43
44
class ConditionVariable {
45
    DISALLOW_COPY_AND_ASSIGN(ConditionVariable);
46
public:
47
    typedef bthread_cond_t*         native_handler_type;
48
    
49
0
    ConditionVariable() {
50
0
        CHECK_EQ(0, bthread_cond_init(&_cond, NULL));
51
0
    }
52
0
    ~ConditionVariable() {
53
0
        CHECK_EQ(0, bthread_cond_destroy(&_cond));
54
0
    }
55
56
0
    native_handler_type native_handler() { return &_cond; }
57
58
0
    void wait(std::unique_lock<bthread::Mutex>& lock) {
59
0
        bthread_cond_wait(&_cond, lock.mutex()->native_handler());
60
0
    }
61
62
0
    void wait(std::unique_lock<bthread_mutex_t>& lock) {
63
0
        bthread_cond_wait(&_cond, lock.mutex());
64
0
    }
65
66
    template<typename Predicate>
67
    void wait(std::unique_lock<bthread::Mutex>& lock, Predicate p) {
68
        while (!p()) {
69
            bthread_cond_wait(&_cond, lock.mutex()->native_handler());
70
        }
71
    }
72
73
    template<typename Predicate>
74
    void wait(std::unique_lock<bthread_mutex_t>& lock, Predicate p) {
75
        while (!p()) {
76
            bthread_cond_wait(&_cond, lock.mutex());
77
        }
78
    }
79
80
    // Unlike std::condition_variable, we return ETIMEDOUT when time expires
81
    // rather than std::timeout
82
    int wait_for(std::unique_lock<bthread::Mutex>& lock,
83
0
                 long timeout_us) {
84
0
        return wait_until(lock, butil::microseconds_from_now(timeout_us));
85
0
    }
86
87
    int wait_for(std::unique_lock<bthread_mutex_t>& lock,
88
0
                 long timeout_us) {
89
0
        return wait_until(lock, butil::microseconds_from_now(timeout_us));
90
0
    }
91
92
    int wait_until(std::unique_lock<bthread::Mutex>& lock,
93
0
                   timespec duetime) {
94
0
        const int rc = bthread_cond_timedwait(
95
0
                &_cond, lock.mutex()->native_handler(), &duetime);
96
0
        return rc == ETIMEDOUT ? ETIMEDOUT : 0;
97
0
    }
98
99
    int wait_until(std::unique_lock<bthread_mutex_t>& lock,
100
0
                   timespec duetime) {
101
0
        const int rc = bthread_cond_timedwait(
102
0
                &_cond, lock.mutex(), &duetime);
103
0
        return rc == ETIMEDOUT ? ETIMEDOUT : 0;
104
0
    }
105
106
0
    void notify_one() {
107
0
        bthread_cond_signal(&_cond);
108
0
    }
109
110
0
    void notify_all() {
111
0
        bthread_cond_broadcast(&_cond);
112
0
    }
113
114
private:
115
    bthread_cond_t                  _cond;
116
};
117
118
}  // namespace bthread
119
120
#endif  //BTHREAD_CONDITION_VARIABLE_H