blob: 182785733efd57ab3faea3ee148e6801d3c49e91 [file] [log] [blame]
Howard Hinnant3e519522010-05-11 19:42:161//===------------------------- mutex.cpp ----------------------------------===//
2//
Chandler Carruth57b08b02019-01-19 10:56:403// 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
Howard Hinnant3e519522010-05-11 19:42:166//
7//===----------------------------------------------------------------------===//
8
Howard Hinnant3e519522010-05-11 19:42:169#include "mutex"
10#include "limits"
11#include "system_error"
Eric Fiseliere8fd1642015-08-18 21:08:5412#include "include/atomic_support.h"
Eric Fiseliera016efb2017-05-31 22:07:4913#include "__undef_macros"
Howard Hinnant3e519522010-05-11 19:42:1614
Petr Hosek996e62e2019-05-30 01:34:4115#ifndef _LIBCPP_HAS_NO_THREADS
Yi Kongd8bdb922019-07-22 20:41:0316#if defined(__unix__) && !defined(__ANDROID__) && defined(__ELF__) && defined(_LIBCPP_HAS_COMMENT_LIB_PRAGMA)
Petr Hosek996e62e2019-05-30 01:34:4117#pragma comment(lib, "pthread")
18#endif
19#endif
20
Howard Hinnant3e519522010-05-11 19:42:1621_LIBCPP_BEGIN_NAMESPACE_STD
Jonathan Roelofsb3fcc672014-09-05 19:45:0522#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnant3e519522010-05-11 19:42:1623
24const defer_lock_t defer_lock = {};
25const try_to_lock_t try_to_lock = {};
26const adopt_lock_t adopt_lock = {};
27
Eric Fiselier8baf8382019-07-07 01:20:5428// ~mutex is defined elsewhere
Howard Hinnant3e519522010-05-11 19:42:1629
30void
31mutex::lock()
32{
Asiri Rathnayakec7e42392016-05-06 14:06:2933 int ec = __libcpp_mutex_lock(&__m_);
Howard Hinnant3e519522010-05-11 19:42:1634 if (ec)
35 __throw_system_error(ec, "mutex lock failed");
36}
37
38bool
Howard Hinnant02e610e2012-07-21 16:13:0939mutex::try_lock() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:1640{
Eric Fiselier08e14772017-01-14 10:27:1241 return __libcpp_mutex_trylock(&__m_);
Howard Hinnant3e519522010-05-11 19:42:1642}
43
44void
Howard Hinnant02e610e2012-07-21 16:13:0945mutex::unlock() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:1646{
Asiri Rathnayakec7e42392016-05-06 14:06:2947 int ec = __libcpp_mutex_unlock(&__m_);
Howard Hinnant12bfc4f2013-09-21 21:26:3748 (void)ec;
Eric Fiseliere49cdfb2017-02-04 23:22:2849 _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed");
Howard Hinnant3e519522010-05-11 19:42:1650}
51
52// recursive_mutex
53
54recursive_mutex::recursive_mutex()
55{
Asiri Rathnayakec7e42392016-05-06 14:06:2956 int ec = __libcpp_recursive_mutex_init(&__m_);
Howard Hinnant3e519522010-05-11 19:42:1657 if (ec)
Asiri Rathnayakec7e42392016-05-06 14:06:2958 __throw_system_error(ec, "recursive_mutex constructor failed");
Howard Hinnant3e519522010-05-11 19:42:1659}
60
61recursive_mutex::~recursive_mutex()
62{
Saleem Abdulrasool58a0dce2017-01-05 17:54:4563 int e = __libcpp_recursive_mutex_destroy(&__m_);
Howard Hinnant12bfc4f2013-09-21 21:26:3764 (void)e;
Eric Fiseliere49cdfb2017-02-04 23:22:2865 _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed");
Howard Hinnant3e519522010-05-11 19:42:1666}
67
68void
69recursive_mutex::lock()
70{
Saleem Abdulrasool58a0dce2017-01-05 17:54:4571 int ec = __libcpp_recursive_mutex_lock(&__m_);
Howard Hinnant3e519522010-05-11 19:42:1672 if (ec)
73 __throw_system_error(ec, "recursive_mutex lock failed");
74}
75
76void
Howard Hinnant02e610e2012-07-21 16:13:0977recursive_mutex::unlock() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:1678{
Saleem Abdulrasool58a0dce2017-01-05 17:54:4579 int e = __libcpp_recursive_mutex_unlock(&__m_);
Howard Hinnant12bfc4f2013-09-21 21:26:3780 (void)e;
Eric Fiseliere49cdfb2017-02-04 23:22:2881 _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed");
Howard Hinnant3e519522010-05-11 19:42:1682}
83
84bool
Howard Hinnant02e610e2012-07-21 16:13:0985recursive_mutex::try_lock() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:1686{
Eric Fiselier08e14772017-01-14 10:27:1287 return __libcpp_recursive_mutex_trylock(&__m_);
Howard Hinnant3e519522010-05-11 19:42:1688}
89
90// timed_mutex
91
92timed_mutex::timed_mutex()
93 : __locked_(false)
94{
95}
96
97timed_mutex::~timed_mutex()
98{
99 lock_guard<mutex> _(__m_);
100}
101
102void
103timed_mutex::lock()
104{
105 unique_lock<mutex> lk(__m_);
106 while (__locked_)
107 __cv_.wait(lk);
108 __locked_ = true;
109}
110
111bool
Howard Hinnant02e610e2012-07-21 16:13:09112timed_mutex::try_lock() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16113{
114 unique_lock<mutex> lk(__m_, try_to_lock);
115 if (lk.owns_lock() && !__locked_)
116 {
117 __locked_ = true;
118 return true;
119 }
120 return false;
121}
122
123void
Howard Hinnant02e610e2012-07-21 16:13:09124timed_mutex::unlock() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16125{
126 lock_guard<mutex> _(__m_);
127 __locked_ = false;
128 __cv_.notify_one();
129}
130
131// recursive_timed_mutex
132
133recursive_timed_mutex::recursive_timed_mutex()
134 : __count_(0),
Marshall Clow2b1d4252019-08-14 16:21:27135 __id_{}
Howard Hinnant3e519522010-05-11 19:42:16136{
137}
138
139recursive_timed_mutex::~recursive_timed_mutex()
140{
141 lock_guard<mutex> _(__m_);
142}
143
144void
145recursive_timed_mutex::lock()
146{
Marshall Clow2b1d4252019-08-14 16:21:27147 __thread_id id = this_thread::get_id();
Howard Hinnant3e519522010-05-11 19:42:16148 unique_lock<mutex> lk(__m_);
Marshall Clow2b1d4252019-08-14 16:21:27149 if (id ==__id_)
Howard Hinnant3e519522010-05-11 19:42:16150 {
151 if (__count_ == numeric_limits<size_t>::max())
152 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
153 ++__count_;
154 return;
155 }
156 while (__count_ != 0)
157 __cv_.wait(lk);
158 __count_ = 1;
159 __id_ = id;
160}
161
162bool
Howard Hinnant02e610e2012-07-21 16:13:09163recursive_timed_mutex::try_lock() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16164{
Marshall Clow2b1d4252019-08-14 16:21:27165 __thread_id id = this_thread::get_id();
Howard Hinnant3e519522010-05-11 19:42:16166 unique_lock<mutex> lk(__m_, try_to_lock);
Marshall Clow2b1d4252019-08-14 16:21:27167 if (lk.owns_lock() && (__count_ == 0 || id == __id_))
Howard Hinnant3e519522010-05-11 19:42:16168 {
169 if (__count_ == numeric_limits<size_t>::max())
170 return false;
171 ++__count_;
172 __id_ = id;
173 return true;
174 }
175 return false;
176}
177
178void
Howard Hinnant02e610e2012-07-21 16:13:09179recursive_timed_mutex::unlock() _NOEXCEPT
Howard Hinnant3e519522010-05-11 19:42:16180{
181 unique_lock<mutex> lk(__m_);
182 if (--__count_ == 0)
183 {
Marshall Clow2e80d012019-08-14 20:54:56184 __id_.__reset();
Howard Hinnant3e519522010-05-11 19:42:16185 lk.unlock();
186 __cv_.notify_one();
187 }
188}
189
Jonathan Roelofsb3fcc672014-09-05 19:45:05190#endif // !_LIBCPP_HAS_NO_THREADS
191
Howard Hinnant3e519522010-05-11 19:42:16192// 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.
197
Jonathan Roelofsb3fcc672014-09-05 19:45:05198#ifndef _LIBCPP_HAS_NO_THREADS
Eric Fiselierea117bf2016-09-28 22:08:13199_LIBCPP_SAFE_STATIC static __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
200_LIBCPP_SAFE_STATIC static __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
Jonathan Roelofsb3fcc672014-09-05 19:45:05201#endif
Howard Hinnant3e519522010-05-11 19:42:16202
Nico Weber0fd00a52019-03-20 22:55:03203void __call_once(volatile once_flag::_State_type& flag, void* arg,
204 void (*func)(void*))
Howard Hinnant3e519522010-05-11 19:42:16205{
Jonathan Roelofsb3fcc672014-09-05 19:45:05206#if defined(_LIBCPP_HAS_NO_THREADS)
207 if (flag == 0)
208 {
209#ifndef _LIBCPP_NO_EXCEPTIONS
210 try
211 {
212#endif // _LIBCPP_NO_EXCEPTIONS
213 flag = 1;
214 func(arg);
Nico Weber0fd00a52019-03-20 22:55:03215 flag = ~once_flag::_State_type(0);
Jonathan Roelofsb3fcc672014-09-05 19:45:05216#ifndef _LIBCPP_NO_EXCEPTIONS
217 }
218 catch (...)
219 {
Nico Weber0fd00a52019-03-20 22:55:03220 flag = 0;
Jonathan Roelofsb3fcc672014-09-05 19:45:05221 throw;
222 }
223#endif // _LIBCPP_NO_EXCEPTIONS
224 }
225#else // !_LIBCPP_HAS_NO_THREADS
Asiri Rathnayakec7e42392016-05-06 14:06:29226 __libcpp_mutex_lock(&mut);
Howard Hinnant3e519522010-05-11 19:42:16227 while (flag == 1)
Asiri Rathnayakec7e42392016-05-06 14:06:29228 __libcpp_condvar_wait(&cv, &mut);
Howard Hinnant3e519522010-05-11 19:42:16229 if (flag == 0)
230 {
Howard Hinnant54b409f2010-08-11 17:04:31231#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16232 try
233 {
Howard Hinnant940e2112010-08-22 00:03:27234#endif // _LIBCPP_NO_EXCEPTIONS
Nico Weber0fd00a52019-03-20 22:55:03235 __libcpp_relaxed_store(&flag, once_flag::_State_type(1));
Asiri Rathnayakec7e42392016-05-06 14:06:29236 __libcpp_mutex_unlock(&mut);
Howard Hinnant3e519522010-05-11 19:42:16237 func(arg);
Asiri Rathnayakec7e42392016-05-06 14:06:29238 __libcpp_mutex_lock(&mut);
Nico Weber0fd00a52019-03-20 22:55:03239 __libcpp_atomic_store(&flag, ~once_flag::_State_type(0),
240 _AO_Release);
Asiri Rathnayakec7e42392016-05-06 14:06:29241 __libcpp_mutex_unlock(&mut);
242 __libcpp_condvar_broadcast(&cv);
Howard Hinnant54b409f2010-08-11 17:04:31243#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16244 }
245 catch (...)
246 {
Asiri Rathnayakec7e42392016-05-06 14:06:29247 __libcpp_mutex_lock(&mut);
Nico Weber0fd00a52019-03-20 22:55:03248 __libcpp_relaxed_store(&flag, once_flag::_State_type(0));
Asiri Rathnayakec7e42392016-05-06 14:06:29249 __libcpp_mutex_unlock(&mut);
250 __libcpp_condvar_broadcast(&cv);
Howard Hinnant3e519522010-05-11 19:42:16251 throw;
252 }
Howard Hinnant940e2112010-08-22 00:03:27253#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnant3e519522010-05-11 19:42:16254 }
255 else
Asiri Rathnayakec7e42392016-05-06 14:06:29256 __libcpp_mutex_unlock(&mut);
Jonathan Roelofsb3fcc672014-09-05 19:45:05257#endif // !_LIBCPP_HAS_NO_THREADS
Howard Hinnant3e519522010-05-11 19:42:16258}
259
260_LIBCPP_END_NAMESPACE_STD