blob: 11b0f7e5292ade4ff2f218a34535fb1a44054d00 [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>
Shoaib Meenai6b734922017-03-02 03:22:18 +0000251 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000253 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254};
255
256template <class _Clock, class _Duration>
257bool
258timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
259{
260 using namespace chrono;
261 unique_lock<mutex> __lk(__m_);
262 bool no_timeout = _Clock::now() < __t;
263 while (no_timeout && __locked_)
264 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
265 if (!__locked_)
266 {
267 __locked_ = true;
268 return true;
269 }
270 return false;
271}
272
Howard Hinnant83eade62013-03-06 23:30:19 +0000273class _LIBCPP_TYPE_VIS recursive_timed_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000274{
275 mutex __m_;
276 condition_variable __cv_;
277 size_t __count_;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000278 __libcpp_thread_id __id_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000279public:
280 recursive_timed_mutex();
281 ~recursive_timed_mutex();
282
283private:
284 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
285 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
286
287public:
288 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000289 bool try_lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000290 template <class _Rep, class _Period>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000291 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantf8f85212010-11-20 19:16:30 +0000293 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294 template <class _Clock, class _Duration>
Shoaib Meenai6b734922017-03-02 03:22:18 +0000295 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000297 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298};
299
300template <class _Clock, class _Duration>
301bool
302recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
303{
304 using namespace chrono;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000305 __libcpp_thread_id __id = __libcpp_thread_get_current_id();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000306 unique_lock<mutex> lk(__m_);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000307 if (__libcpp_thread_id_equal(__id, __id_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308 {
309 if (__count_ == numeric_limits<size_t>::max())
310 return false;
311 ++__count_;
312 return true;
313 }
314 bool no_timeout = _Clock::now() < __t;
315 while (no_timeout && __count_ != 0)
316 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
317 if (__count_ == 0)
318 {
319 __count_ = 1;
320 __id_ = __id;
321 return true;
322 }
323 return false;
324}
325
326template <class _L0, class _L1>
327int
328try_lock(_L0& __l0, _L1& __l1)
329{
330 unique_lock<_L0> __u0(__l0, try_to_lock);
331 if (__u0.owns_lock())
332 {
333 if (__l1.try_lock())
334 {
335 __u0.release();
336 return -1;
337 }
338 else
339 return 1;
340 }
341 return 0;
342}
343
344#ifndef _LIBCPP_HAS_NO_VARIADICS
345
346template <class _L0, class _L1, class _L2, class... _L3>
347int
348try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
349{
350 int __r = 0;
351 unique_lock<_L0> __u0(__l0, try_to_lock);
352 if (__u0.owns_lock())
353 {
354 __r = try_lock(__l1, __l2, __l3...);
355 if (__r == -1)
356 __u0.release();
357 else
358 ++__r;
359 }
360 return __r;
361}
362
Howard Hinnant324bb032010-08-22 00:02:43 +0000363#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000364
365template <class _L0, class _L1>
366void
367lock(_L0& __l0, _L1& __l1)
368{
369 while (true)
370 {
371 {
372 unique_lock<_L0> __u0(__l0);
373 if (__l1.try_lock())
374 {
375 __u0.release();
376 break;
377 }
378 }
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000379 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000380 {
381 unique_lock<_L1> __u1(__l1);
382 if (__l0.try_lock())
383 {
384 __u1.release();
385 break;
386 }
387 }
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000388 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000389 }
390}
391
392#ifndef _LIBCPP_HAS_NO_VARIADICS
393
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000394template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000395void
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000396__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397{
398 while (true)
399 {
400 switch (__i)
401 {
402 case 0:
403 {
404 unique_lock<_L0> __u0(__l0);
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000405 __i = try_lock(__l1, __l2, __l3...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000406 if (__i == -1)
407 {
408 __u0.release();
409 return;
410 }
411 }
412 ++__i;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000413 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000414 break;
415 case 1:
416 {
417 unique_lock<_L1> __u1(__l1);
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000418 __i = try_lock(__l2, __l3..., __l0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000419 if (__i == -1)
420 {
421 __u1.release();
422 return;
423 }
424 }
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000425 if (__i == sizeof...(_L3) + 1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000426 __i = 0;
427 else
428 __i += 2;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000429 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000430 break;
431 default:
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000432 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000433 return;
434 }
435 }
436}
437
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000438template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000439inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000440void
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000441lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442{
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000443 __lock_first(0, __l0, __l1, __l2, __l3...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444}
445
Eric Fiselier10b52a02016-06-14 03:48:09 +0000446template <class _L0>
447inline _LIBCPP_INLINE_VISIBILITY
448void __unlock(_L0& __l0) {
449 __l0.unlock();
450}
451
452template <class _L0, class _L1>
453inline _LIBCPP_INLINE_VISIBILITY
454void __unlock(_L0& __l0, _L1& __l1) {
455 __l0.unlock();
456 __l1.unlock();
457}
458
459template <class _L0, class _L1, class _L2, class ..._L3>
460inline _LIBCPP_INLINE_VISIBILITY
461void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
462 __l0.unlock();
463 __l1.unlock();
464 _VSTD::__unlock(__l2, __l3...);
465}
466
Howard Hinnant324bb032010-08-22 00:02:43 +0000467#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000468
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000469#endif // !_LIBCPP_HAS_NO_THREADS
470
Eric Fiselierc3589a82017-01-04 23:56:00 +0000471struct _LIBCPP_TEMPLATE_VIS once_flag;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000472
473#ifndef _LIBCPP_HAS_NO_VARIADICS
474
475template<class _Callable, class... _Args>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000476_LIBCPP_INLINE_VISIBILITY
477void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000478
Howard Hinnant324bb032010-08-22 00:02:43 +0000479#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000480
481template<class _Callable>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000482_LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000483void call_once(once_flag&, _Callable&);
484
485template<class _Callable>
486_LIBCPP_INLINE_VISIBILITY
487void call_once(once_flag&, const _Callable&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000488
Howard Hinnant324bb032010-08-22 00:02:43 +0000489#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490
Eric Fiselierc3589a82017-01-04 23:56:00 +0000491struct _LIBCPP_TEMPLATE_VIS once_flag
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492{
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000493 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09 +0000494 _LIBCPP_CONSTEXPR
495 once_flag() _NOEXCEPT : __state_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000496
497private:
498 once_flag(const once_flag&); // = delete;
499 once_flag& operator=(const once_flag&); // = delete;
500
501 unsigned long __state_;
502
503#ifndef _LIBCPP_HAS_NO_VARIADICS
504 template<class _Callable, class... _Args>
505 friend
506 void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnant324bb032010-08-22 00:02:43 +0000507#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000508 template<class _Callable>
509 friend
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000510 void call_once(once_flag&, _Callable&);
511
512 template<class _Callable>
513 friend
514 void call_once(once_flag&, const _Callable&);
Howard Hinnant324bb032010-08-22 00:02:43 +0000515#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000516};
517
Howard Hinnantad935d52011-05-16 19:05:11 +0000518#ifndef _LIBCPP_HAS_NO_VARIADICS
519
Howard Hinnant99968442011-11-29 18:15:50 +0000520template <class _Fp>
Howard Hinnantad935d52011-05-16 19:05:11 +0000521class __call_once_param
522{
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000523 _Fp& __f_;
Howard Hinnantad935d52011-05-16 19:05:11 +0000524public:
Howard Hinnantad935d52011-05-16 19:05:11 +0000525 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000526 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantad935d52011-05-16 19:05:11 +0000527
528 _LIBCPP_INLINE_VISIBILITY
529 void operator()()
530 {
Howard Hinnant99968442011-11-29 18:15:50 +0000531 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnantad935d52011-05-16 19:05:11 +0000532 __execute(_Index());
533 }
534
535private:
536 template <size_t ..._Indices>
537 _LIBCPP_INLINE_VISIBILITY
538 void __execute(__tuple_indices<_Indices...>)
539 {
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000540 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
Howard Hinnantad935d52011-05-16 19:05:11 +0000541 }
542};
543
544#else
545
Howard Hinnant99968442011-11-29 18:15:50 +0000546template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000547class __call_once_param
548{
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000549 _Fp& __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000550public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000551 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000552 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000553
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000554 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555 void operator()()
556 {
557 __f_();
558 }
559};
560
Howard Hinnantad935d52011-05-16 19:05:11 +0000561#endif
562
Howard Hinnant99968442011-11-29 18:15:50 +0000563template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000564void
565__call_once_proxy(void* __vp)
566{
Howard Hinnant99968442011-11-29 18:15:50 +0000567 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000568 (*__p)();
569}
570
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000571_LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000572
573#ifndef _LIBCPP_HAS_NO_VARIADICS
574
575template<class _Callable, class... _Args>
576inline _LIBCPP_INLINE_VISIBILITY
577void
578call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
579{
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000580 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000581 {
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000582 typedef tuple<_Callable&&, _Args&&...> _Gp;
583 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
584 __call_once_param<_Gp> __p(__f);
Howard Hinnant99968442011-11-29 18:15:50 +0000585 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000586 }
587}
588
Howard Hinnant324bb032010-08-22 00:02:43 +0000589#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000590
591template<class _Callable>
592inline _LIBCPP_INLINE_VISIBILITY
593void
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000594call_once(once_flag& __flag, _Callable& __func)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000595{
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000596 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597 {
598 __call_once_param<_Callable> __p(__func);
599 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
600 }
601}
602
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000603template<class _Callable>
604inline _LIBCPP_INLINE_VISIBILITY
605void
606call_once(once_flag& __flag, const _Callable& __func)
607{
608 if (__flag.__state_ != ~0ul)
609 {
610 __call_once_param<const _Callable> __p(__func);
611 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
612 }
613}
614
Howard Hinnant324bb032010-08-22 00:02:43 +0000615#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000616
Eric Fiselier10b52a02016-06-14 03:48:09 +0000617
618#if defined(_LIBCPP_ABI_VARIADIC_LOCK_GUARD) \
619 && !defined(_LIBCPP_CXX03_LANG)
620template <>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000621class _LIBCPP_TEMPLATE_VIS lock_guard<> {
Eric Fiselier10b52a02016-06-14 03:48:09 +0000622public:
Eric Fiselier12150a72016-06-15 17:04:40 +0000623 explicit lock_guard() {}
Eric Fiselier10b52a02016-06-14 03:48:09 +0000624 ~lock_guard() = default;
625
626 _LIBCPP_INLINE_VISIBILITY
627 explicit lock_guard(adopt_lock_t) {}
628
629 lock_guard(lock_guard const&) = delete;
630 lock_guard& operator=(lock_guard const&) = delete;
631};
632
633template <class ..._MArgs>
Eric Fiselierc3589a82017-01-04 23:56:00 +0000634class _LIBCPP_TEMPLATE_VIS lock_guard
Eric Fiselier10b52a02016-06-14 03:48:09 +0000635{
636 static_assert(sizeof...(_MArgs) >= 2, "At least 2 lock types required");
637 typedef tuple<_MArgs&...> _MutexTuple;
638
639public:
640 _LIBCPP_INLINE_VISIBILITY
641 explicit lock_guard(_MArgs&... __margs)
642 : __t_(__margs...)
643 {
644 _VSTD::lock(__margs...);
645 }
646
647 _LIBCPP_INLINE_VISIBILITY
648 lock_guard(_MArgs&... __margs, adopt_lock_t)
649 : __t_(__margs...)
650 {
651 }
652
653 _LIBCPP_INLINE_VISIBILITY
654 ~lock_guard() {
655 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
656 __unlock_unpack(_Indices{}, __t_);
657 }
658
659 lock_guard(lock_guard const&) = delete;
660 lock_guard& operator=(lock_guard const&) = delete;
661
662private:
663 template <size_t ..._Indx>
664 _LIBCPP_INLINE_VISIBILITY
665 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
666 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
667 }
668
669 _MutexTuple __t_;
670};
671
672#endif // _LIBCPP_ABI_VARIADIC_LOCK_GUARD
673
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000674_LIBCPP_END_NAMESPACE_STD
675
676#endif // _LIBCPP_MUTEX