1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
|
//===------------------------- mutex.cpp ----------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#define _LIBCPP_BUILDING_MUTEX
#include "mutex"
#include "limits"
#include "system_error"
#include "cassert"
_LIBCPP_BEGIN_NAMESPACE_STD
const defer_lock_t defer_lock = {};
const try_to_lock_t try_to_lock = {};
const adopt_lock_t adopt_lock = {};
mutex::~mutex()
{
pthread_mutex_destroy(&__m_);
}
void
mutex::lock()
{
int ec = pthread_mutex_lock(&__m_);
if (ec)
__throw_system_error(ec, "mutex lock failed");
}
bool
mutex::try_lock() _NOEXCEPT
{
return pthread_mutex_trylock(&__m_) == 0;
}
void
mutex::unlock() _NOEXCEPT
{
int ec = pthread_mutex_unlock(&__m_);
(void)ec;
assert(ec == 0);
}
// recursive_mutex
recursive_mutex::recursive_mutex()
{
pthread_mutexattr_t attr;
int ec = pthread_mutexattr_init(&attr);
if (ec)
goto fail;
ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
if (ec)
{
pthread_mutexattr_destroy(&attr);
goto fail;
}
ec = pthread_mutex_init(&__m_, &attr);
if (ec)
{
pthread_mutexattr_destroy(&attr);
goto fail;
}
ec = pthread_mutexattr_destroy(&attr);
if (ec)
{
pthread_mutex_destroy(&__m_);
goto fail;
}
return;
fail:
__throw_system_error(ec, "recursive_mutex constructor failed");
}
recursive_mutex::~recursive_mutex()
{
int e = pthread_mutex_destroy(&__m_);
(void)e;
assert(e == 0);
}
void
recursive_mutex::lock()
{
int ec = pthread_mutex_lock(&__m_);
if (ec)
__throw_system_error(ec, "recursive_mutex lock failed");
}
void
recursive_mutex::unlock() _NOEXCEPT
{
int e = pthread_mutex_unlock(&__m_);
(void)e;
assert(e == 0);
}
bool
recursive_mutex::try_lock() _NOEXCEPT
{
return pthread_mutex_trylock(&__m_) == 0;
}
// timed_mutex
timed_mutex::timed_mutex()
: __locked_(false)
{
}
timed_mutex::~timed_mutex()
{
lock_guard<mutex> _(__m_);
}
void
timed_mutex::lock()
{
unique_lock<mutex> lk(__m_);
while (__locked_)
__cv_.wait(lk);
__locked_ = true;
}
bool
timed_mutex::try_lock() _NOEXCEPT
{
unique_lock<mutex> lk(__m_, try_to_lock);
if (lk.owns_lock() && !__locked_)
{
__locked_ = true;
return true;
}
return false;
}
void
timed_mutex::unlock() _NOEXCEPT
{
lock_guard<mutex> _(__m_);
__locked_ = false;
__cv_.notify_one();
}
// recursive_timed_mutex
recursive_timed_mutex::recursive_timed_mutex()
: __count_(0),
__id_(0)
{
}
recursive_timed_mutex::~recursive_timed_mutex()
{
lock_guard<mutex> _(__m_);
}
void
recursive_timed_mutex::lock()
{
pthread_t id = pthread_self();
unique_lock<mutex> lk(__m_);
if (pthread_equal(id, __id_))
{
if (__count_ == numeric_limits<size_t>::max())
__throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
++__count_;
return;
}
while (__count_ != 0)
__cv_.wait(lk);
__count_ = 1;
__id_ = id;
}
bool
recursive_timed_mutex::try_lock() _NOEXCEPT
{
pthread_t id = pthread_self();
unique_lock<mutex> lk(__m_, try_to_lock);
if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
{
if (__count_ == numeric_limits<size_t>::max())
return false;
++__count_;
__id_ = id;
return true;
}
return false;
}
void
recursive_timed_mutex::unlock() _NOEXCEPT
{
unique_lock<mutex> lk(__m_);
if (--__count_ == 0)
{
__id_ = 0;
lk.unlock();
__cv_.notify_one();
}
}
// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
// without illegal macros (unexpected macros not beginning with _UpperCase or
// __lowercase), and if it stops spinning waiting threads, then call_once should
// call into dispatch_once_f instead of here. Relevant radar this code needs to
// keep in sync with: 7741191.
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
void
__call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
{
pthread_mutex_lock(&mut);
while (flag == 1)
pthread_cond_wait(&cv, &mut);
if (flag == 0)
{
#ifndef _LIBCPP_NO_EXCEPTIONS
try
{
#endif // _LIBCPP_NO_EXCEPTIONS
flag = 1;
pthread_mutex_unlock(&mut);
func(arg);
pthread_mutex_lock(&mut);
flag = ~0ul;
pthread_mutex_unlock(&mut);
pthread_cond_broadcast(&cv);
#ifndef _LIBCPP_NO_EXCEPTIONS
}
catch (...)
{
pthread_mutex_lock(&mut);
flag = 0ul;
pthread_mutex_unlock(&mut);
pthread_cond_broadcast(&cv);
throw;
}
#endif // _LIBCPP_NO_EXCEPTIONS
}
else
pthread_mutex_unlock(&mut);
}
_LIBCPP_END_NAMESPACE_STD
|