blob: 42195aa8064874aba59b4d04e60cf216fa35481f [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001//===------------------------- mutex.cpp ----------------------------------===//
2//
Howard Hinnantf5256e12010-05-11 21:36:01 +00003// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00004//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00005// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
Howard Hinnant499c61f2012-07-21 16:13:09 +000010#define _LIBCPP_BUILDING_MUTEX
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000011#include "mutex"
12#include "limits"
13#include "system_error"
14#include "cassert"
15
16_LIBCPP_BEGIN_NAMESPACE_STD
17
18const defer_lock_t defer_lock = {};
19const try_to_lock_t try_to_lock = {};
20const adopt_lock_t adopt_lock = {};
21
22mutex::~mutex()
23{
Howard Hinnantec3773c2011-12-01 20:21:04 +000024 pthread_mutex_destroy(&__m_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000025}
26
27void
28mutex::lock()
29{
30 int ec = pthread_mutex_lock(&__m_);
31 if (ec)
32 __throw_system_error(ec, "mutex lock failed");
33}
34
35bool
Howard Hinnant499c61f2012-07-21 16:13:09 +000036mutex::try_lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000037{
38 return pthread_mutex_trylock(&__m_) == 0;
39}
40
41void
Howard Hinnant499c61f2012-07-21 16:13:09 +000042mutex::unlock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000043{
44 int ec = pthread_mutex_unlock(&__m_);
45 assert(ec == 0);
46}
47
48// recursive_mutex
49
50recursive_mutex::recursive_mutex()
51{
52 pthread_mutexattr_t attr;
53 int ec = pthread_mutexattr_init(&attr);
54 if (ec)
55 goto fail;
56 ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
57 if (ec)
58 {
59 pthread_mutexattr_destroy(&attr);
60 goto fail;
61 }
62 ec = pthread_mutex_init(&__m_, &attr);
63 if (ec)
64 {
65 pthread_mutexattr_destroy(&attr);
66 goto fail;
67 }
68 ec = pthread_mutexattr_destroy(&attr);
69 if (ec)
70 {
71 pthread_mutex_destroy(&__m_);
72 goto fail;
73 }
74 return;
75fail:
76 __throw_system_error(ec, "recursive_mutex constructor failed");
77}
78
79recursive_mutex::~recursive_mutex()
80{
81 int e = pthread_mutex_destroy(&__m_);
82 assert(e == 0);
83}
84
85void
86recursive_mutex::lock()
87{
88 int ec = pthread_mutex_lock(&__m_);
89 if (ec)
90 __throw_system_error(ec, "recursive_mutex lock failed");
91}
92
93void
Howard Hinnant499c61f2012-07-21 16:13:09 +000094recursive_mutex::unlock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000095{
96 int e = pthread_mutex_unlock(&__m_);
97 assert(e == 0);
98}
99
100bool
Howard Hinnant499c61f2012-07-21 16:13:09 +0000101recursive_mutex::try_lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000102{
103 return pthread_mutex_trylock(&__m_) == 0;
104}
105
106// timed_mutex
107
108timed_mutex::timed_mutex()
109 : __locked_(false)
110{
111}
112
113timed_mutex::~timed_mutex()
114{
115 lock_guard<mutex> _(__m_);
116}
117
118void
119timed_mutex::lock()
120{
121 unique_lock<mutex> lk(__m_);
122 while (__locked_)
123 __cv_.wait(lk);
124 __locked_ = true;
125}
126
127bool
Howard Hinnant499c61f2012-07-21 16:13:09 +0000128timed_mutex::try_lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000129{
130 unique_lock<mutex> lk(__m_, try_to_lock);
131 if (lk.owns_lock() && !__locked_)
132 {
133 __locked_ = true;
134 return true;
135 }
136 return false;
137}
138
139void
Howard Hinnant499c61f2012-07-21 16:13:09 +0000140timed_mutex::unlock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000141{
142 lock_guard<mutex> _(__m_);
143 __locked_ = false;
144 __cv_.notify_one();
145}
146
147// recursive_timed_mutex
148
149recursive_timed_mutex::recursive_timed_mutex()
150 : __count_(0),
Howard Hinnantadff4892010-05-24 17:49:41 +0000151 __id_(0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000152{
153}
154
155recursive_timed_mutex::~recursive_timed_mutex()
156{
157 lock_guard<mutex> _(__m_);
158}
159
160void
161recursive_timed_mutex::lock()
162{
163 pthread_t id = pthread_self();
164 unique_lock<mutex> lk(__m_);
165 if (pthread_equal(id, __id_))
166 {
167 if (__count_ == numeric_limits<size_t>::max())
168 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
169 ++__count_;
170 return;
171 }
172 while (__count_ != 0)
173 __cv_.wait(lk);
174 __count_ = 1;
175 __id_ = id;
176}
177
178bool
Howard Hinnant499c61f2012-07-21 16:13:09 +0000179recursive_timed_mutex::try_lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000180{
181 pthread_t id = pthread_self();
182 unique_lock<mutex> lk(__m_, try_to_lock);
183 if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
184 {
185 if (__count_ == numeric_limits<size_t>::max())
186 return false;
187 ++__count_;
188 __id_ = id;
189 return true;
190 }
191 return false;
192}
193
194void
Howard Hinnant499c61f2012-07-21 16:13:09 +0000195recursive_timed_mutex::unlock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000196{
197 unique_lock<mutex> lk(__m_);
198 if (--__count_ == 0)
199 {
Howard Hinnantadff4892010-05-24 17:49:41 +0000200 __id_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201 lk.unlock();
202 __cv_.notify_one();
203 }
204}
205
206// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
207// without illegal macros (unexpected macros not beginning with _UpperCase or
208// __lowercase), and if it stops spinning waiting threads, then call_once should
209// call into dispatch_once_f instead of here. Relevant radar this code needs to
210// keep in sync with: 7741191.
211
212static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
213static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
214
215void
216__call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
217{
218 pthread_mutex_lock(&mut);
219 while (flag == 1)
220 pthread_cond_wait(&cv, &mut);
221 if (flag == 0)
222 {
Howard Hinnantd4444702010-08-11 17:04:31 +0000223#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000224 try
225 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000226#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227 flag = 1;
228 pthread_mutex_unlock(&mut);
229 func(arg);
230 pthread_mutex_lock(&mut);
231 flag = ~0ul;
232 pthread_mutex_unlock(&mut);
233 pthread_cond_broadcast(&cv);
Howard Hinnantd4444702010-08-11 17:04:31 +0000234#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000235 }
236 catch (...)
237 {
238 pthread_mutex_lock(&mut);
239 flag = 0ul;
240 pthread_mutex_unlock(&mut);
Howard Hinnant21aefc32010-06-03 16:42:57 +0000241 pthread_cond_broadcast(&cv);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000242 throw;
243 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000244#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000245 }
246 else
247 pthread_mutex_unlock(&mut);
248}
249
250_LIBCPP_END_NAMESPACE_STD