blob: 5f8ba0a08266fda323039224ec247ff56acae1ad [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"
Dan Albert1d4a1ed2016-05-25 22:36:09 -070015#include "support/atomic_support.h"
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000016
17_LIBCPP_BEGIN_NAMESPACE_STD
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +000018#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000019
20const defer_lock_t defer_lock = {};
21const try_to_lock_t try_to_lock = {};
22const adopt_lock_t adopt_lock = {};
23
24mutex::~mutex()
25{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070026 pthread_mutex_destroy(&__m_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000027}
28
29void
30mutex::lock()
31{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070032 int ec = pthread_mutex_lock(&__m_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000033 if (ec)
34 __throw_system_error(ec, "mutex lock failed");
35}
36
37bool
Howard Hinnant499c61f2012-07-21 16:13:09 +000038mutex::try_lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000039{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070040 return pthread_mutex_trylock(&__m_) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000041}
42
43void
Howard Hinnant499c61f2012-07-21 16:13:09 +000044mutex::unlock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000045{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070046 int ec = pthread_mutex_unlock(&__m_);
Howard Hinnantfc910cb2013-09-21 21:26:37 +000047 (void)ec;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048 assert(ec == 0);
49}
50
51// recursive_mutex
52
53recursive_mutex::recursive_mutex()
54{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070055 pthread_mutexattr_t attr;
56 int ec = pthread_mutexattr_init(&attr);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000057 if (ec)
Dan Albert1d4a1ed2016-05-25 22:36:09 -070058 goto fail;
59 ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
60 if (ec)
61 {
62 pthread_mutexattr_destroy(&attr);
63 goto fail;
64 }
65 ec = pthread_mutex_init(&__m_, &attr);
66 if (ec)
67 {
68 pthread_mutexattr_destroy(&attr);
69 goto fail;
70 }
71 ec = pthread_mutexattr_destroy(&attr);
72 if (ec)
73 {
74 pthread_mutex_destroy(&__m_);
75 goto fail;
76 }
77 return;
78fail:
79 __throw_system_error(ec, "recursive_mutex constructor failed");
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000080}
81
82recursive_mutex::~recursive_mutex()
83{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070084 int e = pthread_mutex_destroy(&__m_);
Howard Hinnantfc910cb2013-09-21 21:26:37 +000085 (void)e;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000086 assert(e == 0);
87}
88
89void
90recursive_mutex::lock()
91{
Dan Albert1d4a1ed2016-05-25 22:36:09 -070092 int ec = pthread_mutex_lock(&__m_);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000093 if (ec)
94 __throw_system_error(ec, "recursive_mutex lock failed");
95}
96
97void
Howard Hinnant499c61f2012-07-21 16:13:09 +000098recursive_mutex::unlock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000099{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700100 int e = pthread_mutex_unlock(&__m_);
Howard Hinnantfc910cb2013-09-21 21:26:37 +0000101 (void)e;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000102 assert(e == 0);
103}
104
105bool
Howard Hinnant499c61f2012-07-21 16:13:09 +0000106recursive_mutex::try_lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000107{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700108 return pthread_mutex_trylock(&__m_) == 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000109}
110
111// timed_mutex
112
113timed_mutex::timed_mutex()
114 : __locked_(false)
115{
116}
117
118timed_mutex::~timed_mutex()
119{
120 lock_guard<mutex> _(__m_);
121}
122
123void
124timed_mutex::lock()
125{
126 unique_lock<mutex> lk(__m_);
127 while (__locked_)
128 __cv_.wait(lk);
129 __locked_ = true;
130}
131
132bool
Howard Hinnant499c61f2012-07-21 16:13:09 +0000133timed_mutex::try_lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000134{
135 unique_lock<mutex> lk(__m_, try_to_lock);
136 if (lk.owns_lock() && !__locked_)
137 {
138 __locked_ = true;
139 return true;
140 }
141 return false;
142}
143
144void
Howard Hinnant499c61f2012-07-21 16:13:09 +0000145timed_mutex::unlock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146{
147 lock_guard<mutex> _(__m_);
148 __locked_ = false;
149 __cv_.notify_one();
150}
151
152// recursive_timed_mutex
153
154recursive_timed_mutex::recursive_timed_mutex()
155 : __count_(0),
Howard Hinnantadff4892010-05-24 17:49:41 +0000156 __id_(0)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000157{
158}
159
160recursive_timed_mutex::~recursive_timed_mutex()
161{
162 lock_guard<mutex> _(__m_);
163}
164
165void
166recursive_timed_mutex::lock()
167{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700168 pthread_t id = pthread_self();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169 unique_lock<mutex> lk(__m_);
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700170 if (pthread_equal(id, __id_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000171 {
172 if (__count_ == numeric_limits<size_t>::max())
173 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
174 ++__count_;
175 return;
176 }
177 while (__count_ != 0)
178 __cv_.wait(lk);
179 __count_ = 1;
180 __id_ = id;
181}
182
183bool
Howard Hinnant499c61f2012-07-21 16:13:09 +0000184recursive_timed_mutex::try_lock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000185{
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700186 pthread_t id = pthread_self();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000187 unique_lock<mutex> lk(__m_, try_to_lock);
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700188 if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000189 {
190 if (__count_ == numeric_limits<size_t>::max())
191 return false;
192 ++__count_;
193 __id_ = id;
194 return true;
195 }
196 return false;
197}
198
199void
Howard Hinnant499c61f2012-07-21 16:13:09 +0000200recursive_timed_mutex::unlock() _NOEXCEPT
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201{
202 unique_lock<mutex> lk(__m_);
203 if (--__count_ == 0)
204 {
Howard Hinnantadff4892010-05-24 17:49:41 +0000205 __id_ = 0;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000206 lk.unlock();
207 __cv_.notify_one();
208 }
209}
210
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000211#endif // !_LIBCPP_HAS_NO_THREADS
212
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213// 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.
218
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000219#ifndef _LIBCPP_HAS_NO_THREADS
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700220static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
221static pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000222#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223
Eric Fiselierc6e46692015-07-07 00:27:16 +0000224/// 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.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227void
228__call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
229{
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000230#if defined(_LIBCPP_HAS_NO_THREADS)
231 if (flag == 0)
232 {
233#ifndef _LIBCPP_NO_EXCEPTIONS
234 try
235 {
236#endif // _LIBCPP_NO_EXCEPTIONS
237 flag = 1;
238 func(arg);
239 flag = ~0ul;
240#ifndef _LIBCPP_NO_EXCEPTIONS
241 }
242 catch (...)
243 {
244 flag = 0ul;
245 throw;
246 }
247#endif // _LIBCPP_NO_EXCEPTIONS
248 }
249#else // !_LIBCPP_HAS_NO_THREADS
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700250 pthread_mutex_lock(&mut);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000251 while (flag == 1)
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700252 pthread_cond_wait(&cv, &mut);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253 if (flag == 0)
254 {
Howard Hinnantd4444702010-08-11 17:04:31 +0000255#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256 try
257 {
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000258#endif // _LIBCPP_NO_EXCEPTIONS
Eric Fiselierc6e46692015-07-07 00:27:16 +0000259 __libcpp_relaxed_store(&flag, 1ul);
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700260 pthread_mutex_unlock(&mut);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000261 func(arg);
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700262 pthread_mutex_lock(&mut);
Eric Fiselierc6e46692015-07-07 00:27:16 +0000263 __libcpp_relaxed_store(&flag, ~0ul);
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700264 pthread_mutex_unlock(&mut);
265 pthread_cond_broadcast(&cv);
Howard Hinnantd4444702010-08-11 17:04:31 +0000266#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000267 }
268 catch (...)
269 {
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700270 pthread_mutex_lock(&mut);
Eric Fiselierc6e46692015-07-07 00:27:16 +0000271 __libcpp_relaxed_store(&flag, 0ul);
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700272 pthread_mutex_unlock(&mut);
273 pthread_cond_broadcast(&cv);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274 throw;
275 }
Howard Hinnant16e6e1d2010-08-22 00:03:27 +0000276#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000277 }
278 else
Dan Albert1d4a1ed2016-05-25 22:36:09 -0700279 pthread_mutex_unlock(&mut);
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000280#endif // !_LIBCPP_HAS_NO_THREADS
281
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000282}
283
284_LIBCPP_END_NAMESPACE_STD