Home | History | Annotate | Download | only in include
      1 // <condition_variable> -*- C++ -*-
      2 
      3 // Copyright (C) 2008, 2009 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 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 <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
     44 {
     45   /**
     46    * @defgroup condition_variables Condition Variables
     47    * @ingroup concurrency
     48    *
     49    * Classes for condition_variable support.
     50    * @{
     51    */
     52 
     53   /// condition_variable
     54   class condition_variable
     55   {
     56     typedef chrono::system_clock	__clock_t;
     57     typedef __gthread_cond_t		__native_type;
     58     __native_type			_M_cond;
     59 
     60   public:
     61     typedef __native_type* 		native_handle_type;
     62 
     63     condition_variable();
     64     ~condition_variable();
     65 
     66     condition_variable(const condition_variable&) = delete;
     67     condition_variable& operator=(const condition_variable&) = delete;
     68 
     69     void
     70     notify_one();
     71 
     72     void
     73     notify_all();
     74 
     75     void
     76     wait(unique_lock<mutex>& __lock);
     77 
     78     template<typename _Predicate>
     79       void
     80       wait(unique_lock<mutex>& __lock, _Predicate __p)
     81       {
     82 	while (!__p())
     83 	  wait(__lock);
     84       }
     85 
     86     template<typename _Duration>
     87       bool
     88       wait_until(unique_lock<mutex>& __lock,
     89 		 const chrono::time_point<__clock_t, _Duration>& __atime)
     90       { return __wait_until_impl(__lock, __atime); }
     91 
     92     template<typename _Clock, typename _Duration>
     93       bool
     94       wait_until(unique_lock<mutex>& __lock,
     95 		 const chrono::time_point<_Clock, _Duration>& __atime)
     96       {
     97 	// DR 887 - Sync unknown clock to known clock.
     98 	typename _Clock::time_point __c_entry = _Clock::now();
     99 	__clock_t::time_point __s_entry = __clock_t::now();
    100 	chrono::nanoseconds __delta = __atime - __c_entry;
    101 	__clock_t::time_point __s_atime = __s_entry + __delta;
    102 
    103 	return __wait_until_impl(__lock, __s_atime);
    104       }
    105 
    106     template<typename _Clock, typename _Duration, typename _Predicate>
    107       bool
    108       wait_until(unique_lock<mutex>& __lock,
    109 		 const chrono::time_point<_Clock, _Duration>& __atime,
    110 		 _Predicate __p)
    111       {
    112 	while (!__p())
    113 	  if (!wait_until(__lock, __atime))
    114 	    return __p();
    115 
    116 	return true;
    117       }
    118 
    119     template<typename _Rep, typename _Period>
    120       bool
    121       wait_for(unique_lock<mutex>& __lock,
    122 	       const chrono::duration<_Rep, _Period>& __rtime)
    123       { return wait_until(__lock, __clock_t::now() + __rtime); }
    124 
    125     template<typename _Rep, typename _Period, typename _Predicate>
    126       bool
    127       wait_for(unique_lock<mutex>& __lock,
    128 	       const chrono::duration<_Rep, _Period>& __rtime,
    129 	       _Predicate __p)
    130       { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); }
    131 
    132     native_handle_type
    133     native_handle()
    134     { return &_M_cond; }
    135 
    136   private:
    137     template<typename _Clock, typename _Duration>
    138       bool
    139       __wait_until_impl(unique_lock<mutex>& __lock,
    140 			const chrono::time_point<_Clock, _Duration>& __atime)
    141       {
    142 	chrono::time_point<__clock_t, chrono::seconds> __s =
    143 	  chrono::time_point_cast<chrono::seconds>(__atime);
    144 
    145 	chrono::nanoseconds __ns =
    146 	  chrono::duration_cast<chrono::nanoseconds>(__atime - __s);
    147 
    148 	__gthread_time_t __ts =
    149 	  {
    150 	    static_cast<std::time_t>(__s.time_since_epoch().count()),
    151 	    static_cast<long>(__ns.count())
    152 	  };
    153 
    154 	__gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(),
    155 				 &__ts);
    156 
    157 	return _Clock::now() < __atime;
    158       }
    159   };
    160 
    161   /// condition_variable_any
    162   // Like above, only mutex may not have try_lock.
    163   class condition_variable_any
    164   {
    165     typedef __gthread_cond_t		__native_type;
    166     __native_type			_M_cond;
    167 
    168   public:
    169     typedef __native_type* 		native_handle_type;
    170 
    171     condition_variable_any();
    172     ~condition_variable_any();
    173 
    174     condition_variable_any(const condition_variable_any&) = delete;
    175     condition_variable_any& operator=(const condition_variable_any&) = delete;
    176 
    177     void
    178     notify_one();
    179 
    180     void
    181     notify_all();
    182 
    183     template<typename _Lock>
    184       void
    185       wait(_Lock& __lock);
    186 
    187     template<typename _Lock, typename _Predicate>
    188       void
    189       wait(_Lock& __lock, _Predicate __p);
    190 
    191     template<typename _Lock, typename _Clock, typename _Duration>
    192       bool
    193       wait_until(_Lock& __lock,
    194 		 const chrono::time_point<_Clock, _Duration>& __atime);
    195 
    196     template<typename _Lock, typename _Clock,
    197 	     typename _Duration, typename _Predicate>
    198       bool
    199       wait_until(_Lock& __lock,
    200 		 const chrono::time_point<_Clock, _Duration>& __atime,
    201 		 _Predicate __p);
    202 
    203     template<typename _Lock, typename _Rep, typename _Period>
    204       bool
    205       wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime);
    206 
    207     template<typename _Lock, typename _Rep,
    208 	     typename _Period, typename _Predicate>
    209       bool
    210       wait_for(_Lock& __lock,
    211 	       const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p);
    212 
    213     native_handle_type
    214     native_handle()
    215     { return &_M_cond; }
    216   };
    217 
    218   // @} group condition_variables
    219 }
    220 
    221 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1
    222 
    223 #endif // __GXX_EXPERIMENTAL_CXX0X__
    224 
    225 #endif // _GLIBCXX_CONDITION_VARIABLE
    226