]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/mutex
mutex (try_lock, [...]): Fix.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / mutex
CommitLineData
68a97d24
BK
1// <mutex> -*- C++ -*-
2
d90d97ff 3// Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
68a97d24
BK
4// Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library. This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
748086b7 9// Free Software Foundation; either version 3, or (at your option)
68a97d24
BK
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16
748086b7
JJ
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
68a97d24 20
748086b7
JJ
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24// <http://www.gnu.org/licenses/>.
68a97d24
BK
25
26/** @file mutex
27 * This is a Standard C++ Library header.
28 */
29
30#ifndef _GLIBCXX_MUTEX
31#define _GLIBCXX_MUTEX 1
32
33#pragma GCC system_header
34
35#ifndef __GXX_EXPERIMENTAL_CXX0X__
ab65a4c7 36# include <bits/c++0x_warning.h>
57317d2a 37#else
68a97d24 38
57cb79ef 39#include <tuple>
7b800287
CF
40#include <chrono>
41#include <exception>
42#include <type_traits>
43#include <functional>
44#include <system_error>
68a97d24 45#include <bits/functexcept.h>
4db6bc0f 46#include <bits/gthr.h>
7b800287
CF
47#include <bits/move.h> // for std::swap
48
49#if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
68a97d24 50
53dc5044
PC
51_GLIBCXX_BEGIN_NAMESPACE(std)
52
5b9daa7e
BK
53 /**
54 * @defgroup mutexes Mutexes
55 * @ingroup concurrency
56 *
57 * Classes for mutex support.
58 * @{
59 */
60
68a97d24
BK
61 /// mutex
62 class mutex
63 {
f7459b6c
BK
64 typedef __gthread_mutex_t __native_type;
65 __native_type _M_mutex;
66
68a97d24 67 public:
f7459b6c 68 typedef __native_type* native_handle_type;
68a97d24 69
94a86be0
BK
70#ifdef __GTHREAD_MUTEX_INIT
71 constexpr mutex() : _M_mutex(__GTHREAD_MUTEX_INIT) { }
72#else
68a97d24
BK
73 mutex()
74 {
22ac021b 75 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
4db6bc0f 76 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
68a97d24 77 }
9916a9e4
JW
78
79 ~mutex() { __gthread_mutex_destroy(&_M_mutex); }
94a86be0 80#endif
68a97d24 81
7b800287
CF
82 mutex(const mutex&) = delete;
83 mutex& operator=(const mutex&) = delete;
84
4db6bc0f 85 void
68a97d24
BK
86 lock()
87 {
88 int __e = __gthread_mutex_lock(&_M_mutex);
89
90 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
d3098c94
CF
91 if (__e)
92 __throw_system_error(__e);
68a97d24 93 }
4db6bc0f
BK
94
95 bool
68a97d24
BK
96 try_lock()
97 {
d3098c94
CF
98 // XXX EINVAL, EAGAIN, EBUSY
99 return !__gthread_mutex_trylock(&_M_mutex);
68a97d24
BK
100 }
101
4db6bc0f 102 void
68a97d24
BK
103 unlock()
104 {
22ac021b 105 // XXX EINVAL, EAGAIN, EPERM
d3098c94 106 __gthread_mutex_unlock(&_M_mutex);
68a97d24
BK
107 }
108
4db6bc0f 109 native_handle_type
68a97d24 110 native_handle()
7b800287 111 { return &_M_mutex; }
68a97d24
BK
112 };
113
9916a9e4
JW
114#ifndef __GTHREAD_RECURSIVE_MUTEX_INIT
115 // FIXME: gthreads doesn't define __gthread_recursive_mutex_destroy
116 // so we need to obtain a __gthread_mutex_t to destroy
117 class __destroy_recursive_mutex
118 {
119 template<typename _Mx, typename _Rm>
120 static void
121 _S_destroy_win32(_Mx* __mx, _Rm const* __rmx)
122 {
123 __mx->counter = __rmx->counter;
124 __mx->sema = __rmx->sema;
125 __gthread_mutex_destroy(__mx);
126 }
127
128 public:
129 // matches a gthr-win32.h recursive mutex
130 template<typename _Rm>
131 static typename enable_if<sizeof(&_Rm::sema), void>::type
132 _S_destroy(_Rm* __mx)
133 {
134 __gthread_mutex_t __tmp;
135 _S_destroy_win32(&__tmp, __mx);
136 }
137
138 // matches a recursive mutex with a member 'actual'
139 template<typename _Rm>
140 static typename enable_if<sizeof(&_Rm::actual), void>::type
141 _S_destroy(_Rm* __mx)
142 { __gthread_mutex_destroy(&__mx->actual); }
143
144 // matches when there's only one mutex type
145 template<typename _Rm>
146 static
147 typename enable_if<is_same<_Rm, __gthread_mutex_t>::value, void>::type
148 _S_destroy(_Rm* __mx)
149 { __gthread_mutex_destroy(__mx); }
150 };
151#endif
152
68a97d24
BK
153 /// recursive_mutex
154 class recursive_mutex
155 {
f7459b6c
BK
156 typedef __gthread_recursive_mutex_t __native_type;
157 __native_type _M_mutex;
158
68a97d24 159 public:
f7459b6c 160 typedef __native_type* native_handle_type;
68a97d24 161
9916a9e4
JW
162#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
163 recursive_mutex() : _M_mutex(__GTHREAD_RECURSIVE_MUTEX_INIT) { }
164#else
68a97d24 165 recursive_mutex()
4db6bc0f 166 {
22ac021b 167 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
4db6bc0f 168 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
68a97d24
BK
169 }
170
9916a9e4
JW
171 ~recursive_mutex()
172 { __destroy_recursive_mutex::_S_destroy(&_M_mutex); }
173#endif
174
7b800287
CF
175 recursive_mutex(const recursive_mutex&) = delete;
176 recursive_mutex& operator=(const recursive_mutex&) = delete;
177
4db6bc0f 178 void
68a97d24 179 lock()
4db6bc0f 180 {
68a97d24
BK
181 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
182
183 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
d3098c94
CF
184 if (__e)
185 __throw_system_error(__e);
68a97d24 186 }
4db6bc0f
BK
187
188 bool
68a97d24
BK
189 try_lock()
190 {
22ac021b 191 // XXX EINVAL, EAGAIN, EBUSY
7b800287 192 return !__gthread_recursive_mutex_trylock(&_M_mutex);
68a97d24
BK
193 }
194
4db6bc0f 195 void
68a97d24 196 unlock()
4db6bc0f 197 {
22ac021b 198 // XXX EINVAL, EAGAIN, EBUSY
7b800287 199 __gthread_recursive_mutex_unlock(&_M_mutex);
68a97d24
BK
200 }
201
4db6bc0f 202 native_handle_type
d3098c94 203 native_handle()
7b800287 204 { return &_M_mutex; }
68a97d24
BK
205 };
206
d3098c94
CF
207 /// timed_mutex
208 class timed_mutex
f7459b6c
BK
209 {
210 typedef __gthread_mutex_t __native_type;
211
212#ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
213 typedef chrono::monotonic_clock __clock_t;
214#else
215 typedef chrono::high_resolution_clock __clock_t;
216#endif
217
218 __native_type _M_mutex;
219
d3098c94 220 public:
f7459b6c 221 typedef __native_type* native_handle_type;
d3098c94 222
7b800287 223#ifdef __GTHREAD_MUTEX_INIT
9916a9e4 224 timed_mutex() : _M_mutex(__GTHREAD_MUTEX_INIT) { }
7b800287 225#else
9916a9e4
JW
226 timed_mutex()
227 {
7b800287 228 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
7b800287
CF
229 }
230
9916a9e4
JW
231 ~timed_mutex() { __gthread_mutex_destroy(&_M_mutex); }
232#endif
233
7b800287
CF
234 timed_mutex(const timed_mutex&) = delete;
235 timed_mutex& operator=(const timed_mutex&) = delete;
236
237 void
238 lock()
239 {
240 int __e = __gthread_mutex_lock(&_M_mutex);
241
242 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
243 if (__e)
244 __throw_system_error(__e);
245 }
246
247 bool
248 try_lock()
249 {
250 // XXX EINVAL, EAGAIN, EBUSY
251 return !__gthread_mutex_trylock(&_M_mutex);
252 }
d3098c94
CF
253
254 template <class _Rep, class _Period>
255 bool
7b800287
CF
256 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
257 { return __try_lock_for_impl(__rtime); }
d3098c94
CF
258
259 template <class _Clock, class _Duration>
260 bool
7b800287 261 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
f7459b6c 262 {
7b800287 263 chrono::time_point<_Clock, chrono::seconds> __s =
f7459b6c 264 chrono::time_point_cast<chrono::seconds>(__atime);
7b800287
CF
265
266 chrono::nanoseconds __ns =
f7459b6c 267 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
7b800287 268
f7459b6c
BK
269 __gthread_time_t __ts = {
270 static_cast<std::time_t>(__s.time_since_epoch().count()),
271 static_cast<long>(__ns.count())
272 };
68a97d24 273
f7459b6c 274 return !__gthread_mutex_timedlock(&_M_mutex, &__ts);
7b800287
CF
275 }
276
277 void
278 unlock()
279 {
280 // XXX EINVAL, EAGAIN, EBUSY
281 __gthread_mutex_unlock(&_M_mutex);
282 }
d3098c94
CF
283
284 native_handle_type
285 native_handle()
7b800287 286 { return &_M_mutex; }
d3098c94 287
f7459b6c 288 private:
7b800287
CF
289 template<typename _Rep, typename _Period>
290 typename enable_if<
f7459b6c 291 ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
7b800287
CF
292 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
293 {
f7459b6c
BK
294 __clock_t::time_point __atime = __clock_t::now()
295 + chrono::duration_cast<__clock_t::duration>(__rtime);
7b800287 296
f7459b6c 297 return try_lock_until(__atime);
7b800287
CF
298 }
299
300 template <typename _Rep, typename _Period>
301 typename enable_if<
f7459b6c 302 !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
7b800287
CF
303 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
304 {
f7459b6c 305 __clock_t::time_point __atime = __clock_t::now()
7b800287
CF
306 + ++chrono::duration_cast<__clock_t::duration>(__rtime);
307
f7459b6c 308 return try_lock_until(__atime);
7b800287 309 }
d3098c94
CF
310 };
311
312 /// recursive_timed_mutex
313 class recursive_timed_mutex
314 {
f7459b6c
BK
315 typedef __gthread_recursive_mutex_t __native_type;
316
317#ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
318 typedef chrono::monotonic_clock __clock_t;
319#else
320 typedef chrono::high_resolution_clock __clock_t;
321#endif
322
323 __native_type _M_mutex;
324
d3098c94 325 public:
f7459b6c 326 typedef __native_type* native_handle_type;
7b800287 327
9916a9e4
JW
328#ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
329 recursive_timed_mutex() : _M_mutex(__GTHREAD_RECURSIVE_MUTEX_INIT) { }
330#else
7b800287
CF
331 recursive_timed_mutex()
332 {
333 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
7b800287 334 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
7b800287
CF
335 }
336
9916a9e4
JW
337 ~recursive_timed_mutex()
338 { __destroy_recursive_mutex::_S_destroy(&_M_mutex); }
339#endif
340
7b800287
CF
341 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
342 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
343
344 void
345 lock()
346 {
347 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
348
349 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
350 if (__e)
351 __throw_system_error(__e);
352 }
d3098c94 353
7b800287
CF
354 bool
355 try_lock()
356 {
357 // XXX EINVAL, EAGAIN, EBUSY
358 return !__gthread_recursive_mutex_trylock(&_M_mutex);
359 }
d3098c94
CF
360
361 template <class _Rep, class _Period>
362 bool
7b800287
CF
363 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
364 { return __try_lock_for_impl(__rtime); }
d3098c94
CF
365
366 template <class _Clock, class _Duration>
367 bool
7b800287 368 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
f7459b6c 369 {
7b800287 370 chrono::time_point<_Clock, chrono::seconds> __s =
f7459b6c 371 chrono::time_point_cast<chrono::seconds>(__atime);
7b800287
CF
372
373 chrono::nanoseconds __ns =
f7459b6c 374 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
d3098c94 375
f7459b6c
BK
376 __gthread_time_t __ts = {
377 static_cast<std::time_t>(__s.time_since_epoch().count()),
378 static_cast<long>(__ns.count())
379 };
7b800287 380
f7459b6c 381 return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts);
7b800287
CF
382 }
383
384 void
385 unlock()
386 {
387 // XXX EINVAL, EAGAIN, EBUSY
388 __gthread_recursive_mutex_unlock(&_M_mutex);
389 }
d3098c94
CF
390
391 native_handle_type
392 native_handle()
7b800287
CF
393 { return &_M_mutex; }
394
d3098c94 395 private:
7b800287
CF
396 template<typename _Rep, typename _Period>
397 typename enable_if<
f7459b6c 398 ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
7b800287
CF
399 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
400 {
f7459b6c
BK
401 __clock_t::time_point __atime = __clock_t::now()
402 + chrono::duration_cast<__clock_t::duration>(__rtime);
7b800287 403
f7459b6c 404 return try_lock_until(__atime);
7b800287
CF
405 }
406
407 template <typename _Rep, typename _Period>
408 typename enable_if<
f7459b6c 409 !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
7b800287
CF
410 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
411 {
f7459b6c 412 __clock_t::time_point __atime = __clock_t::now()
7b800287
CF
413 + ++chrono::duration_cast<__clock_t::duration>(__rtime);
414
f7459b6c 415 return try_lock_until(__atime);
7b800287 416 }
d3098c94 417 };
68a97d24
BK
418
419 /// Do not acquire ownership of the mutex.
420 struct defer_lock_t { };
421
422 /// Try to acquire ownership of the mutex without blocking.
423 struct try_to_lock_t { };
424
425 /// Assume the calling thread has already obtained mutex ownership
426 /// and manage it.
427 struct adopt_lock_t { };
428
94a86be0
BK
429 constexpr defer_lock_t defer_lock { };
430 constexpr try_to_lock_t try_to_lock { };
431 constexpr adopt_lock_t adopt_lock { };
68a97d24 432
68a97d24
BK
433 /// @brief Scoped lock idiom.
434 // Acquire the mutex here with a constructor call, then release with
435 // the destructor call in accordance with RAII style.
4db6bc0f 436 template<typename _Mutex>
68a97d24
BK
437 class lock_guard
438 {
439 public:
440 typedef _Mutex mutex_type;
441
442 explicit lock_guard(mutex_type& __m) : _M_device(__m)
443 { _M_device.lock(); }
444
ac498356
JW
445 lock_guard(mutex_type& __m, adopt_lock_t) : _M_device(__m)
446 { } // calling thread owns mutex
68a97d24
BK
447
448 ~lock_guard()
449 { _M_device.unlock(); }
450
7b800287
CF
451 lock_guard(const lock_guard&) = delete;
452 lock_guard& operator=(const lock_guard&) = delete;
453
68a97d24
BK
454 private:
455 mutex_type& _M_device;
68a97d24
BK
456 };
457
458 /// unique_lock
4db6bc0f 459 template<typename _Mutex>
68a97d24
BK
460 class unique_lock
461 {
462 public:
463 typedef _Mutex mutex_type;
f7459b6c 464
d3098c94 465 unique_lock()
7b800287 466 : _M_device(0), _M_owns(false)
d3098c94 467 { }
68a97d24 468
d3098c94 469 explicit unique_lock(mutex_type& __m)
7b800287 470 : _M_device(&__m), _M_owns(false)
4db6bc0f
BK
471 {
472 lock();
68a97d24
BK
473 _M_owns = true;
474 }
4db6bc0f
BK
475
476 unique_lock(mutex_type& __m, defer_lock_t)
d3098c94
CF
477 : _M_device(&__m), _M_owns(false)
478 { }
68a97d24 479
4db6bc0f 480 unique_lock(mutex_type& __m, try_to_lock_t)
d3098c94
CF
481 : _M_device(&__m), _M_owns(_M_device->try_lock())
482 { }
68a97d24 483
4db6bc0f 484 unique_lock(mutex_type& __m, adopt_lock_t)
68a97d24
BK
485 : _M_device(&__m), _M_owns(true)
486 {
487 // XXX calling thread owns mutex
488 }
489
d3098c94 490 template<typename _Clock, typename _Duration>
f7459b6c 491 unique_lock(mutex_type& __m,
7b800287
CF
492 const chrono::time_point<_Clock, _Duration>& __atime)
493 : _M_device(&__m), _M_owns(_M_device->try_lock_until(__atime))
494 { }
68a97d24 495
d3098c94 496 template<typename _Rep, typename _Period>
f7459b6c 497 unique_lock(mutex_type& __m,
7b800287
CF
498 const chrono::duration<_Rep, _Period>& __rtime)
499 : _M_device(&__m), _M_owns(_M_device->try_lock_for(__rtime))
500 { }
68a97d24
BK
501
502 ~unique_lock()
503 {
504 if (_M_owns)
505 unlock();
506 }
507
7b800287
CF
508 unique_lock(const unique_lock&) = delete;
509 unique_lock& operator=(const unique_lock&) = delete;
510
511 unique_lock(unique_lock&& __u)
512 : _M_device(__u._M_device), _M_owns(__u._M_owns)
513 {
514 __u._M_device = 0;
515 __u._M_owns = false;
516 }
517
518 unique_lock& operator=(unique_lock&& __u)
519 {
f7459b6c 520 if(_M_owns)
7b800287 521 unlock();
f7459b6c 522
7b800287 523 unique_lock(std::move(__u)).swap(*this);
68a97d24 524
7b800287
CF
525 __u._M_device = 0;
526 __u._M_owns = false;
f7459b6c 527
7b800287
CF
528 return *this;
529 }
68a97d24 530
4db6bc0f 531 void
68a97d24 532 lock()
4db6bc0f 533 {
7b800287 534 if (!_M_device)
9b3003d5 535 __throw_system_error(int(errc::operation_not_permitted));
7b800287 536 else if (_M_owns)
9b3003d5 537 __throw_system_error(int(errc::resource_deadlock_would_occur));
68a97d24 538 else
7b800287
CF
539 {
540 _M_device->lock();
541 _M_owns = true;
542 }
68a97d24
BK
543 }
544
4db6bc0f 545 bool
68a97d24 546 try_lock()
4db6bc0f 547 {
7b800287 548 if (!_M_device)
9b3003d5 549 __throw_system_error(int(errc::operation_not_permitted));
f7459b6c 550 else if (_M_owns)
9b3003d5 551 __throw_system_error(int(errc::resource_deadlock_would_occur));
f7459b6c 552 else
7b800287 553 {
f7459b6c 554 _M_owns = _M_device->try_lock();
7b800287
CF
555 return _M_owns;
556 }
68a97d24
BK
557 }
558
7b800287 559 template<typename _Clock, typename _Duration>
f7459b6c
BK
560 bool
561 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
562 {
7b800287 563 if (!_M_device)
9b3003d5 564 __throw_system_error(int(errc::operation_not_permitted));
7b800287 565 else if (_M_owns)
9b3003d5 566 __throw_system_error(int(errc::resource_deadlock_would_occur));
7b800287
CF
567 else
568 {
569 _M_owns = _M_device->try_lock_until(__atime);
570 return _M_owns;
571 }
572 }
f7459b6c 573
7b800287
CF
574 template<typename _Rep, typename _Period>
575 bool
576 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
f7459b6c 577 {
7b800287 578 if (!_M_device)
9b3003d5 579 __throw_system_error(int(errc::operation_not_permitted));
7b800287 580 else if (_M_owns)
9b3003d5 581 __throw_system_error(int(errc::resource_deadlock_would_occur));
7b800287
CF
582 else
583 {
584 _M_owns = _M_device->try_lock_for(__rtime);
585 return _M_owns;
586 }
587 }
588
4db6bc0f 589 void
68a97d24 590 unlock()
4db6bc0f 591 {
f7459b6c 592 if (!_M_owns)
9b3003d5 593 __throw_system_error(int(errc::operation_not_permitted));
f7459b6c
BK
594 else if (_M_device)
595 {
596 _M_device->unlock();
597 _M_owns = false;
598 }
68a97d24 599 }
f7459b6c 600
4db6bc0f 601 void
ff74fd13 602 swap(unique_lock& __u)
7b800287
CF
603 {
604 std::swap(_M_device, __u._M_device);
605 std::swap(_M_owns, __u._M_owns);
606 }
68a97d24 607
4db6bc0f
BK
608 mutex_type*
609 release()
610 {
611 mutex_type* __ret = _M_device;
7b800287 612 _M_device = 0;
68a97d24
BK
613 _M_owns = false;
614 return __ret;
615 }
616
4db6bc0f 617 bool
d3098c94
CF
618 owns_lock() const
619 { return _M_owns; }
68a97d24 620
d29d4507 621 explicit operator bool() const
d3098c94 622 { return owns_lock(); }
68a97d24 623
4db6bc0f 624 mutex_type*
68a97d24
BK
625 mutex() const
626 { return _M_device; }
627
628 private:
4db6bc0f
BK
629 mutex_type* _M_device;
630 bool _M_owns; // XXX use atomic_bool
68a97d24
BK
631 };
632
633 template<typename _Mutex>
7b800287
CF
634 inline void
635 swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y)
636 { __x.swap(__y); }
68a97d24 637
57cb79ef
CF
638 template<int _Idx>
639 struct __unlock_impl
640 {
641 template<typename... _Lock>
f7459b6c
BK
642 static void
643 __do_unlock(tuple<_Lock&...>& __locks)
644 {
57cb79ef
CF
645 std::get<_Idx>(__locks).unlock();
646 __unlock_impl<_Idx - 1>::__do_unlock(__locks);
647 }
648 };
f7459b6c 649
57cb79ef
CF
650 template<>
651 struct __unlock_impl<-1>
652 {
653 template<typename... _Lock>
f7459b6c
BK
654 static void
655 __do_unlock(tuple<_Lock&...>&)
656 { }
57cb79ef
CF
657 };
658
9b2b801a
JW
659 template<typename _Lock>
660 unique_lock<_Lock>
661 __try_to_lock(_Lock& __l)
662 { return unique_lock<_Lock>(__l, try_to_lock); }
663
57cb79ef
CF
664 template<int _Idx, bool _Continue = true>
665 struct __try_lock_impl
666 {
667 template<typename... _Lock>
9b2b801a
JW
668 static void
669 __do_try_lock(tuple<_Lock&...>& __locks, int& __idx)
f7459b6c 670 {
9b2b801a
JW
671 __idx = _Idx;
672 auto __lock = __try_to_lock(std::get<_Idx>(__locks));
673 if (__lock.owns_lock())
674 {
675 __try_lock_impl<_Idx + 1, _Idx + 2 < sizeof...(_Lock)>::
676 __do_try_lock(__locks, __idx);
677 if (__idx == -1)
678 __lock.release();
679 }
57cb79ef
CF
680 }
681 };
f7459b6c 682
57cb79ef
CF
683 template<int _Idx>
684 struct __try_lock_impl<_Idx, false>
685 {
686 template<typename... _Lock>
9b2b801a
JW
687 static void
688 __do_try_lock(tuple<_Lock&...>& __locks, int& __idx)
f7459b6c 689 {
9b2b801a
JW
690 __idx = _Idx;
691 auto __lock = __try_to_lock(std::get<_Idx>(__locks));
692 if (__lock.owns_lock())
693 {
694 __idx = -1;
695 __lock.release();
696 }
57cb79ef
CF
697 }
698 };
f7459b6c 699
57cb79ef
CF
700 /** @brief Generic try_lock.
701 * @param __l1 Meets Mutex requirements (try_lock() may throw).
702 * @param __l2 Meets Mutex requirements (try_lock() may throw).
703 * @param __l3 Meets Mutex requirements (try_lock() may throw).
f7459b6c 704 * @return Returns -1 if all try_lock() calls return true. Otherwise returns
57cb79ef
CF
705 * a 0-based index corresponding to the argument that returned false.
706 * @post Either all arguments are locked, or none will be.
707 *
708 * Sequentially calls try_lock() on each argument.
709 */
710 template<typename _Lock1, typename _Lock2, typename... _Lock3>
4db6bc0f 711 int
57cb79ef
CF
712 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
713 {
9b2b801a
JW
714 int __idx;
715 auto __locks = std::tie(__l1, __l2, __l3...);
716 __try
717 { __try_lock_impl<0>::__do_try_lock(__locks, __idx); }
718 __catch(...)
719 { }
720 return __idx;
57cb79ef 721 }
68a97d24 722
9b2b801a
JW
723 /** @brief Generic lock.
724 * @param __l1 Meets Mutex requirements (try_lock() may throw).
725 * @param __l2 Meets Mutex requirements (try_lock() may throw).
726 * @param __l3 Meets Mutex requirements (try_lock() may throw).
727 * @throw An exception thrown by an argument's lock() or try_lock() member.
728 * @post All arguments are locked.
729 *
730 * All arguments are locked via a sequence of calls to lock(), try_lock()
731 * and unlock(). If the call exits via an exception any locks that were
732 * obtained will be released.
733 */
4db6bc0f
BK
734 template<typename _L1, typename _L2, typename ..._L3>
735 void
9b2b801a
JW
736 lock(_L1& __l1, _L2& __l2, _L3&... __l3)
737 {
738 while (true)
739 {
740 unique_lock<_L1> __first(__l1);
741 int __idx;
742 auto __locks = std::tie(__l2, __l3...);
743 __try_lock_impl<0, sizeof...(_L3)>::__do_try_lock(__locks, __idx);
744 if (__idx == -1)
745 {
746 __first.release();
747 return;
748 }
749 }
750 }
68a97d24
BK
751
752 /// once_flag
4db6bc0f 753 struct once_flag
68a97d24 754 {
f7459b6c 755 private:
68a97d24 756 typedef __gthread_once_t __native_type;
f7459b6c 757 __native_type _M_once;
68a97d24 758
f7459b6c 759 public:
94a86be0 760 constexpr once_flag() : _M_once(__GTHREAD_ONCE_INIT) { }
f7459b6c 761
7b800287
CF
762 once_flag(const once_flag&) = delete;
763 once_flag& operator=(const once_flag&) = delete;
68a97d24 764
7b800287
CF
765 template<typename _Callable, typename... _Args>
766 friend void
1b3fad81 767 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args);
68a97d24
BK
768 };
769
7b800287
CF
770#ifdef _GLIBCXX_HAVE_TLS
771 extern __thread void* __once_callable;
772 extern __thread void (*__once_call)();
773
774 template<typename _Callable>
f7459b6c 775 inline void
99827523 776 __once_call_impl()
7b800287
CF
777 {
778 (*(_Callable*)__once_callable)();
779 }
780#else
781 extern function<void()> __once_functor;
99827523 782
efdb7347
JW
783 extern void
784 __set_once_functor_lock_ptr(unique_lock<mutex>*);
785
786 extern mutex&
787 __get_once_mutex();
7b800287
CF
788#endif
789
790 extern "C" void __once_proxy();
791
5b9daa7e 792 /// call_once
68a97d24 793 template<typename _Callable, typename... _Args>
4db6bc0f 794 void
1b3fad81 795 call_once(once_flag& __once, _Callable&& __f, _Args&&... __args)
68a97d24 796 {
7b800287 797#ifdef _GLIBCXX_HAVE_TLS
1b3fad81
JW
798 auto __bound_functor = std::bind<void>(std::forward<_Callable>(__f),
799 std::forward<_Args>(__args)...);
7b800287
CF
800 __once_callable = &__bound_functor;
801 __once_call = &__once_call_impl<decltype(__bound_functor)>;
802#else
efdb7347 803 unique_lock<mutex> __functor_lock(__get_once_mutex());
1b3fad81
JW
804 __once_functor = std::bind<void>(std::forward<_Callable>(__f),
805 std::forward<_Args>(__args)...);
efdb7347 806 __set_once_functor_lock_ptr(&__functor_lock);
7b800287 807#endif
f7459b6c 808
7b800287
CF
809 int __e = __gthread_once(&(__once._M_once), &__once_proxy);
810
cca36d72
PC
811#ifndef _GLIBCXX_HAVE_TLS
812 if (__functor_lock)
efdb7347 813 __set_once_functor_lock_ptr(0);
cca36d72
PC
814#endif
815
4db6bc0f 816 if (__e)
68a97d24
BK
817 __throw_system_error(__e);
818 }
5b9daa7e
BK
819
820 // @} group mutexes
53dc5044 821_GLIBCXX_END_NAMESPACE
68a97d24 822
7b800287
CF
823#endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
824
57317d2a 825#endif // __GXX_EXPERIMENTAL_CXX0X__
68a97d24 826
57317d2a 827#endif // _GLIBCXX_MUTEX