blob: eb8e54ad44214da5fa87ae6fdc119d7e3ac98356 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- mutex ------------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_MUTEX
11#define _LIBCPP_MUTEX
12
13/*
14 mutex synopsis
15
16namespace std
17{
18
19class mutex
20{
21public:
Howard Hinnantbd05d6a2012-07-21 16:13:09 +000022 constexpr mutex() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000023 ~mutex();
24
25 mutex(const mutex&) = delete;
26 mutex& operator=(const mutex&) = delete;
27
28 void lock();
29 bool try_lock();
30 void unlock();
31
32 typedef pthread_mutex_t* native_handle_type;
33 native_handle_type native_handle();
34};
35
36class recursive_mutex
37{
38public:
39 recursive_mutex();
40 ~recursive_mutex();
41
42 recursive_mutex(const recursive_mutex&) = delete;
43 recursive_mutex& operator=(const recursive_mutex&) = delete;
44
45 void lock();
Howard Hinnantbd05d6a2012-07-21 16:13:09 +000046 bool try_lock() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000047 void unlock();
48
49 typedef pthread_mutex_t* native_handle_type;
50 native_handle_type native_handle();
51};
52
53class timed_mutex
54{
55public:
56 timed_mutex();
57 ~timed_mutex();
58
59 timed_mutex(const timed_mutex&) = delete;
60 timed_mutex& operator=(const timed_mutex&) = delete;
61
62 void lock();
63 bool try_lock();
64 template <class Rep, class Period>
65 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
66 template <class Clock, class Duration>
67 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
68 void unlock();
69};
70
71class recursive_timed_mutex
72{
73public:
74 recursive_timed_mutex();
75 ~recursive_timed_mutex();
76
77 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
78 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
79
80 void lock();
Howard Hinnantbd05d6a2012-07-21 16:13:09 +000081 bool try_lock() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +000082 template <class Rep, class Period>
83 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
84 template <class Clock, class Duration>
85 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
86 void unlock();
87};
88
Louis Dionnea7a2beb2019-09-26 14:51:10 +000089struct defer_lock_t { explicit defer_lock_t() = default; };
90struct try_to_lock_t { explicit try_to_lock_t() = default; };
91struct adopt_lock_t { explicit adopt_lock_t() = default; };
Howard Hinnantc51e1022010-05-11 19:42:16 +000092
Marshall Clowf1bf62f2018-01-02 17:17:01 +000093inline constexpr defer_lock_t defer_lock{};
94inline constexpr try_to_lock_t try_to_lock{};
95inline constexpr adopt_lock_t adopt_lock{};
Howard Hinnantc51e1022010-05-11 19:42:16 +000096
97template <class Mutex>
98class lock_guard
99{
100public:
101 typedef Mutex mutex_type;
102
103 explicit lock_guard(mutex_type& m);
104 lock_guard(mutex_type& m, adopt_lock_t);
105 ~lock_guard();
106
107 lock_guard(lock_guard const&) = delete;
108 lock_guard& operator=(lock_guard const&) = delete;
109};
110
Marshall Clow580b6e62017-03-24 03:40:36 +0000111template <class... MutexTypes>
112class scoped_lock // C++17
Eric Fiselier68143d42016-06-14 03:48:09 +0000113{
114public:
Marshall Clow580b6e62017-03-24 03:40:36 +0000115 using mutex_type = Mutex; // If MutexTypes... consists of the single type Mutex
116
117 explicit scoped_lock(MutexTypes&... m);
Marshall Clowf68c6922017-07-27 17:44:03 +0000118 scoped_lock(adopt_lock_t, MutexTypes&... m);
Marshall Clow580b6e62017-03-24 03:40:36 +0000119 ~scoped_lock();
120 scoped_lock(scoped_lock const&) = delete;
121 scoped_lock& operator=(scoped_lock const&) = delete;
Eric Fiselier68143d42016-06-14 03:48:09 +0000122private:
123 tuple<MutexTypes&...> pm; // exposition only
124};
125
Howard Hinnantc51e1022010-05-11 19:42:16 +0000126template <class Mutex>
127class unique_lock
128{
129public:
130 typedef Mutex mutex_type;
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000131 unique_lock() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000132 explicit unique_lock(mutex_type& m);
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000133 unique_lock(mutex_type& m, defer_lock_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000134 unique_lock(mutex_type& m, try_to_lock_t);
135 unique_lock(mutex_type& m, adopt_lock_t);
136 template <class Clock, class Duration>
137 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
138 template <class Rep, class Period>
139 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
140 ~unique_lock();
141
142 unique_lock(unique_lock const&) = delete;
143 unique_lock& operator=(unique_lock const&) = delete;
144
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000145 unique_lock(unique_lock&& u) noexcept;
146 unique_lock& operator=(unique_lock&& u) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
148 void lock();
149 bool try_lock();
150
151 template <class Rep, class Period>
152 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
153 template <class Clock, class Duration>
154 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
155
156 void unlock();
157
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000158 void swap(unique_lock& u) noexcept;
159 mutex_type* release() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000160
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000161 bool owns_lock() const noexcept;
162 explicit operator bool () const noexcept;
163 mutex_type* mutex() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000164};
165
166template <class Mutex>
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000167 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000168
169template <class L1, class L2, class... L3>
170 int try_lock(L1&, L2&, L3&...);
171template <class L1, class L2, class... L3>
172 void lock(L1&, L2&, L3&...);
173
174struct once_flag
175{
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000176 constexpr once_flag() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000177
178 once_flag(const once_flag&) = delete;
179 once_flag& operator=(const once_flag&) = delete;
180};
181
182template<class Callable, class ...Args>
183 void call_once(once_flag& flag, Callable&& func, Args&&... args);
184
185} // std
186
187*/
188
189#include <__config>
190#include <__mutex_base>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400191#include <__threading_support>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000192#include <__utility/forward.h>
Nico Weber03776e72019-03-20 22:55:03 +0000193#include <cstdint>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000194#include <functional>
Eric Fiselier89659d12015-07-07 00:27:16 +0000195#include <memory>
Eric Fiselierb6159752017-04-18 23:05:08 +0000196#ifndef _LIBCPP_CXX03_LANG
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400197# include <tuple>
Howard Hinnant51d62e02011-05-16 19:05:11 +0000198#endif
Marshall Clow0a1e7502018-09-12 19:41:40 +0000199#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000200
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000201#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000202#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000203#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000204
Eric Fiselierf4433a32017-05-31 22:07:49 +0000205_LIBCPP_PUSH_MACROS
206#include <__undef_macros>
207
208
Howard Hinnantc51e1022010-05-11 19:42:16 +0000209_LIBCPP_BEGIN_NAMESPACE_STD
210
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000211#ifndef _LIBCPP_HAS_NO_THREADS
212
Howard Hinnant8331b762013-03-06 23:30:19 +0000213class _LIBCPP_TYPE_VIS recursive_mutex
Howard Hinnantc51e1022010-05-11 19:42:16 +0000214{
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +0000215 __libcpp_recursive_mutex_t __m_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000216
217public:
218 recursive_mutex();
219 ~recursive_mutex();
220
221private:
222 recursive_mutex(const recursive_mutex&); // = delete;
223 recursive_mutex& operator=(const recursive_mutex&); // = delete;
224
225public:
226 void lock();
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000227 bool try_lock() _NOEXCEPT;
228 void unlock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000229
Saleem Abdulrasool0bc37ca2017-01-05 17:54:45 +0000230 typedef __libcpp_recursive_mutex_t* native_handle_type;
231
Howard Hinnantf5f99992010-09-22 18:02:38 +0000232 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233 native_handle_type native_handle() {return &__m_;}
234};
235
Howard Hinnant8331b762013-03-06 23:30:19 +0000236class _LIBCPP_TYPE_VIS timed_mutex
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237{
238 mutex __m_;
239 condition_variable __cv_;
240 bool __locked_;
241public:
242 timed_mutex();
243 ~timed_mutex();
244
245private:
246 timed_mutex(const timed_mutex&); // = delete;
247 timed_mutex& operator=(const timed_mutex&); // = delete;
248
249public:
250 void lock();
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000251 bool try_lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000252 template <class _Rep, class _Period>
Howard Hinnantf5f99992010-09-22 18:02:38 +0000253 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000254 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000255 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000256 template <class _Clock, class _Duration>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000257 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000259 void unlock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000260};
261
262template <class _Clock, class _Duration>
263bool
264timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
265{
266 using namespace chrono;
267 unique_lock<mutex> __lk(__m_);
268 bool no_timeout = _Clock::now() < __t;
269 while (no_timeout && __locked_)
270 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
271 if (!__locked_)
272 {
273 __locked_ = true;
274 return true;
275 }
276 return false;
277}
278
Howard Hinnant8331b762013-03-06 23:30:19 +0000279class _LIBCPP_TYPE_VIS recursive_timed_mutex
Howard Hinnantc51e1022010-05-11 19:42:16 +0000280{
281 mutex __m_;
282 condition_variable __cv_;
283 size_t __count_;
Marshall Clow58655362019-08-14 16:21:27 +0000284 __thread_id __id_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285public:
286 recursive_timed_mutex();
287 ~recursive_timed_mutex();
288
289private:
290 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
291 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
292
293public:
294 void lock();
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000295 bool try_lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000296 template <class _Rep, class _Period>
Howard Hinnantf5f99992010-09-22 18:02:38 +0000297 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000298 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000299 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000300 template <class _Clock, class _Duration>
Shoaib Meenai55f3a462017-03-02 03:22:18 +0000301 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000302 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000303 void unlock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000304};
305
306template <class _Clock, class _Duration>
307bool
308recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
309{
310 using namespace chrono;
Marshall Clow58655362019-08-14 16:21:27 +0000311 __thread_id __id = this_thread::get_id();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000312 unique_lock<mutex> lk(__m_);
Marshall Clow58655362019-08-14 16:21:27 +0000313 if (__id == __id_)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000314 {
315 if (__count_ == numeric_limits<size_t>::max())
316 return false;
317 ++__count_;
318 return true;
319 }
320 bool no_timeout = _Clock::now() < __t;
321 while (no_timeout && __count_ != 0)
322 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
323 if (__count_ == 0)
324 {
325 __count_ = 1;
326 __id_ = __id;
327 return true;
328 }
329 return false;
330}
331
332template <class _L0, class _L1>
333int
334try_lock(_L0& __l0, _L1& __l1)
335{
336 unique_lock<_L0> __u0(__l0, try_to_lock);
337 if (__u0.owns_lock())
338 {
339 if (__l1.try_lock())
340 {
341 __u0.release();
342 return -1;
343 }
344 else
345 return 1;
346 }
347 return 0;
348}
349
Eric Fiselierb6159752017-04-18 23:05:08 +0000350#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000351
352template <class _L0, class _L1, class _L2, class... _L3>
353int
354try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
355{
356 int __r = 0;
357 unique_lock<_L0> __u0(__l0, try_to_lock);
358 if (__u0.owns_lock())
359 {
360 __r = try_lock(__l1, __l2, __l3...);
361 if (__r == -1)
362 __u0.release();
363 else
364 ++__r;
365 }
366 return __r;
367}
368
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400369#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000370
371template <class _L0, class _L1>
372void
373lock(_L0& __l0, _L1& __l1)
374{
375 while (true)
376 {
377 {
378 unique_lock<_L0> __u0(__l0);
379 if (__l1.try_lock())
380 {
381 __u0.release();
382 break;
383 }
384 }
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000385 __libcpp_thread_yield();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000386 {
387 unique_lock<_L1> __u1(__l1);
388 if (__l0.try_lock())
389 {
390 __u1.release();
391 break;
392 }
393 }
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000394 __libcpp_thread_yield();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000395 }
396}
397
Eric Fiselierb6159752017-04-18 23:05:08 +0000398#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000399
Howard Hinnantf8485322011-01-12 22:56:59 +0000400template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000401void
Howard Hinnantf8485322011-01-12 22:56:59 +0000402__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403{
404 while (true)
405 {
406 switch (__i)
407 {
408 case 0:
409 {
410 unique_lock<_L0> __u0(__l0);
Howard Hinnantf8485322011-01-12 22:56:59 +0000411 __i = try_lock(__l1, __l2, __l3...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 if (__i == -1)
413 {
414 __u0.release();
415 return;
416 }
417 }
418 ++__i;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000419 __libcpp_thread_yield();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000420 break;
421 case 1:
422 {
423 unique_lock<_L1> __u1(__l1);
Howard Hinnantf8485322011-01-12 22:56:59 +0000424 __i = try_lock(__l2, __l3..., __l0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000425 if (__i == -1)
426 {
427 __u1.release();
428 return;
429 }
430 }
Howard Hinnantf8485322011-01-12 22:56:59 +0000431 if (__i == sizeof...(_L3) + 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000432 __i = 0;
433 else
434 __i += 2;
Asiri Rathnayakefa2e2032016-05-06 14:06:29 +0000435 __libcpp_thread_yield();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000436 break;
437 default:
Howard Hinnantf8485322011-01-12 22:56:59 +0000438 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000439 return;
440 }
441 }
442}
443
Howard Hinnantf8485322011-01-12 22:56:59 +0000444template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantf5f99992010-09-22 18:02:38 +0000445inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000446void
Howard Hinnantf8485322011-01-12 22:56:59 +0000447lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448{
Howard Hinnantf8485322011-01-12 22:56:59 +0000449 __lock_first(0, __l0, __l1, __l2, __l3...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000450}
451
Eric Fiselier68143d42016-06-14 03:48:09 +0000452template <class _L0>
453inline _LIBCPP_INLINE_VISIBILITY
454void __unlock(_L0& __l0) {
455 __l0.unlock();
456}
457
458template <class _L0, class _L1>
459inline _LIBCPP_INLINE_VISIBILITY
460void __unlock(_L0& __l0, _L1& __l1) {
461 __l0.unlock();
462 __l1.unlock();
463}
464
465template <class _L0, class _L1, class _L2, class ..._L3>
466inline _LIBCPP_INLINE_VISIBILITY
467void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
468 __l0.unlock();
469 __l1.unlock();
470 _VSTD::__unlock(__l2, __l3...);
471}
472
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400473#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000474
Marshall Clow2a7ae6f2017-03-24 05:19:15 +0000475#if _LIBCPP_STD_VER > 14
476template <class ..._Mutexes>
477class _LIBCPP_TEMPLATE_VIS scoped_lock;
478
479template <>
480class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
481public:
482 explicit scoped_lock() {}
483 ~scoped_lock() = default;
484
485 _LIBCPP_INLINE_VISIBILITY
486 explicit scoped_lock(adopt_lock_t) {}
487
488 scoped_lock(scoped_lock const&) = delete;
489 scoped_lock& operator=(scoped_lock const&) = delete;
490};
491
492template <class _Mutex>
Aaron Pucherte1560fb2018-10-09 23:42:29 +0000493class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
Marshall Clow2a7ae6f2017-03-24 05:19:15 +0000494public:
495 typedef _Mutex mutex_type;
496private:
497 mutex_type& __m_;
498public:
499 explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
500 : __m_(__m) {__m_.lock();}
501
502 ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
503
504 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf68c6922017-07-27 17:44:03 +0000505 explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
Marshall Clow2a7ae6f2017-03-24 05:19:15 +0000506 : __m_(__m) {}
Marshall Clow2a7ae6f2017-03-24 05:19:15 +0000507
508 scoped_lock(scoped_lock const&) = delete;
509 scoped_lock& operator=(scoped_lock const&) = delete;
510};
511
512template <class ..._MArgs>
513class _LIBCPP_TEMPLATE_VIS scoped_lock
514{
515 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
516 typedef tuple<_MArgs&...> _MutexTuple;
517
518public:
519 _LIBCPP_INLINE_VISIBILITY
520 explicit scoped_lock(_MArgs&... __margs)
521 : __t_(__margs...)
522 {
523 _VSTD::lock(__margs...);
524 }
525
526 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf68c6922017-07-27 17:44:03 +0000527 scoped_lock(adopt_lock_t, _MArgs&... __margs)
Marshall Clow2a7ae6f2017-03-24 05:19:15 +0000528 : __t_(__margs...)
529 {
530 }
531
532 _LIBCPP_INLINE_VISIBILITY
533 ~scoped_lock() {
534 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
535 __unlock_unpack(_Indices{}, __t_);
536 }
537
538 scoped_lock(scoped_lock const&) = delete;
539 scoped_lock& operator=(scoped_lock const&) = delete;
540
541private:
542 template <size_t ..._Indx>
543 _LIBCPP_INLINE_VISIBILITY
544 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
545 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
546 }
547
548 _MutexTuple __t_;
549};
550
551#endif // _LIBCPP_STD_VER > 14
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000552#endif // !_LIBCPP_HAS_NO_THREADS
553
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000554struct _LIBCPP_TEMPLATE_VIS once_flag;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555
Eric Fiselierb6159752017-04-18 23:05:08 +0000556#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000557
558template<class _Callable, class... _Args>
Howard Hinnanta54386e2012-09-14 00:39:16 +0000559_LIBCPP_INLINE_VISIBILITY
560void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000561
Eric Fiselierb6159752017-04-18 23:05:08 +0000562#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000563
564template<class _Callable>
Howard Hinnanta54386e2012-09-14 00:39:16 +0000565_LIBCPP_INLINE_VISIBILITY
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000566void call_once(once_flag&, _Callable&);
567
568template<class _Callable>
569_LIBCPP_INLINE_VISIBILITY
570void call_once(once_flag&, const _Callable&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000571
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400572#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000573
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000574struct _LIBCPP_TEMPLATE_VIS once_flag
Howard Hinnantc51e1022010-05-11 19:42:16 +0000575{
Howard Hinnantf5f99992010-09-22 18:02:38 +0000576 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000577 _LIBCPP_CONSTEXPR
578 once_flag() _NOEXCEPT : __state_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000579
Nico Weber03776e72019-03-20 22:55:03 +0000580#if defined(_LIBCPP_ABI_MICROSOFT)
581 typedef uintptr_t _State_type;
582#else
583 typedef unsigned long _State_type;
584#endif
585
586
Howard Hinnantc51e1022010-05-11 19:42:16 +0000587private:
588 once_flag(const once_flag&); // = delete;
589 once_flag& operator=(const once_flag&); // = delete;
590
Nico Weber03776e72019-03-20 22:55:03 +0000591 _State_type __state_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000592
Eric Fiselierb6159752017-04-18 23:05:08 +0000593#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000594 template<class _Callable, class... _Args>
595 friend
596 void call_once(once_flag&, _Callable&&, _Args&&...);
Eric Fiselierb6159752017-04-18 23:05:08 +0000597#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598 template<class _Callable>
599 friend
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000600 void call_once(once_flag&, _Callable&);
601
602 template<class _Callable>
603 friend
604 void call_once(once_flag&, const _Callable&);
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400605#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000606};
607
Eric Fiselierb6159752017-04-18 23:05:08 +0000608#ifndef _LIBCPP_CXX03_LANG
Howard Hinnant51d62e02011-05-16 19:05:11 +0000609
Howard Hinnantc834c512011-11-29 18:15:50 +0000610template <class _Fp>
Howard Hinnant51d62e02011-05-16 19:05:11 +0000611class __call_once_param
612{
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000613 _Fp& __f_;
Howard Hinnant51d62e02011-05-16 19:05:11 +0000614public:
Howard Hinnant51d62e02011-05-16 19:05:11 +0000615 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000616 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnant51d62e02011-05-16 19:05:11 +0000617
618 _LIBCPP_INLINE_VISIBILITY
619 void operator()()
620 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000621 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnant51d62e02011-05-16 19:05:11 +0000622 __execute(_Index());
623 }
624
625private:
626 template <size_t ..._Indices>
627 _LIBCPP_INLINE_VISIBILITY
628 void __execute(__tuple_indices<_Indices...>)
629 {
Arthur O'Dwyer34465da2020-12-15 19:32:29 -0500630 _VSTD::__invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
Howard Hinnant51d62e02011-05-16 19:05:11 +0000631 }
632};
633
634#else
635
Howard Hinnantc834c512011-11-29 18:15:50 +0000636template <class _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000637class __call_once_param
638{
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000639 _Fp& __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000640public:
Howard Hinnantf5f99992010-09-22 18:02:38 +0000641 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000642 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000643
Howard Hinnantf5f99992010-09-22 18:02:38 +0000644 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645 void operator()()
646 {
647 __f_();
648 }
649};
650
Howard Hinnant51d62e02011-05-16 19:05:11 +0000651#endif
652
Howard Hinnantc834c512011-11-29 18:15:50 +0000653template <class _Fp>
Louis Dionne530e2402019-11-11 10:21:57 -0500654void _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000655__call_once_proxy(void* __vp)
656{
Howard Hinnantc834c512011-11-29 18:15:50 +0000657 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000658 (*__p)();
659}
660
Nico Weber03776e72019-03-20 22:55:03 +0000661_LIBCPP_FUNC_VIS void __call_once(volatile once_flag::_State_type&, void*,
662 void (*)(void*));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000663
Eric Fiselierb6159752017-04-18 23:05:08 +0000664#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000665
666template<class _Callable, class... _Args>
667inline _LIBCPP_INLINE_VISIBILITY
668void
669call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
670{
Nico Weber03776e72019-03-20 22:55:03 +0000671 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000672 {
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000673 typedef tuple<_Callable&&, _Args&&...> _Gp;
674 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
675 __call_once_param<_Gp> __p(__f);
Howard Hinnantc834c512011-11-29 18:15:50 +0000676 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000677 }
678}
679
Eric Fiselierb6159752017-04-18 23:05:08 +0000680#else // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000681
682template<class _Callable>
683inline _LIBCPP_INLINE_VISIBILITY
684void
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000685call_once(once_flag& __flag, _Callable& __func)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686{
Nico Weber03776e72019-03-20 22:55:03 +0000687 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 {
689 __call_once_param<_Callable> __p(__func);
690 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
691 }
692}
693
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000694template<class _Callable>
695inline _LIBCPP_INLINE_VISIBILITY
696void
697call_once(once_flag& __flag, const _Callable& __func)
698{
Nico Weber03776e72019-03-20 22:55:03 +0000699 if (__libcpp_acquire_load(&__flag.__state_) != ~once_flag::_State_type(0))
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000700 {
701 __call_once_param<const _Callable> __p(__func);
702 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
703 }
704}
705
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400706#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000707
Howard Hinnantc51e1022010-05-11 19:42:16 +0000708_LIBCPP_END_NAMESPACE_STD
709
Eric Fiselierf4433a32017-05-31 22:07:49 +0000710_LIBCPP_POP_MACROS
711
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400712#endif // _LIBCPP_MUTEX