blob: 6669e7698168258611fa9bd412b2d9b6a1217974 [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
9#include "mutex"
10#include "limits"
11#include "system_error"
Eric Fiselier1366d262015-08-18 21:08:54 +000012#include "include/atomic_support.h"
Eric Fiselierf4433a32017-05-31 22:07:49 +000013#include "__undef_macros"
Howard Hinnantc51e1022010-05-11 19:42:16 +000014
Petr Hosek99575aa2019-05-30 01:34:41 +000015#ifndef _LIBCPP_HAS_NO_THREADS
Michał Górny8d676fb2019-12-02 11:49:20 +010016#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
Petr Hosek99575aa2019-05-30 01:34:41 +000017#pragma comment(lib, "pthread")
18#endif
19#endif
20
Howard Hinnantc51e1022010-05-11 19:42:16 +000021_LIBCPP_BEGIN_NAMESPACE_STD
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +000022#ifndef _LIBCPP_HAS_NO_THREADS
Howard Hinnantc51e1022010-05-11 19:42:16 +000023
Louis Dionnea7a2beb2019-09-26 14:51:10 +000024const defer_lock_t defer_lock{};
25const try_to_lock_t try_to_lock{};
26const adopt_lock_t adopt_lock{};
Howard Hinnantc51e1022010-05-11 19:42:16 +000027
Eric Fiselierf6dac862019-07-07 01:20:54 +000028// ~mutex is defined elsewhere
Howard Hinnantc51e1022010-05-11 19:42:16 +000029
30void
31mutex::lock()
32{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000033 int ec = __libcpp_mutex_lock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000034 if (ec)
35 __throw_system_error(ec, "mutex lock failed");
36}
37
38bool
Louis Dionne65358e12021-03-01 12:09:45 -050039mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000040{
Eric Fiselier0ec14472017-01-14 10:27:12 +000041 return __libcpp_mutex_trylock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000042}
43
44void
Louis Dionne65358e12021-03-01 12:09:45 -050045mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000046{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000047 int ec = __libcpp_mutex_unlock(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000048 (void)ec;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000049 _LIBCPP_ASSERT(ec == 0, "call to mutex::unlock failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000050}
51
52// recursive_mutex
53
54recursive_mutex::recursive_mutex()
55{
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000056 int ec = __libcpp_recursive_mutex_init(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000057 if (ec)
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +000058 __throw_system_error(ec, "recursive_mutex constructor failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000059}
60
61recursive_mutex::~recursive_mutex()
62{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000063 int e = __libcpp_recursive_mutex_destroy(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000064 (void)e;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000065 _LIBCPP_ASSERT(e == 0, "call to ~recursive_mutex() failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000066}
67
68void
69recursive_mutex::lock()
70{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000071 int ec = __libcpp_recursive_mutex_lock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000072 if (ec)
73 __throw_system_error(ec, "recursive_mutex lock failed");
74}
75
76void
Louis Dionne65358e12021-03-01 12:09:45 -050077recursive_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000078{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +000079 int e = __libcpp_recursive_mutex_unlock(&__m_);
Howard Hinnant9ad56f32013-09-21 21:26:37 +000080 (void)e;
Eric Fiselier59cd98b2017-02-04 23:22:28 +000081 _LIBCPP_ASSERT(e == 0, "call to recursive_mutex::unlock() failed");
Howard Hinnantc51e1022010-05-11 19:42:16 +000082}
83
84bool
Louis Dionne65358e12021-03-01 12:09:45 -050085recursive_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +000086{
Eric Fiselier0ec14472017-01-14 10:27:12 +000087 return __libcpp_recursive_mutex_trylock(&__m_);
Howard Hinnantc51e1022010-05-11 19:42:16 +000088}
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
Louis Dionne65358e12021-03-01 12:09:45 -0500112timed_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113{
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
Louis Dionne65358e12021-03-01 12:09:45 -0500124timed_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125{
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 Clow58655362019-08-14 16:21:27 +0000135 __id_{}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000136{
137}
138
139recursive_timed_mutex::~recursive_timed_mutex()
140{
141 lock_guard<mutex> _(__m_);
142}
143
144void
145recursive_timed_mutex::lock()
146{
Marshall Clow58655362019-08-14 16:21:27 +0000147 __thread_id id = this_thread::get_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148 unique_lock<mutex> lk(__m_);
Marshall Clow58655362019-08-14 16:21:27 +0000149 if (id ==__id_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150 {
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
Louis Dionne65358e12021-03-01 12:09:45 -0500163recursive_timed_mutex::try_lock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164{
Marshall Clow58655362019-08-14 16:21:27 +0000165 __thread_id id = this_thread::get_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 unique_lock<mutex> lk(__m_, try_to_lock);
Marshall Clow58655362019-08-14 16:21:27 +0000167 if (lk.owns_lock() && (__count_ == 0 || id == __id_))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168 {
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
Louis Dionne65358e12021-03-01 12:09:45 -0500179recursive_timed_mutex::unlock() noexcept
Howard Hinnantc51e1022010-05-11 19:42:16 +0000180{
181 unique_lock<mutex> lk(__m_);
182 if (--__count_ == 0)
183 {
Marshall Clow62172652019-08-14 20:54:56 +0000184 __id_.__reset();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185 lk.unlock();
186 __cv_.notify_one();
187 }
188}
189
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000190#endif // !_LIBCPP_HAS_NO_THREADS
191
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192// 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 Roelofs39cb6bf2014-09-05 19:45:05 +0000198#ifndef _LIBCPP_HAS_NO_THREADS
Arthur O'Dwyer209ff392022-02-08 13:08:59 -0500199static constinit __libcpp_mutex_t mut = _LIBCPP_MUTEX_INITIALIZER;
200static constinit __libcpp_condvar_t cv = _LIBCPP_CONDVAR_INITIALIZER;
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000201#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202
Nico Weber03776e72019-03-20 22:55:03 +0000203void __call_once(volatile once_flag::_State_type& flag, void* arg,
204 void (*func)(void*))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000205{
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000206#if defined(_LIBCPP_HAS_NO_THREADS)
207 if (flag == 0)
208 {
209#ifndef _LIBCPP_NO_EXCEPTIONS
210 try
211 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400212#endif // _LIBCPP_NO_EXCEPTIONS
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000213 flag = 1;
214 func(arg);
Nico Weber03776e72019-03-20 22:55:03 +0000215 flag = ~once_flag::_State_type(0);
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000216#ifndef _LIBCPP_NO_EXCEPTIONS
217 }
218 catch (...)
219 {
Nico Weber03776e72019-03-20 22:55:03 +0000220 flag = 0;
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000221 throw;
222 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400223#endif // _LIBCPP_NO_EXCEPTIONS
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000224 }
225#else // !_LIBCPP_HAS_NO_THREADS
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000226 __libcpp_mutex_lock(&mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000227 while (flag == 1)
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000228 __libcpp_condvar_wait(&cv, &mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229 if (flag == 0)
230 {
Howard Hinnant72f73582010-08-11 17:04:31 +0000231#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232 try
233 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400234#endif // _LIBCPP_NO_EXCEPTIONS
Nico Weber03776e72019-03-20 22:55:03 +0000235 __libcpp_relaxed_store(&flag, once_flag::_State_type(1));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000236 __libcpp_mutex_unlock(&mut);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 func(arg);
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000238 __libcpp_mutex_lock(&mut);
Nico Weber03776e72019-03-20 22:55:03 +0000239 __libcpp_atomic_store(&flag, ~once_flag::_State_type(0),
240 _AO_Release);
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000241 __libcpp_mutex_unlock(&mut);
242 __libcpp_condvar_broadcast(&cv);
Howard Hinnant72f73582010-08-11 17:04:31 +0000243#ifndef _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000244 }
245 catch (...)
246 {
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000247 __libcpp_mutex_lock(&mut);
Nico Weber03776e72019-03-20 22:55:03 +0000248 __libcpp_relaxed_store(&flag, once_flag::_State_type(0));
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000249 __libcpp_mutex_unlock(&mut);
250 __libcpp_condvar_broadcast(&cv);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000251 throw;
252 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400253#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 }
255 else
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000256 __libcpp_mutex_unlock(&mut);
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000257#endif // !_LIBCPP_HAS_NO_THREADS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258}
259
260_LIBCPP_END_NAMESPACE_STD