blob: 9e780aaff64e85dadf2695dc8a6f5f3fb0f82522 [file] [log] [blame]
Louis Dionne9bd93882021-11-17 16:25:01 -05001//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00002//
Chandler Carruthd2012102019-01-19 10:56:40 +00003// 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 Hinnantc51e1022010-05-11 19:42:16 +00006//
7//===----------------------------------------------------------------------===//
8
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -05009#include <limits>
10#include <mutex>
11#include <system_error>
12
Eric Fiselier1366d262015-08-18 21:08:54 +000013#include "include/atomic_support.h"
Howard Hinnantc51e1022010-05-11 19:42:16 +000014
Petr Hosek99575aa2019-05-30 01:34:41 +000015#ifndef _LIBCPP_HAS_NO_THREADS
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050016# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
17# pragma comment(lib, "pthread")
18# endif
Petr Hosek99575aa2019-05-30 01:34:41 +000019#endif
20
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050021_LIBCPP_PUSH_MACROS
22#include <__undef_macros>
23
Howard Hinnantc51e1022010-05-11 19:42:16 +000024_LIBCPP_BEGIN_NAMESPACE_STD
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050025
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000026#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnantc51e1022010-05-11 19:42:16 +000027
Louis Dionnea7a2beb2019-09-26 14:51:10 +000028const defer_lock_t defer_lock{};
29const try_to_lock_t try_to_lock{};
30const adopt_lock_t adopt_lock{};
Howard Hinnantc51e1022010-05-11 19:42:16 +000031
Eric Fiselierf6dac862019-07-07 01:20:54 +000032// ~mutex is defined elsewhere
Howard Hinnantc51e1022010-05-11 19:42:16 +000033
34void
35mutex::lock()
36{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000037 int ec = __libcpp_mutex_lock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000038 if (ec)
39 __throw_system_error(ec, "mutex lock failed");
40}
41
42bool
Louis Dionne65358e12021-03-01 12:09:45 -050043mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000044{
Eric Fiselier0ec14472017-01-14 10:27:12 +000045 return __libcpp_mutex_trylock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000046}
47
48void
Louis Dionne65358e12021-03-01 12:09:45 -050049mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000050{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000051 int ec = __libcpp_mutex_unlock(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000052 (void)ec;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000053 _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000054}
55
56// recursive_mutex
57
58recursive_mutex::recursive_mutex()
59{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000060 int ec = __libcpp_recursive_mutex_init(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000061 if (ec)
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000062 __throw_system_error(ec, "recursive_mutex constructor failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000063}
64
65recursive_mutex::~recursive_mutex()
66{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000067 int e = __libcpp_recursive_mutex_destroy(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000068 (void)e;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000069 _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000070}
71
72void
73recursive_mutex::lock()
74{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000075 int ec = __libcpp_recursive_mutex_lock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000076 if (ec)
77 __throw_system_error(ec, "recursive_mutex lock failed");
78}
79
80void
Louis Dionne65358e12021-03-01 12:09:45 -050081recursive_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000082{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000083 int e = __libcpp_recursive_mutex_unlock(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000084 (void)e;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000085 _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000086}
87
88bool
Louis Dionne65358e12021-03-01 12:09:45 -050089recursive_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000090{
Eric Fiselier0ec14472017-01-14 10:27:12 +000091 return __libcpp_recursive_mutex_trylock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000092}
93
94// timed_mutex
95
96timed_mutex::timed_mutex()
97 : __locked_(false)
98{
99}
100
101timed_mutex::~timed_mutex()
102{
103 lock_guard<mutex> _(__m_);
104}
105
106void
107timed_mutex::lock()
108{
109 unique_lock<mutex> lk(__m_);
110 while (__locked_)
111 __cv_.wait(lk);
112 __locked_ = true;
113}
114
115bool
Louis Dionne65358e12021-03-01 12:09:45 -0500116timed_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000117{
118 unique_lock<mutex> lk(__m_, try_to_lock);
119 if (lk.owns_lock() && !__locked_)
120 {
121 __locked_ = true;
122 return true;
123 }
124 return false;
125}
126
127void
Louis Dionne65358e12021-03-01 12:09:45 -0500128timed_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129{
130 lock_guard<mutex> _(__m_);
131 __locked_ = false;
132 __cv_.notify_one();
133}
134
135// recursive_timed_mutex
136
137recursive_timed_mutex::recursive_timed_mutex()
138 : __count_(0),
Marshall Clow58655362019-08-14 16:21:27 +0000139 __id_{}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000140{
141}
142
143recursive_timed_mutex::~recursive_timed_mutex()
144{
145 lock_guard<mutex> _(__m_);
146}
147
148void
149recursive_timed_mutex::lock()
150{
Marshall Clow58655362019-08-14 16:21:27 +0000151 __thread_id id = this_thread::get_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 unique_lock<mutex> lk(__m_);
Marshall Clow58655362019-08-14 16:21:27 +0000153 if (id ==__id_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154 {
155 if (__count_ == numeric_limits<size_t>::max())
156 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
157 ++__count_;
158 return;
159 }
160 while (__count_ != 0)
161 __cv_.wait(lk);
162 __count_ = 1;
163 __id_ = id;
164}
165
166bool
Louis Dionne65358e12021-03-01 12:09:45 -0500167recursive_timed_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168{
Marshall Clow58655362019-08-14 16:21:27 +0000169 __thread_id id = this_thread::get_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170 unique_lock<mutex> lk(__m_, try_to_lock);
Marshall Clow58655362019-08-14 16:21:27 +0000171 if (lk.owns_lock() && (__count_ == 0 || id == __id_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172 {
173 if (__count_ == numeric_limits<size_t>::max())
174 return false;
175 ++__count_;
176 __id_ = id;
177 return true;
178 }
179 return false;
180}
181
182void
Louis Dionne65358e12021-03-01 12:09:45 -0500183recursive_timed_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000184{
185 unique_lock<mutex> lk(__m_);
186 if (--__count_ == 0)
187 {
Marshall Clow62172652019-08-14 20:54:56 +0000188 __id_.__reset();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189 lk.unlock();
190 __cv_.notify_one();
191 }
192}
193
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000194#endif // !_LIBCPP_HAS_NO_THREADS
195
Howard Hinnantc51e1022010-05-11 19:42:16 +0000196// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
197// without illegal macros (unexpected macros not beginning with _UpperCase or
198// __lowercase), and if it stops spinning waiting threads, then call_once should
199// call into dispatch_once_f instead of here. Relevant radar this code needs to
200// keep in sync with: 7741191.
201
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000202#ifndef _LIBCPP_HAS_NO_THREADS
Arthur O'Dwyer209ff392022-02-08 13:08:59 -0500203static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
204static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000205#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206
Nico Weber03776e72019-03-20 22:55:03 +0000207void __call_once(volatile once_flag::_State_type& flag, void* arg,
208 void (*func)(void*))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209{
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000210#if defined(_LIBCPP_HAS_NO_THREADS)
211 if (flag == 0)
212 {
213#ifndef _LIBCPP_NO_EXCEPTIONS
214 try
215 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400216#endif // _LIBCPP_NO_EXCEPTIONS
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000217 flag = 1;
218 func(arg);
Nico Weber03776e72019-03-20 22:55:03 +0000219 flag = ~once_flag::_State_type(0);
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000220#ifndef _LIBCPP_NO_EXCEPTIONS
221 }
222 catch (...)
223 {
Nico Weber03776e72019-03-20 22:55:03 +0000224 flag = 0;
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000225 throw;
226 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400227#endif // _LIBCPP_NO_EXCEPTIONS
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000228 }
229#else // !_LIBCPP_HAS_NO_THREADS
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000230 __libcpp_mutex_lock(&mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000231 while (flag == 1)
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000232 __libcpp_condvar_wait(&cv, &mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233 if (flag == 0)
234 {
Howard Hinnant72f73582010-08-11 17:04:31 +0000235#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236 try
237 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400238#endif // _LIBCPP_NO_EXCEPTIONS
Nico Weber03776e72019-03-20 22:55:03 +0000239 __libcpp_relaxed_store(&flag, once_flag::_State_type(1));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000240 __libcpp_mutex_unlock(&mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241 func(arg);
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000242 __libcpp_mutex_lock(&mut);
Nico Weber03776e72019-03-20 22:55:03 +0000243 __libcpp_atomic_store(&flag, ~once_flag::_State_type(0),
244 _AO_Release);
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000245 __libcpp_mutex_unlock(&mut);
246 __libcpp_condvar_broadcast(&cv);
Howard Hinnant72f73582010-08-11 17:04:31 +0000247#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000248 }
249 catch (...)
250 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000251 __libcpp_mutex_lock(&mut);
Nico Weber03776e72019-03-20 22:55:03 +0000252 __libcpp_relaxed_store(&flag, once_flag::_State_type(0));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000253 __libcpp_mutex_unlock(&mut);
254 __libcpp_condvar_broadcast(&cv);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000255 throw;
256 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400257#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258 }
259 else
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000260 __libcpp_mutex_unlock(&mut);
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000261#endif // !_LIBCPP_HAS_NO_THREADS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000262}
263
264_LIBCPP_END_NAMESPACE_STD
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -0500265
266_LIBCPP_POP_MACROS