blob: 5cc8ca66969f54a992f6e566390219f066a74ac3 [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
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);
119 scoped_lock(MutexTypes&... m, adopt_lock_t);
120 ~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>
Howard Hinnantad935d52011-05-16 19:05:11 +0000194#ifndef _LIBCPP_HAS_NO_VARIADICS
195#include <tuple>
196#endif
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000197#include <__threading_support>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000198
Howard Hinnant66c6f972011-11-29 16:45:27 +0000199#include <__undef_min_max>
200
Howard Hinnant08e17472011-10-17 20:05:10 +0000201#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000202#pragma GCC system_header
Howard Hinnant08e17472011-10-17 20:05:10 +0000203#endif
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000204
205_LIBCPP_BEGIN_NAMESPACE_STD
206
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000207#ifndef _LIBCPP_HAS_NO_THREADS
208
Howard Hinnant83eade62013-03-06 23:30:19 +0000209class _LIBCPP_TYPE_VIS recursive_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000210{
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000211 __libcpp_recursive_mutex_t __m_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000212
213public:
214 recursive_mutex();
215 ~recursive_mutex();
216
217private:
218 recursive_mutex(const recursive_mutex&); // = delete;
219 recursive_mutex& operator=(const recursive_mutex&); // = delete;
220
221public:
222 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000223 bool try_lock() _NOEXCEPT;
224 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000225
Saleem Abdulrasool3451a652017-01-05 17:54:45 +0000226 typedef __libcpp_recursive_mutex_t* native_handle_type;
227
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000228 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000229 native_handle_type native_handle() {return &__m_;}
230};
231
Howard Hinnant83eade62013-03-06 23:30:19 +0000232class _LIBCPP_TYPE_VIS timed_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000233{
234 mutex __m_;
235 condition_variable __cv_;
236 bool __locked_;
237public:
238 timed_mutex();
239 ~timed_mutex();
240
241private:
242 timed_mutex(const timed_mutex&); // = delete;
243 timed_mutex& operator=(const timed_mutex&); // = delete;
244
245public:
246 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000247 bool try_lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000248 template <class _Rep, class _Period>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000249 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000250 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantf8f85212010-11-20 19:16:30 +0000251 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000252 template <class _Clock, class _Duration>
Shoaib Meenai6b734922017-03-02 03:22:18 +0000253 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000254 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000255 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000256};
257
258template <class _Clock, class _Duration>
259bool
260timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
261{
262 using namespace chrono;
263 unique_lock<mutex> __lk(__m_);
264 bool no_timeout = _Clock::now() < __t;
265 while (no_timeout && __locked_)
266 no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
267 if (!__locked_)
268 {
269 __locked_ = true;
270 return true;
271 }
272 return false;
273}
274
Howard Hinnant83eade62013-03-06 23:30:19 +0000275class _LIBCPP_TYPE_VIS recursive_timed_mutex
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000276{
277 mutex __m_;
278 condition_variable __cv_;
279 size_t __count_;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000280 __libcpp_thread_id __id_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000281public:
282 recursive_timed_mutex();
283 ~recursive_timed_mutex();
284
285private:
286 recursive_timed_mutex(const recursive_timed_mutex&); // = delete;
287 recursive_timed_mutex& operator=(const recursive_timed_mutex&); // = delete;
288
289public:
290 void lock();
Howard Hinnant499c61f2012-07-21 16:13:09 +0000291 bool try_lock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000292 template <class _Rep, class _Period>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000293 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000294 bool try_lock_for(const chrono::duration<_Rep, _Period>& __d)
Howard Hinnantf8f85212010-11-20 19:16:30 +0000295 {return try_lock_until(chrono::steady_clock::now() + __d);}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000296 template <class _Clock, class _Duration>
Shoaib Meenai6b734922017-03-02 03:22:18 +0000297 _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000298 bool try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
Howard Hinnant499c61f2012-07-21 16:13:09 +0000299 void unlock() _NOEXCEPT;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000300};
301
302template <class _Clock, class _Duration>
303bool
304recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t)
305{
306 using namespace chrono;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000307 __libcpp_thread_id __id = __libcpp_thread_get_current_id();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000308 unique_lock<mutex> lk(__m_);
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000309 if (__libcpp_thread_id_equal(__id, __id_))
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000310 {
311 if (__count_ == numeric_limits<size_t>::max())
312 return false;
313 ++__count_;
314 return true;
315 }
316 bool no_timeout = _Clock::now() < __t;
317 while (no_timeout && __count_ != 0)
318 no_timeout = __cv_.wait_until(lk, __t) == cv_status::no_timeout;
319 if (__count_ == 0)
320 {
321 __count_ = 1;
322 __id_ = __id;
323 return true;
324 }
325 return false;
326}
327
328template <class _L0, class _L1>
329int
330try_lock(_L0& __l0, _L1& __l1)
331{
332 unique_lock<_L0> __u0(__l0, try_to_lock);
333 if (__u0.owns_lock())
334 {
335 if (__l1.try_lock())
336 {
337 __u0.release();
338 return -1;
339 }
340 else
341 return 1;
342 }
343 return 0;
344}
345
346#ifndef _LIBCPP_HAS_NO_VARIADICS
347
348template <class _L0, class _L1, class _L2, class... _L3>
349int
350try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3)
351{
352 int __r = 0;
353 unique_lock<_L0> __u0(__l0, try_to_lock);
354 if (__u0.owns_lock())
355 {
356 __r = try_lock(__l1, __l2, __l3...);
357 if (__r == -1)
358 __u0.release();
359 else
360 ++__r;
361 }
362 return __r;
363}
364
Howard Hinnant324bb032010-08-22 00:02:43 +0000365#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000366
367template <class _L0, class _L1>
368void
369lock(_L0& __l0, _L1& __l1)
370{
371 while (true)
372 {
373 {
374 unique_lock<_L0> __u0(__l0);
375 if (__l1.try_lock())
376 {
377 __u0.release();
378 break;
379 }
380 }
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000381 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000382 {
383 unique_lock<_L1> __u1(__l1);
384 if (__l0.try_lock())
385 {
386 __u1.release();
387 break;
388 }
389 }
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000390 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000391 }
392}
393
394#ifndef _LIBCPP_HAS_NO_VARIADICS
395
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000396template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000397void
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000398__lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000399{
400 while (true)
401 {
402 switch (__i)
403 {
404 case 0:
405 {
406 unique_lock<_L0> __u0(__l0);
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000407 __i = try_lock(__l1, __l2, __l3...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000408 if (__i == -1)
409 {
410 __u0.release();
411 return;
412 }
413 }
414 ++__i;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000415 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000416 break;
417 case 1:
418 {
419 unique_lock<_L1> __u1(__l1);
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000420 __i = try_lock(__l2, __l3..., __l0);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000421 if (__i == -1)
422 {
423 __u1.release();
424 return;
425 }
426 }
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000427 if (__i == sizeof...(_L3) + 1)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000428 __i = 0;
429 else
430 __i += 2;
Asiri Rathnayake35ff03b2016-05-06 14:06:29 +0000431 __libcpp_thread_yield();
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000432 break;
433 default:
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000434 __lock_first(__i - 2, __l2, __l3..., __l0, __l1);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000435 return;
436 }
437 }
438}
439
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000440template <class _L0, class _L1, class _L2, class ..._L3>
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000441inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000442void
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000443lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3& ...__l3)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000444{
Howard Hinnant6fd4b662011-01-12 22:56:59 +0000445 __lock_first(0, __l0, __l1, __l2, __l3...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000446}
447
Eric Fiselier10b52a02016-06-14 03:48:09 +0000448template <class _L0>
449inline _LIBCPP_INLINE_VISIBILITY
450void __unlock(_L0& __l0) {
451 __l0.unlock();
452}
453
454template <class _L0, class _L1>
455inline _LIBCPP_INLINE_VISIBILITY
456void __unlock(_L0& __l0, _L1& __l1) {
457 __l0.unlock();
458 __l1.unlock();
459}
460
461template <class _L0, class _L1, class _L2, class ..._L3>
462inline _LIBCPP_INLINE_VISIBILITY
463void __unlock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
464 __l0.unlock();
465 __l1.unlock();
466 _VSTD::__unlock(__l2, __l3...);
467}
468
Howard Hinnant324bb032010-08-22 00:02:43 +0000469#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000470
Jonathan Roelofs8d86b2e2014-09-05 19:45:05 +0000471#endif // !_LIBCPP_HAS_NO_THREADS
472
Eric Fiselierc3589a82017-01-04 23:56:00 +0000473struct _LIBCPP_TEMPLATE_VIS once_flag;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000474
475#ifndef _LIBCPP_HAS_NO_VARIADICS
476
477template<class _Callable, class... _Args>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000478_LIBCPP_INLINE_VISIBILITY
479void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000480
Howard Hinnant324bb032010-08-22 00:02:43 +0000481#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000482
483template<class _Callable>
Howard Hinnant33be35e2012-09-14 00:39:16 +0000484_LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000485void call_once(once_flag&, _Callable&);
486
487template<class _Callable>
488_LIBCPP_INLINE_VISIBILITY
489void call_once(once_flag&, const _Callable&);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000490
Howard Hinnant324bb032010-08-22 00:02:43 +0000491#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000492
Eric Fiselierc3589a82017-01-04 23:56:00 +0000493struct _LIBCPP_TEMPLATE_VIS once_flag
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000494{
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000495 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant499c61f2012-07-21 16:13:09 +0000496 _LIBCPP_CONSTEXPR
497 once_flag() _NOEXCEPT : __state_(0) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000498
499private:
500 once_flag(const once_flag&); // = delete;
501 once_flag& operator=(const once_flag&); // = delete;
502
503 unsigned long __state_;
504
505#ifndef _LIBCPP_HAS_NO_VARIADICS
506 template<class _Callable, class... _Args>
507 friend
508 void call_once(once_flag&, _Callable&&, _Args&&...);
Howard Hinnant324bb032010-08-22 00:02:43 +0000509#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000510 template<class _Callable>
511 friend
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000512 void call_once(once_flag&, _Callable&);
513
514 template<class _Callable>
515 friend
516 void call_once(once_flag&, const _Callable&);
Howard Hinnant324bb032010-08-22 00:02:43 +0000517#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000518};
519
Howard Hinnantad935d52011-05-16 19:05:11 +0000520#ifndef _LIBCPP_HAS_NO_VARIADICS
521
Howard Hinnant99968442011-11-29 18:15:50 +0000522template <class _Fp>
Howard Hinnantad935d52011-05-16 19:05:11 +0000523class __call_once_param
524{
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000525 _Fp& __f_;
Howard Hinnantad935d52011-05-16 19:05:11 +0000526public:
Howard Hinnantad935d52011-05-16 19:05:11 +0000527 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000528 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantad935d52011-05-16 19:05:11 +0000529
530 _LIBCPP_INLINE_VISIBILITY
531 void operator()()
532 {
Howard Hinnant99968442011-11-29 18:15:50 +0000533 typedef typename __make_tuple_indices<tuple_size<_Fp>::value, 1>::type _Index;
Howard Hinnantad935d52011-05-16 19:05:11 +0000534 __execute(_Index());
535 }
536
537private:
538 template <size_t ..._Indices>
539 _LIBCPP_INLINE_VISIBILITY
540 void __execute(__tuple_indices<_Indices...>)
541 {
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000542 __invoke(_VSTD::get<0>(_VSTD::move(__f_)), _VSTD::get<_Indices>(_VSTD::move(__f_))...);
Howard Hinnantad935d52011-05-16 19:05:11 +0000543 }
544};
545
546#else
547
Howard Hinnant99968442011-11-29 18:15:50 +0000548template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000549class __call_once_param
550{
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000551 _Fp& __f_;
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000552public:
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000553 _LIBCPP_INLINE_VISIBILITY
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000554 explicit __call_once_param(_Fp& __f) : __f_(__f) {}
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000555
Howard Hinnantb9af2ea2010-09-22 18:02:38 +0000556 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000557 void operator()()
558 {
559 __f_();
560 }
561};
562
Howard Hinnantad935d52011-05-16 19:05:11 +0000563#endif
564
Howard Hinnant99968442011-11-29 18:15:50 +0000565template <class _Fp>
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000566void
567__call_once_proxy(void* __vp)
568{
Howard Hinnant99968442011-11-29 18:15:50 +0000569 __call_once_param<_Fp>* __p = static_cast<__call_once_param<_Fp>*>(__vp);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000570 (*__p)();
571}
572
Howard Hinnant0f678bd2013-08-12 18:38:34 +0000573_LIBCPP_FUNC_VIS void __call_once(volatile unsigned long&, void*, void(*)(void*));
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000574
575#ifndef _LIBCPP_HAS_NO_VARIADICS
576
577template<class _Callable, class... _Args>
578inline _LIBCPP_INLINE_VISIBILITY
579void
580call_once(once_flag& __flag, _Callable&& __func, _Args&&... __args)
581{
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000582 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000583 {
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000584 typedef tuple<_Callable&&, _Args&&...> _Gp;
585 _Gp __f(_VSTD::forward<_Callable>(__func), _VSTD::forward<_Args>(__args)...);
586 __call_once_param<_Gp> __p(__f);
Howard Hinnant99968442011-11-29 18:15:50 +0000587 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Gp>);
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000588 }
589}
590
Howard Hinnant324bb032010-08-22 00:02:43 +0000591#else // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000592
593template<class _Callable>
594inline _LIBCPP_INLINE_VISIBILITY
595void
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000596call_once(once_flag& __flag, _Callable& __func)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000597{
Kuba Brecka4dbd4fc2016-09-04 09:55:12 +0000598 if (__libcpp_acquire_load(&__flag.__state_) != ~0ul)
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000599 {
600 __call_once_param<_Callable> __p(__func);
601 __call_once(__flag.__state_, &__p, &__call_once_proxy<_Callable>);
602 }
603}
604
Eric Fiselierbc1e44d2015-06-13 02:23:00 +0000605template<class _Callable>
606inline _LIBCPP_INLINE_VISIBILITY
607void
608call_once(once_flag& __flag, const _Callable& __func)
609{
610 if (__flag.__state_ != ~0ul)
611 {
612 __call_once_param<const _Callable> __p(__func);
613 __call_once(__flag.__state_, &__p, &__call_once_proxy<const _Callable>);
614 }
615}
616
Howard Hinnant324bb032010-08-22 00:02:43 +0000617#endif // _LIBCPP_HAS_NO_VARIADICS
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000618
Marshall Clow59bcc872017-03-24 03:40:36 +0000619#if _LIBCPP_STD_VER > 14
620template <class ..._Mutexes>
621class _LIBCPP_TEMPLATE_VIS scoped_lock;
Eric Fiselier10b52a02016-06-14 03:48:09 +0000622
Eric Fiselier10b52a02016-06-14 03:48:09 +0000623template <>
Marshall Clow59bcc872017-03-24 03:40:36 +0000624class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
Eric Fiselier10b52a02016-06-14 03:48:09 +0000625public:
Marshall Clow59bcc872017-03-24 03:40:36 +0000626 explicit scoped_lock() {}
627 ~scoped_lock() = default;
Eric Fiselier10b52a02016-06-14 03:48:09 +0000628
629 _LIBCPP_INLINE_VISIBILITY
Marshall Clow59bcc872017-03-24 03:40:36 +0000630 explicit scoped_lock(adopt_lock_t) {}
Eric Fiselier10b52a02016-06-14 03:48:09 +0000631
Marshall Clow59bcc872017-03-24 03:40:36 +0000632 scoped_lock(scoped_lock const&) = delete;
633 scoped_lock& operator=(scoped_lock const&) = delete;
634};
635
636template <class _Mutex>
637class _LIBCPP_TEMPLATE_VIS scoped_lock<_Mutex> {
638public:
639 typedef _Mutex mutex_type;
640private:
641 mutex_type& __m_;
642public:
643 explicit scoped_lock(mutex_type & __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
644 : __m_(__m) {__m_.lock();}
645
646 ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) {__m_.unlock();}
647
648 _LIBCPP_INLINE_VISIBILITY
649 explicit scoped_lock(mutex_type& __m, adopt_lock_t) _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
650 : __m_(__m) {}
651
652
653 scoped_lock(scoped_lock const&) = delete;
654 scoped_lock& operator=(scoped_lock const&) = delete;
Eric Fiselier10b52a02016-06-14 03:48:09 +0000655};
656
657template <class ..._MArgs>
Marshall Clow59bcc872017-03-24 03:40:36 +0000658class _LIBCPP_TEMPLATE_VIS scoped_lock
Eric Fiselier10b52a02016-06-14 03:48:09 +0000659{
Marshall Clow59bcc872017-03-24 03:40:36 +0000660 static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
Eric Fiselier10b52a02016-06-14 03:48:09 +0000661 typedef tuple<_MArgs&...> _MutexTuple;
662
663public:
664 _LIBCPP_INLINE_VISIBILITY
Marshall Clow59bcc872017-03-24 03:40:36 +0000665 explicit scoped_lock(_MArgs&... __margs)
Eric Fiselier10b52a02016-06-14 03:48:09 +0000666 : __t_(__margs...)
667 {
668 _VSTD::lock(__margs...);
669 }
670
671 _LIBCPP_INLINE_VISIBILITY
Marshall Clow59bcc872017-03-24 03:40:36 +0000672 scoped_lock(_MArgs&... __margs, adopt_lock_t)
Eric Fiselier10b52a02016-06-14 03:48:09 +0000673 : __t_(__margs...)
674 {
675 }
676
677 _LIBCPP_INLINE_VISIBILITY
Marshall Clow59bcc872017-03-24 03:40:36 +0000678 ~scoped_lock() {
Eric Fiselier10b52a02016-06-14 03:48:09 +0000679 typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
680 __unlock_unpack(_Indices{}, __t_);
681 }
682
Marshall Clow59bcc872017-03-24 03:40:36 +0000683 scoped_lock(scoped_lock const&) = delete;
684 scoped_lock& operator=(scoped_lock const&) = delete;
Eric Fiselier10b52a02016-06-14 03:48:09 +0000685
686private:
687 template <size_t ..._Indx>
688 _LIBCPP_INLINE_VISIBILITY
689 static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
690 _VSTD::__unlock(_VSTD::get<_Indx>(__mt)...);
691 }
692
693 _MutexTuple __t_;
694};
695
Marshall Clow59bcc872017-03-24 03:40:36 +0000696#endif // _LIBCPP_STD_VER > 14
Eric Fiselier10b52a02016-06-14 03:48:09 +0000697
Howard Hinnantbc8d3f92010-05-11 19:42:16 +0000698_LIBCPP_END_NAMESPACE_STD
699
700#endif // _LIBCPP_MUTEX