Home | History | Annotate | Download | only in include
      1 // <condition_variable> -*- C++ -*-
      2 
      3 // Copyright (C) 2008, 2009, 2010 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file include/condition_variable
     26  *  This is a Standard C++ Library header.
     27  */
     28 
     29 #ifndef _GLIBCXX_CONDITION_VARIABLE
     30 #define _GLIBCXX_CONDITION_VARIABLE 1
     31 
     32 #pragma GCC system_header
     33 
     34 #ifndef __GXX_EXPERIMENTAL_CXX0X__
     35 # include <bits/c++0x_warning.h>
     36 #else
     37 
     38 #include <chrono>
     39 #include <mutex> // unique_lock
     40 
     41 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1)
     42 
     43 namespace std _GLIBCXX_VISIBILITY(default)
     44 {
     45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     46 
     47   /**
     48    * @defgroup condition_variables Condition Variables
     49    * @ingroup concurrency
     50    *
     51    * Classes for condition_variable support.
     52    * @{
     53    */
     54 
     55   /// cv_status
     56   enum class cv_status { no_timeout, timeout };
     57   
     58   /// condition_variable
     59   class condition_variable
     60   {
     61     typedef chrono::system_clock	__clock_t;
     62     typedef __gthread_cond_t		__native_type;
     63     __native_type			_M_cond;
     64 
     65   public:
     66     typedef __native_type* 		native_handle_type;
     67 
     68     condition_variable() throw ();
     69     ~condition_variable() throw ();
     70 
     71     condition_variable(const condition_variable&) = delete;
     72     condition_variable& operator=(const condition_variable&) = delete;
     73 
     74     void
     75     notify_one();
     76 
     77     void
     78     notify_all();
     79 
     80     void
     81     wait(unique_lock<mutex>& __lock);
     82 
     83     template<typename _Predicate>
     84       void
     85       wait(unique_lock<mutex>& __lock, _Predicate __p)
     86       {
     87 	while (!__p())
     88 	  wait(__lock);
     89       }
     90 
     91     template<typename _Duration>
     92       cv_status
     93       wait_until(unique_lock<mutex>& __lock,
     94 		 const chrono::time_point<__clock_t, _Duration>& __atime)
     95       { return __wait_until_impl(__lock, __atime); }
     96 
     97     template<typename _Clock, typename _Duration>
     98       cv_status
     99       wait_until(unique_lock<mutex>& __lock,
    100 		 const chrono::time_point<_Clock, _Duration>& __atime)
    101       {
    102 	// DR 887 - Sync unknown clock to known clock.
    103 	const typename _Clock::time_point __c_entry = _Clock::now();
    104 	const __clock_t::time_point __s_entry = __clock_t::now();
    105 	const chrono::nanoseconds __delta = __atime - __c_entry;
    106 	const __clock_t::time_point __s_atime = __s_entry + __delta;
    107 
    108 	return __wait_until_impl(__lock, __s_atime);
    109       }
    110 
    111     template<typename _Clock, typename _Duration, typename _Predicate>
    112       bool
    113       wait_until(unique_lock<mutex>& __lock,
    114 		 const chrono::time_point<_Clock, _Duration>& __atime,
    115 		 _Predicate __p)
    116       {
    117 	while (!__p())
    118 	  if (wait_until(__lock, __atime) == cv_status::timeout)
    119 	    return __p();
    120 	return true;
    121       }
    122 
    123     template<typename _Rep, typename _Period>
    124       cv_status
    125       wait_for(unique_lock<mutex>& __lock,
    126 	       const chrono::duration<_Rep, _Period>& __rtime)
    127       { return wait_until(__lock, __clock_t::now() + __rtime); }
    128 
    129     template<typename _Rep, typename _Period, typename _Predicate>
    130       bool
    131       wait_for(unique_lock<mutex>& __lock,
    132 	       const chrono::duration<_Rep, _Period>& __rtime,
    133 	       _Predicate __p)
    134       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
    135 
    136     native_handle_type
    137     native_handle()
    138     { return &_M_cond; }
    139 
    140   private:
    141     template<typename _Clock, typename _Duration>
    142       cv_status
    143       __wait_until_impl(unique_lock<mutex>& __lock,
    144 			const chrono::time_point<_Clock, _Duration>& __atime)
    145       {
    146 	chrono::time_point<__clock_t, chrono::seconds> __s =
    147 	  chrono::time_point_cast<chrono::seconds>(__atime);
    148 
    149 	chrono::nanoseconds __ns =
    150 	  chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
    151 
    152 	__gthread_time_t __ts =
    153 	  {
    154 	    static_cast<std::time_t>(__s.time_since_epoch().count()),
    155 	    static_cast<long>(__ns.count())
    156 	  };
    157 
    158 	__gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
    159 				 &__ts);
    160 
    161 	return (_Clock::now() < __atime
    162 		? cv_status::no_timeout : cv_status::timeout);
    163       }
    164   };
    165 
    166   /// condition_variable_any
    167   // Like above, but mutex is not required to have try_lock.
    168   class condition_variable_any
    169   {
    170     typedef chrono::system_clock	__clock_t;
    171     condition_variable			_M_cond;
    172     mutex				_M_mutex;
    173 
    174   public:
    175     typedef condition_variable::native_handle_type	native_handle_type;
    176 
    177     condition_variable_any() throw ();
    178     ~condition_variable_any() throw ();
    179 
    180     condition_variable_any(const condition_variable_any&) = delete;
    181     condition_variable_any& operator=(const condition_variable_any&) = delete;
    182 
    183     void
    184     notify_one()
    185     {
    186       lock_guard<mutex> __lock(_M_mutex);
    187       _M_cond.notify_one();
    188     }
    189 
    190     void
    191     notify_all()
    192     {
    193       lock_guard<mutex> __lock(_M_mutex);
    194       _M_cond.notify_all();
    195     }
    196 
    197     template<typename _Lock>
    198       void
    199       wait(_Lock& __lock)
    200       {
    201 	// scoped unlock - unlocks in ctor, re-locks in dtor
    202 	struct _Unlock {
    203 	  explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); }
    204 	  ~_Unlock() noexcept(false)
    205 	  {
    206 	    if (uncaught_exception())
    207 	      __try { _M_lock.lock(); } __catch(...) { }
    208 	    else
    209 	      _M_lock.lock();
    210 	  }
    211 	  _Lock& _M_lock;
    212 	};
    213 
    214 	unique_lock<mutex> __my_lock(_M_mutex);
    215 	_Unlock __unlock(__lock);
    216 	// _M_mutex must be unlocked before re-locking __lock so move
    217 	// ownership of _M_mutex lock to an object with shorter lifetime.
    218 	unique_lock<mutex> __my_lock2(std::move(__my_lock));
    219 	_M_cond.wait(__my_lock2);
    220       }
    221       
    222 
    223     template<typename _Lock, typename _Predicate>
    224       void
    225       wait(_Lock& __lock, _Predicate __p)
    226       {
    227 	while (!__p())
    228 	  wait(__lock);
    229       }
    230 
    231     template<typename _Lock, typename _Clock, typename _Duration>
    232       cv_status
    233       wait_until(_Lock& __lock,
    234 		 const chrono::time_point<_Clock, _Duration>& __atime)
    235       {
    236         unique_lock<mutex> __my_lock(_M_mutex);
    237         __lock.unlock();
    238         cv_status __status = _M_cond.wait_until(__my_lock, __atime);
    239         __lock.lock();
    240         return __status;
    241       }
    242 
    243     template<typename _Lock, typename _Clock,
    244 	     typename _Duration, typename _Predicate>
    245       bool
    246       wait_until(_Lock& __lock,
    247 		 const chrono::time_point<_Clock, _Duration>& __atime,
    248 		 _Predicate __p)
    249       {
    250 	while (!__p())
    251 	  if (wait_until(__lock, __atime) == cv_status::timeout)
    252 	    return __p();
    253 	return true;
    254       }
    255 
    256     template<typename _Lock, typename _Rep, typename _Period>
    257       cv_status
    258       wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime)
    259       { return wait_until(__lock, __clock_t::now() + __rtime); }
    260 
    261     template<typename _Lock, typename _Rep,
    262 	     typename _Period, typename _Predicate>
    263       bool
    264       wait_for(_Lock& __lock,
    265 	       const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p)
    266       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
    267 
    268     native_handle_type
    269     native_handle()
    270     { return _M_cond.native_handle(); }
    271   };
    272 
    273   // @} group condition_variables
    274 _GLIBCXX_END_NAMESPACE_VERSION
    275 } // namespace
    276 
    277 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
    278 
    279 #endif // __GXX_EXPERIMENTAL_CXX0X__
    280 
    281 #endif // _GLIBCXX_CONDITION_VARIABLE
    282