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"
15 #include "support/atomic_support.h"
17 _LIBCPP_BEGIN_NAMESPACE_STD
18 #ifndef _LIBCPP_HAS_NO_THREADS
20 const defer_lock_t defer_lock = {};
21 const try_to_lock_t try_to_lock = {};
22 const adopt_lock_t adopt_lock = {};
26 pthread_mutex_destroy(&__m_);
32 int ec = pthread_mutex_lock(&__m_);
34 __throw_system_error(ec, "mutex lock failed");
38 mutex::try_lock() _NOEXCEPT
40 return pthread_mutex_trylock(&__m_) == 0;
44 mutex::unlock() _NOEXCEPT
46 int ec = pthread_mutex_unlock(&__m_);
53 recursive_mutex::recursive_mutex()
55 pthread_mutexattr_t attr;
56 int ec = pthread_mutexattr_init(&attr);
59 ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
62 pthread_mutexattr_destroy(&attr);
65 ec = pthread_mutex_init(&__m_, &attr);
68 pthread_mutexattr_destroy(&attr);
71 ec = pthread_mutexattr_destroy(&attr);
74 pthread_mutex_destroy(&__m_);
79 __throw_system_error(ec, "recursive_mutex constructor failed");
82 recursive_mutex::~recursive_mutex()
84 int e = pthread_mutex_destroy(&__m_);
90 recursive_mutex::lock()
92 int ec = pthread_mutex_lock(&__m_);
94 __throw_system_error(ec, "recursive_mutex lock failed");
98 recursive_mutex::unlock() _NOEXCEPT
100 int e = pthread_mutex_unlock(&__m_);
106 recursive_mutex::try_lock() _NOEXCEPT
108 return pthread_mutex_trylock(&__m_) == 0;
113 timed_mutex::timed_mutex()
118 timed_mutex::~timed_mutex()
120 lock_guard<mutex> _(__m_);
126 unique_lock<mutex> lk(__m_);
133 timed_mutex::try_lock() _NOEXCEPT
135 unique_lock<mutex> lk(__m_, try_to_lock);
136 if (lk.owns_lock() && !__locked_)
145 timed_mutex::unlock() _NOEXCEPT
147 lock_guard<mutex> _(__m_);
152 // recursive_timed_mutex
154 recursive_timed_mutex::recursive_timed_mutex()
160 recursive_timed_mutex::~recursive_timed_mutex()
162 lock_guard<mutex> _(__m_);
166 recursive_timed_mutex::lock()
168 pthread_t id = pthread_self();
169 unique_lock<mutex> lk(__m_);
170 if (pthread_equal(id, __id_))
172 if (__count_ == numeric_limits<size_t>::max())
173 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
177 while (__count_ != 0)
184 recursive_timed_mutex::try_lock() _NOEXCEPT
186 pthread_t id = pthread_self();
187 unique_lock<mutex> lk(__m_, try_to_lock);
188 if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
190 if (__count_ == numeric_limits<size_t>::max())
200 recursive_timed_mutex::unlock() _NOEXCEPT
202 unique_lock<mutex> lk(__m_);
211 #endif // !_LIBCPP_HAS_NO_THREADS
213 // If dispatch_once_f ever handles C++ exceptions, and if one can get to it
214 // without illegal macros (unexpected macros not beginning with _UpperCase or
215 // __lowercase), and if it stops spinning waiting threads, then call_once should
216 // call into dispatch_once_f instead of here. Relevant radar this code needs to
217 // keep in sync with: 7741191.
219 #ifndef _LIBCPP_HAS_NO_THREADS
220 static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
221 static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
224 /// NOTE: Changes to flag are done via relaxed atomic stores
225 /// even though the accesses are protected by a mutex because threads
226 /// just entering 'call_once` concurrently read from flag.
228 __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
230 #if defined(_LIBCPP_HAS_NO_THREADS)
233 #ifndef _LIBCPP_NO_EXCEPTIONS
236 #endif // _LIBCPP_NO_EXCEPTIONS
240 #ifndef _LIBCPP_NO_EXCEPTIONS
247 #endif // _LIBCPP_NO_EXCEPTIONS
249 #else // !_LIBCPP_HAS_NO_THREADS
250 pthread_mutex_lock(&mut);
252 pthread_cond_wait(&cv, &mut);
255 #ifndef _LIBCPP_NO_EXCEPTIONS
258 #endif // _LIBCPP_NO_EXCEPTIONS
259 __libcpp_relaxed_store(&flag, 1ul);
260 pthread_mutex_unlock(&mut);
262 pthread_mutex_lock(&mut);
263 __libcpp_relaxed_store(&flag, ~0ul);
264 pthread_mutex_unlock(&mut);
265 pthread_cond_broadcast(&cv);
266 #ifndef _LIBCPP_NO_EXCEPTIONS
270 pthread_mutex_lock(&mut);
271 __libcpp_relaxed_store(&flag, 0ul);
272 pthread_mutex_unlock(&mut);
273 pthread_cond_broadcast(&cv);
276 #endif // _LIBCPP_NO_EXCEPTIONS
279 pthread_mutex_unlock(&mut);
280 #endif // !_LIBCPP_HAS_NO_THREADS
284 _LIBCPP_END_NAMESPACE_STD