Coverage Report

Created: 2020-03-31 06:27

/Users/buildslave/jenkins/workspace/coverage/llvm-project/libcxx/src/mutex.cpp
Line
Count
Source (jump to first uncovered line)
1
//===------------------------- mutex.cpp ----------------------------------===//
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 "mutex"
10
#include "limits"
11
#include "system_error"
12
#include "include/atomic_support.h"
13
#include "__undef_macros"
14
15
#ifndef _LIBCPP_HAS_NO_THREADS
16
#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
17
#pragma comment(lib, "pthread")
18
#endif
19
#endif
20
21
_LIBCPP_BEGIN_NAMESPACE_STD
22
#ifndef _LIBCPP_HAS_NO_THREADS
23
24
const defer_lock_t  defer_lock{};
25
const try_to_lock_t try_to_lock{};
26
const adopt_lock_t  adopt_lock{};
27
28
// ~mutex is defined elsewhere
29
30
void
31
mutex::lock()
32
13.5k
{
33
13.5k
    int ec = __libcpp_mutex_lock(&__m_);
34
13.5k
    if (ec)
35
0
        __throw_system_error(ec, "mutex lock failed");
36
13.5k
}
37
38
bool
39
mutex::try_lock() _NOEXCEPT
40
0
{
41
0
    return __libcpp_mutex_trylock(&__m_);
42
0
}
43
44
void
45
mutex::unlock() _NOEXCEPT
46
13.5k
{
47
13.5k
    int ec = __libcpp_mutex_unlock(&__m_);
48
13.5k
    (void)ec;
49
13.5k
    _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed");
50
13.5k
}
51
52
// recursive_mutex
53
54
recursive_mutex::recursive_mutex()
55
21.5k
{
56
21.5k
    int ec = __libcpp_recursive_mutex_init(&__m_);
57
21.5k
    if (ec)
58
0
        __throw_system_error(ec, "recursive_mutex constructor failed");
59
21.5k
}
60
61
recursive_mutex::~recursive_mutex()
62
6.54k
{
63
6.54k
    int e = __libcpp_recursive_mutex_destroy(&__m_);
64
6.54k
    (void)e;
65
6.54k
    _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed");
66
6.54k
}
67
68
void
69
recursive_mutex::lock()
70
70.1k
{
71
70.1k
    int ec = __libcpp_recursive_mutex_lock(&__m_);
72
70.1k
    if (ec)
73
0
        __throw_system_error(ec, "recursive_mutex lock failed");
74
70.1k
}
75
76
void
77
recursive_mutex::unlock() _NOEXCEPT
78
70.1k
{
79
70.1k
    int e = __libcpp_recursive_mutex_unlock(&__m_);
80
70.1k
    (void)e;
81
70.1k
    _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed");
82
70.1k
}
83
84
bool
85
recursive_mutex::try_lock() _NOEXCEPT
86
9
{
87
9
    return __libcpp_recursive_mutex_trylock(&__m_);
88
9
}
89
90
// timed_mutex
91
92
timed_mutex::timed_mutex()
93
    : __locked_(false)
94
0
{
95
0
}
96
97
timed_mutex::~timed_mutex()
98
0
{
99
0
    lock_guard<mutex> _(__m_);
100
0
}
101
102
void
103
timed_mutex::lock()
104
0
{
105
0
    unique_lock<mutex> lk(__m_);
106
0
    while (__locked_)
107
0
        __cv_.wait(lk);
108
0
    __locked_ = true;
109
0
}
110
111
bool
112
timed_mutex::try_lock() _NOEXCEPT
113
0
{
114
0
    unique_lock<mutex> lk(__m_, try_to_lock);
115
0
    if (lk.owns_lock() && !__locked_)
116
0
    {
117
0
        __locked_ = true;
118
0
        return true;
119
0
    }
120
0
    return false;
121
0
}
122
123
void
124
timed_mutex::unlock() _NOEXCEPT
125
0
{
126
0
    lock_guard<mutex> _(__m_);
127
0
    __locked_ = false;
128
0
    __cv_.notify_one();
129
0
}
130
131
// recursive_timed_mutex
132
133
recursive_timed_mutex::recursive_timed_mutex()
134
    : __count_(0),
135
      __id_{}
136
0
{
137
0
}
138
139
recursive_timed_mutex::~recursive_timed_mutex()
140
0
{
141
0
    lock_guard<mutex> _(__m_);
142
0
}
143
144
void
145
recursive_timed_mutex::lock()
146
0
{
147
0
    __thread_id id = this_thread::get_id();
148
0
    unique_lock<mutex> lk(__m_);
149
0
    if (id ==__id_)
150
0
    {
151
0
        if (__count_ == numeric_limits<size_t>::max())
152
0
            __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
153
0
        ++__count_;
154
0
        return;
155
0
    }
156
0
    while (__count_ != 0)
157
0
        __cv_.wait(lk);
158
0
    __count_ = 1;
159
0
    __id_ = id;
160
0
}
161
162
bool
163
recursive_timed_mutex::try_lock() _NOEXCEPT
164
0
{
165
0
    __thread_id id = this_thread::get_id();
166
0
    unique_lock<mutex> lk(__m_, try_to_lock);
167
0
    if (lk.owns_lock() && (__count_ == 0 || id == __id_))
168
0
    {
169
0
        if (__count_ == numeric_limits<size_t>::max())
170
0
            return false;
171
0
        ++__count_;
172
0
        __id_ = id;
173
0
        return true;
174
0
    }
175
0
    return false;
176
0
}
177
178
void
179
recursive_timed_mutex::unlock() _NOEXCEPT
180
0
{
181
0
    unique_lock<mutex> lk(__m_);
182
0
    if (--__count_ == 0)
183
0
    {
184
0
        __id_.__reset();
185
0
        lk.unlock();
186
0
        __cv_.notify_one();
187
0
    }
188
0
}
189
190
#endif // !_LIBCPP_HAS_NO_THREADS
191
192
// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
193
// without illegal macros (unexpected macros not beginning with _UpperCase or
194
// __lowercase), and if it stops spinning waiting threads, then call_once should
195
// call into dispatch_once_f instead of here. Relevant radar this code needs to
196
// keep in sync with:  7741191.
197
198
#ifndef _LIBCPP_HAS_NO_THREADS
199
_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
200
_LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
201
#endif
202
203
void __call_once(volatile once_flag::_State_type& flag, void* arg,
204
                 void (*func)(void*))
205
153k
{
206
#if defined(_LIBCPP_HAS_NO_THREADS)
207
    if (flag == 0)
208
    {
209
#ifndef _LIBCPP_NO_EXCEPTIONS
210
        try
211
        {
212
#endif  // _LIBCPP_NO_EXCEPTIONS
213
            flag = 1;
214
            func(arg);
215
            flag = ~once_flag::_State_type(0);
216
#ifndef _LIBCPP_NO_EXCEPTIONS
217
        }
218
        catch (...)
219
        {
220
            flag = 0;
221
            throw;
222
        }
223
#endif  // _LIBCPP_NO_EXCEPTIONS
224
    }
225
#else // !_LIBCPP_HAS_NO_THREADS
226
    __libcpp_mutex_lock(&mut);
227
153k
    while (flag == 1)
228
0
        __libcpp_condvar_wait(&cv, &mut);
229
153k
    if (flag == 0)
230
153k
    {
231
153k
#ifndef _LIBCPP_NO_EXCEPTIONS
232
153k
        try
233
153k
        {
234
153k
#endif  // _LIBCPP_NO_EXCEPTIONS
235
153k
            __libcpp_relaxed_store(&flag, once_flag::_State_type(1));
236
153k
            __libcpp_mutex_unlock(&mut);
237
153k
            func(arg);
238
153k
            __libcpp_mutex_lock(&mut);
239
153k
            __libcpp_atomic_store(&flag, ~once_flag::_State_type(0),
240
153k
                                  _AO_Release);
241
153k
            __libcpp_mutex_unlock(&mut);
242
153k
            __libcpp_condvar_broadcast(&cv);
243
153k
#ifndef _LIBCPP_NO_EXCEPTIONS
244
153k
        }
245
153k
        catch (...)
246
153k
        {
247
0
            __libcpp_mutex_lock(&mut);
248
0
            __libcpp_relaxed_store(&flag, once_flag::_State_type(0));
249
0
            __libcpp_mutex_unlock(&mut);
250
0
            __libcpp_condvar_broadcast(&cv);
251
0
            throw;
252
0
        }
253
0
#endif  // _LIBCPP_NO_EXCEPTIONS
254
0
    }
255
0
    else
256
0
        __libcpp_mutex_unlock(&mut);
257
153k
#endif // !_LIBCPP_HAS_NO_THREADS
258
153k
}
259
260
_LIBCPP_END_NAMESPACE_STD