1 //===------------------------- mutex.cpp ----------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
11 #include "system_error"
12 #include "include/atomic_support.h"
13 #include "__undef_macros"
15 #ifndef _LIBCPP_HAS_NO_THREADS
16 #if defined(__unix__) && defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA)
17 #pragma comment(lib, "pthread")
21 _LIBCPP_BEGIN_NAMESPACE_STD
22 #ifndef _LIBCPP_HAS_NO_THREADS
24 const defer_lock_t defer_lock = {};
25 const try_to_lock_t try_to_lock = {};
26 const adopt_lock_t adopt_lock = {};
28 // ~mutex is defined elsewhere
33 int ec = __libcpp_mutex_lock(&__m_);
35 __throw_system_error(ec, "mutex lock failed");
39 mutex::try_lock() _NOEXCEPT
41 return __libcpp_mutex_trylock(&__m_);
45 mutex::unlock() _NOEXCEPT
47 int ec = __libcpp_mutex_unlock(&__m_);
49 _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed");
54 recursive_mutex::recursive_mutex()
56 int ec = __libcpp_recursive_mutex_init(&__m_);
58 __throw_system_error(ec, "recursive_mutex constructor failed");
61 recursive_mutex::~recursive_mutex()
63 int e = __libcpp_recursive_mutex_destroy(&__m_);
65 _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed");
69 recursive_mutex::lock()
71 int ec = __libcpp_recursive_mutex_lock(&__m_);
73 __throw_system_error(ec, "recursive_mutex lock failed");
77 recursive_mutex::unlock() _NOEXCEPT
79 int e = __libcpp_recursive_mutex_unlock(&__m_);
81 _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed");
85 recursive_mutex::try_lock() _NOEXCEPT
87 return __libcpp_recursive_mutex_trylock(&__m_);
92 timed_mutex::timed_mutex()
97 timed_mutex::~timed_mutex()
99 lock_guard<mutex> _(__m_);
105 unique_lock<mutex> lk(__m_);
112 timed_mutex::try_lock() _NOEXCEPT
114 unique_lock<mutex> lk(__m_, try_to_lock);
115 if (lk.owns_lock() && !__locked_)
124 timed_mutex::unlock() _NOEXCEPT
126 lock_guard<mutex> _(__m_);
131 // recursive_timed_mutex
133 recursive_timed_mutex::recursive_timed_mutex()
139 recursive_timed_mutex::~recursive_timed_mutex()
141 lock_guard<mutex> _(__m_);
145 recursive_timed_mutex::lock()
147 __thread_id id = this_thread::get_id();
148 unique_lock<mutex> lk(__m_);
151 if (__count_ == numeric_limits<size_t>::max())
152 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
156 while (__count_ != 0)
163 recursive_timed_mutex::try_lock() _NOEXCEPT
165 __thread_id id = this_thread::get_id();
166 unique_lock<mutex> lk(__m_, try_to_lock);
167 if (lk.owns_lock() && (__count_ == 0 || id == __id_))
169 if (__count_ == numeric_limits<size_t>::max())
179 recursive_timed_mutex::unlock() _NOEXCEPT
181 unique_lock<mutex> lk(__m_);
190 #endif // !_LIBCPP_HAS_NO_THREADS
192 // If dispatch_once_f ever handles C++ exceptions, and if one can get to it
193 // without illegal macros (unexpected macros not beginning with _UpperCase or
194 // __lowercase), and if it stops spinning waiting threads, then call_once should
195 // call into dispatch_once_f instead of here. Relevant radar this code needs to
196 // keep in sync with: 7741191.
198 #ifndef _LIBCPP_HAS_NO_THREADS
199 _LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
200 _LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
203 void __call_once(volatile once_flag::_State_type& flag, void* arg,
206 #if defined(_LIBCPP_HAS_NO_THREADS)
209 #ifndef _LIBCPP_NO_EXCEPTIONS
212 #endif // _LIBCPP_NO_EXCEPTIONS
215 flag = ~once_flag::_State_type(0);
216 #ifndef _LIBCPP_NO_EXCEPTIONS
223 #endif // _LIBCPP_NO_EXCEPTIONS
225 #else // !_LIBCPP_HAS_NO_THREADS
226 __libcpp_mutex_lock(&mut);
228 __libcpp_condvar_wait(&cv, &mut);
231 #ifndef _LIBCPP_NO_EXCEPTIONS
234 #endif // _LIBCPP_NO_EXCEPTIONS
235 __libcpp_relaxed_store(&flag, once_flag::_State_type(1));
236 __libcpp_mutex_unlock(&mut);
238 __libcpp_mutex_lock(&mut);
239 __libcpp_atomic_store(&flag, ~once_flag::_State_type(0),
241 __libcpp_mutex_unlock(&mut);
242 __libcpp_condvar_broadcast(&cv);
243 #ifndef _LIBCPP_NO_EXCEPTIONS
247 __libcpp_mutex_lock(&mut);
248 __libcpp_relaxed_store(&flag, once_flag::_State_type(0));
249 __libcpp_mutex_unlock(&mut);
250 __libcpp_condvar_broadcast(&cv);
253 #endif // _LIBCPP_NO_EXCEPTIONS
256 __libcpp_mutex_unlock(&mut);
257 #endif // !_LIBCPP_HAS_NO_THREADS
260 _LIBCPP_END_NAMESPACE_STD