blob: 6d2de2b460e2de9d7a5bcf62166d4ec85ae55c24 [file] [log] [blame]
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- mutex ------------------------------------===//
3//
Howard Hinnantf5256e12010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00005//
Howard Hinnantb64f8b02010-11-16 22:09:02 +00006// This file is dual licensed under the MIT and the University of Illinois Open
7// Source Licenses. See LICENSE.TXT for details.
Howard Hinnantbc8d3f92010-05-11 19:42:16 +00008//
9//===----------------------------------------------------------------------===//
10
11#ifndef _LIBCPP_MUTEX
12#define _LIBCPP_MUTEX
13
14/*
15 mutex synopsis
16
17namespace std
18{
19
20class mutex
21{
22public:
Howard Hinnant499c61f2012-07-21 16:13:09 +000023 constexpr mutex() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000024 ~mutex();
25
26 mutex(const mutex&) = delete;
27 mutex& operator=(const mutex&) = delete;
28
29 void lock();
30 bool try_lock();
31 void unlock();
32
33 typedef pthread_mutex_t* native_handle_type;
34 native_handle_type native_handle();
35};
36
37class recursive_mutex
38{
39public:
40 recursive_mutex();
41 ~recursive_mutex();
42
43 recursive_mutex(const recursive_mutex&) = delete;
44 recursive_mutex& operator=(const recursive_mutex&) = delete;
45
46 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +000047 bool try_lock() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000048 void unlock();
49
50 typedef pthread_mutex_t* native_handle_type;
51 native_handle_type native_handle();
52};
53
54class timed_mutex
55{
56public:
57 timed_mutex();
58 ~timed_mutex();
59
60 timed_mutex(const timed_mutex&) = delete;
61 timed_mutex& operator=(const timed_mutex&) = delete;
62
63 void lock();
64 bool try_lock();
65 template <class Rep, class Period>
66 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
67 template <class Clock, class Duration>
68 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
69 void unlock();
70};
71
72class recursive_timed_mutex
73{
74public:
75 recursive_timed_mutex();
76 ~recursive_timed_mutex();
77
78 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
79 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
80
81 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +000082 bool try_lock() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000083 template <class Rep, class Period>
84 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
85 template <class Clock, class Duration>
86 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
87 void unlock();
88};
89
90struct defer_lock_t {};
91struct try_to_lock_t {};
92struct adopt_lock_t {};
93
Marshall Clowc58e4722018-01-02 17:17:01 +000094inline constexpr defer_lock_t defer_lock{};
95inline constexpr try_to_lock_t try_to_lock{};
96inline constexpr adopt_lock_t adopt_lock{};
Howard Hinnantbc8d3f92010-05-11 19:42:16 +000097
98template <class Mutex>
99class lock_guard
100{
101public:
102 typedef Mutex mutex_type;
103
104 explicit lock_guard(mutex_type& m);
105 lock_guard(mutex_type& m, adopt_lock_t);
106 ~lock_guard();
107
108 lock_guard(lock_guard const&) = delete;
109 lock_guard& operator=(lock_guard const&) = delete;
110};
111
Marshall Clow59bcc872017-03-24 03:40:36 +0000112template <class... MutexTypes>
113class scoped_lock // C++17
Eric Fiselier10b52a02016-06-14 03:48:09 +0000114{
115public:
Marshall Clow59bcc872017-03-24 03:40:36 +0000116 using mutex_type = Mutex; // If MutexTypes... consists of the single type Mutex
117
118 explicit scoped_lock(MutexTypes&... m);
Marshall Clowf226a282017-07-27 17:44:03 +0000119 scoped_lock(adopt_lock_t, MutexTypes&... m);
Marshall Clow59bcc872017-03-24 03:40:36 +0000120 ~scoped_lock();
121 scoped_lock(scoped_lock const&) = delete;
122 scoped_lock& operator=(scoped_lock const&) = delete;
Eric Fiselier10b52a02016-06-14 03:48:09 +0000123private:
124 tuple<MutexTypes&...> pm; // exposition only
125};
126
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000127template <class Mutex>
128class unique_lock
129{
130public:
131 typedef Mutex mutex_type;
Howard Hinnant499c61f2012-07-21 16:13:09 +0000132 unique_lock() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133 explicit unique_lock(mutex_type& m);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000134 unique_lock(mutex_type& m, defer_lock_t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000135 unique_lock(mutex_type& m, try_to_lock_t);
136 unique_lock(mutex_type& m, adopt_lock_t);
137 template <class Clock, class Duration>
138 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
139 template <class Rep, class Period>
140 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
141 ~unique_lock();
142
143 unique_lock(unique_lock const&) = delete;
144 unique_lock& operator=(unique_lock const&) = delete;
145
Howard Hinnant499c61f2012-07-21 16:13:09 +0000146 unique_lock(unique_lock&& u) noexcept;
147 unique_lock& operator=(unique_lock&& u) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000148
149 void lock();
150 bool try_lock();
151
152 template <class Rep, class Period>
153 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
154 template <class Clock, class Duration>
155 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
156
157 void unlock();
158
Howard Hinnant499c61f2012-07-21 16:13:09 +0000159 void swap(unique_lock& u) noexcept;
160 mutex_type* release() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000161
Howard Hinnant499c61f2012-07-21 16:13:09 +0000162 bool owns_lock() const noexcept;
163 explicit operator bool () const noexcept;
164 mutex_type* mutex() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000165};
166
167template <class Mutex>
Howard Hinnant499c61f2012-07-21 16:13:09 +0000168 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000169
170template <class L1, class L2, class... L3>
171 int try_lock(L1&, L2&, L3&...);
172template <class L1, class L2, class... L3>
173 void lock(L1&, L2&, L3&...);
174
175struct once_flag
176{
Howard Hinnant499c61f2012-07-21 16:13:09 +0000177 constexpr once_flag() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000178
179 once_flag(const once_flag&) = delete;
180 once_flag& operator=(const once_flag&) = delete;
181};
182
183template<class Callable, class ...Args>
184 void call_once(once_flag& flag, Callable&& func, Args&&... args);
185
186} // std
187
188*/
189
190#include <__config>
191#include <__mutex_base>
192#include <functional>
Eric Fiselierc6e46692015-07-07 00:27:16 +0000193#include <memory>
Eric Fiselier690d9992017-04-18 23:05:08 +0000194#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantad935d52011-05-16 19:05:11 +0000195#include <tuple>
196#endif
Marshall Clowe3973fd2018-09-12 19:41:40 +0000197#include <version>
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000198#include <__threading_support>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000199
Howard Hinnant08e17472011-10-17 20:05:10 +0000200#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000201#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000202#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000203
Eric Fiselier018a3d52017-05-31 22:07:49 +0000204_LIBCPP_PUSH_MACROS
205#include <__undef_macros>
206
207
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208_LIBCPP_BEGIN_NAMESPACE_STD
209
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000210#ifndef _LIBCPP_HAS_NO_THREADS
211
Howard Hinnant83eade62013-03-06 23:30:19 +0000212class _LIBCPP_TYPE_VIS recursive_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000213{
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000214 __libcpp_recursive_mutex_t __m_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000215
216public:
217 recursive_mutex();
218 ~recursive_mutex();
219
220private:
221 recursive_mutex(const recursive_mutex&); // = delete;
222 recursive_mutex& operator=(const recursive_mutex&); // = delete;
223
224public:
225 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000226 bool try_lock() _NOEXCEPT;
227 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000228
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000229 typedef __libcpp_recursive_mutex_t* native_handle_type;
230
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000231 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000232 native_handle_type native_handle() {return &__m_;}
233};
234
Howard Hinnant83eade62013-03-06 23:30:19 +0000235class _LIBCPP_TYPE_VIS timed_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000236{
237 mutex __m_;
238 condition_variable __cv_;
239 bool __locked_;
240public:
241 timed_mutex();
242 ~timed_mutex();
243
244private:
245 timed_mutex(const timed_mutex&); // = delete;
246 timed_mutex& operator=(const timed_mutex&); // = delete;
247
248public:
249 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000250 bool try_lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000251 template <class _Rep, class _Period>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000252 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantf8f85212010-11-20 19:16:30 +0000254 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000255 template <class _Clock, class _Duration>
Shoaib Meenai6b734922017-03-02 03:22:18 +0000256 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000257 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000258 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000259};
260
261template <class _Clock, class _Duration>
262bool
263timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
264{
265 using namespace chrono;
266 unique_lock<mutex> __lk(__m_);
267 bool no_timeout = _Clock::now() < __t;
268 while (no_timeout && __locked_)
269 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
270 if (!__locked_)
271 {
272 __locked_ = true;
273 return true;
274 }
275 return false;
276}
277
Howard Hinnant83eade62013-03-06 23:30:19 +0000278class _LIBCPP_TYPE_VIS recursive_timed_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279{
280 mutex __m_;
281 condition_variable __cv_;
282 size_t __count_;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000283 __libcpp_thread_id __id_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000284public:
285 recursive_timed_mutex();
286 ~recursive_timed_mutex();
287
288private:
289 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
290 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
291
292public:
293 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000294 bool try_lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000295 template <class _Rep, class _Period>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000296 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000297 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantf8f85212010-11-20 19:16:30 +0000298 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000299 template <class _Clock, class _Duration>
Shoaib Meenai6b734922017-03-02 03:22:18 +0000300 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000301 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000302 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000303};
304
305template <class _Clock, class _Duration>
306bool
307recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
308{
309 using namespace chrono;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000310 __libcpp_thread_id __id = __libcpp_thread_get_current_id();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000311 unique_lock<mutex> lk(__m_);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000312 if (__libcpp_thread_id_equal(__id, __id_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000313 {
314 if (__count_ == numeric_limits<size_t>::max())
315 return false;
316 ++__count_;
317 return true;
318 }
319 bool no_timeout = _Clock::now() < __t;
320 while (no_timeout && __count_ != 0)
321 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
322 if (__count_ == 0)
323 {
324 __count_ = 1;
325 __id_ = __id;
326 return true;
327 }
328 return false;
329}
330
331template <class _L0, class _L1>
332int
333try_lock(_L0& __l0, _L1& __l1)
334{
335 unique_lock<_L0> __u0(__l0, try_to_lock);
336 if (__u0.owns_lock())
337 {
338 if (__l1.try_lock())
339 {
340 __u0.release();
341 return -1;
342 }
343 else
344 return 1;
345 }
346 return 0;
347}
348
Eric Fiselier690d9992017-04-18 23:05:08 +0000349#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000350
351template <class _L0, class _L1, class _L2, class... _L3>
352int
353try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
354{
355 int __r = 0;
356 unique_lock<_L0> __u0(__l0, try_to_lock);
357 if (__u0.owns_lock())
358 {
359 __r = try_lock(__l1, __l2, __l3...);
360 if (__r == -1)
361 __u0.release();
362 else
363 ++__r;
364 }
365 return __r;
366}
367
Eric Fiselier690d9992017-04-18 23:05:08 +0000368#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000369
370template <class _L0, class _L1>
371void
372lock(_L0& __l0, _L1& __l1)
373{
374 while (true)
375 {
376 {
377 unique_lock<_L0> __u0(__l0);
378 if (__l1.try_lock())
379 {
380 __u0.release();
381 break;
382 }
383 }
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000384 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000385 {
386 unique_lock<_L1> __u1(__l1);
387 if (__l0.try_lock())
388 {
389 __u1.release();
390 break;
391 }
392 }
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000393 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000394 }
395}
396
Eric Fiselier690d9992017-04-18 23:05:08 +0000397#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000398
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000399template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000400void
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000401__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000402{
403 while (true)
404 {
405 switch (__i)
406 {
407 case 0:
408 {
409 unique_lock<_L0> __u0(__l0);
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000410 __i = try_lock(__l1, __l2, __l3...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000411 if (__i == -1)
412 {
413 __u0.release();
414 return;
415 }
416 }
417 ++__i;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000418 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419 break;
420 case 1:
421 {
422 unique_lock<_L1> __u1(__l1);
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000423 __i = try_lock(__l2, __l3..., __l0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 if (__i == -1)
425 {
426 __u1.release();
427 return;
428 }
429 }
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000430 if (__i == sizeof...(_L3) + 1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431 __i = 0;
432 else
433 __i += 2;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000434 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 break;
436 default:
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000437 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438 return;
439 }
440 }
441}
442
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000443template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000444inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000445void
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000446lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000447{
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000448 __lock_first(0, __l0, __l1, __l2, __l3...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000449}
450
Eric Fiselier10b52a02016-06-14 03:48:09 +0000451template <class _L0>
452inline _LIBCPP_INLINE_VISIBILITY
453void __unlock(_L0& __l0) {
454 __l0.unlock();
455}
456
457template <class _L0, class _L1>
458inline _LIBCPP_INLINE_VISIBILITY
459void __unlock(_L0& __l0, _L1& __l1) {
460 __l0.unlock();
461 __l1.unlock();
462}
463
464template <class _L0, class _L1, class _L2, class ..._L3>
465inline _LIBCPP_INLINE_VISIBILITY
466void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
467 __l0.unlock();
468 __l1.unlock();
469 _VSTD::__unlock(__l2, __l3...);
470}
471
Eric Fiselier690d9992017-04-18 23:05:08 +0000472#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000473
Marshall Clow0c70c792017-03-24 05:19:15 +0000474#if _LIBCPP_STD_VER > 14
475template <class ..._Mutexes>
476class _LIBCPP_TEMPLATE_VIS scoped_lock;
477
478template <>
479class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
480public:
481 explicit scoped_lock() {}
482 ~scoped_lock() = default;
483
484 _LIBCPP_INLINE_VISIBILITY
485 explicit scoped_lock(adopt_lock_t) {}
486
487 scoped_lock(scoped_lock const&) = delete;
488 scoped_lock& operator=(scoped_lock const&) = delete;
489};
490
491template <class _Mutex>
Aaron Puchertf7d5bd22018-10-09 23:42:29 +0000492class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
Marshall Clow0c70c792017-03-24 05:19:15 +0000493public:
494 typedef _Mutex mutex_type;
495private:
496 mutex_type& __m_;
497public:
498 explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
499 : __m_(__m) {__m_.lock();}
500
501 ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
502
503 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf226a282017-07-27 17:44:03 +0000504 explicit scoped_lock(adopt_lock_t, mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
Marshall Clow0c70c792017-03-24 05:19:15 +0000505 : __m_(__m) {}
Marshall Clow0c70c792017-03-24 05:19:15 +0000506
507 scoped_lock(scoped_lock const&) = delete;
508 scoped_lock& operator=(scoped_lock const&) = delete;
509};
510
511template <class ..._MArgs>
512class _LIBCPP_TEMPLATE_VIS scoped_lock
513{
514 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
515 typedef tuple<_MArgs&...> _MutexTuple;
516
517public:
518 _LIBCPP_INLINE_VISIBILITY
519 explicit scoped_lock(_MArgs&... __margs)
520 : __t_(__margs...)
521 {
522 _VSTD::lock(__margs...);
523 }
524
525 _LIBCPP_INLINE_VISIBILITY
Marshall Clowf226a282017-07-27 17:44:03 +0000526 scoped_lock(adopt_lock_t, _MArgs&... __margs)
Marshall Clow0c70c792017-03-24 05:19:15 +0000527 : __t_(__margs...)
528 {
529 }
530
531 _LIBCPP_INLINE_VISIBILITY
532 ~scoped_lock() {
533 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
534 __unlock_unpack(_Indices{}, __t_);
535 }
536
537 scoped_lock(scoped_lock const&) = delete;
538 scoped_lock& operator=(scoped_lock const&) = delete;
539
540private:
541 template <size_t ..._Indx>
542 _LIBCPP_INLINE_VISIBILITY
543 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
544 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
545 }
546
547 _MutexTuple __t_;
548};
549
550#endif // _LIBCPP_STD_VER > 14
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000551#endif // !_LIBCPP_HAS_NO_THREADS
552
Eric Fiselierc3589a82017-01-04 23:56:00 +0000553struct _LIBCPP_TEMPLATE_VIS once_flag;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000554
Eric Fiselier690d9992017-04-18 23:05:08 +0000555#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000556
557template<class _Callable, class... _Args>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000558_LIBCPP_INLINE_VISIBILITY
559void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000560
Eric Fiselier690d9992017-04-18 23:05:08 +0000561#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562
563template<class _Callable>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000564_LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000565void call_once(once_flag&, _Callable&);
566
567template<class _Callable>
568_LIBCPP_INLINE_VISIBILITY
569void call_once(once_flag&, const _Callable&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570
Eric Fiselier690d9992017-04-18 23:05:08 +0000571#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
Eric Fiselierc3589a82017-01-04 23:56:00 +0000573struct _LIBCPP_TEMPLATE_VIS once_flag
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574{
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000575 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09 +0000576 _LIBCPP_CONSTEXPR
577 once_flag() _NOEXCEPT : __state_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000578
579private:
580 once_flag(const once_flag&); // = delete;
581 once_flag& operator=(const once_flag&); // = delete;
582
583 unsigned long __state_;
584
Eric Fiselier690d9992017-04-18 23:05:08 +0000585#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586 template<class _Callable, class... _Args>
587 friend
588 void call_once(once_flag&, _Callable&&, _Args&&...);
Eric Fiselier690d9992017-04-18 23:05:08 +0000589#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000590 template<class _Callable>
591 friend
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000592 void call_once(once_flag&, _Callable&);
593
594 template<class _Callable>
595 friend
596 void call_once(once_flag&, const _Callable&);
Eric Fiselier690d9992017-04-18 23:05:08 +0000597#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000598};
599
Eric Fiselier690d9992017-04-18 23:05:08 +0000600#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantad935d52011-05-16 19:05:11 +0000601
Howard Hinnant99968442011-11-29 18:15:50 +0000602template <class _Fp>
Howard Hinnantad935d52011-05-16 19:05:11 +0000603class __call_once_param
604{
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000605 _Fp& __f_;
Howard Hinnantad935d52011-05-16 19:05:11 +0000606public:
Howard Hinnantad935d52011-05-16 19:05:11 +0000607 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000608 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantad935d52011-05-16 19:05:11 +0000609
610 _LIBCPP_INLINE_VISIBILITY
611 void operator()()
612 {
Howard Hinnant99968442011-11-29 18:15:50 +0000613 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnantad935d52011-05-16 19:05:11 +0000614 __execute(_Index());
615 }
616
617private:
618 template <size_t ..._Indices>
619 _LIBCPP_INLINE_VISIBILITY
620 void __execute(__tuple_indices<_Indices...>)
621 {
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000622 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
Howard Hinnantad935d52011-05-16 19:05:11 +0000623 }
624};
625
626#else
627
Howard Hinnant99968442011-11-29 18:15:50 +0000628template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000629class __call_once_param
630{
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000631 _Fp& __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000632public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000633 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000634 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000635
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000636 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000637 void operator()()
638 {
639 __f_();
640 }
641};
642
Howard Hinnantad935d52011-05-16 19:05:11 +0000643#endif
644
Howard Hinnant99968442011-11-29 18:15:50 +0000645template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000646void
647__call_once_proxy(void* __vp)
648{
Howard Hinnant99968442011-11-29 18:15:50 +0000649 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000650 (*__p)();
651}
652
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000653_LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000654
Eric Fiselier690d9992017-04-18 23:05:08 +0000655#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000656
657template<class _Callable, class... _Args>
658inline _LIBCPP_INLINE_VISIBILITY
659void
660call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
661{
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000662 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000663 {
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000664 typedef tuple<_Callable&&, _Args&&...> _Gp;
665 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
666 __call_once_param<_Gp> __p(__f);
Howard Hinnant99968442011-11-29 18:15:50 +0000667 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000668 }
669}
670
Eric Fiselier690d9992017-04-18 23:05:08 +0000671#else // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672
673template<class _Callable>
674inline _LIBCPP_INLINE_VISIBILITY
675void
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000676call_once(once_flag& __flag, _Callable& __func)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000677{
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000678 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000679 {
680 __call_once_param<_Callable> __p(__func);
681 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
682 }
683}
684
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000685template<class _Callable>
686inline _LIBCPP_INLINE_VISIBILITY
687void
688call_once(once_flag& __flag, const _Callable& __func)
689{
Justin Lebar57c40592017-04-23 16:58:48 +0000690 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000691 {
692 __call_once_param<const _Callable> __p(__func);
693 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
694 }
695}
696
Eric Fiselier690d9992017-04-18 23:05:08 +0000697#endif // _LIBCPP_CXX03_LANG
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000699_LIBCPP_END_NAMESPACE_STD
700
Eric Fiselier018a3d52017-05-31 22:07:49 +0000701_LIBCPP_POP_MACROS
702
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000703#endif // _LIBCPP_MUTEX