blob: a0875a568ec67a349b8cc9bdf4aeb0f24d6d5780 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===--------------------------- mutex ------------------------------------===//
3//
Howard Hinnantc566dc32010-05-11 21:36:01 +00004// The LLVM Compiler Infrastructure
Howard Hinnantc51e1022010-05-11 19:42:16 +00005//
Howard Hinnantee11c312010-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 Hinnantc51e1022010-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 Hinnantbd05d6a2012-07-21 16:13:09 +000023 constexpr mutex() noexcept;
Howard Hinnantc51e1022010-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 Hinnantbd05d6a2012-07-21 16:13:09 +000047 bool try_lock() noexcept;
Howard Hinnantc51e1022010-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 Hinnantbd05d6a2012-07-21 16:13:09 +000082 bool try_lock() noexcept;
Howard Hinnantc51e1022010-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
112template <class Mutex>
113class unique_lock
114{
115public:
116 typedef Mutex mutex_type;
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000117 unique_lock() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118 explicit unique_lock(mutex_type& m);
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000119 unique_lock(mutex_type& m, defer_lock_t) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000120 unique_lock(mutex_type& m, try_to_lock_t);
121 unique_lock(mutex_type& m, adopt_lock_t);
122 template <class Clock, class Duration>
123 unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
124 template <class Rep, class Period>
125 unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
126 ~unique_lock();
127
128 unique_lock(unique_lock const&) = delete;
129 unique_lock& operator=(unique_lock const&) = delete;
130
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000131 unique_lock(unique_lock&& u) noexcept;
132 unique_lock& operator=(unique_lock&& u) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
134 void lock();
135 bool try_lock();
136
137 template <class Rep, class Period>
138 bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
139 template <class Clock, class Duration>
140 bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
141
142 void unlock();
143
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000144 void swap(unique_lock& u) noexcept;
145 mutex_type* release() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000147 bool owns_lock() const noexcept;
148 explicit operator bool () const noexcept;
149 mutex_type* mutex() const noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000150};
151
152template <class Mutex>
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000153 void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000154
155template <class L1, class L2, class... L3>
156 int try_lock(L1&, L2&, L3&...);
157template <class L1, class L2, class... L3>
158 void lock(L1&, L2&, L3&...);
159
160struct once_flag
161{
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000162 constexpr once_flag() noexcept;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000163
164 once_flag(const once_flag&) = delete;
165 once_flag& operator=(const once_flag&) = delete;
166};
167
168template<class Callable, class ...Args>
169 void call_once(once_flag& flag, Callable&& func, Args&&... args);
170
171} // std
172
173*/
174
175#include <__config>
176#include <__mutex_base>
177#include <functional>
Eric Fiselier89659d12015-07-07 00:27:16 +0000178#include <memory>
Howard Hinnant51d62e02011-05-16 19:05:11 +0000179#ifndef _LIBCPP_HAS_NO_VARIADICS
180#include <tuple>
181#endif
Jonathan Roelofs72b390d2015-08-27 17:47:34 +0000182#ifndef _LIBCPP_HAS_NO_THREADS
Sergey Dmitrouk7dfca3d2014-12-08 14:50:21 +0000183#include <sched.h>
Jonathan Roelofs72b390d2015-08-27 17:47:34 +0000184#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000185
Howard Hinnantc5a5fbd2011-11-29 16:45:27 +0000186#include <__undef_min_max>
187
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000188#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000189#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000190#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
192_LIBCPP_BEGIN_NAMESPACE_STD
193
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000194#ifndef _LIBCPP_HAS_NO_THREADS
195
Howard Hinnant8331b762013-03-06 23:30:19 +0000196class _LIBCPP_TYPE_VIS recursive_mutex
Howard Hinnantc51e1022010-05-11 19:42:16 +0000197{
198 pthread_mutex_t __m_;
199
200public:
201 recursive_mutex();
202 ~recursive_mutex();
203
204private:
205 recursive_mutex(const recursive_mutex&); // = delete;
206 recursive_mutex& operator=(const recursive_mutex&); // = delete;
207
208public:
209 void lock();
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000210 bool try_lock() _NOEXCEPT;
211 void unlock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000212
213 typedef pthread_mutex_t* native_handle_type;
Howard Hinnantf5f99992010-09-22 18:02:38 +0000214 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000215 native_handle_type native_handle() {return &__m_;}
216};
217
Howard Hinnant8331b762013-03-06 23:30:19 +0000218class _LIBCPP_TYPE_VIS timed_mutex
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219{
220 mutex __m_;
221 condition_variable __cv_;
222 bool __locked_;
223public:
224 timed_mutex();
225 ~timed_mutex();
226
227private:
228 timed_mutex(const timed_mutex&); // = delete;
229 timed_mutex& operator=(const timed_mutex&); // = delete;
230
231public:
232 void lock();
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000233 bool try_lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 template <class _Rep, class _Period>
Howard Hinnantf5f99992010-09-22 18:02:38 +0000235 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000237 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000238 template <class _Clock, class _Duration>
239 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000240 void unlock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241};
242
243template <class _Clock, class _Duration>
244bool
245timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
246{
247 using namespace chrono;
248 unique_lock<mutex> __lk(__m_);
249 bool no_timeout = _Clock::now() < __t;
250 while (no_timeout && __locked_)
251 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
252 if (!__locked_)
253 {
254 __locked_ = true;
255 return true;
256 }
257 return false;
258}
259
Howard Hinnant8331b762013-03-06 23:30:19 +0000260class _LIBCPP_TYPE_VIS recursive_timed_mutex
Howard Hinnantc51e1022010-05-11 19:42:16 +0000261{
262 mutex __m_;
263 condition_variable __cv_;
264 size_t __count_;
265 pthread_t __id_;
266public:
267 recursive_timed_mutex();
268 ~recursive_timed_mutex();
269
270private:
271 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
272 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
273
274public:
275 void lock();
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000276 bool try_lock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000277 template <class _Rep, class _Period>
Howard Hinnantf5f99992010-09-22 18:02:38 +0000278 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000279 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantc8dbd222010-11-20 19:16:30 +0000280 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000281 template <class _Clock, class _Duration>
282 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000283 void unlock() _NOEXCEPT;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000284};
285
286template <class _Clock, class _Duration>
287bool
288recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
289{
290 using namespace chrono;
291 pthread_t __id = pthread_self();
292 unique_lock<mutex> lk(__m_);
293 if (pthread_equal(__id, __id_))
294 {
295 if (__count_ == numeric_limits<size_t>::max())
296 return false;
297 ++__count_;
298 return true;
299 }
300 bool no_timeout = _Clock::now() < __t;
301 while (no_timeout && __count_ != 0)
302 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
303 if (__count_ == 0)
304 {
305 __count_ = 1;
306 __id_ = __id;
307 return true;
308 }
309 return false;
310}
311
312template <class _L0, class _L1>
313int
314try_lock(_L0& __l0, _L1& __l1)
315{
316 unique_lock<_L0> __u0(__l0, try_to_lock);
317 if (__u0.owns_lock())
318 {
319 if (__l1.try_lock())
320 {
321 __u0.release();
322 return -1;
323 }
324 else
325 return 1;
326 }
327 return 0;
328}
329
330#ifndef _LIBCPP_HAS_NO_VARIADICS
331
332template <class _L0, class _L1, class _L2, class... _L3>
333int
334try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
335{
336 int __r = 0;
337 unique_lock<_L0> __u0(__l0, try_to_lock);
338 if (__u0.owns_lock())
339 {
340 __r = try_lock(__l1, __l2, __l3...);
341 if (__r == -1)
342 __u0.release();
343 else
344 ++__r;
345 }
346 return __r;
347}
348
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000349#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000350
351template <class _L0, class _L1>
352void
353lock(_L0& __l0, _L1& __l1)
354{
355 while (true)
356 {
357 {
358 unique_lock<_L0> __u0(__l0);
359 if (__l1.try_lock())
360 {
361 __u0.release();
362 break;
363 }
364 }
365 sched_yield();
366 {
367 unique_lock<_L1> __u1(__l1);
368 if (__l0.try_lock())
369 {
370 __u1.release();
371 break;
372 }
373 }
374 sched_yield();
375 }
376}
377
378#ifndef _LIBCPP_HAS_NO_VARIADICS
379
Howard Hinnantf8485322011-01-12 22:56:59 +0000380template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000381void
Howard Hinnantf8485322011-01-12 22:56:59 +0000382__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000383{
384 while (true)
385 {
386 switch (__i)
387 {
388 case 0:
389 {
390 unique_lock<_L0> __u0(__l0);
Howard Hinnantf8485322011-01-12 22:56:59 +0000391 __i = try_lock(__l1, __l2, __l3...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392 if (__i == -1)
393 {
394 __u0.release();
395 return;
396 }
397 }
398 ++__i;
399 sched_yield();
400 break;
401 case 1:
402 {
403 unique_lock<_L1> __u1(__l1);
Howard Hinnantf8485322011-01-12 22:56:59 +0000404 __i = try_lock(__l2, __l3..., __l0);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 if (__i == -1)
406 {
407 __u1.release();
408 return;
409 }
410 }
Howard Hinnantf8485322011-01-12 22:56:59 +0000411 if (__i == sizeof...(_L3) + 1)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000412 __i = 0;
413 else
414 __i += 2;
415 sched_yield();
416 break;
417 default:
Howard Hinnantf8485322011-01-12 22:56:59 +0000418 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000419 return;
420 }
421 }
422}
423
Howard Hinnantf8485322011-01-12 22:56:59 +0000424template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantf5f99992010-09-22 18:02:38 +0000425inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000426void
Howard Hinnantf8485322011-01-12 22:56:59 +0000427lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000428{
Howard Hinnantf8485322011-01-12 22:56:59 +0000429 __lock_first(0, __l0, __l1, __l2, __l3...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000430}
431
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000432#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433
Jonathan Roelofs39cb6bf2014-09-05 19:45:05 +0000434#endif // !_LIBCPP_HAS_NO_THREADS
435
Marshall Clow7ed42342014-07-29 21:05:31 +0000436struct _LIBCPP_TYPE_VIS_ONLY once_flag;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000437
438#ifndef _LIBCPP_HAS_NO_VARIADICS
439
440template<class _Callable, class... _Args>
Howard Hinnanta54386e2012-09-14 00:39:16 +0000441_LIBCPP_INLINE_VISIBILITY
442void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000443
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000444#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000445
446template<class _Callable>
Howard Hinnanta54386e2012-09-14 00:39:16 +0000447_LIBCPP_INLINE_VISIBILITY
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000448void call_once(once_flag&, _Callable&);
449
450template<class _Callable>
451_LIBCPP_INLINE_VISIBILITY
452void call_once(once_flag&, const _Callable&);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000453
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000454#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000455
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000456struct _LIBCPP_TYPE_VIS_ONLY once_flag
Howard Hinnantc51e1022010-05-11 19:42:16 +0000457{
Howard Hinnantf5f99992010-09-22 18:02:38 +0000458 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbd05d6a2012-07-21 16:13:09 +0000459 _LIBCPP_CONSTEXPR
460 once_flag() _NOEXCEPT : __state_(0) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461
462private:
463 once_flag(const once_flag&); // = delete;
464 once_flag& operator=(const once_flag&); // = delete;
465
466 unsigned long __state_;
467
468#ifndef _LIBCPP_HAS_NO_VARIADICS
469 template<class _Callable, class... _Args>
470 friend
471 void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000472#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000473 template<class _Callable>
474 friend
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000475 void call_once(once_flag&, _Callable&);
476
477 template<class _Callable>
478 friend
479 void call_once(once_flag&, const _Callable&);
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000480#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000481};
482
Howard Hinnant51d62e02011-05-16 19:05:11 +0000483#ifndef _LIBCPP_HAS_NO_VARIADICS
484
Howard Hinnantc834c512011-11-29 18:15:50 +0000485template <class _Fp>
Howard Hinnant51d62e02011-05-16 19:05:11 +0000486class __call_once_param
487{
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000488 _Fp& __f_;
Howard Hinnant51d62e02011-05-16 19:05:11 +0000489public:
Howard Hinnant51d62e02011-05-16 19:05:11 +0000490 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000491 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnant51d62e02011-05-16 19:05:11 +0000492
493 _LIBCPP_INLINE_VISIBILITY
494 void operator()()
495 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000496 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnant51d62e02011-05-16 19:05:11 +0000497 __execute(_Index());
498 }
499
500private:
501 template <size_t ..._Indices>
502 _LIBCPP_INLINE_VISIBILITY
503 void __execute(__tuple_indices<_Indices...>)
504 {
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000505 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
Howard Hinnant51d62e02011-05-16 19:05:11 +0000506 }
507};
508
509#else
510
Howard Hinnantc834c512011-11-29 18:15:50 +0000511template <class _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000512class __call_once_param
513{
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000514 _Fp& __f_;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515public:
Howard Hinnantf5f99992010-09-22 18:02:38 +0000516 _LIBCPP_INLINE_VISIBILITY
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000517 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000518
Howard Hinnantf5f99992010-09-22 18:02:38 +0000519 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520 void operator()()
521 {
522 __f_();
523 }
524};
525
Howard Hinnant51d62e02011-05-16 19:05:11 +0000526#endif
527
Howard Hinnantc834c512011-11-29 18:15:50 +0000528template <class _Fp>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000529void
530__call_once_proxy(void* __vp)
531{
Howard Hinnantc834c512011-11-29 18:15:50 +0000532 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000533 (*__p)();
534}
535
Howard Hinnanta37d3cf2013-08-12 18:38:34 +0000536_LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000537
538#ifndef _LIBCPP_HAS_NO_VARIADICS
539
540template<class _Callable, class... _Args>
541inline _LIBCPP_INLINE_VISIBILITY
542void
543call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
544{
Eric Fiselier89659d12015-07-07 00:27:16 +0000545 if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546 {
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000547 typedef tuple<_Callable&&, _Args&&...> _Gp;
548 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
549 __call_once_param<_Gp> __p(__f);
Howard Hinnantc834c512011-11-29 18:15:50 +0000550 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000551 }
552}
553
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000554#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000555
556template<class _Callable>
557inline _LIBCPP_INLINE_VISIBILITY
558void
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000559call_once(once_flag& __flag, _Callable& __func)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000560{
Eric Fiselier89659d12015-07-07 00:27:16 +0000561 if (__libcpp_relaxed_load(&__flag.__state_) != ~0ul)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000562 {
563 __call_once_param<_Callable> __p(__func);
564 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
565 }
566}
567
Eric Fiselier1e88fdc2015-06-13 02:23:00 +0000568template<class _Callable>
569inline _LIBCPP_INLINE_VISIBILITY
570void
571call_once(once_flag& __flag, const _Callable& __func)
572{
573 if (__flag.__state_ != ~0ul)
574 {
575 __call_once_param<const _Callable> __p(__func);
576 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
577 }
578}
579
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000580#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000581
582_LIBCPP_END_NAMESPACE_STD
583
584#endif // _LIBCPP_MUTEX