Constify. NFC.
[llvm-project.git] / libcxx / src / mutex.cpp
blobc36bd5549da8ee222f87e8740e6cee62fcb39fd2
1 //===------------------------- mutex.cpp ----------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is dual licensed under the MIT and the University of Illinois Open
6 // Source Licenses. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #define _LIBCPP_BUILDING_MUTEX
11 #include "mutex"
12 #include "limits"
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 = {};
24 mutex::~mutex()
26 __libcpp_mutex_destroy(&__m_);
29 void
30 mutex::lock()
32 int ec = __libcpp_mutex_lock(&__m_);
33 if (ec)
34 __throw_system_error(ec, "mutex lock failed");
37 bool
38 mutex::try_lock() _NOEXCEPT
40 return __libcpp_mutex_trylock(&__m_);
43 void
44 mutex::unlock() _NOEXCEPT
46 int ec = __libcpp_mutex_unlock(&__m_);
47 (void)ec;
48 _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed");
51 // recursive_mutex
53 recursive_mutex::recursive_mutex()
55 int ec = __libcpp_recursive_mutex_init(&__m_);
56 if (ec)
57 __throw_system_error(ec, "recursive_mutex constructor failed");
60 recursive_mutex::~recursive_mutex()
62 int e = __libcpp_recursive_mutex_destroy(&__m_);
63 (void)e;
64 _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed");
67 void
68 recursive_mutex::lock()
70 int ec = __libcpp_recursive_mutex_lock(&__m_);
71 if (ec)
72 __throw_system_error(ec, "recursive_mutex lock failed");
75 void
76 recursive_mutex::unlock() _NOEXCEPT
78 int e = __libcpp_recursive_mutex_unlock(&__m_);
79 (void)e;
80 _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed");
83 bool
84 recursive_mutex::try_lock() _NOEXCEPT
86 return __libcpp_recursive_mutex_trylock(&__m_);
89 // timed_mutex
91 timed_mutex::timed_mutex()
92 : __locked_(false)
96 timed_mutex::~timed_mutex()
98 lock_guard<mutex> _(__m_);
101 void
102 timed_mutex::lock()
104 unique_lock<mutex> lk(__m_);
105 while (__locked_)
106 __cv_.wait(lk);
107 __locked_ = true;
110 bool
111 timed_mutex::try_lock() _NOEXCEPT
113 unique_lock<mutex> lk(__m_, try_to_lock);
114 if (lk.owns_lock() && !__locked_)
116 __locked_ = true;
117 return true;
119 return false;
122 void
123 timed_mutex::unlock() _NOEXCEPT
125 lock_guard<mutex> _(__m_);
126 __locked_ = false;
127 __cv_.notify_one();
130 // recursive_timed_mutex
132 recursive_timed_mutex::recursive_timed_mutex()
133 : __count_(0),
134 __id_(0)
138 recursive_timed_mutex::~recursive_timed_mutex()
140 lock_guard<mutex> _(__m_);
143 void
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");
152 ++__count_;
153 return;
155 while (__count_ != 0)
156 __cv_.wait(lk);
157 __count_ = 1;
158 __id_ = id;
161 bool
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())
169 return false;
170 ++__count_;
171 __id_ = id;
172 return true;
174 return false;
177 void
178 recursive_timed_mutex::unlock() _NOEXCEPT
180 unique_lock<mutex> lk(__m_);
181 if (--__count_ == 0)
183 __id_ = 0;
184 lk.unlock();
185 __cv_.notify_one();
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;
200 #endif
202 void
203 __call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
205 #if defined(_LIBCPP_HAS_NO_THREADS)
206 if (flag == 0)
208 #ifndef _LIBCPP_NO_EXCEPTIONS
211 #endif // _LIBCPP_NO_EXCEPTIONS
212 flag = 1;
213 func(arg);
214 flag = ~0ul;
215 #ifndef _LIBCPP_NO_EXCEPTIONS
217 catch (...)
219 flag = 0ul;
220 throw;
222 #endif // _LIBCPP_NO_EXCEPTIONS
224 #else // !_LIBCPP_HAS_NO_THREADS
225 __libcpp_mutex_lock(&mut);
226 while (flag == 1)
227 __libcpp_condvar_wait(&cv, &mut);
228 if (flag == 0)
230 #ifndef _LIBCPP_NO_EXCEPTIONS
233 #endif // _LIBCPP_NO_EXCEPTIONS
234 __libcpp_relaxed_store(&flag, 1ul);
235 __libcpp_mutex_unlock(&mut);
236 func(arg);
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
243 catch (...)
245 __libcpp_mutex_lock(&mut);
246 __libcpp_relaxed_store(&flag, 0ul);
247 __libcpp_mutex_unlock(&mut);
248 __libcpp_condvar_broadcast(&cv);
249 throw;
251 #endif // _LIBCPP_NO_EXCEPTIONS
253 else
254 __libcpp_mutex_unlock(&mut);
255 #endif // !_LIBCPP_HAS_NO_THREADS
259 _LIBCPP_END_NAMESPACE_STD