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 //===----------------------------------------------------------------------===//
12 #include "system_error"
15 _LIBCPP_BEGIN_NAMESPACE_STD
17 const defer_lock_t defer_lock = {};
18 const try_to_lock_t try_to_lock = {};
19 const adopt_lock_t adopt_lock = {};
23 pthread_mutex_destroy(&__m_);
29 int ec = pthread_mutex_lock(&__m_);
31 __throw_system_error(ec, "mutex lock failed");
37 return pthread_mutex_trylock(&__m_) == 0;
43 int ec = pthread_mutex_unlock(&__m_);
49 recursive_mutex::recursive_mutex()
51 pthread_mutexattr_t attr;
52 int ec = pthread_mutexattr_init(&attr);
55 ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
58 pthread_mutexattr_destroy(&attr);
61 ec = pthread_mutex_init(&__m_, &attr);
64 pthread_mutexattr_destroy(&attr);
67 ec = pthread_mutexattr_destroy(&attr);
70 pthread_mutex_destroy(&__m_);
75 __throw_system_error(ec, "recursive_mutex constructor failed");
78 recursive_mutex::~recursive_mutex()
80 int e = pthread_mutex_destroy(&__m_);
85 recursive_mutex::lock()
87 int ec = pthread_mutex_lock(&__m_);
89 __throw_system_error(ec, "recursive_mutex lock failed");
93 recursive_mutex::unlock()
95 int e = pthread_mutex_unlock(&__m_);
100 recursive_mutex::try_lock()
102 return pthread_mutex_trylock(&__m_) == 0;
107 timed_mutex::timed_mutex()
112 timed_mutex::~timed_mutex()
114 lock_guard<mutex> _(__m_);
120 unique_lock<mutex> lk(__m_);
127 timed_mutex::try_lock()
129 unique_lock<mutex> lk(__m_, try_to_lock);
130 if (lk.owns_lock() && !__locked_)
139 timed_mutex::unlock()
141 lock_guard<mutex> _(__m_);
146 // recursive_timed_mutex
148 recursive_timed_mutex::recursive_timed_mutex()
154 recursive_timed_mutex::~recursive_timed_mutex()
156 lock_guard<mutex> _(__m_);
160 recursive_timed_mutex::lock()
162 pthread_t id = pthread_self();
163 unique_lock<mutex> lk(__m_);
164 if (pthread_equal(id, __id_))
166 if (__count_ == numeric_limits<size_t>::max())
167 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
171 while (__count_ != 0)
178 recursive_timed_mutex::try_lock()
180 pthread_t id = pthread_self();
181 unique_lock<mutex> lk(__m_, try_to_lock);
182 if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
184 if (__count_ == numeric_limits<size_t>::max())
194 recursive_timed_mutex::unlock()
196 unique_lock<mutex> lk(__m_);
205 // If dispatch_once_f ever handles C++ exceptions, and if one can get to it
206 // without illegal macros (unexpected macros not beginning with _UpperCase or
207 // __lowercase), and if it stops spinning waiting threads, then call_once should
208 // call into dispatch_once_f instead of here. Relevant radar this code needs to
209 // keep in sync with: 7741191.
211 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
212 static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
215 __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
217 pthread_mutex_lock(&mut);
219 pthread_cond_wait(&cv, &mut);
222 #ifndef _LIBCPP_NO_EXCEPTIONS
225 #endif // _LIBCPP_NO_EXCEPTIONS
227 pthread_mutex_unlock(&mut);
229 pthread_mutex_lock(&mut);
231 pthread_mutex_unlock(&mut);
232 pthread_cond_broadcast(&cv);
233 #ifndef _LIBCPP_NO_EXCEPTIONS
237 pthread_mutex_lock(&mut);
239 pthread_mutex_unlock(&mut);
240 pthread_cond_broadcast(&cv);
243 #endif // _LIBCPP_NO_EXCEPTIONS
246 pthread_mutex_unlock(&mut);
249 _LIBCPP_END_NAMESPACE_STD