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