3 // Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 <c++0x_warning.h>
43 #include <type_traits>
45 #include <system_error>
46 #include <bits/functexcept.h>
47 #include <bits/gthr.h>
48 #include <bits/move.h> // for std::swap
50 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
55 * @defgroup mutexes Mutexes
56 * @ingroup concurrency
58 * Classes for mutex support.
65 typedef __gthread_mutex_t __native_type;
66 __native_type _M_mutex;
69 typedef __native_type* native_handle_type;
73 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
74 #ifdef __GTHREAD_MUTEX_INIT
75 __native_type __tmp = __GTHREAD_MUTEX_INIT;
78 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
82 mutex(const mutex&) = delete;
83 mutex& operator=(const mutex&) = delete;
88 int __e = __gthread_mutex_lock(&_M_mutex);
90 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
92 __throw_system_error(__e);
98 // XXX EINVAL, EAGAIN, EBUSY
99 return !__gthread_mutex_trylock(&_M_mutex);
105 // XXX EINVAL, EAGAIN, EPERM
106 __gthread_mutex_unlock(&_M_mutex);
111 { return &_M_mutex; }
115 class recursive_mutex
117 typedef __gthread_recursive_mutex_t __native_type;
118 __native_type _M_mutex;
121 typedef __native_type* native_handle_type;
125 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
126 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
127 __native_type __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
130 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
134 recursive_mutex(const recursive_mutex&) = delete;
135 recursive_mutex& operator=(const recursive_mutex&) = delete;
140 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
142 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
144 __throw_system_error(__e);
150 // XXX EINVAL, EAGAIN, EBUSY
151 return !__gthread_recursive_mutex_trylock(&_M_mutex);
157 // XXX EINVAL, EAGAIN, EBUSY
158 __gthread_recursive_mutex_unlock(&_M_mutex);
163 { return &_M_mutex; }
169 typedef __gthread_mutex_t __native_type;
171 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
172 typedef chrono::monotonic_clock __clock_t;
174 typedef chrono::high_resolution_clock __clock_t;
177 __native_type _M_mutex;
180 typedef __native_type* native_handle_type;
184 #ifdef __GTHREAD_MUTEX_INIT
185 __native_type __tmp = __GTHREAD_MUTEX_INIT;
188 __GTHREAD_MUTEX_INIT_FUNCTION(&_M_mutex);
192 timed_mutex(const timed_mutex&) = delete;
193 timed_mutex& operator=(const timed_mutex&) = delete;
198 int __e = __gthread_mutex_lock(&_M_mutex);
200 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
202 __throw_system_error(__e);
208 // XXX EINVAL, EAGAIN, EBUSY
209 return !__gthread_mutex_trylock(&_M_mutex);
212 template <class _Rep, class _Period>
214 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
215 { return __try_lock_for_impl(__rtime); }
217 template <class _Clock, class _Duration>
219 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
221 chrono::time_point<_Clock, chrono::seconds> __s =
222 chrono::time_point_cast<chrono::seconds>(__atime);
224 chrono::nanoseconds __ns =
225 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
227 __gthread_time_t __ts = {
228 static_cast<std::time_t>(__s.time_since_epoch().count()),
229 static_cast<long>(__ns.count())
232 return !__gthread_mutex_timedlock(&_M_mutex, &__ts);
238 // XXX EINVAL, EAGAIN, EBUSY
239 __gthread_mutex_unlock(&_M_mutex);
244 { return &_M_mutex; }
247 template<typename _Rep, typename _Period>
249 ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
250 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
252 __clock_t::time_point __atime = __clock_t::now()
253 + chrono::duration_cast<__clock_t::duration>(__rtime);
255 return try_lock_until(__atime);
258 template <typename _Rep, typename _Period>
260 !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
261 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
263 __clock_t::time_point __atime = __clock_t::now()
264 + ++chrono::duration_cast<__clock_t::duration>(__rtime);
266 return try_lock_until(__atime);
270 /// recursive_timed_mutex
271 class recursive_timed_mutex
273 typedef __gthread_recursive_mutex_t __native_type;
275 #ifdef _GLIBCXX_USE_CLOCK_MONOTONIC
276 typedef chrono::monotonic_clock __clock_t;
278 typedef chrono::high_resolution_clock __clock_t;
281 __native_type _M_mutex;
284 typedef __native_type* native_handle_type;
286 recursive_timed_mutex()
288 // XXX EAGAIN, ENOMEM, EPERM, EBUSY(may), EINVAL(may)
289 #ifdef __GTHREAD_RECURSIVE_MUTEX_INIT
290 __native_type __tmp = __GTHREAD_RECURSIVE_MUTEX_INIT;
293 __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION(&_M_mutex);
297 recursive_timed_mutex(const recursive_timed_mutex&) = delete;
298 recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
303 int __e = __gthread_recursive_mutex_lock(&_M_mutex);
305 // EINVAL, EAGAIN, EBUSY, EINVAL, EDEADLK(may)
307 __throw_system_error(__e);
313 // XXX EINVAL, EAGAIN, EBUSY
314 return !__gthread_recursive_mutex_trylock(&_M_mutex);
317 template <class _Rep, class _Period>
319 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
320 { return __try_lock_for_impl(__rtime); }
322 template <class _Clock, class _Duration>
324 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
326 chrono::time_point<_Clock, chrono::seconds> __s =
327 chrono::time_point_cast<chrono::seconds>(__atime);
329 chrono::nanoseconds __ns =
330 chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
332 __gthread_time_t __ts = {
333 static_cast<std::time_t>(__s.time_since_epoch().count()),
334 static_cast<long>(__ns.count())
337 return !__gthread_recursive_mutex_timedlock(&_M_mutex, &__ts);
343 // XXX EINVAL, EAGAIN, EBUSY
344 __gthread_recursive_mutex_unlock(&_M_mutex);
349 { return &_M_mutex; }
352 template<typename _Rep, typename _Period>
354 ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
355 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
357 __clock_t::time_point __atime = __clock_t::now()
358 + chrono::duration_cast<__clock_t::duration>(__rtime);
360 return try_lock_until(__atime);
363 template <typename _Rep, typename _Period>
365 !ratio_less_equal<__clock_t::period, _Period>::value, bool>::type
366 __try_lock_for_impl(const chrono::duration<_Rep, _Period>& __rtime)
368 __clock_t::time_point __atime = __clock_t::now()
369 + ++chrono::duration_cast<__clock_t::duration>(__rtime);
371 return try_lock_until(__atime);
375 /// Do not acquire ownership of the mutex.
376 struct defer_lock_t { };
378 /// Try to acquire ownership of the mutex without blocking.
379 struct try_to_lock_t { };
381 /// Assume the calling thread has already obtained mutex ownership
383 struct adopt_lock_t { };
385 extern const defer_lock_t defer_lock;
386 extern const try_to_lock_t try_to_lock;
387 extern const adopt_lock_t adopt_lock;
390 * @brief Thrown to indicate errors with lock operations.
392 * @ingroup exceptions
394 class lock_error : public exception
398 what() const throw();
401 /// @brief Scoped lock idiom.
402 // Acquire the mutex here with a constructor call, then release with
403 // the destructor call in accordance with RAII style.
404 template<typename _Mutex>
408 typedef _Mutex mutex_type;
410 explicit lock_guard(mutex_type& __m) : _M_device(__m)
411 { _M_device.lock(); }
413 lock_guard(mutex_type& __m, adopt_lock_t __a) : _M_device(__m)
414 { _M_device.lock(); }
417 { _M_device.unlock(); }
419 lock_guard(const lock_guard&) = delete;
420 lock_guard& operator=(const lock_guard&) = delete;
423 mutex_type& _M_device;
427 template<typename _Mutex>
431 typedef _Mutex mutex_type;
434 : _M_device(0), _M_owns(false)
437 explicit unique_lock(mutex_type& __m)
438 : _M_device(&__m), _M_owns(false)
444 unique_lock(mutex_type& __m, defer_lock_t)
445 : _M_device(&__m), _M_owns(false)
448 unique_lock(mutex_type& __m, try_to_lock_t)
449 : _M_device(&__m), _M_owns(_M_device->try_lock())
452 unique_lock(mutex_type& __m, adopt_lock_t)
453 : _M_device(&__m), _M_owns(true)
455 // XXX calling thread owns mutex
458 template<typename _Clock, typename _Duration>
459 unique_lock(mutex_type& __m,
460 const chrono::time_point<_Clock, _Duration>& __atime)
461 : _M_device(&__m), _M_owns(_M_device->try_lock_until(__atime))
464 template<typename _Rep, typename _Period>
465 unique_lock(mutex_type& __m,
466 const chrono::duration<_Rep, _Period>& __rtime)
467 : _M_device(&__m), _M_owns(_M_device->try_lock_for(__rtime))
476 unique_lock(const unique_lock&) = delete;
477 unique_lock& operator=(const unique_lock&) = delete;
479 unique_lock(unique_lock&& __u)
480 : _M_device(__u._M_device), _M_owns(__u._M_owns)
486 unique_lock& operator=(unique_lock&& __u)
491 unique_lock(std::move(__u)).swap(*this);
503 __throw_system_error(int(errc::operation_not_permitted));
505 __throw_system_error(int(errc::resource_deadlock_would_occur));
517 __throw_system_error(int(errc::operation_not_permitted));
519 __throw_system_error(int(errc::resource_deadlock_would_occur));
522 _M_owns = _M_device->try_lock();
527 template<typename _Clock, typename _Duration>
529 try_lock_until(const chrono::time_point<_Clock, _Duration>& __atime)
532 __throw_system_error(int(errc::operation_not_permitted));
534 __throw_system_error(int(errc::resource_deadlock_would_occur));
537 _M_owns = _M_device->try_lock_until(__atime);
542 template<typename _Rep, typename _Period>
544 try_lock_for(const chrono::duration<_Rep, _Period>& __rtime)
547 __throw_system_error(int(errc::operation_not_permitted));
549 __throw_system_error(int(errc::resource_deadlock_would_occur));
552 _M_owns = _M_device->try_lock_for(__rtime);
561 __throw_system_error(int(errc::operation_not_permitted));
570 swap(unique_lock&& __u)
572 std::swap(_M_device, __u._M_device);
573 std::swap(_M_owns, __u._M_owns);
579 mutex_type* __ret = _M_device;
589 /* explicit */ operator bool () const
590 { return owns_lock(); }
594 { return _M_device; }
597 mutex_type* _M_device;
598 bool _M_owns; // XXX use atomic_bool
601 template<typename _Mutex>
603 swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>& __y)
606 template<typename _Mutex>
608 swap(unique_lock<_Mutex>&& __x, unique_lock<_Mutex>& __y)
611 template<typename _Mutex>
613 swap(unique_lock<_Mutex>& __x, unique_lock<_Mutex>&& __y)
619 template<typename... _Lock>
621 __do_unlock(tuple<_Lock&...>& __locks)
623 std::get<_Idx>(__locks).unlock();
624 __unlock_impl<_Idx - 1>::__do_unlock(__locks);
629 struct __unlock_impl<-1>
631 template<typename... _Lock>
633 __do_unlock(tuple<_Lock&...>&)
637 template<int _Idx, bool _Continue = true>
638 struct __try_lock_impl
640 template<typename... _Lock>
642 __do_try_lock(tuple<_Lock&...>& __locks)
644 if(std::get<_Idx>(__locks).try_lock())
646 return __try_lock_impl<_Idx + 1,
647 _Idx + 2 < sizeof...(_Lock)>::__do_try_lock(__locks);
651 __unlock_impl<_Idx>::__do_unlock(__locks);
658 struct __try_lock_impl<_Idx, false>
660 template<typename... _Lock>
662 __do_try_lock(tuple<_Lock&...>& __locks)
664 if(std::get<_Idx>(__locks).try_lock())
668 __unlock_impl<_Idx>::__do_unlock(__locks);
674 /** @brief Generic try_lock.
675 * @param __l1 Meets Mutex requirements (try_lock() may throw).
676 * @param __l2 Meets Mutex requirements (try_lock() may throw).
677 * @param __l3 Meets Mutex requirements (try_lock() may throw).
678 * @return Returns -1 if all try_lock() calls return true. Otherwise returns
679 * a 0-based index corresponding to the argument that returned false.
680 * @post Either all arguments are locked, or none will be.
682 * Sequentially calls try_lock() on each argument.
684 template<typename _Lock1, typename _Lock2, typename... _Lock3>
686 try_lock(_Lock1& __l1, _Lock2& __l2, _Lock3&... __l3)
688 tuple<_Lock1&, _Lock2&, _Lock3&...> __locks(__l1, __l2, __l3...);
689 return __try_lock_impl<0>::__do_try_lock(__locks);
693 template<typename _L1, typename _L2, typename ..._L3>
695 lock(_L1&, _L2&, _L3&...);
701 typedef __gthread_once_t __native_type;
702 __native_type _M_once;
707 __native_type __tmp = __GTHREAD_ONCE_INIT;
711 once_flag(const once_flag&) = delete;
712 once_flag& operator=(const once_flag&) = delete;
714 template<typename _Callable, typename... _Args>
716 call_once(once_flag& __once, _Callable __f, _Args&&... __args);
719 #ifdef _GLIBCXX_HAVE_TLS
720 extern __thread void* __once_callable;
721 extern __thread void (*__once_call)();
723 template<typename _Callable>
727 (*(_Callable*)__once_callable)();
730 extern function<void()> __once_functor;
733 __set_once_functor_lock_ptr(unique_lock<mutex>*);
739 extern "C" void __once_proxy();
742 template<typename _Callable, typename... _Args>
744 call_once(once_flag& __once, _Callable __f, _Args&&... __args)
746 #ifdef _GLIBCXX_HAVE_TLS
747 auto __bound_functor = bind(__f, __args...);
748 __once_callable = &__bound_functor;
749 __once_call = &__once_call_impl<decltype(__bound_functor)>;
751 unique_lock<mutex> __functor_lock(__get_once_mutex());
752 __once_functor = bind(__f, __args...);
753 __set_once_functor_lock_ptr(&__functor_lock);
756 int __e = __gthread_once(&(__once._M_once), &__once_proxy);
758 #ifndef _GLIBCXX_HAVE_TLS
760 __set_once_functor_lock_ptr(0);
764 __throw_system_error(__e);
770 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
772 #endif // __GXX_EXPERIMENTAL_CXX0X__
774 #endif // _GLIBCXX_MUTEX