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
17 #ifndef _LIBCPP_HAS_NO_THREADS
19 const defer_lock_t defer_lock = {};
20 const try_to_lock_t try_to_lock = {};
21 const adopt_lock_t adopt_lock = {};
25 pthread_mutex_destroy(&__m_);
31 int ec = pthread_mutex_lock(&__m_);
33 __throw_system_error(ec, "mutex lock failed");
37 mutex::try_lock() _NOEXCEPT
39 return pthread_mutex_trylock(&__m_) == 0;
43 mutex::unlock() _NOEXCEPT
45 int ec = pthread_mutex_unlock(&__m_);
52 recursive_mutex::recursive_mutex()
54 pthread_mutexattr_t attr;
55 int ec = pthread_mutexattr_init(&attr);
58 ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
61 pthread_mutexattr_destroy(&attr);
64 ec = pthread_mutex_init(&__m_, &attr);
67 pthread_mutexattr_destroy(&attr);
70 ec = pthread_mutexattr_destroy(&attr);
73 pthread_mutex_destroy(&__m_);
78 __throw_system_error(ec, "recursive_mutex constructor failed");
81 recursive_mutex::~recursive_mutex()
83 int e = pthread_mutex_destroy(&__m_);
89 recursive_mutex::lock()
91 int ec = pthread_mutex_lock(&__m_);
93 __throw_system_error(ec, "recursive_mutex lock failed");
97 recursive_mutex::unlock() _NOEXCEPT
99 int e = pthread_mutex_unlock(&__m_);
105 recursive_mutex::try_lock() _NOEXCEPT
107 return pthread_mutex_trylock(&__m_) == 0;
112 timed_mutex::timed_mutex()
117 timed_mutex::~timed_mutex()
119 lock_guard<mutex> _(__m_);
125 unique_lock<mutex> lk(__m_);
132 timed_mutex::try_lock() _NOEXCEPT
134 unique_lock<mutex> lk(__m_, try_to_lock);
135 if (lk.owns_lock() && !__locked_)
144 timed_mutex::unlock() _NOEXCEPT
146 lock_guard<mutex> _(__m_);
151 // recursive_timed_mutex
153 recursive_timed_mutex::recursive_timed_mutex()
159 recursive_timed_mutex::~recursive_timed_mutex()
161 lock_guard<mutex> _(__m_);
165 recursive_timed_mutex::lock()
167 pthread_t id = pthread_self();
168 unique_lock<mutex> lk(__m_);
169 if (pthread_equal(id, __id_))
171 if (__count_ == numeric_limits<size_t>::max())
172 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
176 while (__count_ != 0)
183 recursive_timed_mutex::try_lock() _NOEXCEPT
185 pthread_t id = pthread_self();
186 unique_lock<mutex> lk(__m_, try_to_lock);
187 if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
189 if (__count_ == numeric_limits<size_t>::max())
199 recursive_timed_mutex::unlock() _NOEXCEPT
201 unique_lock<mutex> lk(__m_);
210 #endif // !_LIBCPP_HAS_NO_THREADS
212 // If dispatch_once_f ever handles C++ exceptions, and if one can get to it
213 // without illegal macros (unexpected macros not beginning with _UpperCase or
214 // __lowercase), and if it stops spinning waiting threads, then call_once should
215 // call into dispatch_once_f instead of here. Relevant radar this code needs to
216 // keep in sync with: 7741191.
218 #ifndef _LIBCPP_HAS_NO_THREADS
219 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
220 static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
224 __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
226 #if defined(_LIBCPP_HAS_NO_THREADS)
229 #ifndef _LIBCPP_NO_EXCEPTIONS
232 #endif // _LIBCPP_NO_EXCEPTIONS
236 #ifndef _LIBCPP_NO_EXCEPTIONS
243 #endif // _LIBCPP_NO_EXCEPTIONS
245 #else // !_LIBCPP_HAS_NO_THREADS
246 pthread_mutex_lock(&mut);
248 pthread_cond_wait(&cv, &mut);
251 #ifndef _LIBCPP_NO_EXCEPTIONS
254 #endif // _LIBCPP_NO_EXCEPTIONS
256 pthread_mutex_unlock(&mut);
258 pthread_mutex_lock(&mut);
260 pthread_mutex_unlock(&mut);
261 pthread_cond_broadcast(&cv);
262 #ifndef _LIBCPP_NO_EXCEPTIONS
266 pthread_mutex_lock(&mut);
268 pthread_mutex_unlock(&mut);
269 pthread_cond_broadcast(&cv);
272 #endif // _LIBCPP_NO_EXCEPTIONS
275 pthread_mutex_unlock(&mut);
276 #endif // !_LIBCPP_HAS_NO_THREADS
280 _LIBCPP_END_NAMESPACE_STD