blob: 01b7420fa3b8a9b56198dcb46b53e5b03b97939d [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
Louis Dionne9bdbeb32022-02-14 13:41:09 -05009#include <__assert>
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050010#include <limits>
11#include <mutex>
12#include <system_error>
13
Eric Fiselier1366d262015-08-18 21:08:54 +000014#include "include/atomic_support.h"
Howard Hinnantc51e1022010-05-11 19:42:16 +000015
Petr Hosek99575aa2019-05-30 01:34:41 +000016#ifndef _LIBCPP_HAS_NO_THREADS
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050017# if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
18# pragma comment(lib, "pthread")
19# endif
Petr Hosek99575aa2019-05-30 01:34:41 +000020#endif
21
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050022_LIBCPP_PUSH_MACROS
23#include <__undef_macros>
24
Howard Hinnantc51e1022010-05-11 19:42:16 +000025_LIBCPP_BEGIN_NAMESPACE_STD
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -050026
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000027#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnantc51e1022010-05-11 19:42:16 +000028
Louis Dionnea7a2beb2019-09-26 14:51:10 +000029const defer_lock_t defer_lock{};
30const try_to_lock_t try_to_lock{};
31const adopt_lock_t adopt_lock{};
Howard Hinnantc51e1022010-05-11 19:42:16 +000032
Eric Fiselierf6dac862019-07-07 01:20:54 +000033// ~mutex is defined elsewhere
Howard Hinnantc51e1022010-05-11 19:42:16 +000034
35void
36mutex::lock()
37{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000038 int ec = __libcpp_mutex_lock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000039 if (ec)
40 __throw_system_error(ec, "mutex lock failed");
41}
42
43bool
Louis Dionne65358e12021-03-01 12:09:45 -050044mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000045{
Eric Fiselier0ec14472017-01-14 10:27:12 +000046 return __libcpp_mutex_trylock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000047}
48
49void
Louis Dionne65358e12021-03-01 12:09:45 -050050mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000051{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000052 int ec = __libcpp_mutex_unlock(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000053 (void)ec;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000054 _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000055}
56
57// recursive_mutex
58
59recursive_mutex::recursive_mutex()
60{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000061 int ec = __libcpp_recursive_mutex_init(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000062 if (ec)
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000063 __throw_system_error(ec, "recursive_mutex constructor failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000064}
65
66recursive_mutex::~recursive_mutex()
67{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000068 int e = __libcpp_recursive_mutex_destroy(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000069 (void)e;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000070 _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000071}
72
73void
74recursive_mutex::lock()
75{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000076 int ec = __libcpp_recursive_mutex_lock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000077 if (ec)
78 __throw_system_error(ec, "recursive_mutex lock failed");
79}
80
81void
Louis Dionne65358e12021-03-01 12:09:45 -050082recursive_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000083{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000084 int e = __libcpp_recursive_mutex_unlock(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000085 (void)e;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000086 _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000087}
88
89bool
Louis Dionne65358e12021-03-01 12:09:45 -050090recursive_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000091{
Eric Fiselier0ec14472017-01-14 10:27:12 +000092 return __libcpp_recursive_mutex_trylock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000093}
94
95// timed_mutex
96
97timed_mutex::timed_mutex()
98 : __locked_(false)
99{
100}
101
102timed_mutex::~timed_mutex()
103{
104 lock_guard<mutex> _(__m_);
105}
106
107void
108timed_mutex::lock()
109{
110 unique_lock<mutex> lk(__m_);
111 while (__locked_)
112 __cv_.wait(lk);
113 __locked_ = true;
114}
115
116bool
Louis Dionne65358e12021-03-01 12:09:45 -0500117timed_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118{
119 unique_lock<mutex> lk(__m_, try_to_lock);
120 if (lk.owns_lock() && !__locked_)
121 {
122 __locked_ = true;
123 return true;
124 }
125 return false;
126}
127
128void
Louis Dionne65358e12021-03-01 12:09:45 -0500129timed_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000130{
131 lock_guard<mutex> _(__m_);
132 __locked_ = false;
133 __cv_.notify_one();
134}
135
136// recursive_timed_mutex
137
138recursive_timed_mutex::recursive_timed_mutex()
139 : __count_(0),
Marshall Clow58655362019-08-14 16:21:27 +0000140 __id_{}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000141{
142}
143
144recursive_timed_mutex::~recursive_timed_mutex()
145{
146 lock_guard<mutex> _(__m_);
147}
148
149void
150recursive_timed_mutex::lock()
151{
Marshall Clow58655362019-08-14 16:21:27 +0000152 __thread_id id = this_thread::get_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153 unique_lock<mutex> lk(__m_);
Marshall Clow58655362019-08-14 16:21:27 +0000154 if (id ==__id_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000155 {
156 if (__count_ == numeric_limits<size_t>::max())
157 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
158 ++__count_;
159 return;
160 }
161 while (__count_ != 0)
162 __cv_.wait(lk);
163 __count_ = 1;
164 __id_ = id;
165}
166
167bool
Louis Dionne65358e12021-03-01 12:09:45 -0500168recursive_timed_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000169{
Marshall Clow58655362019-08-14 16:21:27 +0000170 __thread_id id = this_thread::get_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 unique_lock<mutex> lk(__m_, try_to_lock);
Marshall Clow58655362019-08-14 16:21:27 +0000172 if (lk.owns_lock() && (__count_ == 0 || id == __id_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000173 {
174 if (__count_ == numeric_limits<size_t>::max())
175 return false;
176 ++__count_;
177 __id_ = id;
178 return true;
179 }
180 return false;
181}
182
183void
Louis Dionne65358e12021-03-01 12:09:45 -0500184recursive_timed_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185{
186 unique_lock<mutex> lk(__m_);
187 if (--__count_ == 0)
188 {
Marshall Clow62172652019-08-14 20:54:56 +0000189 __id_.__reset();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000190 lk.unlock();
191 __cv_.notify_one();
192 }
193}
194
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000195#endif // !_LIBCPP_HAS_NO_THREADS
196
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
198// without illegal macros (unexpected macros not beginning with _UpperCase or
199// __lowercase), and if it stops spinning waiting threads, then call_once should
200// call into dispatch_once_f instead of here. Relevant radar this code needs to
201// keep in sync with: 7741191.
202
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000203#ifndef _LIBCPP_HAS_NO_THREADS
Arthur O'Dwyer209ff392022-02-08 13:08:59 -0500204static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
205static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000206#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207
Nico Weber03776e72019-03-20 22:55:03 +0000208void __call_once(volatile once_flag::_State_type& flag, void* arg,
209 void (*func)(void*))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000210{
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000211#if defined(_LIBCPP_HAS_NO_THREADS)
212 if (flag == 0)
213 {
214#ifndef _LIBCPP_NO_EXCEPTIONS
215 try
216 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400217#endif // _LIBCPP_NO_EXCEPTIONS
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000218 flag = 1;
219 func(arg);
Nico Weber03776e72019-03-20 22:55:03 +0000220 flag = ~once_flag::_State_type(0);
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000221#ifndef _LIBCPP_NO_EXCEPTIONS
222 }
223 catch (...)
224 {
Nico Weber03776e72019-03-20 22:55:03 +0000225 flag = 0;
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000226 throw;
227 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400228#endif // _LIBCPP_NO_EXCEPTIONS
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000229 }
230#else // !_LIBCPP_HAS_NO_THREADS
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000231 __libcpp_mutex_lock(&mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232 while (flag == 1)
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000233 __libcpp_condvar_wait(&cv, &mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 if (flag == 0)
235 {
Howard Hinnant72f73582010-08-11 17:04:31 +0000236#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 try
238 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400239#endif // _LIBCPP_NO_EXCEPTIONS
Nico Weber03776e72019-03-20 22:55:03 +0000240 __libcpp_relaxed_store(&flag, once_flag::_State_type(1));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000241 __libcpp_mutex_unlock(&mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000242 func(arg);
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000243 __libcpp_mutex_lock(&mut);
Nico Weber03776e72019-03-20 22:55:03 +0000244 __libcpp_atomic_store(&flag, ~once_flag::_State_type(0),
245 _AO_Release);
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000246 __libcpp_mutex_unlock(&mut);
247 __libcpp_condvar_broadcast(&cv);
Howard Hinnant72f73582010-08-11 17:04:31 +0000248#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000249 }
250 catch (...)
251 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000252 __libcpp_mutex_lock(&mut);
Nico Weber03776e72019-03-20 22:55:03 +0000253 __libcpp_relaxed_store(&flag, once_flag::_State_type(0));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000254 __libcpp_mutex_unlock(&mut);
255 __libcpp_condvar_broadcast(&cv);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 throw;
257 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400258#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 }
260 else
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000261 __libcpp_mutex_unlock(&mut);
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000262#endif // !_LIBCPP_HAS_NO_THREADS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000263}
264
265_LIBCPP_END_NAMESPACE_STD
Arthur O'Dwyercf9bf392022-02-11 13:00:39 -0500266
267_LIBCPP_POP_MACROS