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_);
51 recursive_mutex::recursive_mutex()
53 pthread_mutexattr_t attr;
54 int ec = pthread_mutexattr_init(&attr);
57 ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
60 pthread_mutexattr_destroy(&attr);
63 ec = pthread_mutex_init(&__m_, &attr);
66 pthread_mutexattr_destroy(&attr);
69 ec = pthread_mutexattr_destroy(&attr);
72 pthread_mutex_destroy(&__m_);
77 __throw_system_error(ec, "recursive_mutex constructor failed");
80 recursive_mutex::~recursive_mutex()
82 int e = pthread_mutex_destroy(&__m_);
88 recursive_mutex::lock()
90 int ec = pthread_mutex_lock(&__m_);
92 __throw_system_error(ec, "recursive_mutex lock failed");
96 recursive_mutex::unlock() _NOEXCEPT
98 int e = pthread_mutex_unlock(&__m_);
104 recursive_mutex::try_lock() _NOEXCEPT
106 return pthread_mutex_trylock(&__m_) == 0;
111 timed_mutex::timed_mutex()
116 timed_mutex::~timed_mutex()
118 lock_guard<mutex> _(__m_);
124 unique_lock<mutex> lk(__m_);
131 timed_mutex::try_lock() _NOEXCEPT
133 unique_lock<mutex> lk(__m_, try_to_lock);
134 if (lk.owns_lock() && !__locked_)
143 timed_mutex::unlock() _NOEXCEPT
145 lock_guard<mutex> _(__m_);
150 // recursive_timed_mutex
152 recursive_timed_mutex::recursive_timed_mutex()
158 recursive_timed_mutex::~recursive_timed_mutex()
160 lock_guard<mutex> _(__m_);
164 recursive_timed_mutex::lock()
166 pthread_t id = pthread_self();
167 unique_lock<mutex> lk(__m_);
168 if (pthread_equal(id, __id_))
170 if (__count_ == numeric_limits<size_t>::max())
171 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
175 while (__count_ != 0)
182 recursive_timed_mutex::try_lock() _NOEXCEPT
184 pthread_t id = pthread_self();
185 unique_lock<mutex> lk(__m_, try_to_lock);
186 if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
188 if (__count_ == numeric_limits<size_t>::max())
198 recursive_timed_mutex::unlock() _NOEXCEPT
200 unique_lock<mutex> lk(__m_);
209 // If dispatch_once_f ever handles C++ exceptions, and if one can get to it
210 // without illegal macros (unexpected macros not beginning with _UpperCase or
211 // __lowercase), and if it stops spinning waiting threads, then call_once should
212 // call into dispatch_once_f instead of here. Relevant radar this code needs to
213 // keep in sync with: 7741191.
215 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
216 static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
219 __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
221 pthread_mutex_lock(&mut);
223 pthread_cond_wait(&cv, &mut);
226 #ifndef _LIBCPP_NO_EXCEPTIONS
229 #endif // _LIBCPP_NO_EXCEPTIONS
231 pthread_mutex_unlock(&mut);
233 pthread_mutex_lock(&mut);
235 pthread_mutex_unlock(&mut);
236 pthread_cond_broadcast(&cv);
237 #ifndef _LIBCPP_NO_EXCEPTIONS
241 pthread_mutex_lock(&mut);
243 pthread_mutex_unlock(&mut);
244 pthread_cond_broadcast(&cv);
247 #endif // _LIBCPP_NO_EXCEPTIONS
250 pthread_mutex_unlock(&mut);
253 _LIBCPP_END_NAMESPACE_STD