blob: 8526533f14021ff055fc1e1fa641f1731f2771d7 [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
94constexpr defer_lock_t defer_lock{};
95constexpr try_to_lock_t try_to_lock{};
96constexpr adopt_lock_t adopt_lock{};
97
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
Eric Fiselier10b52a02016-06-14 03:48:09 +0000112template <class... MutexTypes> // Variadic lock_guard only provided in ABI V2.
113class lock_guard
114{
115public:
116 explicit lock_guard(MutexTypes&... m);
117 lock_guard(MutexTypes&... m, adopt_lock_t);
118 ~lock_guard();
119 lock_guard(lock_guard const&) = delete;
120 lock_guard& operator=(lock_guard const&) = delete;
121private:
122 tuple<MutexTypes&...> pm; // exposition only
123};
124
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000125template <class Mutex>
126class unique_lock
127{
128public:
129 typedef Mutex mutex_type;
Howard Hinnant499c61f2012-07-21 16:13:09 +0000130 unique_lock() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000131 explicit unique_lock(mutex_type& m);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000132 unique_lock(mutex_type& m, defer_lock_t) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000133 unique_lock(mutex_type& m, try_to_lock_t);
134 unique_lock(mutex_type& m, adopt_lock_t);
135 template <class Clock, class Duration>
136 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
137 template <class Rep, class Period>
138 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
139 ~unique_lock();
140
141 unique_lock(unique_lock const&) = delete;
142 unique_lock& operator=(unique_lock const&) = delete;
143
Howard Hinnant499c61f2012-07-21 16:13:09 +0000144 unique_lock(unique_lock&& u) noexcept;
145 unique_lock& operator=(unique_lock&& u) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000146
147 void lock();
148 bool try_lock();
149
150 template <class Rep, class Period>
151 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
152 template <class Clock, class Duration>
153 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
154
155 void unlock();
156
Howard Hinnant499c61f2012-07-21 16:13:09 +0000157 void swap(unique_lock& u) noexcept;
158 mutex_type* release() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000159
Howard Hinnant499c61f2012-07-21 16:13:09 +0000160 bool owns_lock() const noexcept;
161 explicit operator bool () const noexcept;
162 mutex_type* mutex() const noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000163};
164
165template <class Mutex>
Howard Hinnant499c61f2012-07-21 16:13:09 +0000166 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000167
168template <class L1, class L2, class... L3>
169 int try_lock(L1&, L2&, L3&...);
170template <class L1, class L2, class... L3>
171 void lock(L1&, L2&, L3&...);
172
173struct once_flag
174{
Howard Hinnant499c61f2012-07-21 16:13:09 +0000175 constexpr once_flag() noexcept;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000176
177 once_flag(const once_flag&) = delete;
178 once_flag& operator=(const once_flag&) = delete;
179};
180
181template<class Callable, class ...Args>
182 void call_once(once_flag& flag, Callable&& func, Args&&... args);
183
184} // std
185
186*/
187
188#include <__config>
189#include <__mutex_base>
190#include <functional>
Eric Fiselierc6e46692015-07-07 00:27:16 +0000191#include <memory>
Howard Hinnantad935d52011-05-16 19:05:11 +0000192#ifndef _LIBCPP_HAS_NO_VARIADICS
193#include <tuple>
194#endif
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000195#include <__threading_support>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000196
Howard Hinnant66c6f972011-11-29 16:45:27 +0000197#include <__undef_min_max>
198
Howard Hinnant08e17472011-10-17 20:05:10 +0000199#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000200#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000201#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202
203_LIBCPP_BEGIN_NAMESPACE_STD
204
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000205#ifndef _LIBCPP_HAS_NO_THREADS
206
Howard Hinnant83eade62013-03-06 23:30:19 +0000207class _LIBCPP_TYPE_VIS recursive_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000208{
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000209 __libcpp_recursive_mutex_t __m_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210
211public:
212 recursive_mutex();
213 ~recursive_mutex();
214
215private:
216 recursive_mutex(const recursive_mutex&); // = delete;
217 recursive_mutex& operator=(const recursive_mutex&); // = delete;
218
219public:
220 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000221 bool try_lock() _NOEXCEPT;
222 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000223
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000224 typedef __libcpp_recursive_mutex_t* native_handle_type;
225
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000226 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000227 native_handle_type native_handle() {return &__m_;}
228};
229
Howard Hinnant83eade62013-03-06 23:30:19 +0000230class _LIBCPP_TYPE_VIS timed_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000231{
232 mutex __m_;
233 condition_variable __cv_;
234 bool __locked_;
235public:
236 timed_mutex();
237 ~timed_mutex();
238
239private:
240 timed_mutex(const timed_mutex&); // = delete;
241 timed_mutex& operator=(const timed_mutex&); // = delete;
242
243public:
244 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000245 bool try_lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000246 template <class _Rep, class _Period>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000247 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantf8f85212010-11-20 19:16:30 +0000249 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250 template <class _Clock, class _Duration>
251 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000252 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000253};
254
255template <class _Clock, class _Duration>
256bool
257timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
258{
259 using namespace chrono;
260 unique_lock<mutex> __lk(__m_);
261 bool no_timeout = _Clock::now() < __t;
262 while (no_timeout && __locked_)
263 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
264 if (!__locked_)
265 {
266 __locked_ = true;
267 return true;
268 }
269 return false;
270}
271
Howard Hinnant83eade62013-03-06 23:30:19 +0000272class _LIBCPP_TYPE_VIS recursive_timed_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000273{
274 mutex __m_;
275 condition_variable __cv_;
276 size_t __count_;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000277 __libcpp_thread_id __id_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000278public:
279 recursive_timed_mutex();
280 ~recursive_timed_mutex();
281
282private:
283 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
284 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
285
286public:
287 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000288 bool try_lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000289 template <class _Rep, class _Period>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000290 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000291 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantf8f85212010-11-20 19:16:30 +0000292 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000293 template <class _Clock, class _Duration>
294 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000295 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296};
297
298template <class _Clock, class _Duration>
299bool
300recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
301{
302 using namespace chrono;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000303 __libcpp_thread_id __id = __libcpp_thread_get_current_id();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000304 unique_lock<mutex> lk(__m_);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000305 if (__libcpp_thread_id_equal(__id, __id_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 {
307 if (__count_ == numeric_limits<size_t>::max())
308 return false;
309 ++__count_;
310 return true;
311 }
312 bool no_timeout = _Clock::now() < __t;
313 while (no_timeout && __count_ != 0)
314 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
315 if (__count_ == 0)
316 {
317 __count_ = 1;
318 __id_ = __id;
319 return true;
320 }
321 return false;
322}
323
324template <class _L0, class _L1>
325int
326try_lock(_L0& __l0, _L1& __l1)
327{
328 unique_lock<_L0> __u0(__l0, try_to_lock);
329 if (__u0.owns_lock())
330 {
331 if (__l1.try_lock())
332 {
333 __u0.release();
334 return -1;
335 }
336 else
337 return 1;
338 }
339 return 0;
340}
341
342#ifndef _LIBCPP_HAS_NO_VARIADICS
343
344template <class _L0, class _L1, class _L2, class... _L3>
345int
346try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
347{
348 int __r = 0;
349 unique_lock<_L0> __u0(__l0, try_to_lock);
350 if (__u0.owns_lock())
351 {
352 __r = try_lock(__l1, __l2, __l3...);
353 if (__r == -1)
354 __u0.release();
355 else
356 ++__r;
357 }
358 return __r;
359}
360
Howard Hinnant324bb032010-08-22 00:02:43 +0000361#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000362
363template <class _L0, class _L1>
364void
365lock(_L0& __l0, _L1& __l1)
366{
367 while (true)
368 {
369 {
370 unique_lock<_L0> __u0(__l0);
371 if (__l1.try_lock())
372 {
373 __u0.release();
374 break;
375 }
376 }
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000377 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000378 {
379 unique_lock<_L1> __u1(__l1);
380 if (__l0.try_lock())
381 {
382 __u1.release();
383 break;
384 }
385 }
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000386 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000387 }
388}
389
390#ifndef _LIBCPP_HAS_NO_VARIADICS
391
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000392template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000393void
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000394__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395{
396 while (true)
397 {
398 switch (__i)
399 {
400 case 0:
401 {
402 unique_lock<_L0> __u0(__l0);
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000403 __i = try_lock(__l1, __l2, __l3...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000404 if (__i == -1)
405 {
406 __u0.release();
407 return;
408 }
409 }
410 ++__i;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000411 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000412 break;
413 case 1:
414 {
415 unique_lock<_L1> __u1(__l1);
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000416 __i = try_lock(__l2, __l3..., __l0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000417 if (__i == -1)
418 {
419 __u1.release();
420 return;
421 }
422 }
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000423 if (__i == sizeof...(_L3) + 1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000424 __i = 0;
425 else
426 __i += 2;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000427 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428 break;
429 default:
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000430 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000431 return;
432 }
433 }
434}
435
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000436template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000437inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000438void
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000439lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440{
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000441 __lock_first(0, __l0, __l1, __l2, __l3...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442}
443
Eric Fiselier10b52a02016-06-14 03:48:09 +0000444template <class _L0>
445inline _LIBCPP_INLINE_VISIBILITY
446void __unlock(_L0& __l0) {
447 __l0.unlock();
448}
449
450template <class _L0, class _L1>
451inline _LIBCPP_INLINE_VISIBILITY
452void __unlock(_L0& __l0, _L1& __l1) {
453 __l0.unlock();
454 __l1.unlock();
455}
456
457template <class _L0, class _L1, class _L2, class ..._L3>
458inline _LIBCPP_INLINE_VISIBILITY
459void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
460 __l0.unlock();
461 __l1.unlock();
462 _VSTD::__unlock(__l2, __l3...);
463}
464
Howard Hinnant324bb032010-08-22 00:02:43 +0000465#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000466
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000467#endif // !_LIBCPP_HAS_NO_THREADS
468
Eric Fiselierc3589a82017-01-04 23:56:00 +0000469struct _LIBCPP_TEMPLATE_VIS once_flag;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470
471#ifndef _LIBCPP_HAS_NO_VARIADICS
472
473template<class _Callable, class... _Args>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000474_LIBCPP_INLINE_VISIBILITY
475void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000476
Howard Hinnant324bb032010-08-22 00:02:43 +0000477#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478
479template<class _Callable>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000480_LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000481void call_once(once_flag&, _Callable&);
482
483template<class _Callable>
484_LIBCPP_INLINE_VISIBILITY
485void call_once(once_flag&, const _Callable&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000486
Howard Hinnant324bb032010-08-22 00:02:43 +0000487#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488
Eric Fiselierc3589a82017-01-04 23:56:00 +0000489struct _LIBCPP_TEMPLATE_VIS once_flag
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490{
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000491 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09 +0000492 _LIBCPP_CONSTEXPR
493 once_flag() _NOEXCEPT : __state_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494
495private:
496 once_flag(const once_flag&); // = delete;
497 once_flag& operator=(const once_flag&); // = delete;
498
499 unsigned long __state_;
500
501#ifndef _LIBCPP_HAS_NO_VARIADICS
502 template<class _Callable, class... _Args>
503 friend
504 void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnant324bb032010-08-22 00:02:43 +0000505#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000506 template<class _Callable>
507 friend
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000508 void call_once(once_flag&, _Callable&);
509
510 template<class _Callable>
511 friend
512 void call_once(once_flag&, const _Callable&);
Howard Hinnant324bb032010-08-22 00:02:43 +0000513#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000514};
515
Howard Hinnantad935d52011-05-16 19:05:11 +0000516#ifndef _LIBCPP_HAS_NO_VARIADICS
517
Howard Hinnant99968442011-11-29 18:15:50 +0000518template <class _Fp>
Howard Hinnantad935d52011-05-16 19:05:11 +0000519class __call_once_param
520{
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000521 _Fp& __f_;
Howard Hinnantad935d52011-05-16 19:05:11 +0000522public:
Howard Hinnantad935d52011-05-16 19:05:11 +0000523 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000524 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantad935d52011-05-16 19:05:11 +0000525
526 _LIBCPP_INLINE_VISIBILITY
527 void operator()()
528 {
Howard Hinnant99968442011-11-29 18:15:50 +0000529 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnantad935d52011-05-16 19:05:11 +0000530 __execute(_Index());
531 }
532
533private:
534 template <size_t ..._Indices>
535 _LIBCPP_INLINE_VISIBILITY
536 void __execute(__tuple_indices<_Indices...>)
537 {
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000538 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
Howard Hinnantad935d52011-05-16 19:05:11 +0000539 }
540};
541
542#else
543
Howard Hinnant99968442011-11-29 18:15:50 +0000544template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000545class __call_once_param
546{
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000547 _Fp& __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000548public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000549 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000550 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000551
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000552 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553 void operator()()
554 {
555 __f_();
556 }
557};
558
Howard Hinnantad935d52011-05-16 19:05:11 +0000559#endif
560
Howard Hinnant99968442011-11-29 18:15:50 +0000561template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000562void
563__call_once_proxy(void* __vp)
564{
Howard Hinnant99968442011-11-29 18:15:50 +0000565 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566 (*__p)();
567}
568
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000569_LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570
571#ifndef _LIBCPP_HAS_NO_VARIADICS
572
573template<class _Callable, class... _Args>
574inline _LIBCPP_INLINE_VISIBILITY
575void
576call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
577{
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000578 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000579 {
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000580 typedef tuple<_Callable&&, _Args&&...> _Gp;
581 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
582 __call_once_param<_Gp> __p(__f);
Howard Hinnant99968442011-11-29 18:15:50 +0000583 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000584 }
585}
586
Howard Hinnant324bb032010-08-22 00:02:43 +0000587#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588
589template<class _Callable>
590inline _LIBCPP_INLINE_VISIBILITY
591void
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000592call_once(once_flag& __flag, _Callable& __func)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000593{
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000594 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595 {
596 __call_once_param<_Callable> __p(__func);
597 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
598 }
599}
600
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000601template<class _Callable>
602inline _LIBCPP_INLINE_VISIBILITY
603void
604call_once(once_flag& __flag, const _Callable& __func)
605{
606 if (__flag.__state_ != ~0ul)
607 {
608 __call_once_param<const _Callable> __p(__func);
609 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
610 }
611}
612
Howard Hinnant324bb032010-08-22 00:02:43 +0000613#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000614
Eric Fiselier10b52a02016-06-14 03:48:09 +0000615
616#if defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD) \
617 && !defined(_LIBCPP_CXX03_LANG)
618template <>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000619class _LIBCPP_TEMPLATE_VIS lock_guard<> {
Eric Fiselier10b52a02016-06-14 03:48:09 +0000620public:
Eric Fiselier12150a72016-06-15 17:04:40 +0000621 explicit lock_guard() {}
Eric Fiselier10b52a02016-06-14 03:48:09 +0000622 ~lock_guard() = default;
623
624 _LIBCPP_INLINE_VISIBILITY
625 explicit lock_guard(adopt_lock_t) {}
626
627 lock_guard(lock_guard const&) = delete;
628 lock_guard& operator=(lock_guard const&) = delete;
629};
630
631template <class ..._MArgs>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000632class _LIBCPP_TEMPLATE_VIS lock_guard
Eric Fiselier10b52a02016-06-14 03:48:09 +0000633{
634 static_assert(sizeof...(_MArgs) >= 2, "At least 2 lock types required");
635 typedef tuple<_MArgs&...> _MutexTuple;
636
637public:
638 _LIBCPP_INLINE_VISIBILITY
639 explicit lock_guard(_MArgs&... __margs)
640 : __t_(__margs...)
641 {
642 _VSTD::lock(__margs...);
643 }
644
645 _LIBCPP_INLINE_VISIBILITY
646 lock_guard(_MArgs&... __margs, adopt_lock_t)
647 : __t_(__margs...)
648 {
649 }
650
651 _LIBCPP_INLINE_VISIBILITY
652 ~lock_guard() {
653 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
654 __unlock_unpack(_Indices{}, __t_);
655 }
656
657 lock_guard(lock_guard const&) = delete;
658 lock_guard& operator=(lock_guard const&) = delete;
659
660private:
661 template <size_t ..._Indx>
662 _LIBCPP_INLINE_VISIBILITY
663 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
664 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
665 }
666
667 _MutexTuple __t_;
668};
669
670#endif // _LIBCPP_ABI_VARIADIC_LOCK_GUARD
671
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000672_LIBCPP_END_NAMESPACE_STD
673
674#endif // _LIBCPP_MUTEX