Coverage Report

Created: 2022-07-16 07:03

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/condition_variable.cpp
Line
Count
Source (jump to first uncovered line)
1
//===----------------------------------------------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include <__config>
10
11
#ifndef _LIBCPP_HAS_NO_THREADS
12
13
#include <condition_variable>
14
#include <thread>
15
#include <system_error>
16
17
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
18
#  pragma comment(lib, "pthread")
19
#endif
20
21
_LIBCPP_PUSH_MACROS
22
#include <__undef_macros>
23
24
_LIBCPP_BEGIN_NAMESPACE_STD
25
26
// ~condition_variable is defined elsewhere.
27
28
void
29
condition_variable::notify_one() noexcept
30
695
{
31
695
    __libcpp_condvar_signal(&__cv_);
32
695
}
33
34
void
35
condition_variable::notify_all() noexcept
36
2.51M
{
37
2.51M
    __libcpp_condvar_broadcast(&__cv_);
38
2.51M
}
39
40
void
41
condition_variable::wait(unique_lock<mutex>& lk) noexcept
42
270
{
43
270
    if (!lk.owns_lock())
44
0
        __throw_system_error(EPERM,
45
0
                                  "condition_variable::wait: mutex not locked");
46
270
    int ec = __libcpp_condvar_wait(&__cv_, lk.mutex()->native_handle());
47
270
    if (ec)
48
0
        __throw_system_error(ec, "condition_variable wait failed");
49
270
}
50
51
void
52
condition_variable::__do_timed_wait(unique_lock<mutex>& lk,
53
     chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp) noexcept
54
110
{
55
110
    using namespace chrono;
56
110
    if (!lk.owns_lock())
57
0
        __throw_system_error(EPERM,
58
0
                            "condition_variable::timed wait: mutex not locked");
59
110
    nanoseconds d = tp.time_since_epoch();
60
110
    if (d > nanoseconds(0x59682F000000E941))
61
0
        d = nanoseconds(0x59682F000000E941);
62
110
    __libcpp_timespec_t ts;
63
110
    seconds s = duration_cast<seconds>(d);
64
110
    typedef decltype(ts.tv_sec) ts_sec;
65
110
    _LIBCPP_CONSTEXPR ts_sec ts_sec_max = numeric_limits<ts_sec>::max();
66
110
    if (s.count() < ts_sec_max)
67
110
    {
68
110
        ts.tv_sec = static_cast<ts_sec>(s.count());
69
110
        ts.tv_nsec = static_cast<decltype(ts.tv_nsec)>((d - s).count());
70
110
    }
71
0
    else
72
0
    {
73
0
        ts.tv_sec = ts_sec_max;
74
0
        ts.tv_nsec = giga::num - 1;
75
0
    }
76
110
    int ec = __libcpp_condvar_timedwait(&__cv_, lk.mutex()->native_handle(), &ts);
77
110
    if (ec != 0 && 
ec != ETIMEDOUT3
)
78
0
        __throw_system_error(ec, "condition_variable timed_wait failed");
79
110
}
80
81
void
82
notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
83
0
{
84
0
    auto& tl_ptr = __thread_local_data();
85
    // If this thread was not created using std::thread then it will not have
86
    // previously allocated.
87
0
    if (tl_ptr.get() == nullptr) {
88
0
        tl_ptr.set_pointer(new __thread_struct);
89
0
    }
90
0
    __thread_local_data()->notify_all_at_thread_exit(&cond, lk.release());
91
0
}
92
93
_LIBCPP_END_NAMESPACE_STD
94
95
_LIBCPP_POP_MACROS
96
97
#endif // !_LIBCPP_HAS_NO_THREADS