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"
14 #include "include/atomic_support.h"
15 #include "__undef_macros"
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 __libcpp_mutex_destroy(&__m_
);
32 int ec
= __libcpp_mutex_lock(&__m_
);
34 __throw_system_error(ec
, "mutex lock failed");
38 mutex::try_lock() _NOEXCEPT
40 return __libcpp_mutex_trylock(&__m_
);
44 mutex::unlock() _NOEXCEPT
46 int ec
= __libcpp_mutex_unlock(&__m_
);
48 _LIBCPP_ASSERT(ec
== 0, "call to mutex::unlock failed");
53 recursive_mutex::recursive_mutex()
55 int ec
= __libcpp_recursive_mutex_init(&__m_
);
57 __throw_system_error(ec
, "recursive_mutex constructor failed");
60 recursive_mutex::~recursive_mutex()
62 int e
= __libcpp_recursive_mutex_destroy(&__m_
);
64 _LIBCPP_ASSERT(e
== 0, "call to ~recursive_mutex() failed");
68 recursive_mutex::lock()
70 int ec
= __libcpp_recursive_mutex_lock(&__m_
);
72 __throw_system_error(ec
, "recursive_mutex lock failed");
76 recursive_mutex::unlock() _NOEXCEPT
78 int e
= __libcpp_recursive_mutex_unlock(&__m_
);
80 _LIBCPP_ASSERT(e
== 0, "call to recursive_mutex::unlock() failed");
84 recursive_mutex::try_lock() _NOEXCEPT
86 return __libcpp_recursive_mutex_trylock(&__m_
);
91 timed_mutex::timed_mutex()
96 timed_mutex::~timed_mutex()
98 lock_guard
<mutex
> _(__m_
);
104 unique_lock
<mutex
> lk(__m_
);
111 timed_mutex::try_lock() _NOEXCEPT
113 unique_lock
<mutex
> lk(__m_
, try_to_lock
);
114 if (lk
.owns_lock() && !__locked_
)
123 timed_mutex::unlock() _NOEXCEPT
125 lock_guard
<mutex
> _(__m_
);
130 // recursive_timed_mutex
132 recursive_timed_mutex::recursive_timed_mutex()
138 recursive_timed_mutex::~recursive_timed_mutex()
140 lock_guard
<mutex
> _(__m_
);
144 recursive_timed_mutex::lock()
146 __libcpp_thread_id id
= __libcpp_thread_get_current_id();
147 unique_lock
<mutex
> lk(__m_
);
148 if (__libcpp_thread_id_equal(id
, __id_
))
150 if (__count_
== numeric_limits
<size_t>::max())
151 __throw_system_error(EAGAIN
, "recursive_timed_mutex lock limit reached");
155 while (__count_
!= 0)
162 recursive_timed_mutex::try_lock() _NOEXCEPT
164 __libcpp_thread_id id
= __libcpp_thread_get_current_id();
165 unique_lock
<mutex
> lk(__m_
, try_to_lock
);
166 if (lk
.owns_lock() && (__count_
== 0 || __libcpp_thread_id_equal(id
, __id_
)))
168 if (__count_
== numeric_limits
<size_t>::max())
178 recursive_timed_mutex::unlock() _NOEXCEPT
180 unique_lock
<mutex
> lk(__m_
);
189 #endif // !_LIBCPP_HAS_NO_THREADS
191 // If dispatch_once_f ever handles C++ exceptions, and if one can get to it
192 // without illegal macros (unexpected macros not beginning with _UpperCase or
193 // __lowercase), and if it stops spinning waiting threads, then call_once should
194 // call into dispatch_once_f instead of here. Relevant radar this code needs to
195 // keep in sync with: 7741191.
197 #ifndef _LIBCPP_HAS_NO_THREADS
198 _LIBCPP_SAFE_STATIC
static __libcpp_mutex_t mut
= _LIBCPP_MUTEX_INITIALIZER
;
199 _LIBCPP_SAFE_STATIC
static __libcpp_condvar_t cv
= _LIBCPP_CONDVAR_INITIALIZER
;
203 __call_once(volatile unsigned long& flag
, void* arg
, void(*func
)(void*))
205 #if defined(_LIBCPP_HAS_NO_THREADS)
208 #ifndef _LIBCPP_NO_EXCEPTIONS
211 #endif // _LIBCPP_NO_EXCEPTIONS
215 #ifndef _LIBCPP_NO_EXCEPTIONS
222 #endif // _LIBCPP_NO_EXCEPTIONS
224 #else // !_LIBCPP_HAS_NO_THREADS
225 __libcpp_mutex_lock(&mut
);
227 __libcpp_condvar_wait(&cv
, &mut
);
230 #ifndef _LIBCPP_NO_EXCEPTIONS
233 #endif // _LIBCPP_NO_EXCEPTIONS
234 __libcpp_relaxed_store(&flag
, 1ul);
235 __libcpp_mutex_unlock(&mut
);
237 __libcpp_mutex_lock(&mut
);
238 __libcpp_atomic_store(&flag
, ~0ul, _AO_Release
);
239 __libcpp_mutex_unlock(&mut
);
240 __libcpp_condvar_broadcast(&cv
);
241 #ifndef _LIBCPP_NO_EXCEPTIONS
245 __libcpp_mutex_lock(&mut
);
246 __libcpp_relaxed_store(&flag
, 0ul);
247 __libcpp_mutex_unlock(&mut
);
248 __libcpp_condvar_broadcast(&cv
);
251 #endif // _LIBCPP_NO_EXCEPTIONS
254 __libcpp_mutex_unlock(&mut
);
255 #endif // !_LIBCPP_HAS_NO_THREADS
259 _LIBCPP_END_NAMESPACE_STD