libstdc++
|
00001 // <condition_variable> -*- C++ -*- 00002 00003 // Copyright (C) 2008, 2009, 2010, 2011, 2012 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 // scoped unlock - unlocks in ctor, re-locks in dtor 00175 template<typename _Lock> 00176 struct _Unlock 00177 { 00178 explicit _Unlock(_Lock& __lk) : _M_lock(__lk) { __lk.unlock(); } 00179 00180 ~_Unlock() noexcept(false) 00181 { 00182 if (uncaught_exception()) 00183 __try { _M_lock.lock(); } __catch(...) { } 00184 else 00185 _M_lock.lock(); 00186 } 00187 00188 _Unlock(const _Unlock&) = delete; 00189 _Unlock& operator=(const _Unlock&) = delete; 00190 00191 _Lock& _M_lock; 00192 }; 00193 00194 public: 00195 typedef condition_variable::native_handle_type native_handle_type; 00196 00197 condition_variable_any() throw (); 00198 ~condition_variable_any() throw (); 00199 00200 condition_variable_any(const condition_variable_any&) = delete; 00201 condition_variable_any& operator=(const condition_variable_any&) = delete; 00202 00203 void 00204 notify_one() 00205 { 00206 lock_guard<mutex> __lock(_M_mutex); 00207 _M_cond.notify_one(); 00208 } 00209 00210 void 00211 notify_all() 00212 { 00213 lock_guard<mutex> __lock(_M_mutex); 00214 _M_cond.notify_all(); 00215 } 00216 00217 template<typename _Lock> 00218 void 00219 wait(_Lock& __lock) 00220 { 00221 unique_lock<mutex> __my_lock(_M_mutex); 00222 _Unlock<_Lock> __unlock(__lock); 00223 // _M_mutex must be unlocked before re-locking __lock so move 00224 // ownership of _M_mutex lock to an object with shorter lifetime. 00225 unique_lock<mutex> __my_lock2(std::move(__my_lock)); 00226 _M_cond.wait(__my_lock2); 00227 } 00228 00229 00230 template<typename _Lock, typename _Predicate> 00231 void 00232 wait(_Lock& __lock, _Predicate __p) 00233 { 00234 while (!__p()) 00235 wait(__lock); 00236 } 00237 00238 template<typename _Lock, typename _Clock, typename _Duration> 00239 cv_status 00240 wait_until(_Lock& __lock, 00241 const chrono::time_point<_Clock, _Duration>& __atime) 00242 { 00243 unique_lock<mutex> __my_lock(_M_mutex); 00244 _Unlock<_Lock> __unlock(__lock); 00245 // _M_mutex must be unlocked before re-locking __lock so move 00246 // ownership of _M_mutex lock to an object with shorter lifetime. 00247 unique_lock<mutex> __my_lock2(std::move(__my_lock)); 00248 return _M_cond.wait_until(__my_lock2, __atime); 00249 } 00250 00251 template<typename _Lock, typename _Clock, 00252 typename _Duration, typename _Predicate> 00253 bool 00254 wait_until(_Lock& __lock, 00255 const chrono::time_point<_Clock, _Duration>& __atime, 00256 _Predicate __p) 00257 { 00258 while (!__p()) 00259 if (wait_until(__lock, __atime) == cv_status::timeout) 00260 return __p(); 00261 return true; 00262 } 00263 00264 template<typename _Lock, typename _Rep, typename _Period> 00265 cv_status 00266 wait_for(_Lock& __lock, const chrono::duration<_Rep, _Period>& __rtime) 00267 { return wait_until(__lock, __clock_t::now() + __rtime); } 00268 00269 template<typename _Lock, typename _Rep, 00270 typename _Period, typename _Predicate> 00271 bool 00272 wait_for(_Lock& __lock, 00273 const chrono::duration<_Rep, _Period>& __rtime, _Predicate __p) 00274 { return wait_until(__lock, __clock_t::now() + __rtime, std::move(__p)); } 00275 00276 native_handle_type 00277 native_handle() 00278 { return _M_cond.native_handle(); } 00279 }; 00280 00281 // @} group condition_variables 00282 _GLIBCXX_END_NAMESPACE_VERSION 00283 } // namespace 00284 00285 #endif // _GLIBCXX_HAS_GTHREADS && _GLIBCXX_USE_C99_STDINT_TR1 00286 00287 #endif // __GXX_EXPERIMENTAL_CXX0X__ 00288 00289 #endif // _GLIBCXX_CONDITION_VARIABLE