1
//===------------------------- mutex.cpp ----------------------------------===//
3
// The LLVM Compiler Infrastructure
5
// This file is dual licensed under the MIT and the University of Illinois Open
6
// Source Licenses. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
#define _LIBCPP_BUILDING_MUTEX
13
#include "system_error"
16
_LIBCPP_BEGIN_NAMESPACE_STD
18
const defer_lock_t defer_lock = {};
19
const try_to_lock_t try_to_lock = {};
20
const adopt_lock_t adopt_lock = {};
24
pthread_mutex_destroy(&__m_);
30
int ec = pthread_mutex_lock(&__m_);
32
__throw_system_error(ec, "mutex lock failed");
36
mutex::try_lock() _NOEXCEPT
38
return pthread_mutex_trylock(&__m_) == 0;
42
mutex::unlock() _NOEXCEPT
44
int ec = pthread_mutex_unlock(&__m_);
50
recursive_mutex::recursive_mutex()
52
pthread_mutexattr_t attr;
53
int ec = pthread_mutexattr_init(&attr);
56
ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
59
pthread_mutexattr_destroy(&attr);
62
ec = pthread_mutex_init(&__m_, &attr);
65
pthread_mutexattr_destroy(&attr);
68
ec = pthread_mutexattr_destroy(&attr);
71
pthread_mutex_destroy(&__m_);
76
__throw_system_error(ec, "recursive_mutex constructor failed");
79
recursive_mutex::~recursive_mutex()
81
int e = pthread_mutex_destroy(&__m_);
86
recursive_mutex::lock()
88
int ec = pthread_mutex_lock(&__m_);
90
__throw_system_error(ec, "recursive_mutex lock failed");
94
recursive_mutex::unlock() _NOEXCEPT
96
int e = pthread_mutex_unlock(&__m_);
101
recursive_mutex::try_lock() _NOEXCEPT
103
return pthread_mutex_trylock(&__m_) == 0;
108
timed_mutex::timed_mutex()
113
timed_mutex::~timed_mutex()
115
lock_guard<mutex> _(__m_);
121
unique_lock<mutex> lk(__m_);
128
timed_mutex::try_lock() _NOEXCEPT
130
unique_lock<mutex> lk(__m_, try_to_lock);
131
if (lk.owns_lock() && !__locked_)
140
timed_mutex::unlock() _NOEXCEPT
142
lock_guard<mutex> _(__m_);
147
// recursive_timed_mutex
149
recursive_timed_mutex::recursive_timed_mutex()
155
recursive_timed_mutex::~recursive_timed_mutex()
157
lock_guard<mutex> _(__m_);
161
recursive_timed_mutex::lock()
163
pthread_t id = pthread_self();
164
unique_lock<mutex> lk(__m_);
165
if (pthread_equal(id, __id_))
167
if (__count_ == numeric_limits<size_t>::max())
168
__throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
172
while (__count_ != 0)
179
recursive_timed_mutex::try_lock() _NOEXCEPT
181
pthread_t id = pthread_self();
182
unique_lock<mutex> lk(__m_, try_to_lock);
183
if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
185
if (__count_ == numeric_limits<size_t>::max())
195
recursive_timed_mutex::unlock() _NOEXCEPT
197
unique_lock<mutex> lk(__m_);
206
// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
207
// without illegal macros (unexpected macros not beginning with _UpperCase or
208
// __lowercase), and if it stops spinning waiting threads, then call_once should
209
// call into dispatch_once_f instead of here. Relevant radar this code needs to
210
// keep in sync with: 7741191.
212
static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
213
static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
216
__call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
218
pthread_mutex_lock(&mut);
220
pthread_cond_wait(&cv, &mut);
223
#ifndef _LIBCPP_NO_EXCEPTIONS
226
#endif // _LIBCPP_NO_EXCEPTIONS
228
pthread_mutex_unlock(&mut);
230
pthread_mutex_lock(&mut);
232
pthread_mutex_unlock(&mut);
233
pthread_cond_broadcast(&cv);
234
#ifndef _LIBCPP_NO_EXCEPTIONS
238
pthread_mutex_lock(&mut);
240
pthread_mutex_unlock(&mut);
241
pthread_cond_broadcast(&cv);
244
#endif // _LIBCPP_NO_EXCEPTIONS
247
pthread_mutex_unlock(&mut);
250
_LIBCPP_END_NAMESPACE_STD