libstdc++
|
00001 // <condition_variable> -*- C++ -*- 00002 00003 // Copyright (C) 2008, 2009, 2010 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 include/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 <bits/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 _GLIBCXX_VISIBILITY(default) 00044 { 00045 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00046 00047 /** 00048 * @defgroup condition_variables Condition Variables 00049 * @ingroup concurrency 00050 * 00051 * Classes for condition_variable support. 00052 * @{ 00053 */ 00054 00055 /// cv_status 00056 enum class cv_status { no_timeout, timeout }; 00057 00058 /// condition_variable 00059 class condition_variable 00060 { 00061 typedef chrono::system_clock __clock_t; 00062 typedef __gthread_cond_t __native_type; 00063 __native_type _M_cond; 00064 00065 public: 00066 typedef __native_type* native_handle_type; 00067 00068 condition_variable() throw (); 00069 ~condition_variable() throw (); 00070 00071 condition_variable(const condition_variable&) = delete; 00072 condition_variable& operator=(const condition_variable&) = delete; 00073 00074 void 00075 notify_one(); 00076 00077 void 00078 notify_all(); 00079 00080 void 00081 wait(unique_lock<mutex>& __lock); 00082 00083 template<typename _Predicate> 00084 void 00085 wait(unique_lock<mutex>& __lock, _Predicate __p) 00086 { 00087 while (!__p()) 00088 wait(__lock); 00089 } 00090 00091 template<typename _Duration> 00092 cv_status 00093 wait_until(unique_lock<mutex>& __lock, 00094 const chrono::time_point<__clock_t, _Duration>& __atime) 00095 { return __wait_until_impl(__lock, __atime); } 00096 00097 template<typename _Clock, typename _Duration> 00098 cv_status 00099 wait_until(unique_lock<mutex>& __lock, 00100 const chrono::time_point<_Clock, _Duration>& __atime) 00101 { 00102 // DR 887 - Sync unknown clock to known clock. 00103 const typename _Clock::time_point __c_entry = _Clock::now(); 00104 const __clock_t::time_point __s_entry = __clock_t::now(); 00105 const chrono::nanoseconds __delta = __atime - __c_entry; 00106 const __clock_t::time_point __s_atime = __s_entry + __delta; 00107 00108 return __wait_until_impl(__lock, __s_atime); 00109 } 00110 00111 template<typename _Clock, typename _Duration, typename _Predicate> 00112 bool 00113 wait_until(unique_lock<mutex>& __lock, 00114 const chrono::time_point<_Clock, _Duration>& __atime, 00115 _Predicate __p) 00116 { 00117 while (!__p()) 00118 if (wait_until(__lock, __atime) == cv_status::timeout) 00119 return __p(); 00120 return true; 00121 } 00122 00123 template<typename _Rep, typename _Period> 00124 cv_status 00125 wait_for(unique_lock<mutex>& __lock, 00126 const chrono::duration<_Rep, _Period>& __rtime) 00127 { return wait_until(__lock, __clock_t::now() + __rtime); } 00128 00129 template<typename _Rep, typename _Period, typename _Predicate> 00130 bool 00131 wait_for(unique_lock<mutex>& __lock, 00132 const chrono::duration<_Rep, _Period>& __rtime, 00133 _Predicate __p) 00134 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); } 00135 00136 native_handle_type 00137 native_handle() 00138 { return &_M_cond; } 00139 00140 private: 00141 template<typename _Clock, typename _Duration> 00142 cv_status 00143 __wait_until_impl(unique_lock<mutex>& __lock, 00144 const chrono::time_point<_Clock, _Duration>& __atime) 00145 { 00146 chrono::time_point<__clock_t, chrono::seconds> __s = 00147 chrono::time_point_cast<chrono::seconds>(__atime); 00148 00149 chrono::nanoseconds __ns = 00150 chrono::duration_cast<chrono::nanoseconds>(__atime - __s); 00151 00152 __gthread_time_t __ts = 00153 { 00154 static_cast<std::time_t>(__s.time_since_epoch().count()), 00155 static_cast<long>(__ns.count()) 00156 }; 00157 00158 __gthread_cond_timedwait(&_M_cond, __lock.mutex()->native_handle(), 00159 &__ts); 00160 00161 return (_Clock::now() < __atime 00162 ? cv_status::no_timeout : cv_status::timeout); 00163 } 00164 }; 00165 00166 /// condition_variable_any 00167 // Like above, but mutex is not required to have try_lock. 00168 class condition_variable_any 00169 { 00170 typedef chrono::system_clock __clock_t; 00171 condition_variable _M_cond; 00172 mutex _M_mutex; 00173 00174 public: 00175 typedef condition_variable::native_handle_type native_handle_type; 00176 00177 condition_variable_any() throw (); 00178 ~condition_variable_any() throw (); 00179 00180 condition_variable_any(const condition_variable_any&) = delete; 00181 condition_variable_any& operator=(const condition_variable_any&) = delete; 00182 00183 void 00184 notify_one() 00185 { 00186 lock_guard<mutex> __lock(_M_mutex); 00187 _M_cond.notify_one(); 00188 } 00189 00190 void 00191 notify_all() 00192 { 00193 lock_guard<mutex> __lock(_M_mutex); 00194 _M_cond.notify_all(); 00195 } 00196 00197 template<typename _Lock> 00198 void 00199 wait(_Lock& __lock) 00200 { 00201 // scoped unlock - unlocks in ctor, re-locks in dtor 00202 struct _Unlock { 00203 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); } 00204 ~_Unlock() noexcept(false) 00205 { 00206 if (uncaught_exception()) 00207 __try { _M_lock.lock(); } __catch(...) { } 00208 else 00209 _M_lock.lock(); 00210 } 00211 _Lock& _M_lock; 00212 }; 00213 00214 unique_lock<mutex> __my_lock(_M_mutex); 00215 _Unlock __unlock(__lock); 00216 // _M_mutex must be unlocked before re-locking __lock so move 00217 // ownership of _M_mutex lock to an object with shorter lifetime. 00218 unique_lock<mutex> __my_lock2(std::move(__my_lock)); 00219 _M_cond.wait(__my_lock2); 00220 } 00221 00222 00223 template<typename _Lock, typename _Predicate> 00224 void 00225 wait(_Lock& __lock, _Predicate __p) 00226 { 00227 while (!__p()) 00228 wait(__lock); 00229 } 00230 00231 template<typename _Lock, typename _Clock, typename _Duration> 00232 cv_status 00233 wait_until(_Lock& __lock, 00234 const chrono::time_point<_Clock, _Duration>& __atime) 00235 { 00236 unique_lock<mutex> __my_lock(_M_mutex); 00237 __lock.unlock(); 00238 cv_status __status = _M_cond.wait_until(__my_lock, __atime); 00239 __lock.lock(); 00240 return __status; 00241 } 00242 00243 template<typename _Lock, typename _Clock, 00244 typename _Duration, typename _Predicate> 00245 bool 00246 wait_until(_Lock& __lock, 00247 const chrono::time_point<_Clock, _Duration>& __atime, 00248 _Predicate __p) 00249 { 00250 while (!__p()) 00251 if (wait_until(__lock, __atime) == cv_status::timeout) 00252 return __p(); 00253 return true; 00254 } 00255 00256 template<typename _Lock, typename _Rep, typename _Period> 00257 cv_status 00258 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime) 00259 { return wait_until(__lock, __clock_t::now() + __rtime); } 00260 00261 template<typename _Lock, typename _Rep, 00262 typename _Period, typename _Predicate> 00263 bool 00264 wait_for(_Lock& __lock, 00265 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p) 00266 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); } 00267 00268 native_handle_type 00269 native_handle() 00270 { return _M_cond.native_handle(); } 00271 }; 00272 00273 // @} group condition_variables 00274 _GLIBCXX_END_NAMESPACE_VERSION 00275 } // namespace 00276 00277 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1 00278 00279 #endif // __GXX_EXPERIMENTAL_CXX0X__ 00280 00281 #endif // _GLIBCXX_CONDITION_VARIABLE