3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 // Free Software Foundation, Inc.
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
9 // Free Software Foundation; either version 3, or (at your option)
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.
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.
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/>.
27 * This is a Standard C++ Library header.
30 #ifndef _GLIBCXX_MUTEX
31 #define _GLIBCXX_MUTEX 1
33 #pragma GCC system_header
35 #ifndef __GXX_EXPERIMENTAL_CXX0X__
36 # include <bits/c++0x_warning.h>
42 #include <type_traits>
44 #include <system_error>
45 #include <bits/functexcept.h>
46 #include <bits/gthr.h>
47 #include <bits/move.h> // for std::swap
49 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
54 * @defgroup mutexes Mutexes
55 * @ingroup concurrency
57 * Classes for mutex support.
64 typedef __gthread_mutex_t __native_type;
65 __native_type _M_mutex;
68 typedef __native_type* native_handle_type;
72 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
73 #ifdef __GTHREAD_MUTEX_INIT
74 __native_type __tmp = __GTHREAD_MUTEX_INIT;
77 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
81 mutex(const mutex&) = delete;
82 mutex& operator=(const mutex&) = delete;
87 int __e = __gthread_mutex_lock(&_M_mutex);
89 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
91 __throw_system_error(__e);
97 // XXX EINVAL, EAGAIN, EBUSY
98 return !__gthread_mutex_trylock(&_M_mutex);
104 // XXX EINVAL, EAGAIN, EPERM
105 __gthread_mutex_unlock(&_M_mutex);
110 { return &_M_mutex; }
114 class recursive_mutex
116 typedef __gthread_recursive_mutex_t __native_type;
117 __native_type _M_mutex;
120 typedef __native_type* native_handle_type;
124 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
125 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
126 __native_type __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
129 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
133 recursive_mutex(const recursive_mutex&) = delete;
134 recursive_mutex& operator=(const recursive_mutex&) = delete;
139 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
141 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
143 __throw_system_error(__e);
149 // XXX EINVAL, EAGAIN, EBUSY
150 return !__gthread_recursive_mutex_trylock(&_M_mutex);
156 // XXX EINVAL, EAGAIN, EBUSY
157 __gthread_recursive_mutex_unlock(&_M_mutex);
162 { return &_M_mutex; }
168 typedef __gthread_mutex_t __native_type;
170 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
171 typedef chrono::monotonic_clock __clock_t;
173 typedef chrono::high_resolution_clock __clock_t;
176 __native_type _M_mutex;
179 typedef __native_type* native_handle_type;
183 #ifdef __GTHREAD_MUTEX_INIT
184 __native_type __tmp = __GTHREAD_MUTEX_INIT;
187 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
191 timed_mutex(const timed_mutex&) = delete;
192 timed_mutex& operator=(const timed_mutex&) = delete;
197 int __e = __gthread_mutex_lock(&_M_mutex);
199 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
201 __throw_system_error(__e);
207 // XXX EINVAL, EAGAIN, EBUSY
208 return !__gthread_mutex_trylock(&_M_mutex);
211 template <class _Rep, class _Period>
213 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
214 { return __try_lock_for_impl(__rtime); }
216 template <class _Clock, class _Duration>
218 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
220 chrono::time_point<_Clock, chrono::seconds> __s =
221 chrono::time_point_cast<chrono::seconds>(__atime);
223 chrono::nanoseconds __ns =
224 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
226 __gthread_time_t __ts = {
227 static_cast<std::time_t>(__s.time_since_epoch().count()),
228 static_cast<long>(__ns.count())
231 return !__gthread_mutex_timedlock(&_M_mutex, &__ts);
237 // XXX EINVAL, EAGAIN, EBUSY
238 __gthread_mutex_unlock(&_M_mutex);
243 { return &_M_mutex; }
246 template<typename _Rep, typename _Period>
248 ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
249 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
251 __clock_t::time_point __atime = __clock_t::now()
252 + chrono::duration_cast<__clock_t::duration>(__rtime);
254 return try_lock_until(__atime);
257 template <typename _Rep, typename _Period>
259 !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
260 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
262 __clock_t::time_point __atime = __clock_t::now()
263 + ++chrono::duration_cast<__clock_t::duration>(__rtime);
265 return try_lock_until(__atime);
269 /// recursive_timed_mutex
270 class recursive_timed_mutex
272 typedef __gthread_recursive_mutex_t __native_type;
274 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
275 typedef chrono::monotonic_clock __clock_t;
277 typedef chrono::high_resolution_clock __clock_t;
280 __native_type _M_mutex;
283 typedef __native_type* native_handle_type;
285 recursive_timed_mutex()
287 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
288 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
289 __native_type __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
292 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
296 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
297 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
302 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
304 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
306 __throw_system_error(__e);
312 // XXX EINVAL, EAGAIN, EBUSY
313 return !__gthread_recursive_mutex_trylock(&_M_mutex);
316 template <class _Rep, class _Period>
318 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
319 { return __try_lock_for_impl(__rtime); }
321 template <class _Clock, class _Duration>
323 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
325 chrono::time_point<_Clock, chrono::seconds> __s =
326 chrono::time_point_cast<chrono::seconds>(__atime);
328 chrono::nanoseconds __ns =
329 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
331 __gthread_time_t __ts = {
332 static_cast<std::time_t>(__s.time_since_epoch().count()),
333 static_cast<long>(__ns.count())
336 return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts);
342 // XXX EINVAL, EAGAIN, EBUSY
343 __gthread_recursive_mutex_unlock(&_M_mutex);
348 { return &_M_mutex; }
351 template<typename _Rep, typename _Period>
353 ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
354 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
356 __clock_t::time_point __atime = __clock_t::now()
357 + chrono::duration_cast<__clock_t::duration>(__rtime);
359 return try_lock_until(__atime);
362 template <typename _Rep, typename _Period>
364 !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
365 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
367 __clock_t::time_point __atime = __clock_t::now()
368 + ++chrono::duration_cast<__clock_t::duration>(__rtime);
370 return try_lock_until(__atime);
374 /// Do not acquire ownership of the mutex.
375 struct defer_lock_t { };
377 /// Try to acquire ownership of the mutex without blocking.
378 struct try_to_lock_t { };
380 /// Assume the calling thread has already obtained mutex ownership
382 struct adopt_lock_t { };
384 extern const defer_lock_t defer_lock;
385 extern const try_to_lock_t try_to_lock;
386 extern const adopt_lock_t adopt_lock;
388 /// @brief Scoped lock idiom.
389 // Acquire the mutex here with a constructor call, then release with
390 // the destructor call in accordance with RAII style.
391 template<typename _Mutex>
395 typedef _Mutex mutex_type;
397 explicit lock_guard(mutex_type& __m) : _M_device(__m)
398 { _M_device.lock(); }
400 lock_guard(mutex_type& __m, adopt_lock_t) : _M_device(__m)
401 { } // calling thread owns mutex
404 { _M_device.unlock(); }
406 lock_guard(const lock_guard&) = delete;
407 lock_guard& operator=(const lock_guard&) = delete;
410 mutex_type& _M_device;
414 template<typename _Mutex>
418 typedef _Mutex mutex_type;
421 : _M_device(0), _M_owns(false)
424 explicit unique_lock(mutex_type& __m)
425 : _M_device(&__m), _M_owns(false)
431 unique_lock(mutex_type& __m, defer_lock_t)
432 : _M_device(&__m), _M_owns(false)
435 unique_lock(mutex_type& __m, try_to_lock_t)
436 : _M_device(&__m), _M_owns(_M_device->try_lock())
439 unique_lock(mutex_type& __m, adopt_lock_t)
440 : _M_device(&__m), _M_owns(true)
442 // XXX calling thread owns mutex
445 template<typename _Clock, typename _Duration>
446 unique_lock(mutex_type& __m,
447 const chrono::time_point<_Clock, _Duration>& __atime)
448 : _M_device(&__m), _M_owns(_M_device->try_lock_until(__atime))
451 template<typename _Rep, typename _Period>
452 unique_lock(mutex_type& __m,
453 const chrono::duration<_Rep, _Period>& __rtime)
454 : _M_device(&__m), _M_owns(_M_device->try_lock_for(__rtime))
463 unique_lock(const unique_lock&) = delete;
464 unique_lock& operator=(const unique_lock&) = delete;
466 unique_lock(unique_lock&& __u)
467 : _M_device(__u._M_device), _M_owns(__u._M_owns)
473 unique_lock& operator=(unique_lock&& __u)
478 unique_lock(std::move(__u)).swap(*this);
490 __throw_system_error(int(errc::operation_not_permitted));
492 __throw_system_error(int(errc::resource_deadlock_would_occur));
504 __throw_system_error(int(errc::operation_not_permitted));
506 __throw_system_error(int(errc::resource_deadlock_would_occur));
509 _M_owns = _M_device->try_lock();
514 template<typename _Clock, typename _Duration>
516 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
519 __throw_system_error(int(errc::operation_not_permitted));
521 __throw_system_error(int(errc::resource_deadlock_would_occur));
524 _M_owns = _M_device->try_lock_until(__atime);
529 template<typename _Rep, typename _Period>
531 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
534 __throw_system_error(int(errc::operation_not_permitted));
536 __throw_system_error(int(errc::resource_deadlock_would_occur));
539 _M_owns = _M_device->try_lock_for(__rtime);
548 __throw_system_error(int(errc::operation_not_permitted));
557 swap(unique_lock& __u)
559 std::swap(_M_device, __u._M_device);
560 std::swap(_M_owns, __u._M_owns);
566 mutex_type* __ret = _M_device;
576 explicit operator bool() const
577 { return owns_lock(); }
581 { return _M_device; }
584 mutex_type* _M_device;
585 bool _M_owns; // XXX use atomic_bool
588 template<typename _Mutex>
590 swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y)
596 template<typename... _Lock>
598 __do_unlock(tuple<_Lock&...>& __locks)
600 std::get<_Idx>(__locks).unlock();
601 __unlock_impl<_Idx - 1>::__do_unlock(__locks);
606 struct __unlock_impl<-1>
608 template<typename... _Lock>
610 __do_unlock(tuple<_Lock&...>&)
614 template<int _Idx, bool _Continue = true>
615 struct __try_lock_impl
617 template<typename... _Lock>
619 __do_try_lock(tuple<_Lock&...>& __locks)
621 if(std::get<_Idx>(__locks).try_lock())
623 return __try_lock_impl<_Idx + 1,
624 _Idx + 2 < sizeof...(_Lock)>::__do_try_lock(__locks);
628 __unlock_impl<_Idx>::__do_unlock(__locks);
635 struct __try_lock_impl<_Idx, false>
637 template<typename... _Lock>
639 __do_try_lock(tuple<_Lock&...>& __locks)
641 if(std::get<_Idx>(__locks).try_lock())
645 __unlock_impl<_Idx>::__do_unlock(__locks);
651 /** @brief Generic try_lock.
652 * @param __l1 Meets Mutex requirements (try_lock() may throw).
653 * @param __l2 Meets Mutex requirements (try_lock() may throw).
654 * @param __l3 Meets Mutex requirements (try_lock() may throw).
655 * @return Returns -1 if all try_lock() calls return true. Otherwise returns
656 * a 0-based index corresponding to the argument that returned false.
657 * @post Either all arguments are locked, or none will be.
659 * Sequentially calls try_lock() on each argument.
661 template<typename _Lock1, typename _Lock2, typename... _Lock3>
663 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
665 tuple<_Lock1&, _Lock2&, _Lock3&...> __locks(__l1, __l2, __l3...);
666 return __try_lock_impl<0>::__do_try_lock(__locks);
670 template<typename _L1, typename _L2, typename ..._L3>
672 lock(_L1&, _L2&, _L3&...);
678 typedef __gthread_once_t __native_type;
679 __native_type _M_once;
684 __native_type __tmp = __GTHREAD_ONCE_INIT;
688 once_flag(const once_flag&) = delete;
689 once_flag& operator=(const once_flag&) = delete;
691 template<typename _Callable, typename... _Args>
693 call_once(once_flag& __once, _Callable __f, _Args&&... __args);
696 #ifdef _GLIBCXX_HAVE_TLS
697 extern __thread void* __once_callable;
698 extern __thread void (*__once_call)();
700 template<typename _Callable>
704 (*(_Callable*)__once_callable)();
707 extern function<void()> __once_functor;
710 __set_once_functor_lock_ptr(unique_lock<mutex>*);
716 extern "C" void __once_proxy();
719 template<typename _Callable, typename... _Args>
721 call_once(once_flag& __once, _Callable __f, _Args&&... __args)
723 #ifdef _GLIBCXX_HAVE_TLS
724 auto __bound_functor = std::bind<void>(__f, __args...);
725 __once_callable = &__bound_functor;
726 __once_call = &__once_call_impl<decltype(__bound_functor)>;
728 unique_lock<mutex> __functor_lock(__get_once_mutex());
729 __once_functor = std::bind<void>(__f, __args...);
730 __set_once_functor_lock_ptr(&__functor_lock);
733 int __e = __gthread_once(&(__once._M_once), &__once_proxy);
735 #ifndef _GLIBCXX_HAVE_TLS
737 __set_once_functor_lock_ptr(0);
741 __throw_system_error(__e);
747 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
749 #endif // __GXX_EXPERIMENTAL_CXX0X__
751 #endif // _GLIBCXX_MUTEX