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