libstdc++
|
00001 // <condition_variable> -*- C++ -*- 00002 00003 // Copyright (C) 2008, 2009 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file condition_variable 00026 * This is a Standard C++ Library header. 00027 */ 00028 00029 #ifndef _GLIBCXX_CONDITION_VARIABLE 00030 #define _GLIBCXX_CONDITION_VARIABLE 1 00031 00032 #pragma GCC system_header 00033 00034 #ifndef __GXX_EXPERIMENTAL_CXX0X__ 00035 # include <c++0x_warning.h> 00036 #else 00037 00038 #include <chrono> 00039 #include <mutex> // unique_lock 00040 00041 #if defined(_GLIBCXX_HAS_GTHREADS) && defined(_GLIBCXX_USE_C99_STDINT_TR1) 00042 00043 namespace std 00044 { 00045 /** 00046 * @defgroup condition_variables Condition Variables 00047 * @ingroup concurrency 00048 * 00049 * Classes for condition_variable support. 00050 * @{ 00051 */ 00052 00053 /// condition_variable 00054 class condition_variable 00055 { 00056 typedef chrono::system_clock __clock_t; 00057 typedef __gthread_cond_t __native_type; 00058 __native_type _M_cond; 00059 00060 public: 00061 typedef __native_type* native_handle_type; 00062 00063 condition_variable(); 00064 ~condition_variable(); 00065 00066 condition_variable(const condition_variable&) = delete; 00067 condition_variable& operator=(const condition_variable&) = delete; 00068 00069 void 00070 notify_one(); 00071 00072 void 00073 notify_all(); 00074 00075 void 00076 wait(unique_lock<mutex>& __lock); 00077 00078 template<typename _Predicate> 00079 void 00080 wait(unique_lock<mutex>& __lock, _Predicate __p) 00081 { 00082 while (!__p()) 00083 wait(__lock); 00084 } 00085 00086 template<typename _Duration> 00087 bool 00088 wait_until(unique_lock<mutex>& __lock, 00089 const chrono::time_point<__clock_t, _Duration>& __atime) 00090 { return __wait_until_impl(__lock, __atime); } 00091 00092 template<typename _Clock, typename _Duration> 00093 bool 00094 wait_until(unique_lock<mutex>& __lock, 00095 const chrono::time_point<_Clock, _Duration>& __atime) 00096 { 00097 // DR 887 - Sync unknown clock to known clock. 00098 typename _Clock::time_point __c_entry = _Clock::now(); 00099 __clock_t::time_point __s_entry = __clock_t::now(); 00100 chrono::nanoseconds __delta = __atime - __c_entry; 00101 __clock_t::time_point __s_atime = __s_entry + __delta; 00102 00103 return __wait_until_impl(__lock, __s_atime); 00104 } 00105 00106 template<typename _Clock, typename _Duration, typename _Predicate> 00107 bool 00108 wait_until(unique_lock<mutex>& __lock, 00109 const chrono::time_point<_Clock, _Duration>& __atime, 00110 _Predicate __p) 00111 { 00112 while (!__p()) 00113 if (!wait_until(__lock, __atime)) 00114 return __p(); 00115 00116 return true; 00117 } 00118 00119 template<typename _Rep, typename _Period> 00120 bool 00121 wait_for(unique_lock<mutex>& __lock, 00122 const chrono::duration<_Rep, _Period>& __rtime) 00123 { return wait_until(__lock, __clock_t::now() + __rtime); } 00124 00125 template<typename _Rep, typename _Period, typename _Predicate> 00126 bool 00127 wait_for(unique_lock<mutex>& __lock, 00128 const chrono::duration<_Rep, _Period>& __rtime, 00129 _Predicate __p) 00130 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); } 00131 00132 native_handle_type 00133 native_handle() 00134 { return &_M_cond; } 00135 00136 private: 00137 template<typename _Clock, typename _Duration> 00138 bool 00139 __wait_until_impl(unique_lock<mutex>& __lock, 00140 const chrono::time_point<_Clock, _Duration>& __atime) 00141 { 00142 chrono::time_point<__clock_t, chrono::seconds> __s = 00143 chrono::time_point_cast<chrono::seconds>(__atime); 00144 00145 chrono::nanoseconds __ns = 00146 chrono::duration_cast<chrono::nanoseconds>(__atime - __s); 00147 00148 __gthread_time_t __ts = 00149 { 00150 static_cast<std::time_t>(__s.time_since_epoch().count()), 00151 static_cast<long>(__ns.count()) 00152 }; 00153 00154 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(), 00155 &__ts); 00156 00157 return _Clock::now() < __atime; 00158 } 00159 }; 00160 00161 /// condition_variable_any 00162 // Like above, only mutex may not have try_lock. 00163 class condition_variable_any 00164 { 00165 typedef __gthread_cond_t __native_type; 00166 __native_type _M_cond; 00167 00168 public: 00169 typedef __native_type* native_handle_type; 00170 00171 condition_variable_any(); 00172 ~condition_variable_any(); 00173 00174 condition_variable_any(const condition_variable_any&) = delete; 00175 condition_variable_any& operator=(const condition_variable_any&) = delete; 00176 00177 void 00178 notify_one(); 00179 00180 void 00181 notify_all(); 00182 00183 template<typename _Lock> 00184 void 00185 wait(_Lock& __lock); 00186 00187 template<typename _Lock, typename _Predicate> 00188 void 00189 wait(_Lock& __lock, _Predicate __p); 00190 00191 template<typename _Lock, typename _Clock, typename _Duration> 00192 bool 00193 wait_until(_Lock& __lock, 00194 const chrono::time_point<_Clock, _Duration>& __atime); 00195 00196 template<typename _Lock, typename _Clock, 00197 typename _Duration, typename _Predicate> 00198 bool 00199 wait_until(_Lock& __lock, 00200 const chrono::time_point<_Clock, _Duration>& __atime, 00201 _Predicate __p); 00202 00203 template<typename _Lock, typename _Rep, typename _Period> 00204 bool 00205 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime); 00206 00207 template<typename _Lock, typename _Rep, 00208 typename _Period, typename _Predicate> 00209 bool 00210 wait_for(_Lock& __lock, 00211 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p); 00212 00213 native_handle_type 00214 native_handle() 00215 { return &_M_cond; } 00216 }; 00217 00218 // @} group condition_variables 00219 } 00220 00221 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1 00222 00223 #endif // __GXX_EXPERIMENTAL_CXX0X__ 00224 00225 #endif // _GLIBCXX_CONDITION_VARIABLE